midhttpexecutionserver.java.svn-base
来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 660 行 · 第 1/2 页
SVN-BASE
660 行
} } else { verboseln("MIDHttpExecutionServer.stop():" + " testProvider == null"); } if (!last_getNextApp) { try { wait(3000); } catch (InterruptedException ie) { verbosetrace(ie); } } super.stop(); } protected class RequestHandler extends BaseHttpServer { protected void handleHead(URL request) throws IOException { String path = request.getFile(); if (path.startsWith(MEDIA_ROOT)) { handleFileRequest(HEAD, path); } else { sendDiagnostics(HTTP_NOT_FOUND); } } protected void handleGet(URL request, DataInputStream in) throws IOException { String path = request.getFile(); String JAMId = ""; String bundleId = ""; String baseGetNextAppURI = testRoot + extraURI; // cmd "get next app" if (path.startsWith(baseGetNextAppURI)) { verboseln("getNextApp"); if (path.length() > baseGetNextAppURI.length()) { char separator = path.charAt(baseGetNextAppURI.length()); boolean properSeparatorBeforeJamName = separator == '/' || separator == '?'; boolean nonEmptyJamName = path.length() > baseGetNextAppURI.length() + 1; if (properSeparatorBeforeJamName && nonEmptyJamName) { JAMId = path.substring(baseGetNextAppURI.length() + 1); } else { System.out.println("ERROR: " + "Invalid request for the test bundle. " + "JAMName is not properly specified."); System.out.println("Invalid request: " + path); // Most logical response code would be // HTTP_BAD_REQUEST, but WTK and some RIs would just // continue to send new requests over and over again. // Responding with HTTP_NOT_FOUND stops that. sendDiagnostics(HTTP_NOT_FOUND, "Invalid request for the test bundle"); return; } } expectedRequest = "getNextTest"; String next_app = ""; if (isDone()) { sendDiagnostics(HTTP_NOT_FOUND); return; } next_app = testProvider.getNextApp(JAMId); if (next_app == null) { sendDiagnostics(HTTP_NOT_FOUND); synchronized (this) { last_getNextApp=true; notifyAll(); } return; } if (next_app.length() == 0) { sendDiagnostics(HTTP_UNAVAILABLE); return; } verboseln("next_app: " + next_app); File jar_path = new File(jarSourceDir, next_app); verboseln("jar_path: " + jar_path); long length = jar_path.length(); //JAD descriptor creation ByteArrayOutputStream bos = appHandler.createDescriptor(request.getHost(), getPort(), next_app, length, mainClass, jarSourceDir); String[] jadAddon = null; MidBundle currentMidBundle = testProvider.getExecutingBundle(next_app); PrintWriter pw = new PrintWriter(new OutputStreamWriter(bos, "ISO8859_1")); if ((jadAddon = currentMidBundle.getJadAddon()) != null) { for (int i = 0; i < jadAddon.length; i++) { pw.println(jadAddon[i]); } } pw.print("\r\n"); pw.close(); setResponseProperty("Content-Type", "text/vnd.sun.j2me.app-descriptor"); if (suiteSigner == null || currentMidBundle.isUntrusted()) { verboseln("suiteSigner is zero"); byte [] bbb = bos.toByteArray(); setResponseProperty("Content-Length", "" + bbb.length); sendDiagnostics(HTTP_OK); flushRawBuffer(bbb); } else { String jadFileName = next_app + ".jad"; File jadFile = new File(jarSourceDir, jadFileName); verboseln("suiteSigner: creating " + jadFileName); try { try { createJadFile(jarSourceDir, jadFileName, bos.toByteArray()); } catch (IOException ioe) { verbosetrace(ioe); testProvider.notifyBundleInError(next_app, i18n.getString("error.createJad"), ioe); sendDiagnostics(HTTP_SERVER_ERROR); return; } verboseln("suiteSigner: signing " + new File(jarSourceDir, next_app).toString() + " and " + jadFile.toString()); try { suiteSigner.sign( new File(jarSourceDir, next_app).toString(), jadFile.toString()); } catch (SignerException e) { verbosetrace(e); testProvider.notifyBundleInError(next_app, "Cannot sign the bundle", e); sendDiagnostics(HTTP_SERVER_ERROR); return; } verboseln("suiteSigner: flushing " + jadFileName); byte [] bbb = getFileContent(jarSourceDir, jadFileName); setResponseProperty("Content-Length", "" + bbb.length); sendDiagnostics(HTTP_OK); flushRawBuffer(bbb); } finally { jadFile.delete(); } } // cmd "get next test" } else if (path.startsWith(testRoot + "getNextTest")) { verboseln("getNextTest"); expectedRequest = "sendTestResult"; if (path.length() > testRoot.length() + "getNextTest".length()) { bundleId = path.substring(testRoot.length() + "getNextTest".length() + 1); } byte[] args = testProvider.getNextTest(bundleId); setResponseProperty("Content-Type", "application/octet-stream"); setResponseProperty("Content-Length", "" + (args == null ? 0 : args.length)); sendDiagnostics(HTTP_OK); if (args != null) { flushRawBuffer(args); } } else if (path.startsWith(testRoot + extraHtmlURI)) { ByteArrayOutputStream bos = createLinkToJad(request.getProtocol(), request.getHost(), request.getPort()); setResponseProperty(HTTP_CONTENT_TYPE, "text/html"); setResponseProperty(HTTP_CONTENT_LENGTH, "" + bos.size()); sendDiagnostics(HTTP_OK); flushRawBuffer(bos.toByteArray()); // assume cmd is a request for file download } else { verboseln("file download: " + path); File file; if (path.startsWith(MEDIA_ROOT)) { handleFileRequest(GET, path); return; } else { // application jar file download request file = new File(jarSourceDir, path); if (file.isFile() && file.canRead()) { try { appHandler.processContent(this, file); return; } catch (IOException ioe) { verboseln("Handler error on processing content: " + ioe.getMessage()); sendDiagnostics(HTTP_SERVER_ERROR); } } } sendDiagnostics(HTTP_NOT_FOUND); } } private ByteArrayOutputStream createLinkToJad(String protocol, String host, int port) throws UnsupportedEncodingException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); PrintWriter pw = new PrintWriter(new OutputStreamWriter( bos, "ISO8859_1")); pw.println("<HTML> <BODY>"); String base = protocol + "://" + host + ":" + port; pw.println("<BR><A HREF=\"" + base + testRoot + extraURI + "\">Test Application</A>"); pw.println("</BODY> </HTML>"); pw.close(); return bos; } /** * Handles <code>GET</code> and <code>HEAD</code> requests for media * files. * * @param path * Path to the file, relative to the server root. * @throws IOException * if I/O error occurs. */ private void handleFileRequest(String httpMethod, String path) throws IOException { assert (GET.equals(httpMethod) || HEAD.equals(httpMethod)) : "invalid HTTP method: " + httpMethod; File file = new File(serverRoot, path); if (file.isFile() && file.canRead()) { try { DataInputStream fin = new DataInputStream(new FileInputStream(file)); byte[] buf = new byte[(int) file.length()]; try { fin.readFully(buf); } finally { try { fin.close(); } catch (IOException e) { e.printStackTrace(); } } setResponseProperty("Content-Length", "" + file.length()); setResponseProperty("Content-Type", ctHandler.getContentType(file)); sendDiagnostics(HTTP_OK); if (GET.equals(httpMethod)) { flushRawBuffer(buf); } } catch (IOException ioe) { verboseln("ExecServer: " + "path=" + path + ", httpMethod=" + httpMethod); verboseln("ExecServer: Error while processing " + "file request: " + ioe.getMessage()); sendDiagnostics(HTTP_SERVER_ERROR); } } } protected void handlePost(URL request, DataInputStream in) throws IOException { String path = request.getFile(); String bundleId = ""; // cmd "send test results" if (path.startsWith(testRoot + "sendTestResult")) { verboseln("sendTestResult"); if (path.length() > testRoot.length() + "sendTestResult".length()) { bundleId = path.substring(testRoot.length() + "sendTestResult".length() + 1); } expectedRequest = "getNextTest or getNextApp"; byte[] buf; int count = -1; String encoding = getRequestProperty("transfer-encoding"); if (encoding != null && encoding.equals("chunked")) { buf = readChunkedData(in); ignoreEntityHeader(in); } else { try { count = Integer.parseInt(getRequestProperty("content-length")); if (count <= 0) { verboseln("Non-positive Content-Length: " + count); sendDiagnostics(HTTP_BAD_REQUEST); return; } buf = new byte[count]; in.readFully(buf); } catch (NumberFormatException nfe) { verboseln("Invalid Content-Length: " + getRequestProperty("content-length")); sendDiagnostics(HTTP_BAD_REQUEST); return; } } testProvider.sendTestResult(buf, bundleId); sendDiagnostics(HTTP_OK); } else { sendDiagnostics(HTTP_NOT_FOUND); } } /** * Gets called if there no incoming connections every * getExpectConnectionTimeout() milliseconds. * In ExecutionServer case complains if there is no * incoming calls. */ protected void noConnectionNotification(long timeout) { int min = (int)timeout/60000; verboseln("Test Execution Server: no requests for " + min + " minutes"); verboseln("Test Execution Server: expected request: " + expectedRequest); } protected boolean isDone() { return MIDHttpExecutionServer.this.isDone(); } private static final String MEDIA_ROOT = "/lib/media/"; } private static final ResourceBundle i18n = ResourceBundle.getBundle("com.sun.cldc.communication.midp.i18n");}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?