001/*
002 *  Copyright 2010 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.plugins.forms.processing;
017
018import java.util.Iterator;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029
030import org.ametys.cms.repository.Content;
031import org.ametys.core.right.RightManager;
032import org.ametys.core.user.CurrentUserProvider;
033import org.ametys.core.user.UserIdentity;
034import org.ametys.plugins.repository.UnknownAmetysObjectException;
035import org.ametys.plugins.repository.version.VersionableAmetysObject;
036import org.ametys.runtime.authentication.AccessDeniedException;
037import org.ametys.runtime.authentication.AuthorizationRequiredException;
038import org.ametys.web.URIPrefixHandler;
039import org.ametys.web.pageaccess.IsSitemapRestrictedAction;
040import org.ametys.web.repository.content.WebContent;
041import org.ametys.web.repository.page.Page;
042import org.ametys.web.repository.site.Site;
043import org.ametys.web.repository.site.SiteManager;
044import org.ametys.web.repository.sitemap.Sitemap;
045import org.ametys.web.skin.Skin;
046import org.ametys.web.skin.SkinsManager;
047
048/**
049 * Get a content.
050 */
051public class GetContentAction extends org.ametys.cms.content.GetContentAction
052{
053    private SiteManager _siteManager;
054    private SkinsManager _skinsManager;
055    private URIPrefixHandler _uriPrefixHandler;
056    private RightManager _rightManager;
057    private CurrentUserProvider _currentUserProvied;
058    
059    @Override
060    public void service(ServiceManager serviceManager) throws ServiceException
061    {
062        super.service(serviceManager);
063        _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE);
064        _skinsManager = (SkinsManager) serviceManager.lookup(SkinsManager.ROLE);
065        _uriPrefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE);
066        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
067        _currentUserProvied = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
068    }
069    
070    @Override
071    public Map<String, String> act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
072    {
073        Map<String, String> result = super.act(redirector, resolver, objectModel, source, parameters);
074        
075        Request request = ObjectModelHelper.getRequest(objectModel);
076        
077        String pluginName = (String) request.getAttribute("pluginName");
078        String url = "";
079        
080        Content content = (Content) request.getAttribute(Content.class.getName());
081        if (content instanceof WebContent)
082        {
083            WebContent webContent = (WebContent) content;
084            
085            // Protecting the form
086            if (!_rightManager.currentUserHasReadAccess(webContent))
087            {
088                UserIdentity user = _currentUserProvied.getUser();
089                if (user == null)
090                {
091                    throw new AuthorizationRequiredException();
092                }
093                else
094                {
095                    throw new AccessDeniedException("User '" + UserIdentity.userIdentityToString(user) + "' can not post form for content '" + webContent.getId() + "'");
096                }
097            }
098            
099            // The user is granted, let's mark the request to ensure that IsSitemapRestrictedAction will not block (since the root autorization required can be stronger)  FORMS-330
100            request.setAttribute(IsSitemapRestrictedAction.REQUEST_ATTRIBUTE_DONOTCHECKRIGHTS, true);
101            
102            String siteName = webContent.getSiteName();
103            String lang = webContent.getLanguage();
104            String template = null;
105            
106            result.put("site", siteName);
107            result.put("lang", lang);
108            
109            // Take the HEAD revision
110            String revision = ((VersionableAmetysObject) content).getRevision();
111            ((VersionableAmetysObject) content).switchToRevision(null);
112            
113            Iterator<Page> pages = webContent.getReferencingPages().iterator();
114            if (pages.hasNext())
115            {
116                template = pages.next().getTemplate();
117            }
118            else
119            {
120                // We certainly come from a virtual page, try to get template from referer
121                Site site = _siteManager.getSite(siteName);
122                Sitemap sitemap = site.getSitemap(lang);
123                
124                String absoluteUriPrefix = _uriPrefixHandler.getAbsoluteUriPrefix(siteName) + "/" + lang + "/";
125                
126                String referer = request.getHeader("Referer");
127                if (referer != null && referer.startsWith(absoluteUriPrefix))
128                {
129                    String pathInSitemap = referer.substring(absoluteUriPrefix.length());
130                    try
131                    {
132                        Page page = sitemap.getChild(pathInSitemap.substring(0, pathInSitemap.indexOf(".html")));
133                        
134                        // Get template from referer page
135                        template = page.getTemplate();
136                    }
137                    catch (UnknownAmetysObjectException e)
138                    {
139                        // No page found, get default template
140                        template = _getDefaultTemplate(site);
141                    }
142                }
143                else
144                {
145                    template = _getDefaultTemplate(site);
146                }
147            }
148            
149            url = "cocoon://" + siteName + "/" + lang + "/_plugins/" + pluginName + "/" + template + "/process-form";
150            result.put("template", template);
151            result.put("url", url);
152            
153            ((VersionableAmetysObject) content).switchToRevision(revision);
154        }
155        else
156        {
157            url = "cocoon:/process-form";
158            result.put("url", url);
159        }
160        
161        return result;
162    }
163    
164    private String _getDefaultTemplate (Site site)
165    {
166        String skinName = site.getSkinId();
167        
168        Skin skin = _skinsManager.getSkin(skinName);
169        
170        Set<String> templates = skin.getTemplates();
171        
172        if (templates.contains("page"))
173        {
174            // Get template 'page' if exists
175            return "page";
176        }
177        
178        // Get the first template
179        return templates.iterator().next();
180    }
181
182}