Search in sources :

Example 11 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project riposte by Nike-Inc.

the class HttpServletRequestWrapperForRequestInfoTest method getQueryString_delegates_to_requestInfo.

@Test
public void getQueryString_delegates_to_requestInfo() {
    // given
    String queryString = "foo=bar&baz=" + UUID.randomUUID().toString();
    QueryStringDecoder queryStringDecoder = new QueryStringDecoder("/some/path?" + queryString);
    doReturn(queryStringDecoder).when(requestInfoMock).getQueryParams();
    // when
    String result = wrapper.getQueryString();
    // then
    assertThat(result).isEqualTo(queryString);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) Test(org.junit.Test)

Example 12 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project java-chassis by ServiceComb.

the class CseClientHttpRequest method execute.

@Override
public ClientHttpResponse execute() throws IOException {
    RequestMeta requestMeta = createRequestMeta(method.name(), uri);
    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(uri.getRawSchemeSpecificPart());
    Map<String, List<String>> queryParams = queryStringDecoder.parameters();
    Object[] args = this.collectArguments(requestMeta, queryParams);
    // 异常流程,直接抛异常出去
    return this.invoke(requestMeta, args);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) List(java.util.List)

Example 13 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder 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 14 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project cloudstack by apache.

the class HttpUploadServerHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    if (msg instanceof HttpRequest) {
        HttpRequest request = this.request = (HttpRequest) msg;
        responseContent.setLength(0);
        if (request.getMethod().equals(HttpMethod.POST)) {
            URI uri = new URI(request.getUri());
            String signature = null;
            String expires = null;
            String metadata = null;
            String hostname = null;
            long contentLength = 0;
            for (Entry<String, String> entry : request.headers()) {
                switch(entry.getKey()) {
                    case HEADER_SIGNATURE:
                        signature = entry.getValue();
                        break;
                    case HEADER_METADATA:
                        metadata = entry.getValue();
                        break;
                    case HEADER_EXPIRES:
                        expires = entry.getValue();
                        break;
                    case HEADER_HOST:
                        hostname = entry.getValue();
                        break;
                    case HttpHeaders.Names.CONTENT_LENGTH:
                        contentLength = Long.parseLong(entry.getValue());
                        break;
                }
            }
            logger.info("HEADER: signature=" + signature);
            logger.info("HEADER: metadata=" + metadata);
            logger.info("HEADER: expires=" + expires);
            logger.info("HEADER: hostname=" + hostname);
            logger.info("HEADER: Content-Length=" + contentLength);
            QueryStringDecoder decoderQuery = new QueryStringDecoder(uri);
            Map<String, List<String>> uriAttributes = decoderQuery.parameters();
            uuid = uriAttributes.get("uuid").get(0);
            logger.info("URI: uuid=" + uuid);
            UploadEntity uploadEntity = null;
            try {
                // Validate the request here
                storageResource.validatePostUploadRequest(signature, metadata, expires, hostname, contentLength, uuid);
                //create an upload entity. This will fail if entity already exists.
                uploadEntity = storageResource.createUploadEntity(uuid, metadata, contentLength);
            } catch (InvalidParameterValueException ex) {
                logger.error("post request validation failed", ex);
                responseContent.append(ex.getMessage());
                writeResponse(ctx.channel(), HttpResponseStatus.BAD_REQUEST);
                requestProcessed = true;
                return;
            }
            if (uploadEntity == null) {
                logger.error("Unable to create upload entity. An exception occurred.");
                responseContent.append("Internal Server Error");
                writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
                requestProcessed = true;
                return;
            }
            //set the base directory to download the file
            DiskFileUpload.baseDirectory = uploadEntity.getInstallPathPrefix();
            logger.info("base directory: " + DiskFileUpload.baseDirectory);
            try {
                //initialize the decoder
                decoder = new HttpPostRequestDecoder(factory, request);
            } catch (ErrorDataDecoderException | IncompatibleDataDecoderException e) {
                logger.error("exception while initialising the decoder", e);
                responseContent.append(e.getMessage());
                writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
                requestProcessed = true;
                return;
            }
        } else {
            logger.warn("received a get request");
            responseContent.append("only post requests are allowed");
            writeResponse(ctx.channel(), HttpResponseStatus.BAD_REQUEST);
            requestProcessed = true;
            return;
        }
    }
    // check if the decoder was constructed before
    if (decoder != null) {
        if (msg instanceof HttpContent) {
            // New chunk is received
            HttpContent chunk = (HttpContent) msg;
            try {
                decoder.offer(chunk);
            } catch (ErrorDataDecoderException e) {
                logger.error("data decoding exception", e);
                responseContent.append(e.getMessage());
                writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR);
                requestProcessed = true;
                return;
            }
            if (chunk instanceof LastHttpContent) {
                writeResponse(ctx.channel(), readFileUploadData());
                reset();
            }
        }
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) IncompatibleDataDecoderException(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.IncompatibleDataDecoderException) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) URI(java.net.URI) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) UploadEntity(org.apache.cloudstack.storage.template.UploadEntity) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) List(java.util.List) ErrorDataDecoderException(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.ErrorDataDecoderException) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 15 with QueryStringDecoder

use of io.netty.handler.codec.http.QueryStringDecoder in project vert.x by eclipse.

the class HttpUtils method params.

static MultiMap params(String uri) {
    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(uri);
    Map<String, List<String>> prms = queryStringDecoder.parameters();
    MultiMap params = new CaseInsensitiveHeaders();
    if (!prms.isEmpty()) {
        for (Map.Entry<String, List<String>> entry : prms.entrySet()) {
            params.add(entry.getKey(), entry.getValue());
        }
    }
    return params;
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) MultiMap(io.vertx.core.MultiMap) CaseInsensitiveHeaders(io.vertx.core.http.CaseInsensitiveHeaders) List(java.util.List) MultiMap(io.vertx.core.MultiMap) Map(java.util.Map)

Aggregations

QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)28 Test (org.junit.Test)11 List (java.util.List)10 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)5 HttpContent (io.netty.handler.codec.http.HttpContent)5 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)4 HashMap (java.util.HashMap)4 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)3 IOException (java.io.IOException)3 Map (java.util.Map)3 UUID (java.util.UUID)3 ClientHead (com.corundumstudio.socketio.handler.ClientHead)2 ByteBuf (io.netty.buffer.ByteBuf)2 Channel (io.netty.channel.Channel)2 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)2 HttpResponse (io.netty.handler.codec.http.HttpResponse)2