001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.fileupload.servlet;
018:
019: import java.io.IOException;
020: import java.util.List;
021:
022: import javax.servlet.http.HttpServletRequest;
023:
024: import org.apache.commons.fileupload.FileItemFactory;
025: import org.apache.commons.fileupload.FileItemIterator;
026: import org.apache.commons.fileupload.FileUpload;
027: import org.apache.commons.fileupload.FileUploadException;
028:
029: /**
030: * <p>High level API for processing file uploads.</p>
031: *
032: * <p>This class handles multiple files per single HTML widget, sent using
033: * <code>multipart/mixed</code> encoding type, as specified by
034: * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link
035: * #parseRequest(HttpServletRequest)} to acquire a list of {@link
036: * org.apache.commons.fileupload.FileItem}s associated with a given HTML
037: * widget.</p>
038: *
039: * <p>How the data for individual parts is stored is determined by the factory
040: * used to create them; a given part may be in memory, on disk, or somewhere
041: * else.</p>
042: *
043: * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
044: * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
045: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
046: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
047: * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
048: * @author Sean C. Sullivan
049: *
050: * @version $Id: ServletFileUpload.java 479484 2006-11-27 01:06:53Z jochen $
051: */
052: public class ServletFileUpload extends FileUpload {
053:
054: // ---------------------------------------------------------- Class methods
055:
056: /**
057: * Utility method that determines whether the request contains multipart
058: * content.
059: *
060: * @param request The servlet request to be evaluated. Must be non-null.
061: *
062: * @return <code>true</code> if the request is multipart;
063: * <code>false</code> otherwise.
064: */
065: public static final boolean isMultipartContent(
066: HttpServletRequest request) {
067: if (!"post".equals(request.getMethod().toLowerCase())) {
068: return false;
069: }
070: String contentType = request.getContentType();
071: if (contentType == null) {
072: return false;
073: }
074: if (contentType.toLowerCase().startsWith(MULTIPART)) {
075: return true;
076: }
077: return false;
078: }
079:
080: // ----------------------------------------------------------- Constructors
081:
082: /**
083: * Constructs an uninitialised instance of this class. A factory must be
084: * configured, using <code>setFileItemFactory()</code>, before attempting
085: * to parse requests.
086: *
087: * @see FileUpload#FileUpload(FileItemFactory)
088: */
089: public ServletFileUpload() {
090: super ();
091: }
092:
093: /**
094: * Constructs an instance of this class which uses the supplied factory to
095: * create <code>FileItem</code> instances.
096: *
097: * @see FileUpload#FileUpload()
098: * @param fileItemFactory The factory to use for creating file items.
099: */
100: public ServletFileUpload(FileItemFactory fileItemFactory) {
101: super (fileItemFactory);
102: }
103:
104: // --------------------------------------------------------- Public methods
105:
106: /**
107: * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
108: * compliant <code>multipart/form-data</code> stream.
109: *
110: * @param request The servlet request to be parsed.
111: *
112: * @return A list of <code>FileItem</code> instances parsed from the
113: * request, in the order that they were transmitted.
114: *
115: * @throws FileUploadException if there are problems reading/parsing
116: * the request or storing files.
117: */
118: public List /* FileItem */parseRequest(HttpServletRequest request)
119: throws FileUploadException {
120: return parseRequest(new ServletRequestContext(request));
121: }
122:
123: /**
124: * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
125: * compliant <code>multipart/form-data</code> stream.
126: *
127: * @param request The servlet request to be parsed.
128: *
129: * @return An iterator to instances of <code>FileItemStream</code>
130: * parsed from the request, in the order that they were
131: * transmitted.
132: *
133: * @throws FileUploadException if there are problems reading/parsing
134: * the request or storing files.
135: * @throws IOException An I/O error occurred. This may be a network
136: * error while communicating with the client or a problem while
137: * storing the uploaded content.
138: */
139: public FileItemIterator getItemIterator(HttpServletRequest request)
140: throws FileUploadException, IOException {
141: return super
142: .getItemIterator(new ServletRequestContext(request));
143: }
144: }
|