Search in sources :

Example 1 with Attribute

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

the class PostRequest method create.

public static IServletRequest create(FullHttpRequest request) throws IOException {
    List<String> names = new ArrayList<>();
    List<String> values = new ArrayList<>();
    HttpPostRequestDecoder decoder = null;
    try {
        decoder = new HttpPostRequestDecoder(request);
    } catch (Exception e) {
        //ignore. this means that the body of the POST request does not have key value pairs
        LOGGER.log(Level.WARNING, "Failed to decode a post message. Fix the API not to have queries as POST body", e);
    }
    if (decoder != null) {
        try {
            List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas();
            for (InterfaceHttpData data : bodyHttpDatas) {
                if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.Attribute)) {
                    Attribute attr = (MixedAttribute) data;
                    names.add(data.getName());
                    values.add(attr.getValue());
                }
            }
        } finally {
            decoder.destroy();
        }
    }
    return new PostRequest(request, new QueryStringDecoder(request.uri()).parameters(), names, values);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) Attribute(io.netty.handler.codec.http.multipart.Attribute) MixedAttribute(io.netty.handler.codec.http.multipart.MixedAttribute) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) ArrayList(java.util.ArrayList) MixedAttribute(io.netty.handler.codec.http.multipart.MixedAttribute) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) IOException(java.io.IOException)

Example 2 with Attribute

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute in project dubbo by alibaba.

the class HttpCommandDecoder method decode.

public static CommandContext decode(HttpRequest request) {
    CommandContext commandContext = null;
    if (request != null) {
        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
        String path = queryStringDecoder.path();
        String[] array = path.split("/");
        if (array.length == 2) {
            String name = array[1];
            // process GET request and POST request separately. Check url for GET, and check body for POST
            if (request.getMethod() == HttpMethod.GET) {
                if (queryStringDecoder.parameters().isEmpty()) {
                    commandContext = CommandContextFactory.newInstance(name);
                    commandContext.setHttp(true);
                } else {
                    List<String> valueList = new ArrayList<String>();
                    for (List<String> values : queryStringDecoder.parameters().values()) {
                        valueList.addAll(values);
                    }
                    commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}), true);
                }
            } else if (request.getMethod() == HttpMethod.POST) {
                HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(request);
                List<String> valueList = new ArrayList<String>();
                for (InterfaceHttpData interfaceHttpData : httpPostRequestDecoder.getBodyHttpDatas()) {
                    if (interfaceHttpData.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
                        Attribute attribute = (Attribute) interfaceHttpData;
                        try {
                            valueList.add(attribute.getValue());
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
                if (valueList.isEmpty()) {
                    commandContext = CommandContextFactory.newInstance(name);
                    commandContext.setHttp(true);
                } else {
                    commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}), true);
                }
            }
        }
    }
    return commandContext;
}
Also used : CommandContext(com.alibaba.dubbo.qos.command.CommandContext) Attribute(io.netty.handler.codec.http.multipart.Attribute) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with Attribute

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute in project duangframework by tcrct.

the class PostDecoder method decoder.

@Override
public Map<String, String[]> decoder() throws Exception {
    HttpPostRequestDecoder requestDecoder = new HttpPostRequestDecoder(HTTP_DATA_FACTORY, request);
    List<InterfaceHttpData> paramsList = requestDecoder.getBodyHttpDatas();
    if (null != paramsList && !paramsList.isEmpty()) {
        Map<String, List<String>> params = new HashMap<>();
        for (InterfaceHttpData httpData : paramsList) {
            Attribute attribute = (Attribute) httpData;
            String key = attribute.getName();
            String value = attribute.getValue();
            parseValue2List(params, key, value);
            paramsMap.put(key, params.get(key).toArray(EMPTY_ARRAYS));
        }
    }
    return paramsMap;
}
Also used : HashMap(java.util.HashMap) Attribute(io.netty.handler.codec.http.multipart.Attribute) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) List(java.util.List) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)

Example 4 with Attribute

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute 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 5 with Attribute

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

the class AbstractHandlerTest method testFileCleanup.

@Test
public void testFileCleanup() throws Exception {
    final Path dir = temporaryFolder.newFolder().toPath();
    final Path file = dir.resolve("file");
    Files.createFile(file);
    RestfulGateway mockRestfulGateway = new TestingRestfulGateway.Builder().build();
    final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () -> CompletableFuture.completedFuture(mockRestfulGateway);
    CompletableFuture<Void> requestProcessingCompleteFuture = new CompletableFuture<>();
    TestHandler handler = new TestHandler(requestProcessingCompleteFuture, mockGatewayRetriever);
    RouteResult<?> routeResult = new RouteResult<>("", "", Collections.emptyMap(), Collections.emptyMap(), "");
    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, TestHandler.TestHeaders.INSTANCE.getTargetRestEndpointURL(), Unpooled.wrappedBuffer(new byte[0]));
    RoutedRequest<?> routerRequest = new RoutedRequest<>(routeResult, request);
    Attribute<FileUploads> attribute = new SimpleAttribute();
    attribute.set(new FileUploads(dir));
    Channel channel = mock(Channel.class);
    when(channel.attr(any(AttributeKey.class))).thenReturn(attribute);
    ChannelHandlerContext context = mock(ChannelHandlerContext.class);
    when(context.channel()).thenReturn(channel);
    handler.respondAsLeader(context, routerRequest, mockRestfulGateway);
    // the (asynchronous) request processing is not yet complete so the files should still exist
    Assert.assertTrue(Files.exists(file));
    requestProcessingCompleteFuture.complete(null);
    Assert.assertFalse(Files.exists(file));
}
Also used : Path(java.nio.file.Path) 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) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) ChannelHandlerContext(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext) AttributeKey(org.apache.flink.shaded.netty4.io.netty.util.AttributeKey) CompletableFuture(java.util.concurrent.CompletableFuture) RouteResult(org.apache.flink.runtime.rest.handler.router.RouteResult) RoutedRequest(org.apache.flink.runtime.rest.handler.router.RoutedRequest) TestingRestfulGateway(org.apache.flink.runtime.webmonitor.TestingRestfulGateway) RestfulGateway(org.apache.flink.runtime.webmonitor.RestfulGateway) TestingRestfulGateway(org.apache.flink.runtime.webmonitor.TestingRestfulGateway) Test(org.junit.Test)

Aggregations

Attribute (io.netty.handler.codec.http.multipart.Attribute)10 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)9 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)8 IOException (java.io.IOException)8 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)3 HttpRequest (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest)3 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)2 StreamResetException (io.vertx.core.http.StreamResetException)2 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)2 DefaultFullHttpRequest (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest)2 Attribute (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute)2 CommandContext (com.alibaba.dubbo.qos.command.CommandContext)1 ByteBuf (io.netty.buffer.ByteBuf)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1