View Javadoc
1 package org.apache.turbine.services.upload; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.lang.reflect.Constructor; 58 import java.util.Enumeration; 59 import java.util.Vector; 60 import javax.servlet.http.HttpServletRequest; 61 import org.apache.turbine.util.ParameterParser; 62 import org.apache.turbine.util.TurbineException; 63 64 /*** 65 * <p> This is an implementation of the {@link UploadService} using 66 * the O'Reilly multipart request parser from the book <i>Java Servlet 67 * Programming</i> by Jason Hunter. 68 * 69 * <p> This class is intended for compatibity with old code. Use 70 * {@link TurbineUploadService} in new applications. 71 * 72 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a> 73 * @version $Id: OReillyUploadService.java,v 1.2 2002/07/11 16:53:23 mpoeschl Exp $ 74 * @deprecated use TurbineUploadService 75 */ 76 public class OReillyUploadService 77 extends BaseUploadService 78 { 79 /*** Holds a vector of the filenames that were uploaded. */ 80 private Vector filenames = null; 81 82 /*** 83 * Get a list of the files that were uploaded. 84 * 85 * @return A Vector with the filename(s) that were uploaded. 86 */ 87 public Vector getFilenames() 88 { 89 return filenames; 90 } 91 92 /*** 93 * Initiate the multipartParse in the uploader. This will upload 94 * the files to the path given. 95 * 96 * @param req The servlet request to be parsed. 97 * @param params The ParameterParser instance to insert form 98 * fields into. 99 * @param path The location where the files should be stored. 100 * @exception IOException, if there are problems reading/parsing 101 * the request or storing files. 102 */ 103 public void parseRequest( HttpServletRequest req, 104 ParameterParser params, 105 String path ) 106 throws TurbineException 107 { 108 try 109 { 110 // Check if com.oreilly.servlet.MultipartRequest exists, 111 // and if not, exit gracefully. Do not want to force 112 // Jason Hunter's package to be present for Dash 113 // compilation. 114 Class[] argSignature = { Class.forName("javax.servlet.ServletRequest"), path.getClass(), Integer.TYPE }; 115 Class multiClass = Class.forName( "com.oreilly.servlet.MultipartRequest" ); 116 Constructor multiConstructor= multiClass.getConstructor( argSignature ); 117 // Pass in the request, a directory to save uploads to, 118 // and the maximum POST size we should handle (set to 119 // maximum since, we assume this is checked before calling 120 // this method). 121 Object[] args = { req, path, new Integer(Integer.MAX_VALUE) }; 122 Object multiInstance = (Object) multiConstructor.newInstance( args ); 123 argSignature = new Class[0]; 124 args = new Object[0]; 125 Enumeration parameters = (Enumeration) 126 multiClass.getMethod("getParameterNames", argSignature) 127 .invoke(multiInstance, args); 128 Enumeration files = (Enumeration) 129 multiClass.getMethod("getFileNames", argSignature) 130 .invoke(multiInstance, args); 131 132 argSignature = new Class[1]; 133 args = new Object[1]; 134 if ( parameters != null ) 135 { 136 while(parameters.hasMoreElements()) 137 { 138 String tmp = (String) parameters.nextElement(); 139 argSignature[0] = tmp.getClass(); 140 args[0] = (Object)tmp; 141 // Method only returns last value, if parameter is 142 // multivalued. 143 params.add( tmp, 144 (String)multiClass 145 .getMethod("getParameter", argSignature) 146 .invoke(multiInstance, args) ); 147 } 148 } 149 150 filenames = new Vector(); 151 if ( files != null ) 152 { 153 while(files.hasMoreElements()) 154 { 155 String tmp = (String) files.nextElement(); 156 argSignature[0] = tmp.getClass(); 157 args[0] = (Object)tmp; 158 159 String filename = (String)multiClass 160 .getMethod("getFilesystemName", argSignature) 161 .invoke(multiInstance, args); 162 if (filename != null) 163 filenames.addElement(filename); 164 } 165 } 166 } 167 catch(ClassNotFoundException cnfe) 168 { 169 throw new TurbineException ("UploadFile action was attempted and a handler has not been"+ 170 " registered in the TurbineResources.properties, so com.oreilly.servlet.Multipart was tried" + 171 " and has not been installed. Turbine uses a class from this package to" + 172 " parse and save the file from the POST data. You must supply a class" + 173 " to use UploadFile. com.oreilly.servlet is available as part of purchase of" + 174 " Jason Hunter's Java Servlet Programming book."); 175 } 176 catch(Exception e) 177 { 178 throw new TurbineException("Failed to perform file upload using O'Reilly uploader", e); 179 } 180 } 181 }

This page was automatically generated by Maven