1 31 32 package org.apache.commons.httpclient.methods.multipart; 33 34 import java.io.ByteArrayOutputStream ; 35 import java.io.IOException ; 36 import java.io.OutputStream ; 37 38 import org.apache.commons.httpclient.HttpConstants; 39 import org.apache.commons.logging.Log; 40 import org.apache.commons.logging.LogFactory; 41 42 53 public abstract class Part { 54 55 56 private static final Log LOG = LogFactory.getLog(Part.class); 57 58 60 61 protected static final String BOUNDARY = "----------------314159265358979323846"; 62 63 64 protected static final byte[] BOUNDARY_BYTES = HttpConstants.getAsciiBytes(BOUNDARY); 65 66 67 protected static final String CRLF = "\r\n"; 68 69 70 protected static final byte[] CRLF_BYTES = HttpConstants.getAsciiBytes(CRLF); 71 72 73 protected static final String QUOTE = "\""; 74 75 76 protected static final byte[] QUOTE_BYTES = 77 HttpConstants.getAsciiBytes(QUOTE); 78 79 80 protected static final String EXTRA = "--"; 81 82 83 protected static final byte[] EXTRA_BYTES = 84 HttpConstants.getAsciiBytes(EXTRA); 85 86 87 protected static final String CONTENT_DISPOSITION = "Content-Disposition: form-data; name="; 88 89 90 protected static final byte[] CONTENT_DISPOSITION_BYTES = 91 HttpConstants.getAsciiBytes(CONTENT_DISPOSITION); 92 93 94 protected static final String CONTENT_TYPE = "Content-Type: "; 95 96 97 protected static final byte[] CONTENT_TYPE_BYTES = 98 HttpConstants.getAsciiBytes(CONTENT_TYPE); 99 100 101 protected static final String CHARSET = "; charset="; 102 103 104 protected static final byte[] CHARSET_BYTES = 105 HttpConstants.getAsciiBytes(CHARSET); 106 107 108 protected static final String CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding: "; 109 110 111 protected static final byte[] CONTENT_TRANSFER_ENCODING_BYTES = 112 HttpConstants.getAsciiBytes(CONTENT_TRANSFER_ENCODING); 113 114 118 public static String getBoundary() { 119 return BOUNDARY; 120 } 121 122 126 public abstract String getName(); 127 128 132 public abstract String getContentType(); 133 134 139 public abstract String getCharSet(); 140 141 145 public abstract String getTransferEncoding(); 146 147 152 protected void sendStart(OutputStream out) throws IOException { 153 LOG.trace("enter sendStart(OutputStream out)"); 154 out.write(EXTRA_BYTES); 155 out.write(BOUNDARY_BYTES); 156 out.write(CRLF_BYTES); 157 } 158 159 165 protected void sendDispositionHeader(OutputStream out) throws IOException { 166 LOG.trace("enter sendDispositionHeader(OutputStream out)"); 167 out.write(CONTENT_DISPOSITION_BYTES); 168 out.write(QUOTE_BYTES); 169 out.write(HttpConstants.getAsciiBytes(getName())); 170 out.write(QUOTE_BYTES); 171 } 172 173 178 protected void sendContentTypeHeader(OutputStream out) throws IOException { 179 LOG.trace("enter sendContentTypeHeader(OutputStream out)"); 180 String contentType = getContentType(); 181 if (contentType != null) { 182 out.write(CRLF_BYTES); 183 out.write(CONTENT_TYPE_BYTES); 184 out.write(HttpConstants.getAsciiBytes(contentType)); 185 String charSet = getCharSet(); 186 if (charSet != null) { 187 out.write(CHARSET_BYTES); 188 out.write(HttpConstants.getAsciiBytes(charSet)); 189 } 190 } 191 } 192 193 200 protected void sendTransferEncodingHeader(OutputStream out) throws IOException { 201 LOG.trace("enter sendTransferEncodingHeader(OutputStream out)"); 202 String transferEncoding = getTransferEncoding(); 203 if (transferEncoding != null) { 204 out.write(CRLF_BYTES); 205 out.write(CONTENT_TRANSFER_ENCODING_BYTES); 206 out.write(HttpConstants.getAsciiBytes(transferEncoding)); 207 } 208 } 209 210 215 protected void sendEndOfHeader(OutputStream out) throws IOException { 216 LOG.trace("enter sendEndOfHeader(OutputStream out)"); 217 out.write(CRLF_BYTES); 218 out.write(CRLF_BYTES); 219 } 220 221 226 protected abstract void sendData(OutputStream out) throws IOException ; 227 228 234 protected abstract long lengthOfData() throws IOException ; 235 236 241 protected void sendEnd(OutputStream out) throws IOException { 242 LOG.trace("enter sendEnd(OutputStream out)"); 243 out.write(CRLF_BYTES); 244 } 245 246 254 public void send(OutputStream out) throws IOException { 255 LOG.trace("enter send(OutputStream out)"); 256 sendStart(out); 257 sendDispositionHeader(out); 258 sendContentTypeHeader(out); 259 sendTransferEncodingHeader(out); 260 sendEndOfHeader(out); 261 sendData(out); 262 sendEnd(out); 263 } 264 265 266 274 public long length() throws IOException { 275 LOG.trace("enter length()"); 276 ByteArrayOutputStream overhead = new ByteArrayOutputStream (); 277 sendStart(overhead); 278 sendDispositionHeader(overhead); 279 sendContentTypeHeader(overhead); 280 sendTransferEncodingHeader(overhead); 281 sendEndOfHeader(overhead); 282 sendEnd(overhead); 283 return overhead.size() + lengthOfData(); 284 } 285 286 291 public String toString() { 292 return this.getName(); 293 } 294 295 303 public static void sendParts(OutputStream out, final Part[] parts) 304 throws IOException { 305 LOG.trace("enter sendParts(OutputStream out, Parts[])"); 306 if (parts == null) { 307 throw new IllegalArgumentException ("Parts may not be null"); 308 } 309 for (int i = 0; i < parts.length; i++) { 310 parts[i].send(out); 311 } 312 out.write(EXTRA_BYTES); 313 out.write(BOUNDARY_BYTES); 314 out.write(EXTRA_BYTES); 315 out.write(CRLF_BYTES); 316 } 317 318 327 public static long getLengthOfParts(final Part[] parts) 328 throws IOException { 329 LOG.trace("getLengthOfParts(Parts[])"); 330 if (parts == null) { 331 throw new IllegalArgumentException ("Parts may not be null"); 332 } 333 long total = 0; 334 for (int i = 0; i < parts.length; i++) { 335 total += parts[i].length(); 336 } 337 total += EXTRA_BYTES.length; 338 total += BOUNDARY_BYTES.length; 339 total += EXTRA_BYTES.length; 340 total += CRLF_BYTES.length; 341 return total; 342 } 343 } 344 | Popular Tags |