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.plugins.site.proxy; 017 018import java.util.ArrayList; 019import java.util.List; 020 021import org.apache.avalon.framework.configuration.Configurable; 022import org.apache.avalon.framework.configuration.Configuration; 023import org.apache.avalon.framework.configuration.ConfigurationException; 024import org.apache.cocoon.environment.Request; 025import org.apache.cocoon.environment.Response; 026import org.apache.cocoon.environment.Session; 027import org.apache.http.HttpResponse; 028import org.apache.http.client.methods.HttpUriRequest; 029 030import org.ametys.runtime.plugin.component.AbstractLogEnabled; 031 032/** 033 * Proxies a session attribute to the back-office, under the form of a request header prefixed by "X-Ametys-Session-". 034 */ 035public class SessionAttributeRequestProxy extends AbstractLogEnabled implements BackOfficeRequestProxy, Configurable 036{ 037 /** The header prefix */ 038 public static final String HEADER_PREFIX = "X-Ametys-Session-"; 039 040 /** The list of session attribute to proxy */ 041 protected List<String> _attributes = new ArrayList<>(); 042 043 public void configure(Configuration configuration) throws ConfigurationException 044 { 045 for (Configuration attributeConfig : configuration.getChildren("attribute")) 046 { 047 _attributes.add(attributeConfig.getValue()); 048 } 049 } 050 051 public void prepareBackOfficeRequest(Request request, HttpUriRequest backOfficeRequest) 052 { 053 Session session = request.getSession(false); 054 if (session != null) 055 { 056 for (String attributeName : _attributes) 057 { 058 Object attribute = session.getAttribute(attributeName); 059 if (attribute != null) 060 { 061 backOfficeRequest.setHeader(HEADER_PREFIX + attributeName, attribute.toString()); 062 } 063 } 064 } 065 } 066 067 public void handleBackOfficeResponse(Response response, HttpResponse backOfficeResponse) 068 { 069 // nothing to do 070 } 071}