Search in sources :

Example 1 with FastContentWriter

use of com.yahoo.jdisc.handler.FastContentWriter in project vespa by vespa-engine.

the class BindingsOverviewHandler method handleRequest.

@Override
public ContentChannel handleRequest(com.yahoo.jdisc.Request request, ResponseHandler handler) {
    JSONObject json;
    int statusToReturn;
    if (request instanceof HttpRequest && ((HttpRequest) request).getMethod() != Method.GET) {
        json = errorMessageInJson();
        statusToReturn = com.yahoo.jdisc.Response.Status.METHOD_NOT_ALLOWED;
    } else {
        json = new StatusResponse(bindingsConfig).render();
        statusToReturn = com.yahoo.jdisc.Response.Status.OK;
    }
    FastContentWriter writer = new FastContentWriter(new ResponseDispatch() {

        @Override
        protected com.yahoo.jdisc.Response newResponse() {
            com.yahoo.jdisc.Response response = new com.yahoo.jdisc.Response(statusToReturn);
            response.headers().add("Content-Type", Arrays.asList(new String[] { "application/json" }));
            return response;
        }
    }.connect(handler));
    try {
        writer.write(json.toString());
    } finally {
        writer.close();
    }
    return new IgnoredContent();
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) ResponseDispatch(com.yahoo.jdisc.handler.ResponseDispatch) JSONObject(org.json.JSONObject) FastContentWriter(com.yahoo.jdisc.handler.FastContentWriter)

Example 2 with FastContentWriter

use of com.yahoo.jdisc.handler.FastContentWriter in project vespa by vespa-engine.

the class ApplicationStatusHandler method handleRequest.

@Override
public ContentChannel handleRequest(com.yahoo.jdisc.Request request, ResponseHandler handler) {
    JSONObject json = new StatusResponse(applicationJson, clientsJson, serversJson, requestFiltersJson, responseFiltersJson, bindingsConfig).render();
    FastContentWriter writer = new FastContentWriter(new ResponseDispatch() {

        @Override
        protected com.yahoo.jdisc.Response newResponse() {
            com.yahoo.jdisc.Response response = new com.yahoo.jdisc.Response(com.yahoo.jdisc.Response.Status.OK);
            response.headers().add("Content-Type", Arrays.asList(new String[] { "application/json" }));
            return response;
        }
    }.connect(handler));
    writer.write(json.toString());
    writer.close();
    return new IgnoredContent();
}
Also used : ResponseDispatch(com.yahoo.jdisc.handler.ResponseDispatch) JSONObject(org.json.JSONObject) FastContentWriter(com.yahoo.jdisc.handler.FastContentWriter)

Example 3 with FastContentWriter

use of com.yahoo.jdisc.handler.FastContentWriter in project vespa by vespa-engine.

the class SecurityFilterUtils method sendErrorResponse.

public static void sendErrorResponse(ResponseHandler responseHandler, int statusCode, String message) {
    Response response = new Response(statusCode);
    response.headers().put("Content-Type", "application/json");
    ObjectNode errorMessage = mapper.createObjectNode();
    errorMessage.put("message", message);
    try (FastContentWriter writer = ResponseDispatch.newInstance(response).connectFastWriter(responseHandler)) {
        writer.write(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorMessage));
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}
Also used : Response(com.yahoo.jdisc.Response) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FastContentWriter(com.yahoo.jdisc.handler.FastContentWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 4 with FastContentWriter

use of com.yahoo.jdisc.handler.FastContentWriter in project vespa by vespa-engine.

the class FilterUtils method write.

/**
 * Write HTTP response using given handler
 */
public static void write(HttpResponse response, ResponseHandler handler) {
    response.headers().put("Content-Type", response.getContentType());
    try (FastContentWriter writer = ResponseDispatch.newInstance(response.getJdiscResponse()).connectFastWriter(handler)) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            response.render(out);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
        writer.write(out.toByteArray());
    }
}
Also used : FastContentWriter(com.yahoo.jdisc.handler.FastContentWriter) UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Aggregations

FastContentWriter (com.yahoo.jdisc.handler.FastContentWriter)4 ResponseDispatch (com.yahoo.jdisc.handler.ResponseDispatch)2 JSONObject (org.json.JSONObject)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Response (com.yahoo.jdisc.Response)1 HttpRequest (com.yahoo.jdisc.http.HttpRequest)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1