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.web.editor; 017 018import java.util.Collections; 019 020import org.apache.cocoon.components.ContextHelper; 021import org.apache.cocoon.environment.Request; 022 023import org.ametys.cms.transformation.URIResolver; 024import org.ametys.core.util.URIUtils; 025import org.ametys.plugins.explorer.resources.Resource; 026import org.ametys.plugins.repository.UnknownAmetysObjectException; 027import org.ametys.runtime.i18n.I18nizableText; 028import org.ametys.web.repository.page.Page; 029 030/** 031 * {@link URIResolver} for type "attachment-page".<br> 032 * These links point to a file from the attachments of the current Page. 033 */ 034public class PageAttachmentURIResolver extends AttachmentURIResolver 035{ 036 @Override 037 public String getType() 038 { 039 return "attachment-page"; 040 } 041 042 private String _getUrl(String uri, boolean download, boolean absolute, boolean internal, String keyword, String addBeforeExtension) 043 { 044 Request request = ContextHelper.getRequest(_context); 045 Page page; 046 String path; 047 048 try 049 { 050 Resource resource = (Resource) _resolver.resolveById(uri); 051 path = resource.getResourcePath(); 052 053 page = (Page) request.getAttribute(Page.class.getName()); 054 } 055 catch (UnknownAmetysObjectException e) 056 { 057 getLogger().warn("Link to unexisting resource " + uri); 058 return ""; 059 } 060 catch (Exception e) 061 { 062 throw new IllegalStateException(e); 063 } 064 065 int i = path.lastIndexOf("."); 066 String extension = path.substring(i); 067 path = path.substring(0, i); 068 069 StringBuilder resultPath = new StringBuilder(); 070 resultPath.append(getUriPrefix(page, download, absolute, internal)); 071 072 resultPath.append("/").append(page.getSitemapName()) 073 .append("/").append(page.getPathInSitemap()) 074 .append("/").append(keyword) 075 .append(path) 076 .append(addBeforeExtension) 077 .append(extension); 078 079 return resultPath.toString(); 080 } 081 082 @Override 083 public String resolve(String uri, boolean download, boolean absolute, boolean internal) 084 { 085 String encodedPath = URIUtils.encodePath(_getUrl(uri, download, absolute, internal, "_attachments", "")); 086 return URIUtils.encodeURI(encodedPath, download ? Collections.singletonMap("download", "true") : null); 087 } 088 089 @Override 090 public String resolveBoundedImage(String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal) 091 { 092 if (maxHeight == 0 && maxWidth == 0) 093 { 094 return resolve(uri, download, absolute, internal); 095 } 096 097 String encodedPath = URIUtils.encodePath(_getUrl(uri, download, absolute, internal, "_attachments-images", "_max" + maxHeight + "x" + maxWidth)); 098 return URIUtils.encodeURI(encodedPath, download ? Collections.singletonMap("download", "true") : null); 099 } 100 101 @Override 102 public String resolveImage(String uri, int height, int width, boolean download, boolean absolute, boolean internal) 103 { 104 if (height == 0 && width == 0) 105 { 106 return resolve(uri, download, absolute, internal); 107 } 108 109 String encodedPath = URIUtils.encodePath(_getUrl(uri, download, absolute, internal, "_attachments-images", "_" + height + "x" + width)); 110 return URIUtils.encodeURI(encodedPath, download ? Collections.singletonMap("download", "true") : null); 111 } 112 113 @Override 114 public I18nizableText getLabel(String uri) 115 { 116 try 117 { 118 Resource resource = (Resource) _resolver.resolveById(uri); 119 return new I18nizableText("plugin.web", "PLUGINS_WEB_LINK_PAGEATTACHMENT_LABEL", Collections.singletonList(resource.getResourcePath())); 120 } 121 catch (UnknownAmetysObjectException e) 122 { 123 return new I18nizableText("plugin.web", "PLUGINS_WEB_LINK_PAGEATTACHMENT_UNKNOWN"); 124 } 125 } 126}