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.plugins.repository.UnknownAmetysObjectException; 032import org.ametys.plugins.repository.version.VersionableAmetysObject; 033import org.ametys.web.URIPrefixHandler; 034import org.ametys.web.repository.content.WebContent; 035import org.ametys.web.repository.page.Page; 036import org.ametys.web.repository.site.Site; 037import org.ametys.web.repository.site.SiteManager; 038import org.ametys.web.repository.sitemap.Sitemap; 039import org.ametys.web.skin.Skin; 040import org.ametys.web.skin.SkinsManager; 041 042/** 043 * Get a content. 044 */ 045public class GetContentAction extends org.ametys.cms.content.GetContentAction 046{ 047 private SiteManager _siteManager; 048 private SkinsManager _skinsManager; 049 private URIPrefixHandler _uriPrefixHandler; 050 051 @Override 052 public void service(ServiceManager serviceManager) throws ServiceException 053 { 054 super.service(serviceManager); 055 _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE); 056 _skinsManager = (SkinsManager) serviceManager.lookup(SkinsManager.ROLE); 057 _uriPrefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE); 058 } 059 060 @Override 061 public Map<String, String> act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 062 { 063 Map<String, String> result = super.act(redirector, resolver, objectModel, source, parameters); 064 065 Request request = ObjectModelHelper.getRequest(objectModel); 066 067 String pluginName = (String) request.getAttribute("pluginName"); 068 String url = ""; 069 070 Content content = (Content) request.getAttribute(Content.class.getName()); 071 if (content instanceof WebContent) 072 { 073 WebContent webContent = (WebContent) content; 074 075 String siteName = webContent.getSiteName(); 076 String lang = webContent.getLanguage(); 077 String template = null; 078 079 result.put("site", siteName); 080 result.put("lang", lang); 081 082 // Take the HEAD revision 083 String revision = ((VersionableAmetysObject) content).getRevision(); 084 ((VersionableAmetysObject) content).switchToRevision(null); 085 086 Iterator<Page> pages = webContent.getReferencingPages().iterator(); 087 if (pages.hasNext()) 088 { 089 template = pages.next().getTemplate(); 090 } 091 else 092 { 093 // We certainly come from a virtual page, try to get template from referer 094 Site site = _siteManager.getSite(siteName); 095 Sitemap sitemap = site.getSitemap(lang); 096 097 String absoluteUriPrefix = _uriPrefixHandler.getAbsoluteUriPrefix(siteName) + "/" + lang + "/"; 098 099 String referer = request.getHeader("Referer"); 100 if (referer != null && referer.startsWith(absoluteUriPrefix)) 101 { 102 String pathInSitemap = referer.substring(absoluteUriPrefix.length()); 103 try 104 { 105 Page page = sitemap.getChild(pathInSitemap.substring(0, pathInSitemap.indexOf(".html"))); 106 107 // Get template from referer page 108 template = page.getTemplate(); 109 } 110 catch (UnknownAmetysObjectException e) 111 { 112 // No page found, get default template 113 template = _getDefaultTemplate(site); 114 } 115 } 116 else 117 { 118 template = _getDefaultTemplate(site); 119 } 120 } 121 122 url = "cocoon://" + siteName + "/" + lang + "/_plugins/" + pluginName + "/" + template + "/process-form"; 123 result.put("template", template); 124 result.put("url", url); 125 126 ((VersionableAmetysObject) content).switchToRevision(revision); 127 } 128 else 129 { 130 url = "cocoon:/process-form"; 131 result.put("url", url); 132 } 133 134 return result; 135 } 136 137 private String _getDefaultTemplate (Site site) 138 { 139 String skinName = site.getSkinId(); 140 141 Skin skin = _skinsManager.getSkin(skinName); 142 143 Set<String> templates = skin.getTemplates(); 144 145 if (templates.contains("page")) 146 { 147 // Get template 'page' if exists 148 return "page"; 149 } 150 151 // Get the first template 152 return templates.iterator().next(); 153 } 154 155}