001/* 002 * Copyright 2017 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.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 type "site-parameter".<br> 041 * These links point to a file of a site 042 */ 043public class SiteParameterUriResolver extends AbstractURIResolver implements Serviceable 044{ 045 private SiteManager _siteManager; 046 private URIPrefixHandler _webPrefixHandler; 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 } 054 055 @Override 056 public String getType() 057 { 058 return "site-parameter"; 059 } 060 061 @Override 062 protected String _resolve(String uri, String uriArgument, boolean download, boolean absolute, boolean internal) 063 { 064 Site site = null; 065 String siteName = null; 066 String parameter = null; 067 Binary binary = null; 068 069 try 070 { 071 String[] path = uri.split(";"); 072 siteName = path[0]; 073 parameter = path[1]; 074 075 site = _siteManager.getSite(siteName); 076 077 binary = site.getValue(parameter); 078 } 079 catch (UnknownAmetysObjectException e) 080 { 081 getLogger().warn("No parameter " + parameter + " for site " + siteName); 082 return ""; 083 } 084 085 StringBuilder resultPath = new StringBuilder(); 086 resultPath.append(_webPrefixHandler.computeUriPrefix(siteName, absolute, internal)); 087 088 String filename = FilenameUtils.encodeName(binary.getFilename()); 089 090 String baseName = org.apache.commons.io.FilenameUtils.getBaseName(filename); 091 String extension = org.apache.commons.io.FilenameUtils.getExtension(filename); 092 093 resultPath.append("/_site/") 094 .append(parameter) 095 .append("/") 096 .append(baseName) 097 .append(uriArgument) 098 .append(extension.isEmpty() ? "" : "." + extension); 099 100 Map<String, String> params = download ? Collections.singletonMap("download", "true") : null; 101 return internal ? URIUtils.buildURI(resultPath.toString(), params) : URIUtils.encodeURI(resultPath.toString(), params); 102 } 103 104 @Override 105 protected String resolveImageAsBase64(String uri, int height, int width, int maxHeight, int maxWidth, int cropHeight, int cropWidth) 106 { 107 Site site = null; 108 String siteName = null; 109 String parameter = null; 110 Binary binary = null; 111 112 try 113 { 114 String[] path = uri.split(";"); 115 siteName = path[0]; 116 parameter = path[1]; 117 118 site = _siteManager.getSite(siteName); 119 120 binary = site.getValue(parameter); 121 122 try (InputStream dataIs = binary.getInputStream()) 123 { 124 return ImageResolverHelper.resolveImageAsBase64(dataIs, binary.getMimeType(), height, width, maxHeight, maxWidth, cropHeight, cropWidth); 125 } 126 } 127 catch (UnknownAmetysObjectException e) 128 { 129 getLogger().warn("No parameter " + parameter + " for site " + siteName); 130 return ""; 131 } 132 catch (Exception e) 133 { 134 throw new IllegalStateException(e); 135 } 136 } 137 138 public String getMimeType(String uri) 139 { 140 Site site = null; 141 String siteName = null; 142 String parameter = null; 143 Binary binary = null; 144 145 try 146 { 147 String[] path = uri.split(";"); 148 siteName = path[0]; 149 parameter = path[1]; 150 151 site = _siteManager.getSite(siteName); 152 153 binary = site.getValue(parameter); 154 return binary.getMimeType(); 155 } 156 catch (UnknownAmetysObjectException e) 157 { 158 getLogger().warn("No parameter " + parameter + " for site " + siteName); 159 return ""; 160 } 161 catch (Exception e) 162 { 163 throw new IllegalStateException(e); 164 } 165 } 166 167 @Override 168 public CHECK checkLink(String uri, boolean shortTest) 169 { 170 return CHECK.SUCCESS; 171 } 172 173 @Override 174 public I18nizableText getLabel(String uri) 175 { 176 return null; 177 } 178}