Example usage for io.netty.handler.codec.http QueryStringDecoder QueryStringDecoder

List of usage examples for io.netty.handler.codec.http QueryStringDecoder QueryStringDecoder

Introduction

In this page you can find the example usage for io.netty.handler.codec.http QueryStringDecoder QueryStringDecoder.

Prototype

public QueryStringDecoder(URI uri, Charset charset, int maxParams) 

Source Link

Document

Creates a new decoder that decodes the specified URI encoded in the specified charset.

Usage

From source file:com.github.mike10004.seleniumhelp.TrafficMonitorFilter.java

License:Apache License

protected void captureRequestContent(HttpRequest httpRequest, byte[] fullMessage, HarRequest harRequest) {
    if (fullMessage.length == 0) {
        return;/*from w w  w .j ava2  s. c o m*/
    }

    String contentType = HttpHeaders.getHeader(httpRequest, HttpHeaders.Names.CONTENT_TYPE);
    if (contentType == null) {
        log.warn("No content type specified in request to {}. Content will be treated as {}",
                httpRequest.getUri(), BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE);
        contentType = BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE;
    }

    HarPostData postData = new HarPostData();
    harRequest.setPostData(postData);

    postData.setMimeType(contentType);

    final boolean urlEncoded = contentType.startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);

    Charset charset;
    try {
        charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentType);
    } catch (UnsupportedCharsetException e) {
        log.warn(
                "Found unsupported character set in Content-Type header '{}' in HTTP request to {}. Content will not be captured in HAR.",
                contentType, httpRequest.getUri(), e);
        return;
    }

    if (charset == null) {
        // no charset specified, so use the default -- but log a message since this might not encode the data correctly
        charset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
        log.debug("No charset specified; using charset {} to decode contents to {}", charset,
                httpRequest.getUri());
    }

    if (urlEncoded) {
        String textContents = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);

        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(textContents, charset, false);

        ImmutableList.Builder<HarPostDataParam> paramBuilder = ImmutableList.builder();

        for (Map.Entry<String, List<String>> entry : queryStringDecoder.parameters().entrySet()) {
            for (String value : entry.getValue()) {
                paramBuilder.add(new HarPostDataParam(entry.getKey(), value));
            }
        }

        harRequest.getPostData().setParams(paramBuilder.build());
    } else {
        //TODO: implement capture of files and multipart form data

        // not URL encoded, so let's grab the body of the POST and capture that
        String postBody = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);
        harRequest.getPostData().setText(postBody);
    }
}

From source file:org.restexpress.Request.java

License:Apache License

/**
 * Returns the body as a Map of name/value pairs from a url-form-encoded form submission.
 * The values returned are URL-decoded depending on the value of shouldDecode.
 * // w ww. j  a v a2  s .c o m
 * @param shouldDecode true if the returned values should be URL-decoded
 * @return
 */
public Map<String, List<String>> getBodyFromUrlFormEncoded(boolean shouldDecode) {
    if (shouldDecode) {
        QueryStringDecoder qsd = new QueryStringDecoder(getBody().toString(ContentType.CHARSET),
                ContentType.CHARSET, false);
        return qsd.parameters();
    }

    QueryStringParser qsp = new QueryStringParser(getBody().toString(ContentType.CHARSET), false);
    return qsp.getParameters();
}

From source file:ratpack.session.clientside.internal.DefaultClientSessionService.java

License:Apache License

@Override
public ConcurrentMap<String, Object> deserializeSession(Cookie cookie) {
    ConcurrentMap<String, Object> sessionStorage = new ConcurrentHashMap<>();

    String encodedPairs = cookie == null ? null : cookie.value();
    if (encodedPairs != null) {
        String[] parts = encodedPairs.split(SESSION_SEPARATOR);
        if (parts.length == 2) {
            byte[] urlEncoded = DECODER.decode(parts[0]);
            byte[] digest = DECODER.decode(parts[1]);

            try {
                byte[] expectedDigest = signer.sign(Unpooled.wrappedBuffer(urlEncoded));

                if (Arrays.equals(digest, expectedDigest)) {
                    byte[] message;
                    if (crypto == null) {
                        message = urlEncoded;
                    } else {
                        message = crypto.decrypt(Unpooled.wrappedBuffer(urlEncoded));
                    }//from  ww w.j  a  va2s.c om

                    String payload = new String(message, CharsetUtil.UTF_8);
                    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(payload, CharsetUtil.UTF_8,
                            false);
                    Map<String, List<String>> decoded = queryStringDecoder.parameters();
                    for (Map.Entry<String, List<String>> entry : decoded.entrySet()) {
                        String value = entry.getValue().isEmpty() ? null : entry.getValue().get(0);
                        sessionStorage.put(entry.getKey(), value);
                    }
                }
            } catch (Exception e) {
                throw Exceptions.uncheck(e);
            }
        }
    }

    return sessionStorage;
}