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.web;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.parameters.Parameters;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.acting.ServiceableAction;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Redirector;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.environment.SourceResolver;
028import org.apache.commons.lang.StringUtils;
029
030import org.ametys.plugins.repository.UnknownAmetysObjectException;
031import org.ametys.web.site.SiteConfigurationExtensionPoint;
032
033/**
034 * Check that a site's configuration is valid.<br>
035 * If not, the action can either redirect to the configuration screen or send a HTTP 503 error.
036 */
037public class CheckSiteConfigurationAction extends ServiceableAction
038{
039    
040    /** The site configuration. */
041    protected SiteConfigurationExtensionPoint _siteConfiguration;
042    
043    @Override
044    public void service(ServiceManager serviceManager) throws ServiceException
045    {
046        super.service(serviceManager);
047        _siteConfiguration = (SiteConfigurationExtensionPoint) manager.lookup(SiteConfigurationExtensionPoint.ROLE);
048    }
049    
050    @Override
051    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
052    {
053        Request request = ObjectModelHelper.getRequest(objectModel);
054        
055        String siteName = parameters.getParameter("siteName", "");
056        if (StringUtils.isEmpty(siteName))
057        {
058            siteName = (String) request.getAttribute("site");
059        }
060        if (StringUtils.isEmpty(siteName))
061        {
062            siteName = (String) request.getAttribute("siteName");
063        }
064        
065        // Check that the site configuration is valid.
066        try
067        {
068            if (siteName != null && !_siteConfiguration.isValid(siteName))
069            {
070                // In case of unconfigured site: "redirect" to redirect on the configuration screen, "send-503" to send a 503 error.
071                String action = parameters.getParameter("not-configured-action", "redirect");
072                if ("send-503".equals(action))
073                {
074                    // Invalid configuration: send a 503 to indicate that the site is currently unavailable.
075                    redirector.sendStatus(503);
076                }
077                else
078                {
079                    try
080                    {
081                        // Invalid configuration: redirect to the configuration screen.
082                        redirector.redirect(false, "cocoon://_admin/_plugins/web/administrator/public/load-config/" + siteName + ".html");
083                    }
084                    catch (Exception e)
085                    {
086                        throw new RuntimeException("Cannot redirect to the site configuration screen.", e);
087                    }
088                }
089            }
090        }
091        catch (UnknownAmetysObjectException e)
092        {
093            // Ignore.
094        }
095        
096        return null;
097    }
098
099}