001/* 002 * Copyright 2011 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.blog.posts; 017 018import java.net.URI; 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; 027 028import org.ametys.cms.repository.Content; 029import org.ametys.cms.transformation.ConsistencyChecker.CHECK; 030import org.ametys.cms.transformation.URIResolver; 031import org.ametys.plugins.blog.BlogPageHandler; 032import org.ametys.plugins.repository.AmetysObjectResolver; 033import org.ametys.plugins.repository.UnknownAmetysObjectException; 034import org.ametys.runtime.i18n.I18nizableText; 035import org.ametys.web.URIPrefixHandler; 036import org.ametys.web.renderingcontext.RenderingContext; 037import org.ametys.web.renderingcontext.RenderingContextHandler; 038import org.ametys.web.repository.content.WebContent; 039import org.ametys.web.repository.page.Page; 040import org.ametys.web.repository.site.SiteManager; 041 042/** 043 * Resolves post page uri. 044 */ 045public class PostUriResolver extends AbstractLogEnabled implements URIResolver, Serviceable, Contextualizable 046{ 047 /** The ametys object resolver. */ 048 protected AmetysObjectResolver _resolver; 049 050 /** The blog cache manager. */ 051 protected BlogPageHandler _blogRootPageHandler; 052 053 /** The site manager. */ 054 protected SiteManager _siteManager; 055 056 /** The avalon context. */ 057 protected Context _context; 058 059 private RenderingContextHandler _renderingContextHandler; 060 061 private URIPrefixHandler _uriPrefixHandler; 062 063 @Override 064 public void contextualize(Context context) throws ContextException 065 { 066 _context = context; 067 } 068 069 @Override 070 public void service(ServiceManager manager) throws ServiceException 071 { 072 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 073 _blogRootPageHandler = (BlogPageHandler) manager.lookup(BlogPageHandler.ROLE); 074 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 075 _renderingContextHandler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE); 076 _uriPrefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE); 077 } 078 079 @Override 080 public String getType() 081 { 082 return "blog-post"; 083 } 084 085 @Override 086 public String resolve(String uri, boolean download, boolean absolute, boolean internal) 087 { 088 RenderingContext currentContext = _renderingContextHandler.getRenderingContext(); 089 090 String pagePath; 091 String pageId; 092 String siteName = null; 093 094 try 095 { 096 Content content = _resolver.resolveById(uri); 097 098 if (content instanceof WebContent) 099 { 100 WebContent webContent = (WebContent) content; 101 siteName = webContent.getSiteName(); 102 103 Page page = _blogRootPageHandler.getPostPage(webContent); 104 pageId = page.getId(); 105 pagePath = page.getSitemap().getName() + "/" + page.getPathInSitemap() + ".html"; 106 } 107 else 108 { 109 getLogger().warn("Link to unexisting page " + uri); 110 return ""; 111 } 112 } 113 catch (UnknownAmetysObjectException e) 114 { 115 getLogger().warn("Link to unexisting page " + uri); 116 return ""; 117 } 118 119 try 120 { 121 if (currentContext == RenderingContext.FRONT) 122 { 123 String url = _siteManager.getSite(siteName).getUrl(); 124 return url + "/" + pagePath; 125 } 126 127 if (currentContext != RenderingContext.BACK) 128 { 129 StringBuilder result = new StringBuilder(); 130 131 if (internal) 132 { 133 result.append("cocoon://").append(siteName); 134 } 135 else if (absolute) 136 { 137 result.append(_uriPrefixHandler.getAbsoluteUriPrefix(siteName)); 138 } 139 else 140 { 141 result.append(_uriPrefixHandler.getUriPrefix(siteName)); 142 } 143 144 result.append("/").append(pagePath); 145 146 return new URI(null, null, result.toString(), null).toASCIIString(); 147 } 148 else // back 149 { 150 return "javascript:parent.Ametys.tool.ToolsManager.openTool('uitool-page', {id:'" + pageId + "'});"; 151 } 152 } 153 catch (Exception e) 154 { 155 throw new IllegalStateException(e); 156 } 157 } 158 159 @Override 160 public String resolveImage(String uri, int height, int width, boolean download, boolean absolute, boolean internal) 161 { 162 return resolve(uri, download, absolute, internal); 163 } 164 165 @Override 166 public String resolveImageAsBase64(String uri, int height, int width) 167 { 168 return resolve(uri, false, false, false); 169 } 170 171 @Override 172 public String resolveBoundedImage(String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal) 173 { 174 return resolve(uri, download, absolute, internal); 175 } 176 177 @Override 178 public String resolveBoundedImageAsBase64(String uri, int maxHeight, int maxWidth) 179 { 180 return resolve(uri, false, false, false); 181 } 182 183 @Override 184 public CHECK checkLink(String uri, boolean shortTest) 185 { 186 try 187 { 188 _resolver.resolveById(uri); 189 190 return CHECK.SUCCESS; 191 } 192 catch (UnknownAmetysObjectException e) 193 { 194 return CHECK.NOT_FOUND; 195 } 196 } 197 198 @Override 199 public I18nizableText getLabel(String uri) 200 { 201 try 202 { 203 Content content = _resolver.resolveById(uri); 204 205 return new I18nizableText(content.getTitle()); 206 } 207 catch (UnknownAmetysObjectException e) 208 { 209 return new I18nizableText(""); 210 } 211 } 212}