1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/MultipartPostMethod.java,v 1.26 2004/06/13 20:22:19 olegk Exp $
  3. * $Revision: 1.26 $
  4. * $Date: 2004/06/13 20:22:19 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 2002-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient.methods;
  30. import java.io.File;
  31. import java.io.FileNotFoundException;
  32. import java.io.IOException;
  33. import java.io.OutputStream;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import org.apache.commons.httpclient.HttpConnection;
  37. import org.apache.commons.httpclient.HttpException;
  38. import org.apache.commons.httpclient.HttpState;
  39. import org.apache.commons.httpclient.methods.multipart.FilePart;
  40. import org.apache.commons.httpclient.methods.multipart.Part;
  41. import org.apache.commons.httpclient.methods.multipart.StringPart;
  42. import org.apache.commons.logging.Log;
  43. import org.apache.commons.logging.LogFactory;
  44. /**
  45. * Implements the HTTP multipart POST method.
  46. * <p>
  47. * The HTTP multipart POST method is defined in section 3.3 of
  48. * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC1867</a>:
  49. * <blockquote>
  50. * The media-type multipart/form-data follows the rules of all multipart
  51. * MIME data streams as outlined in RFC 1521. The multipart/form-data contains
  52. * a series of parts. Each part is expected to contain a content-disposition
  53. * header where the value is "form-data" and a name attribute specifies
  54. * the field name within the form, e.g., 'content-disposition: form-data;
  55. * name="xxxxx"', where xxxxx is the field name corresponding to that field.
  56. * Field names originally in non-ASCII character sets may be encoded using
  57. * the method outlined in RFC 1522.
  58. * </blockquote>
  59. * </p>
  60. * <p>
  61. *
  62. * @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a>
  63. * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  64. * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
  65. * @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
  66. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  67. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  68. *
  69. * @since 2.0
  70. */
  71. public class MultipartPostMethod extends ExpectContinueMethod {
  72. /** The Content-Type for multipart/form-data. */
  73. public static final String MULTIPART_FORM_CONTENT_TYPE =
  74. "multipart/form-data";
  75. /** Log object for this class. */
  76. private static final Log LOG = LogFactory.getLog(MultipartPostMethod.class);
  77. /** The parameters for this method */
  78. private final List parameters = new ArrayList();
  79. /**
  80. * No-arg constructor.
  81. */
  82. public MultipartPostMethod() {
  83. super();
  84. }
  85. /**
  86. * Constructor specifying a URI.
  87. *
  88. * @param uri either an absolute or relative URI
  89. */
  90. public MultipartPostMethod(String uri) {
  91. super(uri);
  92. }
  93. /**
  94. * Returns <tt>true</tt>
  95. *
  96. * @return <tt>true</tt>
  97. *
  98. * @since 2.0beta1
  99. */
  100. protected boolean hasRequestContent() {
  101. return true;
  102. }
  103. /**
  104. * Returns <tt>"POST"</tt>.
  105. * @return <tt>"POST"</tt>
  106. */
  107. public String getName() {
  108. return "POST";
  109. }
  110. /**
  111. * Adds a text field part
  112. *
  113. * @param parameterName The name of the parameter.
  114. * @param parameterValue The value of the parameter.
  115. */
  116. public void addParameter(String parameterName, String parameterValue) {
  117. LOG.trace("enter addParameter(String parameterName, String parameterValue)");
  118. Part param = new StringPart(parameterName, parameterValue);
  119. parameters.add(param);
  120. }
  121. /**
  122. * Adds a binary file part
  123. *
  124. * @param parameterName The name of the parameter
  125. * @param parameterFile The name of the file.
  126. * @throws FileNotFoundException If the file cannot be found.
  127. */
  128. public void addParameter(String parameterName, File parameterFile)
  129. throws FileNotFoundException {
  130. LOG.trace("enter MultipartPostMethod.addParameter(String parameterName, "
  131. + "File parameterFile)");
  132. Part param = new FilePart(parameterName, parameterFile);
  133. parameters.add(param);
  134. }
  135. /**
  136. * Adds a binary file part with the given file name
  137. *
  138. * @param parameterName The name of the parameter
  139. * @param fileName The file name
  140. * @param parameterFile The file
  141. * @throws FileNotFoundException If the file cannot be found.
  142. */
  143. public void addParameter(String parameterName, String fileName, File parameterFile)
  144. throws FileNotFoundException {
  145. LOG.trace("enter MultipartPostMethod.addParameter(String parameterName, "
  146. + "String fileName, File parameterFile)");
  147. Part param = new FilePart(parameterName, fileName, parameterFile);
  148. parameters.add(param);
  149. }
  150. /**
  151. * Adds a part.
  152. *
  153. * @param part The part to add.
  154. */
  155. public void addPart (final Part part) {
  156. LOG.trace("enter addPart(Part part)");
  157. parameters.add(part);
  158. }
  159. /**
  160. * Returns all parts.
  161. *
  162. * @return an array of containing all parts
  163. */
  164. public Part[] getParts() {
  165. return (Part[]) parameters.toArray(new Part[parameters.size()]);
  166. }
  167. /**
  168. * Adds a <tt>Content-Length</tt> request header, as long as no
  169. * <tt>Content-Length</tt> request header already exists.
  170. *
  171. * @param state current state of http requests
  172. * @param conn the connection to use for I/O
  173. *
  174. * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
  175. * can be recovered from.
  176. * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
  177. * cannot be recovered from.
  178. *
  179. * @since 3.0
  180. */
  181. protected void addContentLengthRequestHeader(HttpState state,
  182. HttpConnection conn)
  183. throws IOException, HttpException {
  184. LOG.trace("enter EntityEnclosingMethod.addContentLengthRequestHeader("
  185. + "HttpState, HttpConnection)");
  186. if (getRequestHeader("Content-Length") == null) {
  187. long len = getRequestContentLength();
  188. addRequestHeader("Content-Length", String.valueOf(len));
  189. }
  190. removeRequestHeader("Transfer-Encoding");
  191. }
  192. /**
  193. * Adds a <tt>Content-Type</tt> request header.
  194. *
  195. * @param state current state of http requests
  196. * @param conn the connection to use for I/O
  197. *
  198. * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
  199. * can be recovered from.
  200. * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
  201. * cannot be recovered from.
  202. *
  203. * @since 3.0
  204. */
  205. protected void addContentTypeRequestHeader(HttpState state,
  206. HttpConnection conn)
  207. throws IOException, HttpException {
  208. LOG.trace("enter EntityEnclosingMethod.addContentTypeRequestHeader("
  209. + "HttpState, HttpConnection)");
  210. if (!parameters.isEmpty()) {
  211. StringBuffer buffer = new StringBuffer(MULTIPART_FORM_CONTENT_TYPE);
  212. if (Part.getBoundary() != null) {
  213. buffer.append("; boundary=");
  214. buffer.append(Part.getBoundary());
  215. }
  216. setRequestHeader("Content-Type", buffer.toString());
  217. }
  218. }
  219. /**
  220. * Populates the request headers map to with additional
  221. * {@link org.apache.commons.httpclient.Header headers} to be submitted to
  222. * the given {@link HttpConnection}.
  223. *
  224. * <p>
  225. * This implementation adds tt>Content-Length</tt> and <tt>Content-Type</tt>
  226. * headers, when appropriate.
  227. * </p>
  228. *
  229. * <p>
  230. * Subclasses may want to override this method to to add additional
  231. * headers, and may choose to invoke this implementation (via
  232. * <tt>super</tt>) to add the "standard" headers.
  233. * </p>
  234. *
  235. * @param state the {@link HttpState state} information associated with this method
  236. * @param conn the {@link HttpConnection connection} used to execute
  237. * this HTTP method
  238. *
  239. * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
  240. * can be recovered from.
  241. * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
  242. * cannot be recovered from.
  243. *
  244. * @see #writeRequestHeaders
  245. */
  246. protected void addRequestHeaders(HttpState state, HttpConnection conn)
  247. throws IOException, HttpException {
  248. LOG.trace("enter MultipartPostMethod.addRequestHeaders(HttpState state, "
  249. + "HttpConnection conn)");
  250. super.addRequestHeaders(state, conn);
  251. addContentLengthRequestHeader(state, conn);
  252. addContentTypeRequestHeader(state, conn);
  253. }
  254. /**
  255. * Writes the request body to the given {@link HttpConnection connection}.
  256. *
  257. * @param state the {@link HttpState state} information associated with this method
  258. * @param conn the {@link HttpConnection connection} used to execute
  259. * this HTTP method
  260. *
  261. * @return <tt>true</tt>
  262. *
  263. * @throws IOException if an I/O (transport) error occurs. Some transport exceptions
  264. * can be recovered from.
  265. * @throws HttpException if a protocol exception occurs. Usually protocol exceptions
  266. * cannot be recovered from.
  267. */
  268. protected boolean writeRequestBody(HttpState state, HttpConnection conn)
  269. throws IOException, HttpException {
  270. LOG.trace("enter MultipartPostMethod.writeRequestBody(HttpState state, "
  271. + "HttpConnection conn)");
  272. OutputStream out = conn.getRequestOutputStream();
  273. Part.sendParts(out, getParts());
  274. return true;
  275. }
  276. /**
  277. * <p>Return the length of the request body.</p>
  278. *
  279. * <p>Once this method has been invoked, the request parameters cannot be
  280. * altered until the method is {@link #recycle recycled}.</p>
  281. *
  282. * @return The request content length.
  283. */
  284. protected long getRequestContentLength() throws IOException {
  285. LOG.trace("enter MultipartPostMethod.getRequestContentLength()");
  286. return Part.getLengthOfParts(getParts());
  287. }
  288. /**
  289. * Recycles the HTTP method so that it can be used again.
  290. * Note that all of the instance variables will be reset
  291. * once this method has been called. This method will also
  292. * release the connection being used by this HTTP method.
  293. *
  294. * @see #releaseConnection()
  295. *
  296. * @deprecated no longer supported and will be removed in the future
  297. * version of HttpClient
  298. */
  299. public void recycle() {
  300. LOG.trace("enter MultipartPostMethod.recycle()");
  301. super.recycle();
  302. parameters.clear();
  303. }
  304. }