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.web.editor; 017 018import java.util.Collections; 019 020import org.apache.avalon.framework.context.Context; 021import org.apache.avalon.framework.context.ContextException; 022import org.apache.avalon.framework.context.Contextualizable; 023import org.apache.avalon.framework.logger.AbstractLogEnabled; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027import org.apache.cocoon.components.ContextHelper; 028import org.apache.cocoon.environment.Request; 029 030import org.ametys.cms.transformation.ConsistencyChecker.CHECK; 031import org.ametys.cms.transformation.URIResolver; 032import org.ametys.core.util.URIUtils; 033import org.ametys.plugins.repository.AmetysObjectResolver; 034import org.ametys.plugins.repository.UnknownAmetysObjectException; 035import org.ametys.runtime.i18n.I18nizableText; 036import org.ametys.web.URIPrefixHandler; 037import org.ametys.web.WebHelper; 038import org.ametys.web.content.GetSiteAction; 039import org.ametys.web.renderingcontext.RenderingContext; 040import org.ametys.web.renderingcontext.RenderingContextHandler; 041import org.ametys.web.repository.page.Page; 042import org.ametys.web.repository.site.SiteManager; 043import org.ametys.web.transformation.xslt.AmetysXSLTHelper; 044 045/** 046 * {@link URIResolver} for type "page".<br> 047 * These links point to a CMS page. 048 */ 049public class PageURIResolver extends AbstractLogEnabled implements URIResolver, Serviceable, Contextualizable 050{ 051 private AmetysObjectResolver _resolver; 052 private SiteManager _siteManager; 053 private Context _context; 054 private RenderingContextHandler _renderingContextHandler; 055 private URIPrefixHandler _prefixHandler; 056 057 @Override 058 public void contextualize(Context context) throws ContextException 059 { 060 _context = context; 061 } 062 063 @Override 064 public void service(ServiceManager manager) throws ServiceException 065 { 066 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 067 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 068 _renderingContextHandler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE); 069 _prefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE); 070 } 071 072 @Override 073 public String getType() 074 { 075 return "page"; 076 } 077 078 @Override 079 public String resolve(String uri, boolean download, boolean absolute, boolean internal) 080 { 081 Request request = ContextHelper.getRequest(_context); 082 083 // Issue CMS-3391 084 @SuppressWarnings("deprecation") 085 String currentSite = (String) request.getAttribute(GetSiteAction.OVERRIDE_SITE_REQUEST_ATTR); 086 if (currentSite == null) 087 { 088 currentSite = WebHelper.getSiteName(request); 089 } 090 091 RenderingContext context = _renderingContextHandler.getRenderingContext(); 092 093 String pagePath; 094 String siteName = null; 095 096 try 097 { 098 Page page = _resolver.resolveById(uri); 099 pagePath = page.getSitemap().getName() + "/" + page.getPathInSitemap() + ".html"; 100 siteName = page.getSiteName(); 101 } 102 catch (UnknownAmetysObjectException e) 103 { 104 getLogger().warn("Link to unexisting page " + uri); 105 return ""; 106 } 107 108 if (!siteName.equals(currentSite) && context == RenderingContext.FRONT) 109 { 110 String url = _siteManager.getSite(siteName).getUrl(); 111 112 if (AmetysXSLTHelper.isEditionMode()) 113 { 114 url += "/edition"; 115 } 116 117 return url + "/" + pagePath; 118 } 119 120 if (!(context == RenderingContext.BACK)) 121 { 122 StringBuilder result = new StringBuilder(); 123 124 result.append(_prefixHandler.computeUriPrefix(siteName, absolute, internal)); 125 result.append("/").append(pagePath); 126 127 return URIUtils.encodePath(result.toString()); 128 } 129 else // back 130 { 131 return "javascript:parent.Ametys.tool.ToolsManager.openTool('uitool-page', {id:'" + uri + "', followNavigation: true}) && void(0);"; 132 } 133 } 134 135 @Override 136 public String resolveImage(String uri, int height, int width, boolean download, boolean absolute, boolean internal) 137 { 138 return resolve(uri, download, absolute, internal); 139 } 140 141 @Override 142 public String resolveImageAsBase64(String uri, int height, int width) 143 { 144 return resolve(uri, false, false, false); 145 } 146 147 @Override 148 public String resolveBoundedImage(String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal) 149 { 150 return resolve(uri, download, absolute, internal); 151 } 152 153 @Override 154 public String resolveBoundedImageAsBase64(String uri, int maxHeight, int maxWidth) 155 { 156 return resolve(uri, false, false, false); 157 } 158 159 @Override 160 public String resolveCroppedImage(String uri, int cropHeight, int cropWidth, boolean download, boolean absolute, boolean internal) 161 { 162 return resolve(uri, download, absolute, internal); 163 } 164 165 @Override 166 public String resolveCroppedImageAsBase64(String uri, int cropHeight, int cropWidth) 167 { 168 return resolve(uri, false, false, false); 169 } 170 171 @Override 172 public CHECK checkLink(String uri, boolean shortTest) 173 { 174 try 175 { 176 _resolver.resolveById(uri); 177 return CHECK.SUCCESS; 178 } 179 catch (UnknownAmetysObjectException e) 180 { 181 return CHECK.UNKNOWN; 182 } 183 } 184 185 @Override 186 public I18nizableText getLabel(String uri) 187 { 188 try 189 { 190 Page page = _resolver.resolveById(uri); 191 return new I18nizableText("plugin.web", "PLUGINS_WEB_LINK_PAGE_LABEL", Collections.singletonList(page.getTitle())); 192 } 193 catch (UnknownAmetysObjectException e) 194 { 195 return new I18nizableText("plugin.web", "PLUGINS_WEB_LINK_PAGE_UNKNOWN"); 196 } 197 } 198}