001/*
002 * Copyright 2023 Web-Legacy
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.apache.tiles.request.jakarta.servlet.extractor;
017
018import java.util.Enumeration;
019
020import org.apache.tiles.request.attribute.AttributeExtractor;
021
022import jakarta.servlet.http.HttpServletRequest;
023
024/**
025 * Extracts attributes from request scope.
026 *
027 * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for
028 * Jakarta EE 9.</p>
029 */
030public class RequestScopeExtractor implements AttributeExtractor {
031
032    /**
033     * The servlet request.
034     */
035    private HttpServletRequest request;
036
037    /**
038     * Constructor.
039     *
040     * @param request The servlet request.
041     */
042    public RequestScopeExtractor(HttpServletRequest request) {
043        this.request = request;
044    }
045
046    /**
047     * Sets a value for the given key.
048     *
049     * @param name  The key of the attribute.
050     * @param value The value of the attribute.
051     */
052    @Override
053    public void setValue(String name, Object value) {
054        request.setAttribute(name, value);
055    }
056
057    /**
058     * Removes an attribute.
059     *
060     * @param name The key of the attribute to remove.
061     */
062    @Override
063    public void removeValue(String name) {
064        request.removeAttribute(name);
065    }
066
067    /**
068     * The enumeration of the keys in the stored attributes.
069     *
070     * @return The keys.
071     */
072    @Override
073    public Enumeration<String> getKeys() {
074        return request.getAttributeNames();
075    }
076
077    /**
078     * Returns the value of the attribute with the given key.
079     *
080     * @param key The key of the attribute.
081     *
082     * @return The value.
083     */
084    @Override
085    public Object getValue(String key) {
086        return request.getAttribute(key);
087    }
088}