Adding additional headers on a ServletRequest


References

Problem

Simply, we can’t add or modify request headers on given HttpServletRequest instances.

Solution

The only solution, at least mentioned in above links, is using HttpServletRequestWrapper class. I wrote, for my own, a class which wraps additional headers around the actual request instance.

public class RequestHeaderWrapper extends HttpServletRequestWrapper {

    public RequestHeaderWrapper(
        final HttpServletRequest request,
        final Map<String, List<String>> precedingHeaders,
        final Map<String, List<String>> succeedingHeaders) {

        super(request);

        headers = new HashMap<>();

        if (precedingHeaders != null) {
            for (final String name : precedingHeaders.keySet()) {
                List<String> values = headers.get(name);
                if (values == null) {
                    values = new ArrayList<>();
                    headers.put(name, values);
                }
                values.addAll(precedingHeaders.get(name));
            }
        }

        for (final Enumeration<String> names = request.getHeaderNames();
             names.hasMoreElements();) {
            final String name = names.nextElement();
            List<String> value = headers.get(name);
            if (value == null) {
                value = new ArrayList<>();
                headers.put(name, value);
            }
            value.addAll(Collections.list(request.getHeaders(name)));
        }

        if (succeedingHeaders != null) {
            for (final String name : succeedingHeaders.keySet()) {
                List<String> values = headers.get(name);
                if (values == null) {
                    values = new ArrayList<>();
                    headers.put(name, values);
                }
                values.addAll(succeedingHeaders.get(name));
            }
        }
    }

    @Override
    public String getHeader(final String name) {
        final List<String> values = headers.get(name);
        if (values != null && !values.isEmpty()) {
            return values.get(0);
        }
        return null;
    }

    @Override
    public Enumeration<String> getHeaders(final String name) {
        List<String> values = headers.get(name);
        if (values == null) {
            return Collections.emptyEnumeration();
        }
        return Collections.enumeration(values);
    }

    @Override
    public Enumeration<String> getHeaderNames() {
        return Collections.enumeration(headers.keySet());
    }

    private final Map<String, List<String>> headers;
}

Full and latest source code is here and the apidocs is here.

Apache Maven

Please check the central if you’re using Apache Maven.

<dependency>
  <groupId>com.googlecode.jinahya</groupId>
  <artifactId>jinahya-ee</artifactId>
  <version>@@?</version>
</dependency>

Usage

There are some factory methods for simple use.

public class MyFilter implements Filter {

    @Override
    public void doFilter(final ServletRequest request,
                         final ServletResponse response,
                         final FilterChain chain)
        throws IOException, ServletException {

        final ServletRequest wrapper =
            RequestHeaderWrapper.newPrecedingInstance(
                requerst, "Accept", "application/xml");
        chain.doFilter(wrapper, response);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s