package es.tid.fiware.iot.ac.rs;

/*
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import com.sun.jersey.api.core.HttpContext;
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Jersey Provider implementation for {@link Correlator} annotation.
 */
public class CorrelatorProvider
        extends AbstractHttpContextInjectable<String>
        implements InjectableProvider<Correlator, Type> {

    private final String correlatorHeader;

    private static final Logger LOGGER = LoggerFactory.getLogger(CorrelatorProvider.class);

    public CorrelatorProvider(String correlatorHeader) {
        this.correlatorHeader = correlatorHeader;
    }

    @Override
    public String getValue(HttpContext httpContext) {
        List<String> headers = httpContext.getRequest().getRequestHeader(correlatorHeader);
        if (headers == null || headers.size() == 0) {
            LOGGER.error("Correlator Header {} not present", correlatorHeader);
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        } else if (headers.size() > 1) {
            LOGGER.error("Too many Correlator Headers {}:", correlatorHeader, headers);
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        } else {
            LOGGER.debug("correlator found {}:", headers.get(0));
            MDC.put("client", headers.get(0));
            return headers.get(0);
        }
    }

    @Override
    public ComponentScope getScope() {
        return ComponentScope.PerRequest;
    }

    @Override
    public Injectable getInjectable(ComponentContext componentContext, Correlator correlator, Type type) {
        if (type.equals(String.class)) {
            return this;
        }
        return null;
    }
}

@@ -2088,16 +2088,18 @@
       if
+ (
  (header
@@ -2104,19 +2104,61 @@
 ers 
-=
+!
 = null
- %7C%7C 
+) && (headers.size() %3E 0) ) %7B%0A            if (
 head
@@ -2171,17 +2171,17 @@
 ze() == 
-0
+1
 ) %7B%0A    
@@ -2192,23 +2192,27 @@
     
+    
 LOGGER.
-error(%22C
+debug(%22c
 orre
@@ -2221,168 +2221,152 @@
 tor 
-Header %7B%7D not present%22, correlatorHeader);%0A            throw new WebApplicationException(Response.Status.BAD_REQUEST);%0A        %7D else if (headers.size() %3E 1) %7B%0A
+found %7B%7D:%22, headers.get(0));%0A                MDC.put(%22client%22, headers.get(0));%0A                return headers.get(0);%0A            %7D else %7B%0A    
     
@@ -2461,16 +2461,20 @@
         
+    
 throw ne
@@ -2529,16 +2529,30 @@
 QUEST);%0A
+            %7D%0A
         
@@ -2601,81 +2601,18 @@
 tor 
+not 
 found
- %7B%7D:%22, headers.get(0));%0A            MDC.put(%22client%22, headers.get(0)
+%22
 );%0A 
@@ -2625,38 +2625,28 @@
      return 
-headers.get(0)
+null
 ;%0A        %7D%0A
@@ -2644,32 +2644,33 @@
        %7D%0A    %7D%0A%0A
+%0A
     @Override%0A  

allow non correlator header