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.plugins.skineditor.readers; 017 018import java.io.IOException; 019import java.io.InputStream; 020import java.util.Arrays; 021import java.util.Collection; 022import java.util.Map; 023 024import org.apache.avalon.framework.parameters.Parameters; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.cocoon.ProcessingException; 028import org.apache.cocoon.environment.ObjectModelHelper; 029import org.apache.cocoon.environment.Response; 030import org.apache.cocoon.environment.SourceResolver; 031import org.apache.cocoon.reading.ServiceableReader; 032import org.apache.commons.io.IOUtils; 033import org.apache.excalibur.source.Source; 034import org.apache.excalibur.source.impl.FileSource; 035import org.xml.sax.SAXException; 036 037import org.ametys.core.util.ImageHelper; 038import org.ametys.core.util.URIUtils; 039 040/** 041 * Reader for temporary skin resources 042 */ 043public class SkinResourceReader extends ServiceableReader 044{ 045 /** The source resolver */ 046 protected org.apache.excalibur.source.SourceResolver _srcResolver; 047 /** The file source */ 048 protected FileSource _file; 049 050 private boolean _readForDownload; 051 052 private int _width; 053 private int _height; 054 private int _maxWidth; 055 private int _maxHeight; 056 private Collection<String> _allowedFormats = Arrays.asList(new String[]{"png", "gif", "jpg", "jpeg"}); 057 058 @Override 059 public void service(ServiceManager smanager) throws ServiceException 060 { 061 _srcResolver = (org.apache.excalibur.source.SourceResolver) smanager.lookup(org.apache.excalibur.source.SourceResolver.ROLE); 062 } 063 064 @Override 065 public void setup(SourceResolver res, Map objModel, String src, Parameters par) throws ProcessingException, SAXException, IOException 066 { 067 super.setup(res, objModel, src, par); 068 069 String skin = par.getParameter("skin", null); 070 String path = par.getParameter("path", null); 071 072 assert skin != null || path != null; 073 074 _readForDownload = par.getParameterAsBoolean("download", false); 075 076 // parameters for image resizing 077 _width = par.getParameterAsInteger("width", 0); 078 _height = par.getParameterAsInteger("height", 0); 079 _maxWidth = par.getParameterAsInteger("maxWidth", 0); 080 _maxHeight = par.getParameterAsInteger("maxHeight", 0); 081 082 _file = (FileSource) _getSource(skin, path); 083 } 084 085 @Override 086 public void generate() throws IOException, SAXException, ProcessingException 087 { 088 try (InputStream is = _file.getInputStream();) 089 { 090 String name = _file.getName(); 091 name = name.replaceAll("\\\\", "\\\\\\\\"); 092 name = name.replaceAll("\\\"", "\\\\\\\""); 093 094 Response response = ObjectModelHelper.getResponse(objectModel); 095 096 if (_readForDownload) 097 { 098 response.setHeader("Content-Disposition", "attachment; filename=\"" + name + "\""); 099 } 100 101 if (_width > 0 || _height > 0 || _maxHeight > 0 || _maxWidth > 0) 102 { 103 // it's an image, which must be resized 104 int i = name.lastIndexOf('.'); 105 String format = i != -1 ? name.substring(i + 1) : "png"; 106 format = _allowedFormats.contains(format) ? format : "png"; 107 108 ImageHelper.generateThumbnail(is, out, format, _height, _width, _maxHeight, _maxWidth); 109 } 110 else 111 { 112 response.setHeader("Content-Length", Long.toString(_file.getContentLength())); 113 114 IOUtils.copy(is, out); 115 } 116 } 117 } 118 119 private Source _getSource (String skinName, String path) throws IOException 120 { 121 String decodedPath = URIUtils.decode(path); 122 decodedPath = decodedPath.startsWith("/") ? decodedPath : "/" + decodedPath; 123 124 return _srcResolver.resolveURI("ametys-home://skins/temp/" + skinName + decodedPath); 125 } 126}