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.transformation.URIResolver; 027import org.ametys.core.util.URLEncoder; 028import org.ametys.plugins.repository.UnknownAmetysObjectException; 029import org.ametys.plugins.repository.metadata.RichText; 030import org.ametys.web.URIPrefixHandler; 031import org.ametys.web.WebHelper; 032 033/** 034 * {@link URIResolver} for type "local".resources local to a Content 035 * These links or images point resources local to a Content. 036 */ 037public class LocalURIResolver extends org.ametys.cms.transformation.LocalURIResolver 038{ 039 /** The URI prefix handler */ 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, "/_contents", ""); 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, "/_contents-images", "_" + 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, "/_contents-images", "_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 prefix a prefix to be prepended to the generated path. Should not be null. 105 * @param suffix a suffix to be appended to the generated path. Should not be null. 106 * @return the path to the resource. 107 */ 108 protected String _resolve(String uri, boolean download, boolean absolute, boolean internal, String prefix, String suffix) 109 { 110 URIInfo infos = getInfos(uri, true); 111 112 Request request = ContextHelper.getRequest(_context); 113 114 String siteName = WebHelper.getSiteName(request, infos.getContent()); 115 if (siteName == null) 116 { 117 return null; 118 } 119 120 RichText richText = _getMeta(infos.getContent().getMetadataHolder(), infos.getMetadata()); 121 try 122 { 123 richText.getAdditionalDataFolder().getFile(infos.getPath()); 124 } 125 catch (UnknownAmetysObjectException e) 126 { 127 getLogger().warn("Cannot resolve link " + uri); 128 return ""; 129 } 130 131 Map<String, String> params = new HashMap<>(); 132 133 if (download) 134 { 135 params.put("download", "true"); 136 } 137 138 if (infos.getContentVersion() != null) 139 { 140 params.put("contentVersion", infos.getContentVersion()); 141 } 142 143 StringBuilder resultPath = new StringBuilder(); 144 145 if (internal) 146 { 147 resultPath.append("cocoon://").append(siteName); 148 } 149 else if (absolute) 150 { 151 resultPath.append(_prefixHandler.getAbsoluteUriPrefix(siteName)); 152 } 153 else 154 { 155 resultPath.append(_prefixHandler.getUriPrefix(siteName)); 156 } 157 158 resultPath.append(prefix) 159 .append(infos.getContent().getPath().replaceAll(":", "%3A")) 160 .append("/_metadata/").append(infos.getMetadata()) 161 .append("/_data/") 162 .append(URLEncoder.encodePath(infos.getPath())) 163 .append(suffix); 164 165 return URLEncoder.encodeURI(resultPath.toString(), params); 166 } 167}