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