001/* 002 * Copyright 2026 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.workspaces.project; 017 018import java.io.InputStream; 019import java.util.Collections; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.avalon.framework.service.Serviceable; 025 026import org.ametys.cms.data.Binary; 027import org.ametys.cms.transformation.AbstractURIResolver; 028import org.ametys.cms.transformation.ConsistencyChecker.CHECK; 029import org.ametys.cms.transformation.URIResolver; 030import org.ametys.core.util.FilenameUtils; 031import org.ametys.core.util.ImageResolverHelper; 032import org.ametys.core.util.URIUtils; 033import org.ametys.plugins.repository.UnknownAmetysObjectException; 034import org.ametys.runtime.i18n.I18nizableText; 035import org.ametys.web.URIPrefixHandler; 036import org.ametys.web.repository.site.Site; 037import org.ametys.web.repository.site.SiteManager; 038 039/** 040 * {@link URIResolver} for typ "workspaces-site-parameter" 041 */ 042public class WorkspacesSiteParameterUriResolver extends AbstractURIResolver implements Serviceable 043{ 044 private SiteManager _siteManager; 045 private URIPrefixHandler _webPrefixHandler; 046 private ProjectManager _projectManager; 047 048 @Override 049 public void service(ServiceManager manager) throws ServiceException 050 { 051 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 052 _webPrefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE); 053 _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE); 054 } 055 056 @Override 057 public String getType() 058 { 059 return "workspaces-site-parameter"; 060 } 061 062 @Override 063 protected String _resolve(String uri, String uriArgument, boolean download, boolean absolute, boolean internal) 064 { 065 Site site = null; 066 String siteName = null; 067 String parameter = null; 068 Binary binary = null; 069 070 try 071 { 072 String[] path = uri.split(";"); 073 siteName = path[0]; 074 parameter = path[1]; 075 076 site = _siteManager.getSite(siteName); 077 078 binary = site.getValue(parameter); 079 } 080 catch (UnknownAmetysObjectException e) 081 { 082 getLogger().warn("No parameter " + parameter + " for site " + siteName); 083 return ""; 084 } 085 086 return createURL(absolute, internal, download, binary.getFilename(), parameter, site, uriArgument); 087 } 088 089 /** 090 * Create the url 091 * @param absolute for an absolute url 092 * @param internal for an internal url 093 * @param download for a download url 094 * @param filename The filename 095 * @param parameter The site parameter name 096 * @param site The site 097 * @param uriArgument The extra arguments 098 * @return The url 099 */ 100 protected String createURL(boolean absolute, boolean internal, boolean download, String filename, String parameter, Site site, String uriArgument) 101 { 102 StringBuilder resultPath = new StringBuilder(); 103 resultPath.append(_webPrefixHandler.computeUriPrefix(_projectManager.getCatalogSiteName(), absolute, internal)); 104 105 resultPath.append(getPath(filename, parameter, site, uriArgument)); 106 107 Map<String, String> params = download ? Collections.singletonMap("download", "true") : null; 108 return internal ? URIUtils.buildURI(resultPath.toString(), params) : URIUtils.encodeURI(resultPath.toString(), params); 109 } 110 111 /** 112 * Compute the path for the url 113 * @param filename The filename 114 * @param parameter The site parameter name 115 * @param site The site 116 * @param uriArgument The extra arguments 117 * @return The path 118 */ 119 protected String getPath(String filename, String parameter, Site site, String uriArgument) 120 { 121 StringBuilder resultPath = new StringBuilder(); 122 123 String encodedFilename = FilenameUtils.encodeName(filename); 124 125 String baseName = org.apache.commons.io.FilenameUtils.getBaseName(encodedFilename); 126 String extension = org.apache.commons.io.FilenameUtils.getExtension(encodedFilename); 127 128 resultPath.append("/_plugins/workspaces/site/") 129 .append(site.getName()) 130 .append("/") 131 .append(parameter) 132 .append("/") 133 .append(baseName) 134 .append(uriArgument) 135 .append(extension.isEmpty() ? "" : "." + extension); 136 137 return resultPath.toString(); 138 } 139 140 @Override 141 protected String resolveImageAsBase64(String uri, int height, int width, int maxHeight, int maxWidth, int cropHeight, int cropWidth) 142 { 143 Site site = null; 144 String siteName = null; 145 String parameter = null; 146 Binary binary = null; 147 148 try 149 { 150 String[] path = uri.split(";"); 151 siteName = path[0]; 152 parameter = path[1]; 153 154 site = _siteManager.getSite(siteName); 155 156 binary = site.getValue(parameter); 157 158 try (InputStream dataIs = binary.getInputStream()) 159 { 160 return ImageResolverHelper.resolveImageAsBase64(dataIs, binary.getMimeType(), height, width, maxHeight, maxWidth, cropHeight, cropWidth); 161 } 162 } 163 catch (UnknownAmetysObjectException e) 164 { 165 getLogger().warn("No parameter " + parameter + " for site " + siteName); 166 return ""; 167 } 168 catch (Exception e) 169 { 170 throw new IllegalStateException(e); 171 } 172 } 173 174 public String getMimeType(String uri) 175 { 176 Site site = null; 177 String siteName = null; 178 String parameter = null; 179 Binary binary = null; 180 181 try 182 { 183 String[] path = uri.split(";"); 184 siteName = path[0]; 185 parameter = path[1]; 186 187 site = _siteManager.getSite(siteName); 188 189 binary = site.getValue(parameter); 190 return binary.getMimeType(); 191 } 192 catch (UnknownAmetysObjectException e) 193 { 194 getLogger().warn("No parameter " + parameter + " for site " + siteName); 195 return ""; 196 } 197 catch (Exception e) 198 { 199 throw new IllegalStateException(e); 200 } 201 } 202 203 @Override 204 public CHECK checkLink(String uri, boolean shortTest) 205 { 206 return CHECK.SUCCESS; 207 } 208 209 @Override 210 public I18nizableText getLabel(String uri) 211 { 212 return null; 213 } 214}