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.Redirector;
029import org.apache.cocoon.environment.SourceResolver;
030
031
032/**
033 * This action determines which xsl will display the exception.
034 */
035public class ExceptionAction extends ServiceableAction implements ThreadSafe, Contextualizable
036{
037    private Context _context;
038
039    @Override
040    public void contextualize(Context context) throws ContextException
041    {
042        _context = context;
043    }
044    
045    public Map<String, String> act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
046    {
047        // Le composant peut ne pas exister si le pluginManager n'est pas chargé
048        // Il faut justement afficher les erreurs correctement.
049        ExceptionHandler exceptionHandler;
050        if (manager.hasService(ExceptionHandler.ROLE))
051        {
052            exceptionHandler = (ExceptionHandler) manager.lookup(ExceptionHandler.ROLE);
053        }
054        else
055        {
056            exceptionHandler = new DefaultExceptionHandler((org.apache.cocoon.environment.Context) _context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT));
057        }
058        
059        String uri = exceptionHandler.getExceptionXSLURI(source);
060        
061        Map<String, String> result = new HashMap<>();
062        result.put("xsl", uri);
063        return result;
064    }
065}