diff src/org/eclipse/jetty/util/MultiPartInputStream.java @ 859:3dcc52e17535

simplify multipart
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 21 Sep 2016 16:15:19 -0600
parents 7fb7c1915788
children
line wrap: on
line diff
--- a/src/org/eclipse/jetty/util/MultiPartInputStream.java	Wed Sep 21 15:02:19 2016 -0600
+++ b/src/org/eclipse/jetty/util/MultiPartInputStream.java	Wed Sep 21 16:15:19 2016 -0600
@@ -43,7 +43,6 @@
 import java.util.StringTokenizer;
 import java.util.Base64;
 
-import javax.servlet.MultipartConfigElement;
 import javax.servlet.ServletException;
 import javax.servlet.http.Part;
 
@@ -61,18 +60,13 @@
 {
 	private static final Logger LOG = LoggerFactory.getLogger(MultiPartInputStream.class);
 
-	public static final MultipartConfigElement  __DEFAULT_MULTIPART_CONFIG = new MultipartConfigElement(System.getProperty("java.io.tmpdir"));
 	protected InputStream _in;
-	protected MultipartConfigElement _config;
 	protected String _contentType;
 	protected MultiMap<String> _parts;
-	protected File _tmpDir;
-	protected File _contextTmpDir;
-	protected boolean _deleteOnExit;
 	
 	
 	
-	public class MultiPart implements Part
+	public final class MultiPart implements Part
 	{
 		protected String _name;
 		protected String _filename;
@@ -82,7 +76,6 @@
 		protected String _contentType;
 		protected MultiMap<String> _headers;
 		protected long _size = 0;
-		protected boolean _temporary = true;
 
 		public MultiPart (String name, String filename) 
 		throws IOException
@@ -125,11 +118,6 @@
 		protected void write (int b)
 		throws IOException
 		{      
-			if (MultiPartInputStream.this._config.getMaxFileSize() > 0 && _size + 1 > MultiPartInputStream.this._config.getMaxFileSize())
-				throw new IllegalStateException ("Multipart Mime part "+_name+" exceeds max filesize");
-			
-			if (MultiPartInputStream.this._config.getFileSizeThreshold() > 0 && _size + 1 > MultiPartInputStream.this._config.getFileSizeThreshold() && _file==null)
-				createFile();
 			_out.write(b);   
 			_size ++;
 		}
@@ -137,12 +125,6 @@
 		protected void write (byte[] bytes, int offset, int length) 
 		throws IOException
 		{ 
-			if (MultiPartInputStream.this._config.getMaxFileSize() > 0 && _size + length > MultiPartInputStream.this._config.getMaxFileSize())
-				throw new IllegalStateException ("Multipart Mime part "+_name+" exceeds max filesize");
-			
-			if (MultiPartInputStream.this._config.getFileSizeThreshold() > 0 && _size + length > MultiPartInputStream.this._config.getFileSizeThreshold() && _file==null)
-				createFile();
-			
 			_out.write(bytes, offset, length);
 			_size += length;
 		}
@@ -150,9 +132,8 @@
 		protected void createFile ()
 		throws IOException
 		{
-			_file = File.createTempFile("MultiPart", "", MultiPartInputStream.this._tmpDir);
-			if (_deleteOnExit)
-				_file.deleteOnExit();
+			_file = File.createTempFile("MultiPart", null);
+			_file.deleteOnExit();
 			FileOutputStream fos = new FileOutputStream(_file);
 			BufferedOutputStream bos = new BufferedOutputStream(fos);
 			
@@ -253,36 +234,7 @@
 		 */
 		public void write(String fileName) throws IOException
 		{
-			if (_file == null)
-			{
-				_temporary = false;
-				
-				//part data is only in the ByteArrayOutputStream and never been written to disk
-				_file = new File (_tmpDir, fileName);
-
-				BufferedOutputStream bos = null;
-				try
-				{
-					bos = new BufferedOutputStream(new FileOutputStream(_file));
-					_bout.writeTo(bos);
-					bos.flush();
-				}
-				finally
-				{
-					if (bos != null)
-						bos.close();
-					_bout = null;
-				}
-			}
-			else
-			{
-				//the part data is already written to a temporary file, just rename it
-				_temporary = false;
-				
-				File f = new File(_tmpDir, fileName);
-				if (_file.renameTo(f))
-					_file = f;
-			}
+			throw new UnsupportedOperationException();
 		}
 		
 		/** 
@@ -303,7 +255,7 @@
 		 */
 		public void cleanUp() throws IOException
 		{
-			if (_temporary && _file != null && _file.exists())
+			if (_file != null && _file.exists())
 				_file.delete();
 		}
 		
@@ -337,17 +289,10 @@
 	 * @param config MultipartConfigElement 
 	 * @param contextTmpDir javax.servlet.context.tempdir
 	 */
-	public MultiPartInputStream (InputStream in, String contentType, MultipartConfigElement config, File contextTmpDir)
+	public MultiPartInputStream (InputStream in, String contentType)
 	{
 		_in = new ReadLineInputStream(in);
 	   _contentType = contentType;
-	   _config = config;
-	   _contextTmpDir = contextTmpDir;
-	   if (_contextTmpDir == null)
-		   _contextTmpDir = new File (System.getProperty("java.io.tmpdir"));
-	   
-	   if (_config == null)
-		   _config = new MultipartConfigElement(_contextTmpDir.getAbsolutePath());
 	}
 
 	/**
@@ -455,24 +400,6 @@
 		//if its not a multipart request, don't parse it
 		if (_contentType == null || !_contentType.startsWith("multipart/form-data"))
 			return;
- 
-		//sort out the location to which to write the files
-		
-		if (_config.getLocation() == null)
-			_tmpDir = _contextTmpDir;
-		else if ("".equals(_config.getLocation()))
-			_tmpDir = _contextTmpDir;
-		else
-		{
-			File f = new File (_config.getLocation());
-			if (f.isAbsolute())
-				_tmpDir = f;
-			else
-				_tmpDir = new File (_contextTmpDir, _config.getLocation());
-		}
-	  
-		if (!_tmpDir.exists())
-			_tmpDir.mkdirs();
 
 		String contentTypeBoundary = "";
 		int bstart = _contentType.indexOf("boundary=");
@@ -540,8 +467,6 @@
 					break;
 				
 				total += line.length();
-				if (_config.getMaxRequestSize() > 0 && total > _config.getMaxRequestSize())
-					throw new IllegalStateException ("Request exceeds maxRequestSize ("+_config.getMaxRequestSize()+")");
 
 				//get content-disposition and content-type
 				int c=line.indexOf(':',0);
@@ -649,8 +574,6 @@
 					while((c=(state!=-2)?state:partInput.read())!=-1)
 					{
 						total ++;
-						if (_config.getMaxRequestSize() > 0 && total > _config.getMaxRequestSize())
-							throw new IllegalStateException("Request exceeds maxRequestSize ("+_config.getMaxRequestSize()+")");
 						
 						state=-2;
 						
@@ -740,17 +663,6 @@
 			throw new IOException("Incomplete parts");
 	}
 	
-	public void setDeleteOnExit(boolean deleteOnExit)
-	{
-		_deleteOnExit = deleteOnExit;
-	}
-
-
-	public boolean isDeleteOnExit()
-	{
-		return _deleteOnExit;
-	}
-
 
 	/* ------------------------------------------------------------ */
 	private String value(String nameEqualsValue, boolean splitAfterSpace)