public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { final String reason = baseRequest.getResponse().getReason(); try { if (reason != null && reason.startsWith("{")) { ServletUtils.sendJsonError(request, response, baseRequest.getResponse().getStatus(), new JSONObject(reason)); } else { ServletUtils.sendJsonError(request, response, baseRequest.getResponse().getStatus(), reason); } } catch (final JSONException e) { response.sendError(500); } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { String method = request.getMethod(); if ( !method.equals(HttpMethod.GET.asString()) && !method.equals(HttpMethod.POST.asString()) && !method.equals(HttpMethod.HEAD.asString()) ) return ; response.setContentType(MimeTypes.Type.TEXT_PLAIN_UTF_8.asString()) ; response.setHeader(HttpNames.hCacheControl, "must-revalidate,no-cache,no-store"); response.setHeader(HttpNames.hPragma, "no-cache"); int code = response.getStatus() ; String message=(response instanceof Response)?((Response)response).getReason(): HttpSC.getMessage(code) ; response.getOutputStream().print(format("Error %d: %s\n", code, message)) ; } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { String method = request.getMethod(); if ( !method.equals(HttpMethod.GET.asString()) && !method.equals(HttpMethod.POST.asString()) && !method.equals(HttpMethod.HEAD.asString()) ) return ; response.setContentType(MimeTypes.Type.TEXT_PLAIN_UTF_8.asString()) ; response.setHeader(HttpNames.hCacheControl, "must-revalidate,no-cache,no-store"); response.setHeader(HttpNames.hPragma, "no-cache"); int code = response.getStatus() ; String message=(response instanceof Response)?((Response)response).getReason(): HttpSC.getMessage(code) ; response.getOutputStream().print(format("Error %d: %s\n", code, message)) ; } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { String method = request.getMethod(); if ( !method.equals(HttpMethod.GET.asString()) && !method.equals(HttpMethod.POST.asString()) && !method.equals(HttpMethod.HEAD.asString()) ) return ; response.setContentType(MimeTypes.Type.TEXT_PLAIN_UTF_8.asString()) ; ServletOps.setNoCache(response) ; int code = response.getStatus() ; String message=(response instanceof Response)?((Response)response).getReason(): HttpSC.getMessage(code) ; response.getOutputStream().print(format("Error %d: %s\n", code, message)) ; } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { String method = request.getMethod(); if ( !method.equals(HttpMethod.GET.asString()) && !method.equals(HttpMethod.POST.asString()) && !method.equals(HttpMethod.HEAD.asString()) ) return ; response.setContentType(MimeTypes.Type.TEXT_PLAIN_UTF_8.asString()) ; response.setHeader(HttpNames.hCacheControl, "must-revalidate,no-cache,no-store"); response.setHeader(HttpNames.hPragma, "no-cache"); int code = response.getStatus() ; String message=(response instanceof Response)?((Response)response).getReason(): HttpSC.getMessage(code) ; response.getOutputStream().print(format("Error %d: %s\n", code, message)) ; } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { String method = request.getMethod(); if ( !method.equals(HttpMethod.GET.asString()) && !method.equals(HttpMethod.POST.asString()) && !method.equals(HttpMethod.HEAD.asString()) ) return ; response.setContentType(MimeTypes.Type.TEXT_PLAIN_UTF_8.asString()) ; ServletOps.setNoCache(response) ; int code = response.getStatus() ; String message=(response instanceof Response)?((Response)response).getReason(): HttpSC.getMessage(code) ; response.getOutputStream().print(format("Error %d: %s\n", code, message)) ; } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { String method = request.getMethod(); if ( !method.equals(HttpMethod.GET.asString()) && !method.equals(HttpMethod.POST.asString()) && !method.equals(HttpMethod.HEAD.asString()) ) return ; response.setContentType(MimeTypes.Type.TEXT_PLAIN_UTF_8.asString()) ; ServletOps.setNoCache(response) ; ByteArrayOutputStream bytes = new ByteArrayOutputStream(1024) ; try ( Writer writer = IO.asUTF8(bytes) ) { String reason = (response instanceof Response) ? ((Response)response).getReason() : null; handleErrorPage(request, writer, response.getStatus(), reason) ; writer.flush(); response.setContentLength(bytes.size()) ; response.getOutputStream().write(bytes.toByteArray()) ; } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { String method = request.getMethod(); if ( !method.equals(HttpMethod.GET.asString()) && !method.equals(HttpMethod.POST.asString()) && !method.equals(HttpMethod.HEAD.asString()) ) return ; response.setContentType(MimeTypes.Type.TEXT_PLAIN_UTF_8.asString()) ; ServletOps.setNoCache(response) ; ByteArrayOutputStream bytes = new ByteArrayOutputStream(1024) ; try ( Writer writer = IO.asUTF8(bytes) ) { String reason = (response instanceof Response) ? ((Response)response).getReason() : null; handleErrorPage(request, writer, response.getStatus(), reason) ; writer.flush(); response.setContentLength(bytes.size()) ; response.getOutputStream().write(bytes.toByteArray()) ; } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { String method = request.getMethod(); if (!HttpMethod.GET.is(method) && !HttpMethod.POST.is(method) && !HttpMethod.HEAD.is(method)) { baseRequest.setHandled(true); return; } baseRequest.setHandled(true); response.setContentType(MimeTypes.Type.TEXT_HTML_UTF_8.asString()); response.setHeader(HttpHeader.CACHE_CONTROL.asString(), "must-revalidate,no-cache,no-store"); ByteArrayOutputStream out = new ByteArrayOutputStream(4096); OutputStreamWriter writer = new OutputStreamWriter(out, Charsets.UTF_8); String reason=(response instanceof Response)?((Response)response).getReason():null; handleErrorPage(request, writer, response.getStatus(), reason); writer.flush(); response.setContentLength(out.size()); response.getOutputStream().write(out.toByteArray(), 0, out.size()); writer.close(); } }
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(); if(!method.equals(HttpMethods.GET) && !method.equals(HttpMethods.POST) && !method.equals(HttpMethods.HEAD)) return; response.setContentType(MimeTypes.TEXT_HTML_8859_1); if (_cacheControl!=null) response.setHeader(HttpHeaders.CACHE_CONTROL, _cacheControl); ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096); handleErrorPage(request, writer, connection.getResponse().getStatus(), connection.getResponse().getReason()); writer.flush(); response.setContentLength(writer.size()); writer.writeTo(response.getOutputStream()); writer.destroy(); }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { baseRequest.setHandled(true); String method = request.getMethod(); if(!HttpMethod.GET.is(method) && !HttpMethod.POST.is(method) && !HttpMethod.HEAD.is(method)) return; response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString()); if (_cacheControl!=null) response.setHeader(HttpHeader.CACHE_CONTROL.asString(), _cacheControl); ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096); String reason=(response instanceof Response)?((Response)response).getReason():null; handleErrorPage(request, writer, response.getStatus(), reason); writer.flush(); response.setContentLength(writer.size()); writer.writeTo(response.getOutputStream()); writer.destroy(); }
public WebServer(@NotNull SharedContext context, @Nullable Server server, @NotNull WebServerConfig config, @NotNull EncryptionFactory tokenFactory) { this.context = context; this.server = server; this.config = config; this.tokenFactory = tokenFactory; if (server != null) { final ServletContextHandler contextHandler = new ServletContextHandler(); contextHandler.setContextPath("/"); handler = contextHandler.getServletHandler(); final RequestLogHandler logHandler = new RequestLogHandler(); logHandler.setRequestLog((request, response) -> { final User user = (User) request.getAttribute(User.class.getName()); final String userName = (user == null || user.isAnonymous()) ? "" : user.getUserName(); log.info("{}:{} - {} - \"{} {}\" {} {}", request.getRemoteHost(), request.getRemotePort(), userName, request.getMethod(), request.getHttpURI(), response.getStatus(), response.getReason()); }); final HandlerCollection handlers = new HandlerCollection(); handlers.addHandler(contextHandler); handlers.addHandler(logHandler); server.setHandler(handlers); } else { handler = null; } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { baseRequest.setHandled(true); String method = request.getMethod(); if(!HttpMethod.GET.is(method) && !HttpMethod.POST.is(method) && !HttpMethod.HEAD.is(method)) return; response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString()); if (_cacheControl!=null) response.setHeader(HttpHeader.CACHE_CONTROL.asString(), _cacheControl); ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096); String reason=(response instanceof Response)?((Response)response).getReason():null; handleErrorPage(request, writer, response.getStatus(), reason); writer.flush(); response.setContentLength(writer.size()); writer.writeTo(response.getOutputStream()); writer.destroy(); }
protected MetaData.Response newResponseMetaData() { return new MetaData.Response(_channel.getRequest().getHttpVersion(), getStatus(), getReason(), _fields, getLongContentLength()); }
protected MetaData.Response newResponseMetaData() { return new MetaData.Response(_channel.getRequest().getHttpVersion(), getStatus(), getReason(), _fields, getLongContentLength()); }
protected ResponseInfo newResponseInfo() { if (_status == HttpStatus.NOT_SET_000) _status = HttpStatus.OK_200; return new ResponseInfo(_channel.getRequest().getHttpVersion(), _fields, getLongContentLength(), getStatus(), getReason(), _channel.getRequest().isHead()); }
protected MetaData.Response newResponseMetaData() { MetaData.Response info = new MetaData.Response(_channel.getRequest().getHttpVersion(), getStatus(), getReason(), _fields, getLongContentLength()); info.setTrailerSupplier(getTrailers()); return info; }
protected ResponseInfo newResponseInfo() { if (_status == HttpStatus.NOT_SET_000) _status = HttpStatus.OK_200; return new ResponseInfo(_channel.getRequest().getHttpVersion(), _fields, getLongContentLength(), getStatus(), getReason(), _channel.getRequest().isHead()); }
protected ResponseInfo newResponseInfo() { return new ResponseInfo(_channel.getRequest().getHttpVersion(), _fields, getLongContentLength(), getStatus(), getReason(), _channel.getRequest().isHead()); }
public void handle(HttpChannel connection) throws IOException, ServletException { final String target=connection.getRequest().getPathInfo(); final Request request=connection.getRequest(); final Response response=connection.getResponse(); if (LOG.isDebugEnabled()) LOG.debug("{} on {}{}{} {} {}{}{}", request.getDispatcherType(), connection, System.lineSeparator(), request.getMethod(), target, request.getProtocol(), System.lineSeparator(), request.getHttpFields()); if (HttpMethod.OPTIONS.is(request.getMethod()) || "*".equals(target)) { if (!HttpMethod.OPTIONS.is(request.getMethod())) response.sendError(HttpStatus.BAD_REQUEST_400); handleOptions(request,response); if (!request.isHandled()) handle(target, request, request, response); } else handle(target, request, request, response); if (LOG.isDebugEnabled()) LOG.debug("RESPONSE for {} h={}{}{} {}{}{}", target, request.isHandled(), System.lineSeparator(), response.getStatus(), response.getReason(), System.lineSeparator(), response.getHttpFields()); }