/*
 * Copyright 2014 JBoss Inc
 *
 * Licensed 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.
 */
package io.apiman.gateway.engine.beans;

import java.util.HashMap;

/**
 * Extends the basic {@link HashMap} class in order to provide case insensitive 
 * lookups.
 *
 * @author eric.wittmann@redhat.com
 */
public class HeaderHashMap extends HashMap<String, String> {
    
    private static final long serialVersionUID = -8627183971399152775L;

    private HashMap<String, String> caseInsensitiveIndex = new HashMap<>();
    
    /**
     * Constructor.
     */
    public HeaderHashMap() {
    }
    
    /**
     * @see java.util.HashMap#get(java.lang.Object)
     */
    @Override
    public String get(Object key) {
        String rval = super.get(key);
        if (rval == null) {
            String trimmedKey = trim((String)key);
            rval = caseInsensitiveIndex.get(trimmedKey.toLowerCase());
        }
        return rval;
    }
    
    /**
     * @see java.util.HashMap#containsKey(java.lang.Object)
     */
    @Override
    public boolean containsKey(Object key) {
        boolean rval = super.containsKey(key);
        if (!rval) {
            String trimmedKey = trim((String)key);
            rval = caseInsensitiveIndex.containsKey(trimmedKey.toLowerCase());
        }
        return rval;
    }
    
    /**
     * @see java.util.HashMap#put(java.lang.Object, java.lang.Object)
     */
    @Override
    public String put(String key, String value) {
        String trimmedKey = trim(key);
        String trimmedValue = trim(value);
        String rval = super.put(trimmedKey, trimmedValue);
        caseInsensitiveIndex.put(trimmedKey.toLowerCase(), trimmedValue);
        return rval;
    }
    
    /**
     * @see java.util.HashMap#remove(java.lang.Object)
     */
    @Override
    public String remove(Object key) {
        String trimmedKey = trim((String)key);
        caseInsensitiveIndex.remove(trimmedKey.toLowerCase());
        return super.remove(trimmedKey);
    }

    /**
     * Trim string of whitespace.
     * 
     * @param string string to trim
     * @return trimmed string, or null if null was provided.
     */
    private static String trim(String string) {
        return string == null ? null : string.trim();
    }

}

@@ -738,18 +738,17 @@
 ensitive
- 
 %0A
+
  * looku
@@ -855,21 +855,17 @@
 ring%3E %7B%0A
-    
 %0A
+
     priv
@@ -1005,21 +1005,17 @@
 ap%3C%3E();%0A
-    
 %0A
+
     /**%0A
@@ -1073,28 +1073,186 @@
 p() %7B%0A    %7D%0A
+%0A
     
+/**%0A     * @see java.util.HashMap#clear()%0A     */%0A    @Override%0A    public void clear() %7B%0A        super.clear();%0A        caseInsensitiveIndex.clear();%0A    %7D%0A
 %0A    /**%0A   
@@ -1575,37 +1575,33 @@
 urn rval;%0A    %7D%0A
-    
 %0A
+
     /**%0A     * @
@@ -1946,37 +1946,33 @@
 urn rval;%0A    %7D%0A
-    
 %0A
+
     /**%0A     * @
@@ -2351,13 +2351,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -2672,23 +2672,22 @@
 espace.%0A
+
      *
- 
 %0A     * 

fix a bug in the header hashmap impl