Search in sources :

Example 1 with ByteArrayISO8859Writer

use of org.eclipse.jetty.util.ByteArrayISO8859Writer in project jetty.project by eclipse.

the class DefaultHandler method handle.

/* ------------------------------------------------------------ */
/*
     * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
     */
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    if (response.isCommitted() || baseRequest.isHandled())
        return;
    baseRequest.setHandled(true);
    String method = request.getMethod();
    // little cheat for common request
    if (_serveIcon && _favicon != null && HttpMethod.GET.is(method) && request.getRequestURI().equals("/favicon.ico")) {
        if (request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.toString()) == _faviconModified)
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        else {
            response.setStatus(HttpServletResponse.SC_OK);
            response.setContentType("image/x-icon");
            response.setContentLength(_favicon.length);
            response.setDateHeader(HttpHeader.LAST_MODIFIED.toString(), _faviconModified);
            response.setHeader(HttpHeader.CACHE_CONTROL.toString(), "max-age=360000,public");
            response.getOutputStream().write(_favicon);
        }
        return;
    }
    if (!_showContexts || !HttpMethod.GET.is(method) || !request.getRequestURI().equals("/")) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    response.setContentType(MimeTypes.Type.TEXT_HTML.toString());
    try (ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500)) {
        writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
        writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
        writer.write("No context on this server matched or handled this request.<BR>");
        writer.write("Contexts known to this server are: <ul>");
        Server server = getServer();
        Handler[] handlers = server == null ? null : server.getChildHandlersByClass(ContextHandler.class);
        for (int i = 0; handlers != null && i < handlers.length; i++) {
            ContextHandler context = (ContextHandler) handlers[i];
            if (context.isRunning()) {
                writer.write("<li><a href=\"");
                if (context.getVirtualHosts() != null && context.getVirtualHosts().length > 0)
                    writer.write(request.getScheme() + "://" + context.getVirtualHosts()[0] + ":" + request.getLocalPort());
                writer.write(context.getContextPath());
                if (context.getContextPath().length() > 1 && context.getContextPath().endsWith("/"))
                    writer.write("/");
                writer.write("\">");
                writer.write(context.getContextPath());
                if (context.getVirtualHosts() != null && context.getVirtualHosts().length > 0)
                    writer.write("&nbsp;@&nbsp;" + context.getVirtualHosts()[0] + ":" + request.getLocalPort());
                writer.write("&nbsp;--->&nbsp;");
                writer.write(context.toString());
                writer.write("</a></li>\n");
            } else {
                writer.write("<li>");
                writer.write(context.getContextPath());
                if (context.getVirtualHosts() != null && context.getVirtualHosts().length > 0)
                    writer.write("&nbsp;@&nbsp;" + context.getVirtualHosts()[0] + ":" + request.getLocalPort());
                writer.write("&nbsp;--->&nbsp;");
                writer.write(context.toString());
                if (context.isFailed())
                    writer.write(" [failed]");
                if (context.isStopped())
                    writer.write(" [stopped]");
                writer.write("</li>\n");
            }
        }
        writer.write("</ul><hr>");
        baseRequest.getHttpChannel().getHttpConfiguration().writePoweredBy(writer, "<a href=\"http://eclipse.org/jetty\"><img border=0 src=\"/favicon.ico\"/></a>&nbsp;", "<hr/>\n");
        writer.write("\n</BODY>\n</HTML>\n");
        writer.flush();
        response.setContentLength(writer.size());
        try (OutputStream out = response.getOutputStream()) {
            writer.writeTo(out);
        }
    }
}
Also used : Server(org.eclipse.jetty.server.Server) OutputStream(java.io.OutputStream) Handler(org.eclipse.jetty.server.Handler) ByteArrayISO8859Writer(org.eclipse.jetty.util.ByteArrayISO8859Writer)

Example 2 with ByteArrayISO8859Writer

use of org.eclipse.jetty.util.ByteArrayISO8859Writer in project elasticsearch-jetty by sonian.

the class JettyHttpServerErrorHandler method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
    AbstractHttpConnection connection = AbstractHttpConnection.getCurrentConnection();
    connection.getRequest().setHandled(true);
    String method = request.getMethod();
    response.setContentType(MimeTypes.TEXT_PLAIN_8859_1);
    ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(4096);
    writer.write(request.getAttribute(Dispatcher.ERROR_STATUS_CODE) + " " + request.getAttribute(Dispatcher.ERROR_MESSAGE) + " " + request.getAttribute(Dispatcher.ERROR_REQUEST_URI));
    writer.flush();
    response.setContentLength(writer.size());
    writer.writeTo(response.getOutputStream());
    writer.destroy();
}
Also used : ByteArrayISO8859Writer(org.eclipse.jetty.util.ByteArrayISO8859Writer) AbstractHttpConnection(org.eclipse.jetty.server.AbstractHttpConnection)

Example 3 with ByteArrayISO8859Writer

use of org.eclipse.jetty.util.ByteArrayISO8859Writer in project ddf by codice.

the class DefaultErrorHandler method handleError.

@Override
public void handleError(int code, String message, String type, Throwable throwable, String uri, HttpServletRequest request, HttpServletResponse response) {
    initIndexHtml();
    String stack = ExceptionUtils.getFullStackTrace(throwable);
    Map<String, String> jsonMap = new HashMap<>();
    jsonMap.put("code", String.valueOf(code));
    jsonMap.put("message", message);
    jsonMap.put("type", type);
    jsonMap.put("throwable", stack);
    jsonMap.put("uri", uri);
    String data = toJson(jsonMap);
    String encodedBytes = Base64.getEncoder().encodeToString(data.getBytes(StandardCharsets.UTF_8));
    String localIndexHtml = indexHtml.replace("WINDOW_DATA", "\"" + encodedBytes + "\"");
    response.setStatus(code);
    response.setContentType("text/html");
    try (ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(BUFFER_SIZE)) {
        writer.write(localIndexHtml);
        writer.flush();
        writer.writeTo(response.getOutputStream());
    } catch (IOException e) {
        LOGGER.debug("Unable to write error html data to client.");
    }
}
Also used : HashMap(java.util.HashMap) ByteArrayISO8859Writer(org.eclipse.jetty.util.ByteArrayISO8859Writer) IOException(java.io.IOException)

Aggregations

ByteArrayISO8859Writer (org.eclipse.jetty.util.ByteArrayISO8859Writer)3 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 AbstractHttpConnection (org.eclipse.jetty.server.AbstractHttpConnection)1 Handler (org.eclipse.jetty.server.Handler)1 Server (org.eclipse.jetty.server.Server)1