Search in sources :

Example 36 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project xipki by xipki.

the class HealthCheckServlet method service0.

private FullHttpResponse service0(FullHttpRequest request, ServletURI servletUri, SSLSession sslSession) {
    HttpVersion version = request.protocolVersion();
    HttpMethod method = request.method();
    if (method != HttpMethod.GET) {
        return createErrorResponse(version, HttpResponseStatus.METHOD_NOT_ALLOWED);
    }
    try {
        if (server == null) {
            LOG.error("server in servlet not configured");
            return createErrorResponse(version, HttpResponseStatus.INTERNAL_SERVER_ERROR);
        }
        ResponderAndPath responderAndPath = server.getResponderForPath(servletUri.getPath());
        if (responderAndPath == null) {
            return createErrorResponse(version, HttpResponseStatus.NOT_FOUND);
        }
        HealthCheckResult healthResult = server.healthCheck(responderAndPath.getResponder());
        HttpResponseStatus status = healthResult.isHealthy() ? HttpResponseStatus.OK : HttpResponseStatus.INTERNAL_SERVER_ERROR;
        byte[] respBytes = healthResult.toJsonMessage(true).getBytes();
        return createResponse(version, status, HealthCheckServlet.CT_RESPONSE, respBytes);
    } catch (Throwable th) {
        if (th instanceof EOFException) {
            LogUtil.warn(LOG, th, "connection reset by peer");
        } else {
            LOG.error("Throwable thrown, this should not happen", th);
        }
        return createErrorResponse(version, HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) EOFException(java.io.EOFException) HealthCheckResult(org.xipki.common.HealthCheckResult) ResponderAndPath(org.xipki.ocsp.api.ResponderAndPath) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 37 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project activemq-artemis by apache.

the class HttpAcceptorHandler method channelRead.

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
    FullHttpRequest request = (FullHttpRequest) msg;
    HttpMethod method = request.method();
    // if we are a post then we send upstream, otherwise we are just being prompted for a response.
    if (method.equals(HttpMethod.POST)) {
        ctx.fireChannelRead(ReferenceCountUtil.retain(((FullHttpRequest) msg).content()));
        // add a new response
        responses.put(new ResponseHolder(System.currentTimeMillis() + responseTime, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)));
        ReferenceCountUtil.release(msg);
        return;
    }
    super.channelRead(ctx, msg);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 38 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project flink by apache.

the class RestClient method createRequest.

private static Request createRequest(String targetAddress, String targetUrl, HttpMethod httpMethod, ByteBuf jsonPayload, Collection<FileUpload> fileUploads) throws IOException {
    if (fileUploads.isEmpty()) {
        HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, targetUrl, jsonPayload);
        httpRequest.headers().set(HttpHeaders.Names.HOST, targetAddress).set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE).add(HttpHeaders.Names.CONTENT_LENGTH, jsonPayload.capacity()).add(HttpHeaders.Names.CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);
        return new SimpleRequest(httpRequest);
    } else {
        HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, targetUrl);
        httpRequest.headers().set(HttpHeaders.Names.HOST, targetAddress).set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
        // takes care of splitting the request into multiple parts
        HttpPostRequestEncoder bodyRequestEncoder;
        try {
            // we could use mixed attributes here but we have to ensure that the minimum size is
            // greater than
            // any file as the upload otherwise fails
            DefaultHttpDataFactory httpDataFactory = new DefaultHttpDataFactory(true);
            // the FileUploadHandler explicitly checks for multipart headers
            bodyRequestEncoder = new HttpPostRequestEncoder(httpDataFactory, httpRequest, true);
            Attribute requestAttribute = new MemoryAttribute(FileUploadHandler.HTTP_ATTRIBUTE_REQUEST);
            requestAttribute.setContent(jsonPayload);
            bodyRequestEncoder.addBodyHttpData(requestAttribute);
            int fileIndex = 0;
            for (FileUpload fileUpload : fileUploads) {
                Path path = fileUpload.getFile();
                if (Files.isDirectory(path)) {
                    throw new IllegalArgumentException("Upload of directories is not supported. Dir=" + path);
                }
                File file = path.toFile();
                LOG.trace("Adding file {} to request.", file);
                bodyRequestEncoder.addBodyFileUpload("file_" + fileIndex, file, fileUpload.getContentType(), false);
                fileIndex++;
            }
        } catch (HttpPostRequestEncoder.ErrorDataEncoderException e) {
            throw new IOException("Could not encode request.", e);
        }
        try {
            httpRequest = bodyRequestEncoder.finalizeRequest();
        } catch (HttpPostRequestEncoder.ErrorDataEncoderException e) {
            throw new IOException("Could not finalize request.", e);
        }
        return new MultipartRequest(httpRequest, bodyRequestEncoder);
    }
}
Also used : HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest) Path(java.nio.file.Path) DefaultFullHttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest) MemoryAttribute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.MemoryAttribute) Attribute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute) HttpPostRequestEncoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) IOException(java.io.IOException) MemoryAttribute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.MemoryAttribute) DefaultHttpDataFactory(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) File(java.io.File)

