Search in sources :

Example 46 with HttpResponseStatus

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);
    }
}
Also used : HttpRequestMetadataRetriever(org.xipki.ca.server.api.HttpRequestMetadataRetriever) Rest(org.xipki.ca.server.api.Rest) RestResponse(org.xipki.ca.server.api.RestResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) AuditEvent(org.xipki.audit.AuditEvent) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpMethod(io.netty.handler.codec.http.HttpMethod) Date(java.util.Date)

Example 47 with HttpResponseStatus

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);
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 48 with HttpResponseStatus

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"));
}
Also used : Request(ratpack.http.Request) Logger(org.slf4j.Logger) Context(ratpack.handling.Context) LoggerFactory(org.slf4j.LoggerFactory) Throwables(com.google.common.base.Throwables) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Request(ratpack.http.Request)

Example 49 with HttpResponseStatus

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);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 50 with HttpResponseStatus

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);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpEntity(org.apache.http.HttpEntity) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) HttpVersion(io.netty.handler.codec.http.HttpVersion)

Aggregations

HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)73 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)16 ByteBuf (io.netty.buffer.ByteBuf)15 HttpMethod (io.netty.handler.codec.http.HttpMethod)11 IOException (java.io.IOException)11 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)10 HttpResponse (io.netty.handler.codec.http.HttpResponse)10 HttpVersion (io.netty.handler.codec.http.HttpVersion)9 URI (java.net.URI)7 Test (org.junit.Test)7 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)6 URISyntaxException (java.net.URISyntaxException)6 Map (java.util.Map)6 Test (org.junit.jupiter.api.Test)6 Channel (io.netty.channel.Channel)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 HttpHeaderValues (io.netty.handler.codec.http.HttpHeaderValues)4 Duration (java.time.Duration)4 ArrayList (java.util.ArrayList)4