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.FilenameUtils; 025import org.ametys.core.util.URIUtils; 026import org.ametys.plugins.explorer.resources.Resource; 027import org.ametys.plugins.repository.UnknownAmetysObjectException; 028import org.ametys.runtime.i18n.I18nizableText; 029import org.ametys.web.repository.page.Page; 030 031/** 032 * {@link URIResolver} for type "attachment-page".<br> 033 * These links point to a file from the attachments of the current Page. 034 */ 035public class PageAttachmentURIResolver extends AttachmentURIResolver 036{ 037 @Override 038 public String getType() 039 { 040 return "attachment-page"; 041 } 042 043 @Override 044 public String _resolve(String uri, String uriArgument, boolean download, boolean absolute, boolean internal) 045 { 046 Request request = ContextHelper.getRequest(_context); 047 Page page; 048 String path; 049 050 try 051 { 052 Resource resource = (Resource) _resolver.resolveById(uri); 053 path = resource.getResourcePath(); 054 055 page = (Page) request.getAttribute(Page.class.getName()); 056 } 057 catch (UnknownAmetysObjectException e) 058 { 059 getLogger().warn("Link to unexisting resource " + uri); 060 return ""; 061 } 062 catch (Exception e) 063 { 064 throw new IllegalStateException(e); 065 } 066 067 String filename = FilenameUtils.encodePath(path); 068 String basePath = org.apache.commons.io.FilenameUtils.removeExtension(filename); 069 String extension = org.apache.commons.io.FilenameUtils.getExtension(filename); 070 071 StringBuilder resultPath = new StringBuilder(); 072 resultPath.append(getUriPrefix(page, download, absolute, internal)); 073 074 resultPath.append("/").append(page.getSitemapName()) 075 .append("/").append(page.getPathInSitemap()) 076 .append("/_attachment") 077 .append(basePath) 078 .append(uriArgument) 079 .append(extension.isEmpty() ? "" : "." + extension); 080 081 return URIUtils.encodeURI(resultPath.toString(), download ? Collections.singletonMap("download", "true") : null); 082 } 083 084 @Override 085 public I18nizableText getLabel(String uri) 086 { 087 try 088 { 089 Resource resource = (Resource) _resolver.resolveById(uri); 090 return new I18nizableText("plugin.web", "PLUGINS_WEB_LINK_PAGEATTACHMENT_LABEL", Collections.singletonList(resource.getResourcePath())); 091 } 092 catch (UnknownAmetysObjectException e) 093 { 094 return new I18nizableText("plugin.web", "PLUGINS_WEB_LINK_PAGEATTACHMENT_UNKNOWN"); 095 } 096 } 097}