001/*
002 *  Copyright 2021 Anyware Services
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.ametys.web;
017
018import java.util.Optional;
019
020import org.apache.avalon.framework.component.Component;
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.cocoon.components.ContextHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.environment.Session;
027
028import org.ametys.core.util.SessionAttributeProvider;
029import org.ametys.runtime.plugin.component.AbstractLogEnabled;
030
031/**
032 * Component for getting a session attribute, potentially proxied by the front-office.
033 */
034public class WebSessionAttributeProvider extends AbstractLogEnabled implements SessionAttributeProvider, Component, Contextualizable
035{
036    private Context _context;
037    
038    public void contextualize(Context context) throws ContextException
039    {
040        _context = context;        
041    }
042    
043    /**
044     * Retrieve the session attribute, either proxied by the back-office, or in the back-office session.
045     * @param name the attribute name
046     * @return the value stored either in the front-office session, or in the back-office session, or null if none
047     */
048    public Optional<Object> getSessionAttribute(String name)
049    {
050        Request request = ContextHelper.getRequest(_context);
051        if (request != null)
052        {
053            String attributeFromHeader = request.getHeader("X-Ametys-Session-" + name);
054            if (attributeFromHeader != null)
055            {
056                return Optional.of(attributeFromHeader);
057            }
058            
059            Session session = request.getSession(false);
060            if (session != null)
061            {
062                Object attribute = session.getAttribute(name);
063                if (attribute instanceof String str)
064                {
065                    return Optional.of(str);
066                }
067                else if (attribute != null)
068                {
069                    throw new UnsupportedOperationException("Only attribute of type String are supported by the WebSessionAttributeProvider.");
070                }
071            }
072        }
073        return Optional.empty();
074    }
075}