001/*
002 *  Copyright 2012 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.runtime.exception;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.thread.ThreadSafe;
026import org.apache.cocoon.Constants;
027import org.apache.cocoon.acting.ServiceableAction;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.environment.Session;
032import org.apache.cocoon.environment.SourceResolver;
033
034import org.ametys.core.authentication.AuthenticateAction;
035
036
037/**
038 * This action determines which xsl will display the exception.
039 */
040public class ExceptionAction extends ServiceableAction implements ThreadSafe, Contextualizable
041{
042    private Context _context;
043
044    @Override
045    public void contextualize(Context context) throws ContextException
046    {
047        _context = context;
048    }
049    
050    public Map<String, String> act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
051    {
052        // The component may not exist if the PluginManager is not loaded
053        // We just need to display the errors correctly
054        ExceptionHandler exceptionHandler;
055        if (manager.hasService(ExceptionHandler.ROLE))
056        {
057            exceptionHandler = (ExceptionHandler) manager.lookup(ExceptionHandler.ROLE);
058        }
059        else
060        {
061            exceptionHandler = new DefaultExceptionHandler((org.apache.cocoon.environment.Context) _context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT));
062        }
063        
064        String uri = exceptionHandler.getExceptionXSLURI(source);
065        
066        Map<String, String> result = new HashMap<>();
067        result.put("xsl", uri);
068        
069        // When a session was just created, the exception destroyed the cookie by resetting the response, let's recreate it
070        Request request = ObjectModelHelper.getRequest(objectModel);
071        Session session = request.getSession(false);
072        if (session != null && session.isNew())
073        {
074            AuthenticateAction.renewSession(request);
075        }
076        
077        return result;
078    }
079}