Search in sources :

Example 76 with HttpRequest

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

the class HttpTestClient method sendPatchRequest.

/**
 * Sends a simple PATCH request to the given path. You only specify the $path part of
 * http://$host:$host/$path.
 *
 * @param path The $path to PATCH (http://$host:$host/$path)
 */
public void sendPatchRequest(String path, Duration timeout) throws TimeoutException, InterruptedException {
    if (!path.startsWith("/")) {
        path = "/" + path;
    }
    HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PATCH, path);
    getRequest.headers().set(HttpHeaders.Names.HOST, host);
    getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    sendRequest(getRequest, timeout);
}
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) DefaultFullHttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest)

Example 77 with HttpRequest

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

the class HttpTestClient method sendGetRequest.

/**
 * Sends a simple GET request to the given path. You only specify the $path part of
 * http://$host:$host/$path.
 *
 * @param path The $path to GET (http://$host:$host/$path)
 */
public void sendGetRequest(String path, Duration timeout) throws TimeoutException, InterruptedException {
    if (!path.startsWith("/")) {
        path = "/" + path;
    }
    HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
    getRequest.headers().set(HttpHeaders.Names.HOST, host);
    getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
    sendRequest(getRequest, timeout);
}
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) DefaultFullHttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest)

Example 78 with HttpRequest

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

the class HttpRequestHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    try {
        if (msg instanceof HttpRequest) {
            currentRequest = (HttpRequest) msg;
            currentRequestPath = null;
            if (currentDecoder != null) {
                currentDecoder.destroy();
                currentDecoder = null;
            }
            if (currentRequest.getMethod() == HttpMethod.GET || currentRequest.getMethod() == HttpMethod.DELETE) {
                // directly delegate to the router
                ctx.fireChannelRead(currentRequest);
            } else if (currentRequest.getMethod() == HttpMethod.POST) {
                // POST comes in multiple objects. First the request, then the contents
                // keep the request and path for the remaining objects of the POST request
                currentRequestPath = new QueryStringDecoder(currentRequest.getUri(), ENCODING).path();
                currentDecoder = new HttpPostRequestDecoder(DATA_FACTORY, currentRequest, ENCODING);
            } else {
                throw new IOException("Unsupported HTTP method: " + currentRequest.getMethod().name());
            }
        } else if (currentDecoder != null && msg instanceof HttpContent) {
            // received new chunk, give it to the current decoder
            HttpContent chunk = (HttpContent) msg;
            currentDecoder.offer(chunk);
            try {
                while (currentDecoder.hasNext()) {
                    InterfaceHttpData data = currentDecoder.next();
                    if (data.getHttpDataType() == HttpDataType.FileUpload && tmpDir != null) {
                        DiskFileUpload file = (DiskFileUpload) data;
                        if (file.isCompleted()) {
                            String name = file.getFilename();
                            File target = new File(tmpDir, UUID.randomUUID() + "_" + name);
                            if (!tmpDir.exists()) {
                                logExternalUploadDirDeletion(tmpDir);
                                checkAndCreateUploadDir(tmpDir);
                            }
                            file.renameTo(target);
                            QueryStringEncoder encoder = new QueryStringEncoder(currentRequestPath);
                            encoder.addParam("filepath", target.getAbsolutePath());
                            encoder.addParam("filename", name);
                            currentRequest.setUri(encoder.toString());
                        }
                    }
                }
            } catch (EndOfDataDecoderException ignored) {
            }
            if (chunk instanceof LastHttpContent) {
                HttpRequest request = currentRequest;
                currentRequest = null;
                currentRequestPath = null;
                currentDecoder.destroy();
                currentDecoder = null;
                // fire next channel handler
                ctx.fireChannelRead(request);
            }
        } else {
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    } catch (Throwable t) {
        currentRequest = null;
        currentRequestPath = null;
        if (currentDecoder != null) {
            currentDecoder.destroy();
            currentDecoder = null;
        }
        if (ctx.channel().isActive()) {
            byte[] bytes = ExceptionUtils.stringifyException(t).getBytes(ENCODING);
            DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));
            response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
            response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
            ctx.writeAndFlush(response);
        }
    }
}
Also used : HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) DefaultFullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse) EndOfDataDecoderException(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException) IOException(java.io.IOException) LastHttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent) HttpPostRequestDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) QueryStringDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder) DiskFileUpload(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskFileUpload) InterfaceHttpData(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData) File(java.io.File) LastHttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent) HttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent) QueryStringEncoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder)

Example 79 with HttpRequest

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

the class RouterHandler method routed.

private void routed(ChannelHandlerContext channelHandlerContext, RouteResult<?> routeResult, HttpRequest httpRequest) {
    ChannelInboundHandler handler = (ChannelInboundHandler) routeResult.target();
    // The handler may have been added (keep alive)
    ChannelPipeline pipeline = channelHandlerContext.pipeline();
    ChannelHandler addedHandler = pipeline.get(ROUTED_HANDLER_NAME);
    if (handler != addedHandler) {
        if (addedHandler == null) {
            pipeline.addAfter(ROUTER_HANDLER_NAME, ROUTED_HANDLER_NAME, handler);
        } else {
            pipeline.replace(addedHandler, ROUTED_HANDLER_NAME, handler);
        }
    }
    RoutedRequest<?> request = new RoutedRequest<>(routeResult, httpRequest);
    channelHandlerContext.fireChannelRead(request.retain());
}
Also used : ChannelHandler(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler) SimpleChannelInboundHandler(org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler) ChannelInboundHandler(org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandler) ChannelPipeline(org.apache.flink.shaded.netty4.io.netty.channel.ChannelPipeline)

Example 80 with HttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest 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)

Aggregations

HttpRequest (io.netty.handler.codec.http.HttpRequest)332 Test (org.junit.Test)115 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)111 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)102 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)71 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)67 HttpResponse (io.netty.handler.codec.http.HttpResponse)65 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)51 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)45 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)36 ByteBuf (io.netty.buffer.ByteBuf)35 HttpContent (io.netty.handler.codec.http.HttpContent)34 Test (org.junit.jupiter.api.Test)34 Channel (io.netty.channel.Channel)31 HttpMethod (io.netty.handler.codec.http.HttpMethod)31 URI (java.net.URI)30 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)28 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)26 IOException (java.io.IOException)25 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)22