Example 39 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project netty by netty.

the class RtspMethods method valueOf.

/**
 * Returns the {@link HttpMethod} represented by the specified name.
 * If the specified name is a standard RTSP getMethod name, a cached instance
 * will be returned.  Otherwise, a new instance will be returned.
 */
public static HttpMethod valueOf(String name) {
    name = checkNonEmptyAfterTrim(name, "name").toUpperCase();
    HttpMethod result = methodMap.get(name);
    if (result != null) {
        return result;
    } else {
        return HttpMethod.valueOf(name);
    }
}
Also used : HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 40 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project wildfly by wildfly.

the class TestingOcspServer method getHttpResponse.

public HttpResponse getHttpResponse(HttpRequest request, HttpOcspServlet servlet) {
    byte[] body;
    HttpMethod method;
    if (request.getBody() == null) {
        method = HttpMethod.GET;
        body = request.getPath().getValue().split("/ocsp/", 2)[1].getBytes(UTF_8);
    } else {
        method = HttpMethod.POST;
        body = request.getBody().getRawBytes();
    }
    ByteBuf buffer = Unpooled.wrappedBuffer(body);
    FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, method, request.getPath().getValue(), buffer);
    for (Header header : request.getHeaderList()) {
        for (NottableString value : header.getValues()) {
            nettyRequest.headers().add(header.getName().getValue(), value.getValue());
        }
    }
    FullHttpResponse nettyResponse;
    try {
        nettyResponse = servlet.service(nettyRequest, new ServletURI(request.getPath().getValue()), null, SslReverseProxyMode.NONE);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    HttpResponse response = response().withStatusCode(nettyResponse.status().code()).withBody(nettyResponse.content().array());
    for (Map.Entry<String, String> header : nettyResponse.headers()) {
        response.withHeader(header.getKey(), header.getValue());
    }
    return response;
}
Also used : ServletURI(org.xipki.http.servlet.ServletURI) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpResponse(org.mockserver.model.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) NottableString(org.mockserver.model.NottableString) ByteBuf(io.netty.buffer.ByteBuf) SQLException(java.sql.SQLException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Header(org.mockserver.model.Header) NottableString(org.mockserver.model.NottableString) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Map(java.util.Map) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Aggregations

HttpMethod (io.netty.handler.codec.http.HttpMethod)83 Test (org.junit.Test)44 HttpRequest (io.netty.handler.codec.http.HttpRequest)24 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)23 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)20 URI (java.net.URI)15 IOException (java.io.IOException)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 DefaultHttpClient (org.jocean.http.client.impl.DefaultHttpClient)11 TestChannelCreator (org.jocean.http.client.impl.TestChannelCreator)11 TestChannelPool (org.jocean.http.client.impl.TestChannelPool)11 DefaultSignalClient (org.jocean.http.rosa.impl.DefaultSignalClient)11 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)11 Subscription (rx.Subscription)11 Action2 (rx.functions.Action2)11 ByteBuf (io.netty.buffer.ByteBuf)10 HttpVersion (io.netty.handler.codec.http.HttpVersion)10 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9 Map (java.util.Map)9 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)8