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