package com.bq.oss.lib.ws.filter;

import java.io.IOException;
import java.util.HashMap;

import javax.annotation.Priority;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.MultivaluedMap;

import com.bq.oss.lib.ws.api.error.ErrorResponseFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Created by Francisco Sanchez on 29/10/14.
 */
@PreMatching @Priority(1) public class HeadersQueryParamsFilter extends OptionalContainerRequestFilter {
    private static final String HEADER_KEY = "headers";
    private final ObjectMapper objectMapper;
    private final TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {};

    public HeadersQueryParamsFilter(boolean enabled, ObjectMapper objectMapper) {
        super(enabled);
        this.objectMapper = objectMapper;
    }

    @Override
    public void filter(ContainerRequestContext request) {
        String headerQueryParameters = request.getUriInfo().getQueryParameters().getFirst(HEADER_KEY);
        if (null != headerQueryParameters) {
            try {
                replaceHeaders(request.getHeaders(), objectMapper.readValue(headerQueryParameters, typeRef));
            } catch (IOException e) {
                throw new WebApplicationException(ErrorResponseFactory.getInstance().badRequest(
                        new com.bq.oss.lib.ws.model.Error("bad_request", "invalid query parameter format: " + HEADER_KEY)));
            }
        }
    }

    private void replaceHeaders(MultivaluedMap<String, String> headers, HashMap<String, String> queryParamHeaders) {
        queryParamHeaders.keySet().forEach(key -> headers.putSingle(key, queryParamHeaders.get(key)));
    }


}

@@ -39,432 +39,521 @@
 ort 
-java.io.IOException;%0Aimport java.util.HashMap;%0A%0Aimport javax.annotation.Priority;%0Aimport javax.ws.rs.WebApplicationException;%0Aimport javax.ws.rs.container.ContainerRequestContext;%0Aimport javax.ws.rs.container.PreMatching;%0Aimport javax.ws.rs.core.MultivaluedMap;%0A%0Aimport com.bq.oss.lib.ws.api.error.ErrorResponseFactory;%0Aimport com.fasterxml.jackson.core.type.TypeReference;%0Aimport com.fasterxml.jackson.databind.ObjectMapper
+com.bq.oss.lib.ws.api.error.ErrorResponseFactory;%0Aimport com.fasterxml.jackson.core.type.TypeReference;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport javax.annotation.Priority;%0Aimport javax.ws.rs.WebApplicationException;%0Aimport javax.ws.rs.container.ContainerRequestContext;%0Aimport javax.ws.rs.container.PreMatching;%0Aimport java.io.IOException;%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.net.URLDecoder;%0Aimport java.util.HashMap
 ;%0A%0A/
@@ -709,16 +709,17 @@
 ilter %7B%0A
+%0A
     priv
@@ -928,16 +928,112 @@
 () %7B%7D;%0A%0A
+    private static final Logger LOG = LoggerFactory.getLogger(HeadersQueryParamsFilter.class);%0A%0A
     publ
@@ -1437,101 +1437,524 @@
     
-replaceHeaders(request.getHeaders(), objectMapper.readValue(headerQueryParameters, typeRef));
+HashMap%3CString, String%3E queryParamHeaders = objectMapper.readValue(headerQueryParameters, typeRef);%0A                for (String key : queryParamHeaders.keySet()) %7B%0A                    try %7B%0A                        request.getHeaders().putSingle(key, URLDecoder.decode(queryParamHeaders.get(key), %22UTF-8%22));%0A                    %7D catch (UnsupportedEncodingException ignored) %7B%0A                        LOG.error(%22Cannot decode query param value: %22 + queryParamHeaders.get(key));%0A                    %7D%0A                %7D
 %0A   
@@ -2243,236 +2243,7 @@
    %7D
-%0A%0A    private void replaceHeaders(MultivaluedMap%3CString, String%3E headers, HashMap%3CString, String%3E queryParamHeaders) %7B%0A        queryParamHeaders.keySet().forEach(key -%3E headers.putSingle(key, queryParamHeaders.get(key)));%0A    %7D%0A%0A
 %0A%7D%0A

Fix bug filter headerQueryParams