View Javadoc
1   /*
2    * Copyright 2023 Web-Legacy
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.tiles.request.jakarta.servlet.extractor;
17  
18  import java.util.Enumeration;
19  
20  import org.apache.tiles.request.attribute.EnumeratedValuesExtractor;
21  
22  import jakarta.servlet.http.HttpServletRequest;
23  import jakarta.servlet.http.HttpServletResponse;
24  
25  /**
26   * Extract header values from an HTTP request.
27   *
28   * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for
29   * Jakarta EE 9.</p>
30   */
31  public class HeaderExtractor implements EnumeratedValuesExtractor {
32  
33      /**
34       * The request.
35       */
36      private HttpServletRequest request;
37  
38      /**
39       * The response.
40       */
41      private HttpServletResponse response;
42  
43      /**
44       * Constructor.
45       *
46       * @param request  The request.
47       * @param response The response.
48       */
49      public HeaderExtractor(HttpServletRequest request,
50              HttpServletResponse response) {
51  
52          this.request = request;
53          this.response = response;
54      }
55  
56      /**
57       * The enumeration of the keys in the stored attributes.
58       *
59       * @return The keys.
60       */
61      @Override
62      public Enumeration<String> getKeys() {
63          return request.getHeaderNames();
64      }
65  
66      /**
67       * Returns the value of the attribute with the given key.
68       *
69       * @param key The key of the attribute.
70       *
71       * @return The value.
72       */
73      @Override
74      public String getValue(String key) {
75          return request.getHeader(key);
76      }
77  
78      /**
79       * Returns the values stored at the given key.
80       *
81       * @param key The key of the attribute.
82       *
83       * @return The values of the attribute.
84       */
85      @Override
86      public Enumeration<String> getValues(String key) {
87          return request.getHeaders(key);
88      }
89  
90      /**
91       * Sets a value for the given key.
92       *
93       * @param key The key of the attribute.
94       *
95       * @param value The value of the attribute.
96       */
97      @Override
98      public void setValue(String key, String value) {
99          response.setHeader(key, value);
100     }
101 }