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.net.URI; 019import java.util.Collections; 020 021import org.apache.avalon.framework.context.Context; 022import org.apache.avalon.framework.context.ContextException; 023import org.apache.avalon.framework.context.Contextualizable; 024import org.apache.avalon.framework.logger.AbstractLogEnabled; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.avalon.framework.service.Serviceable; 028import org.apache.cocoon.components.ContextHelper; 029import org.apache.cocoon.environment.Request; 030 031import org.ametys.cms.transformation.ConsistencyChecker.CHECK; 032import org.ametys.cms.transformation.URIResolver; 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 RenderingContext context = _renderingContextHandler.getRenderingContext(); 091 092 String pagePath; 093 String siteName = null; 094 095 try 096 { 097 Page page = _resolver.resolveById(uri); 098 pagePath = page.getSitemap().getName() + "/" + page.getPathInSitemap() + ".html"; 099 siteName = page.getSiteName(); 100 } 101 catch (UnknownAmetysObjectException e) 102 { 103 getLogger().warn("Link to unexisting page " + uri); 104 return ""; 105 } 106 107 try 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 return url + "/" + pagePath; 118 } 119 120 if (!(context == RenderingContext.BACK)) 121 { 122 StringBuilder result = new StringBuilder(); 123 124 if (internal) 125 { 126 result.append("cocoon://").append(siteName); 127 } 128 else if (absolute) 129 { 130 result.append(_prefixHandler.getAbsoluteUriPrefix(siteName)); 131 } 132 else 133 { 134 result.append(_prefixHandler.getUriPrefix(siteName)); 135 } 136 137 result.append("/").append(pagePath); 138 139 return new URI(null, null, result.toString(), null).toASCIIString(); 140 } 141 else // back 142 { 143 return "javascript:parent.Ametys.tool.ToolsManager.openTool('uitool-page', {id:'" + uri + "', followNavigation: true}) && void(0);"; 144 } 145 } 146 catch (Exception e) 147 { 148 throw new IllegalStateException(e); 149 } 150 } 151 152 @Override 153 public String resolveImage(String uri, int height, int width, boolean download, boolean absolute, boolean internal) 154 { 155 return resolve(uri, download, absolute, internal); 156 } 157 158 @Override 159 public String resolveImageAsBase64(String uri, int height, int width) 160 { 161 return resolve(uri, false, false, false); 162 } 163 164 @Override 165 public String resolveBoundedImage(String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal) 166 { 167 return resolve(uri, download, absolute, internal); 168 } 169 170 @Override 171 public String resolveBoundedImageAsBase64(String uri, int maxHeight, int maxWidth) 172 { 173 return resolve(uri, false, false, false); 174 } 175 176 @Override 177 public String resolveCroppedImage(String uri, int cropHeight, int cropWidth, boolean download, boolean absolute, boolean internal) 178 { 179 return resolve(uri, download, absolute, internal); 180 } 181 182 @Override 183 public String resolveCroppedImageAsBase64(String uri, int cropHeight, int cropWidth) 184 { 185 return resolve(uri, false, false, false); 186 } 187 188 @Override 189 public CHECK checkLink(String uri, boolean shortTest) 190 { 191 try 192 { 193 _resolver.resolveById(uri); 194 return CHECK.SUCCESS; 195 } 196 catch (UnknownAmetysObjectException e) 197 { 198 return CHECK.UNKNOWN; 199 } 200 } 201 202 @Override 203 public I18nizableText getLabel(String uri) 204 { 205 try 206 { 207 Page page = _resolver.resolveById(uri); 208 return new I18nizableText("plugin.web", "PLUGINS_WEB_LINK_PAGE_LABEL", Collections.singletonList(page.getTitle())); 209 } 210 catch (UnknownAmetysObjectException e) 211 { 212 return new I18nizableText("plugin.web", "PLUGINS_WEB_LINK_PAGE_UNKNOWN"); 213 } 214 } 215}