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.HashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.cocoon.components.ContextHelper; 024import org.apache.cocoon.environment.Request; 025 026import org.ametys.cms.data.RichText; 027import org.ametys.cms.transformation.URIResolver; 028import org.ametys.core.util.FilenameUtils; 029import org.ametys.core.util.URIUtils; 030import org.ametys.web.URIPrefixHandler; 031import org.ametys.web.WebHelper; 032 033/** 034 * {@link URIResolver} for type "local". 035 * These links or images point to resources local to a rich text. 036 */ 037public class LocalURIResolver extends org.ametys.cms.transformation.LocalURIResolver 038{ 039 /** the {@link URIPrefixHandler} s*/ 040 protected URIPrefixHandler _prefixHandler; 041 042 @Override 043 public void service(ServiceManager manager) throws ServiceException 044 { 045 super.service(manager); 046 _prefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE); 047 } 048 049 @Override 050 public String resolve(String uri, boolean download, boolean absolute, boolean internal) 051 { 052 String result = _resolve(uri, download, absolute, internal, ""); 053 054 if (result == null) 055 { 056 super.resolve(uri, download, absolute, internal); 057 } 058 059 return result; 060 } 061 062 @Override 063 public String resolveImage(String uri, int height, int width, boolean download, boolean absolute, boolean internal) 064 { 065 if (height == 0 && width == 0) 066 { 067 return resolve(uri, download, absolute, internal); 068 } 069 070 String result = _resolve(uri, download, absolute, internal, "_" + height + "x" + width); 071 072 if (result == null) 073 { 074 super.resolveImage(uri, height, width, download, absolute, internal); 075 } 076 077 return result; 078 } 079 080 @Override 081 public String resolveBoundedImage(String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal) 082 { 083 if (maxHeight == 0 && maxWidth == 0) 084 { 085 return resolve(uri, download, absolute, internal); 086 } 087 088 String result = _resolve(uri, download, absolute, internal, "_max" + maxWidth + "x" + maxWidth); 089 090 if (result == null) 091 { 092 super.resolveBoundedImage(uri, maxHeight, maxWidth, download, absolute, internal); 093 } 094 095 return result; 096 } 097 098 /** 099 * Actually resolves the URI. 100 * @param uri the link URI. 101 * @param download true if the pointed resource is to be downloaded. 102 * @param absolute true if the url must be absolute 103 * @param internal true to get an internal URI. 104 * @param suffix a suffix to be appended to the generated path. Should not be null. 105 * @return the path to the resource. 106 */ 107 protected String _resolve(String uri, boolean download, boolean absolute, boolean internal, String suffix) 108 { 109 URIInfo infos = getInfos(uri, true, null); 110 111 Request request = ContextHelper.getRequest(_context); 112 113 String siteName = WebHelper.getSiteName(request, infos.getContent()); 114 if (siteName == null) 115 { 116 return null; 117 } 118 119 return _resolve(infos, uri, siteName, download, absolute, internal, suffix); 120 } 121 122 /** 123 * Actually resolves the URI. 124 * @param infos info about parsed uri. 125 * @param uri the link URI. 126 * @param siteName the siteName, if any. 127 * @param download true if the pointed resource is to be downloaded. 128 * @param absolute true if the url must be absolute 129 * @param internal true to get an internal URI. 130 * @param suffix a suffix to be appended to the generated path. Should not be null. 131 * @return the path to the resource. 132 */ 133 protected String _resolve(URIInfo infos, String uri, String siteName, boolean download, boolean absolute, boolean internal, String suffix) 134 { 135 try 136 { 137 RichText richText = infos.getContent().getValue(infos.getAttribute()); 138 richText.getAttachment(infos.getFilename()); 139 } 140 catch (Exception e) 141 { 142 getLogger().warn("Cannot resolve link " + uri, e); 143 return ""; 144 } 145 146 StringBuilder resultPath = new StringBuilder(); 147 148 String filename = FilenameUtils.encodeName(infos.getFilename()); 149 150 String baseName = org.apache.commons.io.FilenameUtils.getBaseName(filename); 151 String extension = org.apache.commons.io.FilenameUtils.getExtension(filename); 152 153 resultPath.append(_prefixHandler.computeUriPrefix(siteName, absolute, internal)) 154 .append("/_richText-file") 155 .append(FilenameUtils.encodePath(infos.getContent().getPath())) 156 .append("/_attribute/").append(infos.getAttribute()) 157 .append("/_data/") 158 .append(baseName) 159 .append(suffix) 160 .append(extension.isEmpty() ? "" : "." + extension); 161 162 Map<String, String> params = new HashMap<>(); 163 164 if (download) 165 { 166 params.put("download", "true"); 167 } 168 169 if (infos.getContentVersion() != null) 170 { 171 params.put("contentVersion", infos.getContentVersion()); 172 } 173 174 return URIUtils.encodeURI(resultPath.toString(), params); 175 } 176}