Enhancements for the aai-common library
[aai/aai-common.git] / aai-aaf-auth / src / main / java / org / onap / aai / aaf / auth / AafRequestWrapper.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.aaf.auth;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletRequestWrapper;
25 import java.util.*;
26
27 /**
28  * The AafRequestWrapper sets the user in the principal name
29  */
30 public class AafRequestWrapper extends HttpServletRequestWrapper {
31
32     private final Map<String, String> customHeaders;
33
34     public AafRequestWrapper(HttpServletRequest request) {
35         super(request);
36         this.customHeaders = new HashMap<String, String>();
37     }
38
39     public void putHeader(String name, String value) {
40         this.customHeaders.put(name, value);
41     }
42
43     @Override
44     public String getHeader(String name) {
45         String headerValue = customHeaders.get(name);
46         if (headerValue != null) {
47             return headerValue;
48         }
49         return (((HttpServletRequest) getRequest()).getHeader(name));
50     }
51
52     @Override
53     public Enumeration<String> getHeaderNames() {
54         Set<String> nameSet = new HashSet<String>(customHeaders.keySet());
55
56         Enumeration<String> e = ((HttpServletRequest) getRequest()).getHeaderNames();
57         while (e.hasMoreElements()) {
58             String headerName = e.nextElement();
59             nameSet.add(headerName);
60         }
61         return Collections.enumeration(nameSet);
62     }
63
64     @Override
65     public Enumeration<String> getHeaders(String name) {
66         String myHeaderValue = customHeaders.get(name);
67         Set<String> headerValueSet = new HashSet<String>();
68         if (myHeaderValue != null) {
69             headerValueSet.add(myHeaderValue);
70         }
71         Enumeration<String> e = ((HttpServletRequest) getRequest()).getHeaders(name);
72         while (e.hasMoreElements()) {
73             String headerValue = e.nextElement();
74             headerValueSet.add(headerValue);
75         }
76         return Collections.enumeration(headerValueSet);
77     }
78 }