Author: jochen Date: Sun Aug 6 16:34:18 2006 New Revision: 429209 URL: http://svn.apache.org/viewvc?rev=429209&view=rev Log: Reworked the new streaming API in order to throw IOExceptions rather than mask them as FileUploadExceptions. See FILEUPLOAD-71 for the reasons.
Modified: jakarta/commons/proper/fileupload/trunk/.settings/org.eclipse.jdt.core.prefs jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemIterator.java jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadException.java jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/portlet/PortletFileUpload.java jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/servlet/ServletFileUpload.java Modified: jakarta/commons/proper/fileupload/trunk/.settings/org.eclipse.jdt.core.prefs URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/.settings/org.eclipse.jdt.core.prefs?rev=429209&r1=429208&r2=429209&view=diff ============================================================================== --- jakarta/commons/proper/fileupload/trunk/.settings/org.eclipse.jdt.core.prefs (original) +++ jakarta/commons/proper/fileupload/trunk/.settings/org.eclipse.jdt.core.prefs Sun Aug 6 16:34:18 2006 @@ -1,4 +1,4 @@ -#Mon Jun 19 03:18:34 CEST 2006 +#Mon Aug 07 02:40:54 CEST 2006 eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=disabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.1 @@ -8,17 +8,67 @@ org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.doc.comment.support=enabled +org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning org.eclipse.jdt.core.compiler.problem.assertIdentifier=ignore +org.eclipse.jdt.core.compiler.problem.autoboxing=warning +org.eclipse.jdt.core.compiler.problem.deprecation=warning +org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled +org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled +org.eclipse.jdt.core.compiler.problem.discouragedReference=warning +org.eclipse.jdt.core.compiler.problem.emptyStatement=warning org.eclipse.jdt.core.compiler.problem.enumIdentifier=ignore +org.eclipse.jdt.core.compiler.problem.fallthroughCase=warning +org.eclipse.jdt.core.compiler.problem.fieldHiding=warning +org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning +org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning +org.eclipse.jdt.core.compiler.problem.forbiddenReference=error +org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning +org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning +org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=warning +org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=warning org.eclipse.jdt.core.compiler.problem.invalidJavadoc=error org.eclipse.jdt.core.compiler.problem.invalidJavadocTags=enabled org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsDeprecatedRef=disabled org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsNotVisibleRef=disabled org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsVisibility=private +org.eclipse.jdt.core.compiler.problem.localVariableHiding=warning +org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning +org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=warning org.eclipse.jdt.core.compiler.problem.missingJavadocComments=warning org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsOverriding=disabled org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsVisibility=public org.eclipse.jdt.core.compiler.problem.missingJavadocTags=ignore org.eclipse.jdt.core.compiler.problem.missingJavadocTagsOverriding=disabled org.eclipse.jdt.core.compiler.problem.missingJavadocTagsVisibility=public +org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=warning +org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning +org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning +org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning +org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore +org.eclipse.jdt.core.compiler.problem.nullReference=warning +org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning +org.eclipse.jdt.core.compiler.problem.parameterAssignment=warning +org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=error +org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning +org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled +org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning +org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled +org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore +org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning +org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning +org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=warning +org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning +org.eclipse.jdt.core.compiler.problem.unnecessaryElse=warning +org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=warning +org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=warning +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled +org.eclipse.jdt.core.compiler.problem.unusedImport=warning +org.eclipse.jdt.core.compiler.problem.unusedLabel=warning +org.eclipse.jdt.core.compiler.problem.unusedLocal=warning +org.eclipse.jdt.core.compiler.problem.unusedParameter=warning +org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled +org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled +org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning +org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning org.eclipse.jdt.core.compiler.source=1.3 Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemIterator.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemIterator.java?rev=429209&r1=429208&r2=429209&view=diff ============================================================================== --- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemIterator.java (original) +++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemIterator.java Sun Aug 6 16:34:18 2006 @@ -15,6 +15,7 @@ */ package org.apache.commons.fileupload; +import java.io.IOException; import java.util.NoSuchElementException; @@ -27,12 +28,12 @@ * Returns, whether another instance of [EMAIL PROTECTED] FileItemStream} * is available. */ - boolean hasNext() throws FileUploadException; + boolean hasNext() throws FileUploadException, IOException; /** * Returns the next available [EMAIL PROTECTED] FileItemStream}. * @throws NoSuchElementException No more items are available. Use * [EMAIL PROTECTED] #hasNext()} to prevent this exception. */ - FileItemStream next() throws FileUploadException; + FileItemStream next() throws FileUploadException, IOException; } Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java?rev=429209&r1=429208&r2=429209&view=diff ============================================================================== --- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java (original) +++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java Sun Aug 6 16:34:18 2006 @@ -271,9 +271,12 @@ * * @throws FileUploadException if there are problems reading/parsing * the request or storing files. + * @throws IOException An I/O error occurred. This may be a network + * error while communicating with the client or a problem while + * storing the uploaded content. */ public FileItemIterator getItemIterator(RequestContext ctx) - throws FileUploadException { + throws FileUploadException, IOException { return new FileItemIteratorImpl(ctx); } @@ -291,28 +294,34 @@ */ public List /* FileItem */ parseRequest(RequestContext ctx) throws FileUploadException { - FileItemIterator iter = getItemIterator(ctx); - List items = new ArrayList(); - FileItemFactory fac = getFileItemFactory(); - final byte[] buffer = new byte[8192]; - while (iter.hasNext()) { - FileItemStream item = iter.next(); - FileItem fileItem = fac.createItem(item.getFieldName(), - item.getContentType(), item.isFormField(), - item.getName()); - try { - StreamUtil.copy(item.openStream(), fileItem.getOutputStream(), - true, buffer); - } catch (FileUploadIOException e) { - throw (FileUploadException) e.getCause(); - } catch (IOException e) { - throw new IOFileUploadException( - "Processing of " + MULTIPART_FORM_DATA - + " request failed. " + e.getMessage(), e); - } - items.add(fileItem); + try { + FileItemIterator iter = getItemIterator(ctx); + List items = new ArrayList(); + FileItemFactory fac = getFileItemFactory(); + final byte[] buffer = new byte[8192]; + while (iter.hasNext()) { + FileItemStream item = iter.next(); + FileItem fileItem = fac.createItem(item.getFieldName(), + item.getContentType(), item.isFormField(), + item.getName()); + try { + StreamUtil.copy(item.openStream(), fileItem.getOutputStream(), + true, buffer); + } catch (FileUploadIOException e) { + throw (FileUploadException) e.getCause(); + } catch (IOException e) { + throw new IOFileUploadException( + "Processing of " + MULTIPART_FORM_DATA + + " request failed. " + e.getMessage(), e); + } + items.add(fileItem); + } + return items; + } catch (FileUploadIOException e) { + throw (FileUploadException) e.getCause(); + } catch (IOException e) { + throw new FileUploadException(e.getMessage(), e); } - return items; } @@ -534,7 +543,7 @@ private boolean itemValid; private boolean eof; - FileItemIteratorImpl(RequestContext ctx) throws FileUploadException { + FileItemIteratorImpl(RequestContext ctx) throws FileUploadException, IOException { if (ctx == null) { throw new NullPointerException("ctx parameter"); } @@ -551,48 +560,40 @@ + contentType); } - try { - InputStream input = ctx.getInputStream(); - - if (sizeMax >= 0) { - int requestSize = ctx.getContentLength(); - if (requestSize == -1) { - input = new LimitedInputStream(input, sizeMax); - } else { - if (sizeMax >= 0 && requestSize > sizeMax) { - throw new SizeLimitExceededException( - "the request was rejected because its size (" + requestSize - + ") exceeds the configured maximum (" + sizeMax + ")", - requestSize, sizeMax); - } - } - } - - String charEncoding = headerEncoding; - if (charEncoding == null) { - charEncoding = ctx.getCharacterEncoding(); - } + InputStream input = ctx.getInputStream(); - boundary = getBoundary(contentType); - if (boundary == null) { - throw new FileUploadException( - "the request was rejected because " - + "no multipart boundary was found"); - } + if (sizeMax >= 0) { + int requestSize = ctx.getContentLength(); + if (requestSize == -1) { + input = new LimitedInputStream(input, sizeMax); + } else { + if (sizeMax >= 0 && requestSize > sizeMax) { + throw new SizeLimitExceededException( + "the request was rejected because its size (" + requestSize + + ") exceeds the configured maximum (" + sizeMax + ")", + requestSize, sizeMax); + } + } + } + + String charEncoding = headerEncoding; + if (charEncoding == null) { + charEncoding = ctx.getCharacterEncoding(); + } + + boundary = getBoundary(contentType); + if (boundary == null) { + throw new FileUploadException( + "the request was rejected because " + + "no multipart boundary was found"); + } + + notifier = new MultipartStream.ProgressNotifier(listener, ctx.getContentLength()); + multi = new MultipartStream(input, boundary, notifier); + multi.setHeaderEncoding(charEncoding); - notifier = new MultipartStream.ProgressNotifier(listener, ctx.getContentLength()); - multi = new MultipartStream(input, boundary, notifier); - multi.setHeaderEncoding(charEncoding); - - skipPreamble = true; - findNextItem(); - } catch (FileUploadIOException e) { - throw (FileUploadException) e.getCause(); - } catch (IOException e) { - throw new FileUploadException( - "Processing of " + MULTIPART_FORM_DATA - + " request failed. " + e.getMessage()); - } + skipPreamble = true; + findNextItem(); } private boolean findNextItem() throws IOException { @@ -635,15 +636,14 @@ multi.setBoundary(subBoundary); skipPreamble = true; continue; - } else { - String fileName = getFileName(headers); - currentItem = new FileItemStreamImpl(fileName, - fieldName, getHeader(headers, CONTENT_TYPE), - fileName == null); - notifier.noteItem(); - itemValid = true; - return true; } + String fileName = getFileName(headers); + currentItem = new FileItemStreamImpl(fileName, + fieldName, getHeader(headers, CONTENT_TYPE), + fileName == null); + notifier.noteItem(); + itemValid = true; + return true; } } else { String fileName = getFileName(headers); @@ -660,25 +660,17 @@ } } - public boolean hasNext() throws FileUploadException { + public boolean hasNext() throws FileUploadException, IOException { if (eof) { return false; } if (itemValid) { return true; } - try { - return findNextItem(); - } catch (FileUploadIOException e) { - throw (FileUploadException) e.getCause(); - } catch (IOException e) { - throw new FileUploadException( - "Processing of " + MULTIPART_FORM_DATA - + " request failed. " + e.getMessage()); - } + return findNextItem(); } - public FileItemStream next() throws FileUploadException { + public FileItemStream next() throws FileUploadException, IOException { if (eof || (!itemValid && !hasNext())) { throw new NoSuchElementException(); } @@ -879,5 +871,4 @@ public void setProgressListener(ProgressListener pListener) { listener = pListener; } - } Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadException.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadException.java?rev=429209&r1=429208&r2=429209&view=diff ============================================================================== --- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadException.java (original) +++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadException.java Sun Aug 6 16:34:18 2006 @@ -15,19 +15,24 @@ */ package org.apache.commons.fileupload; +import java.io.PrintStream; +import java.io.PrintWriter; + /** * Exception for errors encountered while processing the request. * * @author <a href="mailto:[EMAIL PROTECTED]">John McNally</a> * @version $Id$ */ -public class FileUploadException - extends Exception { - +public class FileUploadException extends Exception { + private static final long serialVersionUID = 8881893724388807504L; + private final Throwable cause; + /** * Constructs a new <code>FileUploadException</code> without message. */ public FileUploadException() { + this(null, null); } /** @@ -37,8 +42,31 @@ * @param msg the error message. */ public FileUploadException(final String msg) { - super(msg); + this(msg, null); + } + + /** + * Creates a new <code>FileUploadException</code> with the given + * detail message and cause. + */ + public FileUploadException(String msg, Throwable cause) { + super(msg); + this.cause = cause; } + public void printStackTrace(PrintStream stream) { + super.printStackTrace(stream); + if (cause != null) { + stream.println("Caused by:"); + cause.printStackTrace(stream); + } + } + public void printStackTrace(PrintWriter writer) { + super.printStackTrace(writer); + if (cause != null) { + writer.println("Caused by:"); + cause.printStackTrace(writer); + } + } } Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/portlet/PortletFileUpload.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/portlet/PortletFileUpload.java?rev=429209&r1=429208&r2=429209&view=diff ============================================================================== --- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/portlet/PortletFileUpload.java (original) +++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/portlet/PortletFileUpload.java Sun Aug 6 16:34:18 2006 @@ -15,6 +15,7 @@ */ package org.apache.commons.fileupload.portlet; +import java.io.IOException; import java.util.List; import javax.portlet.ActionRequest; @@ -129,8 +130,11 @@ * * @throws FileUploadException if there are problems reading/parsing * the request or storing files. + * @throws IOException An I/O error occurred. This may be a network + * error while communicating with the client or a problem while + * storing the uploaded content. */ - public FileItemIterator getItemIterator(ActionRequest request) throws FileUploadException { + public FileItemIterator getItemIterator(ActionRequest request) throws FileUploadException, IOException { return super.getItemIterator(new PortletRequestContext(request)); } } Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/servlet/ServletFileUpload.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/servlet/ServletFileUpload.java?rev=429209&r1=429208&r2=429209&view=diff ============================================================================== --- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/servlet/ServletFileUpload.java (original) +++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/servlet/ServletFileUpload.java Sun Aug 6 16:34:18 2006 @@ -15,6 +15,7 @@ */ package org.apache.commons.fileupload.servlet; +import java.io.IOException; import java.util.List; import javax.servlet.http.HttpServletRequest; @@ -136,8 +137,12 @@ * * @throws FileUploadException if there are problems reading/parsing * the request or storing files. + * @throws IOException An I/O error occurred. This may be a network + * error while communicating with the client or a problem while + * storing the uploaded content. */ - public FileItemIterator getItemIterator(HttpServletRequest request) throws FileUploadException { + public FileItemIterator getItemIterator(HttpServletRequest request) + throws FileUploadException, IOException { return super.getItemIterator(new ServletRequestContext(request)); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]