fixing warnings from checkstyle in common-app-api
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / impl / MutableHttpServletRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.common.impl;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletRequestWrapper;
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Set;
31
32 public final class MutableHttpServletRequest extends HttpServletRequestWrapper {
33     // holds custom header and value mapping
34     private final Map<String, String> customHeaders;
35
36     public MutableHttpServletRequest(HttpServletRequest request) {
37         super(request);
38         this.customHeaders = new HashMap<>();
39     }
40
41     public void putHeader(String name, String value) {
42         this.customHeaders.put(name, value);
43     }
44
45     public String getHeader(String name) {
46         // check the custom headers first
47         String headerValue = customHeaders.get(name);
48
49         if (headerValue != null) {
50             return headerValue;
51         }
52         // else return from into the original wrapped object
53         return ((HttpServletRequest) getRequest()).getHeader(name);
54     }
55
56     public Enumeration<String> getHeaderNames() {
57         // create a set of the custom header names
58         Set<String> set = new HashSet<>(customHeaders.keySet());
59
60         // now add the headers from the wrapped request object
61         @SuppressWarnings("unchecked")
62         Enumeration<String> e = ((HttpServletRequest) getRequest()).getHeaderNames();
63         while (e.hasMoreElements()) {
64             // add the names of the request headers into the list
65             String n = e.nextElement();
66             set.add(n);
67         }
68
69         // create an enumeration from the set and return
70         return Collections.enumeration(set);
71     }
72 }