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.AttributeExtractor;
21
22 import jakarta.servlet.http.HttpServletRequest;
23
24 /**
25 * Extracts attributes from request scope.
26 *
27 * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for
28 * Jakarta EE 9.</p>
29 */
30 public class RequestScopeExtractor implements AttributeExtractor {
31
32 /**
33 * The servlet request.
34 */
35 private HttpServletRequest request;
36
37 /**
38 * Constructor.
39 *
40 * @param request The servlet request.
41 */
42 public RequestScopeExtractor(HttpServletRequest request) {
43 this.request = request;
44 }
45
46 /**
47 * Sets a value for the given key.
48 *
49 * @param name The key of the attribute.
50 * @param value The value of the attribute.
51 */
52 @Override
53 public void setValue(String name, Object value) {
54 request.setAttribute(name, value);
55 }
56
57 /**
58 * Removes an attribute.
59 *
60 * @param name The key of the attribute to remove.
61 */
62 @Override
63 public void removeValue(String name) {
64 request.removeAttribute(name);
65 }
66
67 /**
68 * The enumeration of the keys in the stored attributes.
69 *
70 * @return The keys.
71 */
72 @Override
73 public Enumeration<String> getKeys() {
74 return request.getAttributeNames();
75 }
76
77 /**
78 * Returns the value of the attribute with the given key.
79 *
80 * @param key The key of the attribute.
81 *
82 * @return The value.
83 */
84 @Override
85 public Object getValue(String key) {
86 return request.getAttribute(key);
87 }
88 }