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.SiteConfigurationManager;
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    /** The site configuration manager. */
040    protected SiteConfigurationManager _siteConfigurationManager;
041    
042    @Override
043    public void service(ServiceManager serviceManager) throws ServiceException
044    {
045        super.service(serviceManager);
046        _siteConfigurationManager = (SiteConfigurationManager) manager.lookup(SiteConfigurationManager.ROLE);
047    }
048    
049    @Override
050    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
051    {
052        Request request = ObjectModelHelper.getRequest(objectModel);
053        
054        String siteName = parameters.getParameter("siteName", "");
055        if (StringUtils.isEmpty(siteName))
056        {
057            siteName = (String) request.getAttribute("site");
058        }
059        if (StringUtils.isEmpty(siteName))
060        {
061            siteName = (String) request.getAttribute("siteName");
062        }
063        
064        // Check that the site configuration is valid.
065        try
066        {
067            if (siteName != null && !_siteConfigurationManager.isSiteConfigurationValid(siteName))
068            {
069                // In case of unconfigured site: "redirect" to redirect on the configuration screen, "send-503" to send a 503 error.
070                String action = parameters.getParameter("not-configured-action", "redirect");
071                if ("send-503".equals(action))
072                {
073                    // Invalid configuration: send a 503 to indicate that the site is currently unavailable.
074                    redirector.sendStatus(503);
075                }
076                else
077                {
078                    try
079                    {
080                        // Invalid configuration: redirect to the configuration screen.
081                        redirector.redirect(false, "cocoon://_admin/_plugins/web/administrator/public/load-config/" + siteName + ".html");
082                    }
083                    catch (Exception e)
084                    {
085                        throw new RuntimeException("Cannot redirect to the site configuration screen.", e);
086                    }
087                }
088            }
089        }
090        catch (UnknownAmetysObjectException e)
091        {
092            // Ignore.
093        }
094        
095        return null;
096    }
097
098}