01: /*
02: * Copyright 2005-2007 Noelios Consulting.
03: *
04: * The contents of this file are subject to the terms of the Common Development
05: * and Distribution License (the "License"). You may not use this file except in
06: * compliance with the License.
07: *
08: * You can obtain a copy of the license at
09: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
10: * language governing permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL HEADER in each file and
13: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
14: * applicable, add the following below this CDDL HEADER, with the fields
15: * enclosed by brackets "[]" replaced with your own identifying information:
16: * Portions Copyright [yyyy] [name of copyright owner]
17: */
18:
19: package org.restlet.ext.fileupload;
20:
21: import java.util.List;
22:
23: import org.apache.commons.fileupload.FileItem;
24: import org.apache.commons.fileupload.FileItemFactory;
25: import org.apache.commons.fileupload.FileUpload;
26: import org.apache.commons.fileupload.FileUploadException;
27: import org.restlet.data.Request;
28: import org.restlet.resource.Representation;
29:
30: /**
31: * High level API for processing file uploads. This class handles multiple files
32: * per single HTML widget, sent using multipart/mixed encoding type, as
33: * specified by RFC 1867. Use parseRequest(Call) to acquire a list of FileItems
34: * associated with a given HTML widget.How the data for individual parts is
35: * stored is determined by the factory used to create them; a given part may be
36: * in memory, on disk, or somewhere else.
37: *
38: * @author Jerome Louvel (contact@noelios.com)
39: */
40: public class RestletFileUpload extends FileUpload {
41: /**
42: * Constructs an uninitialised instance of this class. A factory must be
43: * configured, using <code>setFileItemFactory()</code>, before attempting
44: * to parse request entity.
45: *
46: * @see RestletFileUpload#RestletFileUpload(FileItemFactory)
47: */
48: public RestletFileUpload() {
49: super ();
50: }
51:
52: /**
53: * Constructs an instance of this class which uses the supplied factory to
54: * create <code>FileItem</code> instances.
55: *
56: * @see RestletFileUpload#RestletFileUpload()
57: */
58: public RestletFileUpload(FileItemFactory fileItemFactory) {
59: super (fileItemFactory);
60: }
61:
62: /**
63: * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
64: * compliant <code>multipart/form-data</code> input representation.
65: *
66: * @param request
67: * The request containing the entity to be parsed.
68: * @return A list of <code>FileItem</code> instances parsed, in the order
69: * that they were transmitted.
70: * @throws FileUploadException
71: * if there are problems reading/parsing the request or storing
72: * files.
73: */
74: @SuppressWarnings("unchecked")
75: public List<FileItem> parseRequest(Request request)
76: throws FileUploadException {
77: return parseRequest(new RepresentationContext(request
78: .getEntity()));
79: }
80:
81: /**
82: * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
83: * compliant <code>multipart/form-data</code> input representation.
84: *
85: * @param multipartForm
86: * The multipart representation to be parsed.
87: * @return A list of <code>FileItem</code> instances parsed, in the order
88: * that they were transmitted.
89: * @throws FileUploadException
90: * if there are problems reading/parsing the request or storing
91: * files.
92: */
93: @SuppressWarnings("unchecked")
94: public List<FileItem> parseRepresentation(
95: Representation multipartForm) throws FileUploadException {
96: return parseRequest(new RepresentationContext(multipartForm));
97: }
98:
99: }
|