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.ServletContext;
023
024/**
025 * Extract attributes from application scope.
026 *
027 * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for
028 * Jakarta EE 9.</p>
029 */
030public class ApplicationScopeExtractor implements AttributeExtractor {
031
032    /**
033     * The servlet context.
034     */
035    private ServletContext context;
036
037    /**
038     * Constructor.
039     *
040     * @param context The servlet context.
041     */
042    public ApplicationScopeExtractor(ServletContext context) {
043        this.context = context;
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        context.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        context.removeAttribute(name);
065    }
066
067    /**
068     * The enumeration of the keys of the stored attributes.
069     *
070     * @return The keys.
071     */
072    @Override
073    public Enumeration<String> getKeys() {
074        return context.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 context.getAttribute(key);
087    }
088}