public Integer handleResponse(ESXX esxx, Context cx, Response response) throws Exception { // Output HTTP headers final PrintWriter out = new PrintWriter(createWriter(outStream, "US-ASCII")); out.println("Status: " + response.getStatus()); out.println("Content-Type: " + response.getContentType(true)); response.enumerateHeaders( new Response.HeaderEnumerator() { public void header(String name, String value) { out.println(name + ": " + value); } }); out.println(); out.flush(); response.writeResult(esxx, cx, outStream); getErrorWriter().flush(); getDebugWriter().flush(); outStream.flush(); return 0; }
public Integer handleError(ESXX esxx, Context cx, Throwable ex) { String title = "ESXX Server Error"; int code = 500; if (ex instanceof ESXXException) { code = ((ESXXException) ex).getStatus(); } StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); out.println(esxx.getHTMLHeader()); out.println("<h2>" + title + "</h2>"); out.println("<h3>Unhandled exception: " + ex.getClass().getSimpleName() + "</h3>"); if (ex instanceof ESXXException || ex instanceof javax.xml.stream.XMLStreamException || ex instanceof javax.xml.transform.TransformerException) { out.println("<p><tt>" + encodeXMLContent(ex.getMessage()) + "</tt></p>"); } else if (ex instanceof RhinoException) { out.println("<pre>"); out.println(ex.getClass().getSimpleName() + ": " + encodeXMLContent(ex.getMessage())); out.println(((RhinoException) ex).getScriptStackTrace(new ESXX.JSFilenameFilter())); out.println("</pre>"); } else { out.println("<pre>"); ex.printStackTrace(out); out.println("</pre>"); } out.println(esxx.getHTMLFooter()); out.close(); try { return handleResponse( esxx, cx, new Response(code, "text/html; charset=UTF-8", sw.toString(), null)); } catch (Exception ex2) { // Hmm return 20; } }