001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/methods/MultipartPostMethod.java,v 1.27 2004/10/06 03:39:59 mbecke Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.methods;
032:
033: import java.io.File;
034: import java.io.FileNotFoundException;
035: import java.io.IOException;
036: import java.io.OutputStream;
037: import java.util.ArrayList;
038: import java.util.List;
039:
040: import org.apache.commons.httpclient.HttpConnection;
041: import org.apache.commons.httpclient.HttpException;
042: import org.apache.commons.httpclient.HttpState;
043: import org.apache.commons.httpclient.methods.multipart.FilePart;
044: import org.apache.commons.httpclient.methods.multipart.Part;
045: import org.apache.commons.httpclient.methods.multipart.StringPart;
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048:
049: /**
050: * Implements the HTTP multipart POST method.
051: * <p>
052: * The HTTP multipart POST method is defined in section 3.3 of
053: * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC1867</a>:
054: * <blockquote>
055: * The media-type multipart/form-data follows the rules of all multipart
056: * MIME data streams as outlined in RFC 1521. The multipart/form-data contains
057: * a series of parts. Each part is expected to contain a content-disposition
058: * header where the value is "form-data" and a name attribute specifies
059: * the field name within the form, e.g., 'content-disposition: form-data;
060: * name="xxxxx"', where xxxxx is the field name corresponding to that field.
061: * Field names originally in non-ASCII character sets may be encoded using
062: * the method outlined in RFC 1522.
063: * </blockquote>
064: * </p>
065: * <p>
066: *
067: * @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a>
068: * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
069: * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
070: * @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
071: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
072: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
073: *
074: * @since 2.0
075: *
076: * @deprecated Use {@link org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity}
077: * in conjunction with {@link org.apache.commons.httpclient.methods.PostMethod} instead.
078: */
079: public class MultipartPostMethod extends ExpectContinueMethod {
080:
081: /** The Content-Type for multipart/form-data. */
082: public static final String MULTIPART_FORM_CONTENT_TYPE = "multipart/form-data";
083:
084: /** Log object for this class. */
085: private static final Log LOG = LogFactory
086: .getLog(MultipartPostMethod.class);
087:
088: /** The parameters for this method */
089: private final List parameters = new ArrayList();
090:
091: /**
092: * No-arg constructor.
093: */
094: public MultipartPostMethod() {
095: super ();
096: }
097:
098: /**
099: * Constructor specifying a URI.
100: *
101: * @param uri either an absolute or relative URI
102: */
103: public MultipartPostMethod(String uri) {
104: super (uri);
105: }
106:
107: /**
108: * Returns <tt>true</tt>
109: *
110: * @return <tt>true</tt>
111: *
112: * @since 2.0beta1
113: */
114: protected boolean hasRequestContent() {
115: return true;
116: }
117:
118: /**
119: * Returns <tt>"POST"</tt>.
120: * @return <tt>"POST"</tt>
121: */
122: public String getName() {
123: return "POST";
124: }
125:
126: /**
127: * Adds a text field part
128: *
129: * @param parameterName The name of the parameter.
130: * @param parameterValue The value of the parameter.
131: */
132: public void addParameter(String parameterName, String parameterValue) {
133: LOG
134: .trace("enter addParameter(String parameterName, String parameterValue)");
135: Part param = new StringPart(parameterName, parameterValue);
136: parameters.add(param);
137: }
138:
139: /**
140: * Adds a binary file part
141: *
142: * @param parameterName The name of the parameter
143: * @param parameterFile The name of the file.
144: * @throws FileNotFoundException If the file cannot be found.
145: */
146: public void addParameter(String parameterName, File parameterFile)
147: throws FileNotFoundException {
148: LOG
149: .trace("enter MultipartPostMethod.addParameter(String parameterName, "
150: + "File parameterFile)");
151: Part param = new FilePart(parameterName, parameterFile);
152: parameters.add(param);
153: }
154:
155: /**
156: * Adds a binary file part with the given file name
157: *
158: * @param parameterName The name of the parameter
159: * @param fileName The file name
160: * @param parameterFile The file
161: * @throws FileNotFoundException If the file cannot be found.
162: */
163: public void addParameter(String parameterName, String fileName,
164: File parameterFile) throws FileNotFoundException {
165: LOG
166: .trace("enter MultipartPostMethod.addParameter(String parameterName, "
167: + "String fileName, File parameterFile)");
168: Part param = new FilePart(parameterName, fileName,
169: parameterFile);
170: parameters.add(param);
171: }
172:
173: /**
174: * Adds a part.
175: *
176: * @param part The part to add.
177: */
178: public void addPart(final Part part) {
179: LOG.trace("enter addPart(Part part)");
180: parameters.add(part);
181: }
182:
183: /**
184: * Returns all parts.
185: *
186: * @return an array of containing all parts
187: */
188: public Part[] getParts() {
189: return (Part[]) parameters.toArray(new Part[parameters.size()]);
190: }
191:
192: /**
193: * Adds a <tt>Content-Length</tt> request header, as long as no
194: * <tt>Content-Length</tt> request header already exists.
195: *
196: * @param state current state of http requests
197: * @param conn the connection to use for I/O
198: *
199: * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
200: * can be recovered from.
201: * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
202: * cannot be recovered from.
203: *
204: * @since 3.0
205: */
206: protected void addContentLengthRequestHeader(HttpState state,
207: HttpConnection conn) throws IOException, HttpException {
208: LOG
209: .trace("enter EntityEnclosingMethod.addContentLengthRequestHeader("
210: + "HttpState, HttpConnection)");
211:
212: if (getRequestHeader("Content-Length") == null) {
213: long len = getRequestContentLength();
214: addRequestHeader("Content-Length", String.valueOf(len));
215: }
216: removeRequestHeader("Transfer-Encoding");
217: }
218:
219: /**
220: * Adds a <tt>Content-Type</tt> request header.
221: *
222: * @param state current state of http requests
223: * @param conn the connection to use for I/O
224: *
225: * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
226: * can be recovered from.
227: * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
228: * cannot be recovered from.
229: *
230: * @since 3.0
231: */
232: protected void addContentTypeRequestHeader(HttpState state,
233: HttpConnection conn) throws IOException, HttpException {
234: LOG
235: .trace("enter EntityEnclosingMethod.addContentTypeRequestHeader("
236: + "HttpState, HttpConnection)");
237:
238: if (!parameters.isEmpty()) {
239: StringBuffer buffer = new StringBuffer(
240: MULTIPART_FORM_CONTENT_TYPE);
241: if (Part.getBoundary() != null) {
242: buffer.append("; boundary=");
243: buffer.append(Part.getBoundary());
244: }
245: setRequestHeader("Content-Type", buffer.toString());
246: }
247: }
248:
249: /**
250: * Populates the request headers map to with additional
251: * {@link org.apache.commons.httpclient.Header headers} to be submitted to
252: * the given {@link HttpConnection}.
253: *
254: * <p>
255: * This implementation adds tt>Content-Length</tt> and <tt>Content-Type</tt>
256: * headers, when appropriate.
257: * </p>
258: *
259: * <p>
260: * Subclasses may want to override this method to to add additional
261: * headers, and may choose to invoke this implementation (via
262: * <tt>super</tt>) to add the "standard" headers.
263: * </p>
264: *
265: * @param state the {@link HttpState state} information associated with this method
266: * @param conn the {@link HttpConnection connection} used to execute
267: * this HTTP method
268: *
269: * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
270: * can be recovered from.
271: * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
272: * cannot be recovered from.
273: *
274: * @see #writeRequestHeaders
275: */
276: protected void addRequestHeaders(HttpState state,
277: HttpConnection conn) throws IOException, HttpException {
278: LOG
279: .trace("enter MultipartPostMethod.addRequestHeaders(HttpState state, "
280: + "HttpConnection conn)");
281: super .addRequestHeaders(state, conn);
282: addContentLengthRequestHeader(state, conn);
283: addContentTypeRequestHeader(state, conn);
284: }
285:
286: /**
287: * Writes the request body to the given {@link HttpConnection connection}.
288: *
289: * @param state the {@link HttpState state} information associated with this method
290: * @param conn the {@link HttpConnection connection} used to execute
291: * this HTTP method
292: *
293: * @return <tt>true</tt>
294: *
295: * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
296: * can be recovered from.
297: * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
298: * cannot be recovered from.
299: */
300: protected boolean writeRequestBody(HttpState state,
301: HttpConnection conn) throws IOException, HttpException {
302: LOG
303: .trace("enter MultipartPostMethod.writeRequestBody(HttpState state, "
304: + "HttpConnection conn)");
305: OutputStream out = conn.getRequestOutputStream();
306: Part.sendParts(out, getParts());
307: return true;
308: }
309:
310: /**
311: * <p>Return the length of the request body.</p>
312: *
313: * <p>Once this method has been invoked, the request parameters cannot be
314: * altered until the method is {@link #recycle recycled}.</p>
315: *
316: * @return The request content length.
317: */
318: protected long getRequestContentLength() throws IOException {
319: LOG
320: .trace("enter MultipartPostMethod.getRequestContentLength()");
321: return Part.getLengthOfParts(getParts());
322: }
323:
324: /**
325: * Recycles the HTTP method so that it can be used again.
326: * Note that all of the instance variables will be reset
327: * once this method has been called. This method will also
328: * release the connection being used by this HTTP method.
329: *
330: * @see #releaseConnection()
331: *
332: * @deprecated no longer supported and will be removed in the future
333: * version of HttpClient
334: */
335: public void recycle() {
336: LOG.trace("enter MultipartPostMethod.recycle()");
337: super.recycle();
338: parameters.clear();
339: }
340: }
|