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 */
016
017package org.ametys.web;
018
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.avalon.framework.thread.ThreadSafe;
026import org.apache.cocoon.ResourceNotFoundException;
027import org.apache.cocoon.acting.AbstractAction;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.SourceResolver;
031
032import org.ametys.web.renderingcontext.RenderingContext;
033import org.ametys.web.renderingcontext.RenderingContextHandler;
034
035/**
036 * Used to protect back-office only pipelines.
037 * If the current {@link RenderingContext} is "front", an exception is thrown.
038 */
039public class CheckNotFrontAction extends AbstractAction implements Serviceable, ThreadSafe
040{
041    private RenderingContextHandler _renderingContextHandler;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _renderingContextHandler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE);
047    }
048    
049    @Override
050    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
051    {
052        RenderingContext context = _renderingContextHandler.getRenderingContext();
053        
054        if (context == RenderingContext.FRONT)
055        {
056            throw new ResourceNotFoundException("Access denied to '" + ObjectModelHelper.getRequest(objectModel).getSitemapURI() + "' when coming from front-office.");
057        }
058        
059        return EMPTY_MAP;
060    }
061}