org.eclipse.jetty.http.HttpHeaders Java Examples

The following examples show how to use org.eclipse.jetty.http.HttpHeaders. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonErrorHandler.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
    baseRequest.setHandled(true);
    String method = request.getMethod();
    if(!method.equals(HttpMethods.HEAD) && !method.equals(HttpMethods.GET) &&
            !method.equals(HttpMethods.POST) && !method.equals(PATCH_METHOD) && !method.equals(HttpMethods.PUT) &&
             !method.equals(HttpMethods.DELETE)) {
        return;
    }

    final String message;
    final ErrorCode error;
    final String note;
    if(response.getStatus() == HttpServletResponse.SC_NOT_FOUND) {
        message = "Path not found";
        if (!request.getRequestURI().contains("/v1/")) {
            note = "try including /v1/ in the path";
        } else {
            note = null;
        }
        error = ErrorCode.MALFORMED_REQUEST;
    } else {
        if (response instanceof Response) {
            note = ((Response)response).getReason();
        } else {
            note = null;
        }
        message = HttpStatus.getMessage(response.getStatus());
        error = ErrorCode.INTERNAL_ERROR;
    }

    response.setContentType(MediaType.APPLICATION_JSON);
    response.setHeader(HttpHeaders.CACHE_CONTROL, getCacheControl());

    StringBuilder builder = new StringBuilder();
    RestResponseBuilder.formatJsonError(builder, error.getFormattedValue(), message, note);
    builder.append('\n');

    response.setContentLength(builder.length());
    OutputStream out = response.getOutputStream();
    out.write(builder.toString().getBytes());
    out.close();
}