View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/MultipartPostMethod.java,v 1.17 2003/04/07 19:23:36 olegk Exp $ 3 * $Revision: 1.17 $ 4 * $Date: 2003/04/07 19:23:36 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 2002-2003 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 package org.apache.commons.httpclient.methods; 65 66 import java.io.File; 67 import java.io.FileNotFoundException; 68 import java.io.IOException; 69 import java.io.OutputStream; 70 import java.util.ArrayList; 71 import java.util.List; 72 73 import org.apache.commons.httpclient.HttpConnection; 74 import org.apache.commons.httpclient.HttpException; 75 import org.apache.commons.httpclient.HttpState; 76 import org.apache.commons.httpclient.methods.multipart.FilePart; 77 import org.apache.commons.httpclient.methods.multipart.Part; 78 import org.apache.commons.httpclient.methods.multipart.StringPart; 79 import org.apache.commons.logging.Log; 80 import org.apache.commons.logging.LogFactory; 81 82 /*** 83 * POST Method for Multipart encoded forms. 84 * 85 * @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a> 86 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> 87 * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a> 88 * @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a> 89 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 90 * 91 * @since 2.0 92 */ 93 public class MultipartPostMethod extends ExpectContinueMethod { 94 95 /*** The Content-Type for multipart/form-data. */ 96 public static final String MULTIPART_FORM_CONTENT_TYPE = 97 "multipart/form-data"; 98 99 /*** Log object for this class. */ 100 private static final Log LOG = LogFactory.getLog(MultipartPostMethod.class); 101 102 /*** The parameters for this method */ 103 private final List parameters = new ArrayList(); 104 105 /*** 106 * No-arg constructor. 107 */ 108 public MultipartPostMethod() { 109 super(); 110 } 111 112 /*** 113 * Constructor specifying a URI. 114 * 115 * @param uri either an absolute or relative URI 116 */ 117 public MultipartPostMethod(String uri) { 118 super(uri); 119 } 120 121 /*** 122 * Constructor specifying a URI and tempDir. 123 * 124 * @param uri either an absolute or relative URI 125 * @param tempDir directory to store temp files in 126 */ 127 public MultipartPostMethod(String uri, String tempDir) { 128 super(uri, tempDir); 129 } 130 131 /*** 132 * Constructor specifying a URI, tempDir and tempFile. 133 * 134 * @param uri either an absolute or relative URI 135 * @param tempDir directory to store temp files in 136 * @param tempFile file to store temporary data in 137 */ 138 public MultipartPostMethod(String uri, String tempDir, String tempFile) { 139 super(uri, tempDir, tempFile); 140 } 141 142 /*** 143 * Returns <tt>true</tt> 144 * 145 * @return <tt>true</tt> 146 * 147 * @since 2.0beta1 148 */ 149 protected boolean hasRequestContent() { 150 return true; 151 } 152 153 /*** 154 * Returns <tt>"POST"</tt>. 155 * @return <tt>"POST"</tt> 156 */ 157 public String getName() { 158 return "POST"; 159 } 160 161 /*** 162 * Add a parameter 163 * @param parameterName The name of the parameter. 164 * @param parameterValue The value of the parameter. 165 */ 166 public void addParameter(String parameterName, String parameterValue) { 167 LOG.trace("enter addParameter(String parameterName, String parameterValue)"); 168 Part param = new StringPart(parameterName, parameterValue); 169 parameters.add(param); 170 } 171 172 /*** 173 * Add a parameter 174 * @param parameterName The name of the parameter 175 * @param parameterFile The name of the file. 176 * @throws FileNotFoundException If the file cannot be found. 177 */ 178 public void addParameter(String parameterName, File parameterFile) 179 throws FileNotFoundException { 180 LOG.trace("enter MultipartPostMethod.addParameter(String parameterName, " 181 + "File parameterFile)"); 182 Part param = new FilePart(parameterName, parameterFile); 183 parameters.add(param); 184 } 185 186 /*** 187 * Add a parameter. 188 * 189 * @param parameterName The name of the parameter 190 * @param fileName The file name 191 * @param parameterFile The file 192 * @throws FileNotFoundException If the file cannot be found. 193 */ 194 public void addParameter(String parameterName, String fileName, File parameterFile) 195 throws FileNotFoundException { 196 LOG.trace("enter MultipartPostMethod.addParameter(String parameterName, " 197 + "String fileName, File parameterFile)"); 198 Part param = new FilePart(parameterName, fileName, parameterFile); 199 parameters.add(param); 200 } 201 202 /*** 203 * Adds another part to this post. 204 * @param part The part to add. 205 */ 206 public void addPart (final Part part) { 207 LOG.trace("enter addPart(Part part)"); 208 parameters.add(part); 209 } 210 211 /*** 212 * Return all parts. 213 * 214 * @return an array of containing all parts 215 */ 216 public Part[] getParts() { 217 return (Part[]) parameters.toArray(new Part[parameters.size()]); 218 } 219 /*** 220 * Add content type header and set the <tt>Expect</tt> header 221 * if it has not already been set, in addition to the "standard" 222 * set of headers 223 * 224 * @param state the client state 225 * @param conn the {@link HttpConnection} the headers will eventually be 226 * written to 227 * 228 * @throws IOException when an error occurs writing the request 229 * @throws HttpException when a HTTP protocol error occurs 230 */ 231 protected void addRequestHeaders(HttpState state, HttpConnection conn) 232 throws IOException, HttpException { 233 LOG.trace("enter MultipartPostMethod.addRequestHeaders(HttpState state, " 234 + "HttpConnection conn)"); 235 super.addRequestHeaders(state, conn); 236 237 if (!parameters.isEmpty()) { 238 StringBuffer buffer = new StringBuffer(MULTIPART_FORM_CONTENT_TYPE); 239 if (Part.getBoundary() != null) { 240 buffer.append("; boundary="); 241 buffer.append(Part.getBoundary()); 242 } 243 setRequestHeader("Content-Type", buffer.toString()); 244 } 245 } 246 247 /*** 248 * Write the request body. 249 * 250 * @param state the client state 251 * @param conn the connection to write to 252 * 253 * @return <tt>true</tt> 254 * @throws IOException when i/o errors occur reading the response 255 * @throws HttpException when a protocol error occurs or state is invalid 256 */ 257 protected boolean writeRequestBody(HttpState state, HttpConnection conn) 258 throws IOException, HttpException { 259 LOG.trace("enter MultipartPostMethod.writeRequestBody(HttpState state, " 260 + "HttpConnection conn)"); 261 OutputStream out = conn.getRequestOutputStream(); 262 Part.sendParts(out, getParts()); 263 return true; 264 } 265 266 /*** 267 * <p>Return the length of the request body.</p> 268 * 269 * <p>Once this method has been invoked, the request parameters cannot be 270 * altered until I am {@link #recycle recycled}.</p> 271 * 272 * @return The request content length. 273 */ 274 protected int getRequestContentLength() { 275 LOG.trace("enter MultipartPostMethod.getRequestContentLength()"); 276 try { 277 long len = Part.getLengthOfParts(getParts()); 278 // Chop the length to the max int value. 279 if (len <= Integer.MAX_VALUE) { 280 return (int) len; 281 } else { 282 return (Integer.MAX_VALUE); 283 } 284 } catch (IOException e) { 285 // Can't throw an IOException and still override 286 throw new RuntimeException(e.toString()); 287 } 288 } 289 290 291 /*** 292 * Clear my request body. 293 */ 294 public void recycle() { 295 LOG.trace("enter MultipartPostMethod.recycle()"); 296 super.recycle(); 297 parameters.clear(); 298 } 299 }

This page was automatically generated by Maven