Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package io.undertow.server.handlers;
 
 import java.util.Deque;
 import java.util.Map;
 
A handler that will decode the URL and query parameters to the specified charset.

If you are using this handler you must set the io.undertow.UndertowOptions.DECODE_URL parameter to false.

This is not as efficient as using the parsers built in UTF-8 decoder. Unless you need to decode to something other than UTF-8 you should rely on the parsers decoding instead.

Author(s):
Stuart Douglas
public class URLDecodingHandler implements HttpHandler {
    private final HttpHandler next;
    private final String charset;
    public URLDecodingHandler(final HttpHandler nextfinal String charset) {
        this. = next;
        this. = charset;
    }
    @Override
    public void handleRequest(final HttpServerExchange exchangethrows Exception {
        boolean decodeDone = exchange.getConnection().getUndertowOptions().get(.true);
        if (!decodeDone) {
            final StringBuilder sb = new StringBuilder();
            final boolean decodeSlash = exchange.getConnection().getUndertowOptions().get(.false);
            exchange.setRequestPath(URLUtils.decode(exchange.getRequestPath(), decodeSlashsb));
            exchange.setRelativePath(URLUtils.decode(exchange.getRelativePath(), decodeSlashsb));
            exchange.setResolvedPath(URLUtils.decode(exchange.getResolvedPath(), decodeSlashsb));
            if (!exchange.getQueryString().isEmpty()) {
                final TreeMap<StringDeque<String>> newParams = new TreeMap<StringDeque<String>>();
                for (Map.Entry<StringDeque<String>> param : exchange.getQueryParameters().entrySet()) {
                    final Deque<StringnewVales = new ArrayDeque<String>(param.getValue().size());
                    for (String val : param.getValue()) {
                        newVales.add(URLUtils.decode(valtruesb));
                    }
                    newParams.put(URLUtils.decode(param.getKey(), truesb), newVales);
                }
                exchange.getQueryParameters().clear();
                exchange.getQueryParameters().putAll(newParams);
            }
        }
        .handleRequest(exchange);
    }
New to GrepCode? Check out our FAQ X