use of io.netty.handler.codec.http.HttpResponseStatus in project xipki by xipki.
the class HttpRestServlet method service.
@Override
public FullHttpResponse service(FullHttpRequest request, ServletURI servletUri, SSLSession sslSession, SslReverseProxyMode sslReverseProxyMode) {
HttpVersion version = request.protocolVersion();
HttpMethod method = request.method();
if (method != HttpMethod.POST && method != HttpMethod.GET) {
return createErrorResponse(version, HttpResponseStatus.METHOD_NOT_ALLOWED);
}
AuditEvent event = new AuditEvent(new Date());
try {
Rest rest = responderManager.getRest();
HttpRequestMetadataRetriever httpRetriever = new HttpRequestMetadataRetrieverImpl(request, servletUri, sslSession, sslReverseProxyMode);
byte[] requestBytes = readContent(request);
RestResponse response = rest.service(servletUri.getPath(), event, requestBytes, httpRetriever);
HttpResponseStatus status = HttpResponseStatus.valueOf(response.getStatusCode());
FullHttpResponse resp = createResponse(version, status, response.getContentType(), response.getBody());
for (String headerName : response.getHeaders().keySet()) {
resp.headers().add(headerName, response.getHeaders().get(headerName));
}
return resp;
} finally {
event.finish();
auditServiceRegister.getAuditService().logEvent(event);
}
}
use of io.netty.handler.codec.http.HttpResponseStatus in project turbo-rpc by hank-whu.
the class RestHttResponseEncoder method doResponse.
private void doResponse(ChannelHandlerContext ctx, ChannelPromise promise, RestHttpResponse restHttpResponse) {
ByteBuf buffer = ctx.alloc().ioBuffer();
Object msg = restHttpResponse.getResult();
HttpResponseStatus status = restHttpResponse.getStatus();
boolean keepAlive = restHttpResponse.isKeepAlive();
Throwable throwable = null;
if (msg == null) {
buffer.writeBytes(UnsafeStringUtils.getUTF8Bytes(""));
} else if (msg instanceof Throwable) {
throwable = (Throwable) msg;
buffer.writeBytes(UnsafeStringUtils.getUTF8Bytes(throwable.getMessage()));
} else {
try {
jsonMapper.write(buffer, msg);
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, buffer, false);
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
} catch (Throwable e) {
if (logger.isWarnEnabled()) {
logger.warn("error ", e);
}
status = INTERNAL_SERVER_ERROR;
buffer.clear();
buffer.writeBytes(UnsafeStringUtils.getUTF8Bytes(e.getMessage()));
}
}
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, buffer, false);
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
doResponseFilter(ctx, restHttpResponse.getRequest(), response, restHttpResponse.getInvoker(), throwable);
if (keepAlive) {
response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
ctx.write(response, promise);
} else {
ctx.write(response, promise).addListener(CLOSE);
}
}
use of io.netty.handler.codec.http.HttpResponseStatus in project ratpack by ratpack.
the class DefaultDevelopmentErrorHandler method error.
/**
* Prints the string "Client error «statusCode»" to the response as text with the given status code.
*
* @param ctx The ctx
* @param statusCode The 4xx status code that explains the problem
*/
@Override
public void error(Context ctx, int statusCode) throws Exception {
HttpResponseStatus status = HttpResponseStatus.valueOf(statusCode);
Request request = ctx.getRequest();
LOGGER.error(statusCode + " client error for request to " + request.getRawUri());
ctx.getResponse().status(statusCode);
ctx.byContent(s -> s.plainText(() -> ctx.render("Client error " + statusCode)).html(() -> new ErrorPageRenderer() {
protected void render() {
render(ctx, status.reasonPhrase(), w -> messages(w, "Client Error", () -> meta(w, m -> m.put("URI:", request.getRawUri()).put("Method:", request.getMethod().getName()).put("Status Code:", status.code()).put("Phrase:", status.reasonPhrase()))));
}
}).noMatch("text/plain"));
}
use of io.netty.handler.codec.http.HttpResponseStatus in project BRFS by zhangnianli.
the class DefaultNettyHandleResultCallback method completed.
@Override
public void completed(HandleResult result) {
HttpResponseStatus status = result.isSuccess() ? HttpResponseStatus.OK : HttpResponseStatus.INTERNAL_SERVER_ERROR;
byte[] errorBytes = result.getCause() != null ? BrStringUtils.toUtf8Bytes(result.getCause().toString()) : new byte[0];
byte[] dataBytes = result.getData() != null ? result.getData() : new byte[0];
ByteBuf content = Unpooled.wrappedBuffer(Bytes.concat(errorBytes, dataBytes));
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
ResponseSender.sendResponse(context, response);
}
use of io.netty.handler.codec.http.HttpResponseStatus in project moco by dreamhead.
the class AbstractProxyResponseHandler method setupNormalResponse.
private HttpResponse setupNormalResponse(final org.apache.http.HttpResponse remoteResponse) throws IOException {
HttpVersion httpVersion = HttpVersion.valueOf(remoteResponse.getProtocolVersion().toString());
HttpResponseStatus status = HttpResponseStatus.valueOf(remoteResponse.getStatusLine().getStatusCode());
FullHttpResponse response = new DefaultFullHttpResponse(httpVersion, status);
response.setStatus(status);
Arrays.stream(remoteResponse.getAllHeaders()).filter(this::isResponseHeader).forEach(header -> response.headers().set(header.getName(), header.getValue()));
HttpEntity entity = remoteResponse.getEntity();
if (entity != null) {
byte[] content = toByteArray(entity);
if (content.length > 0) {
ByteBuf buffer = Unpooled.copiedBuffer(content);
response.content().writeBytes(buffer);
}
}
return newResponse(response);
}
Aggregations