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();
}
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();
}
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);
}
}
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());
}
}
Aggregations