1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/Part.java,v 1.14 2004/04/18 23:51:37 jsdever Exp $
  3. * $Revision: 1.14 $
  4. * $Date: 2004/04/18 23:51:37 $
  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.multipart;
  30. import java.io.ByteArrayOutputStream;
  31. import java.io.IOException;
  32. import java.io.OutputStream;
  33. import org.apache.commons.httpclient.util.EncodingUtil;
  34. import org.apache.commons.logging.Log;
  35. import org.apache.commons.logging.LogFactory;
  36. /**
  37. * Abstract class for one Part of a multipart post object.
  38. *
  39. * @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a>
  40. * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  41. * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
  42. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  43. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  44. *
  45. * @since 2.0
  46. */
  47. public abstract class Part {
  48. /** Log object for this class. */
  49. private static final Log LOG = LogFactory.getLog(Part.class);
  50. //TODO: Make this configurable
  51. /** The boundary */
  52. protected static final String BOUNDARY = "----------------314159265358979323846";
  53. /** The boundary as a byte array */
  54. protected static final byte[] BOUNDARY_BYTES = EncodingUtil.getAsciiBytes(BOUNDARY);
  55. /** Carriage return/linefeed */
  56. protected static final String CRLF = "\r\n";
  57. /** Carriage return/linefeed as a byte array */
  58. protected static final byte[] CRLF_BYTES = EncodingUtil.getAsciiBytes(CRLF);
  59. /** Content dispostion characters */
  60. protected static final String QUOTE = "\"";
  61. /** Content dispostion as a byte array */
  62. protected static final byte[] QUOTE_BYTES =
  63. EncodingUtil.getAsciiBytes(QUOTE);
  64. /** Extra characters */
  65. protected static final String EXTRA = "--";
  66. /** Extra characters as a byte array */
  67. protected static final byte[] EXTRA_BYTES =
  68. EncodingUtil.getAsciiBytes(EXTRA);
  69. /** Content dispostion characters */
  70. protected static final String CONTENT_DISPOSITION = "Content-Disposition: form-data; name=";
  71. /** Content dispostion as a byte array */
  72. protected static final byte[] CONTENT_DISPOSITION_BYTES =
  73. EncodingUtil.getAsciiBytes(CONTENT_DISPOSITION);
  74. /** Content type header */
  75. protected static final String CONTENT_TYPE = "Content-Type: ";
  76. /** Content type header as a byte array */
  77. protected static final byte[] CONTENT_TYPE_BYTES =
  78. EncodingUtil.getAsciiBytes(CONTENT_TYPE);
  79. /** Content charset */
  80. protected static final String CHARSET = "; charset=";
  81. /** Content charset as a byte array */
  82. protected static final byte[] CHARSET_BYTES =
  83. EncodingUtil.getAsciiBytes(CHARSET);
  84. /** Content type header */
  85. protected static final String CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding: ";
  86. /** Content type header as a byte array */
  87. protected static final byte[] CONTENT_TRANSFER_ENCODING_BYTES =
  88. EncodingUtil.getAsciiBytes(CONTENT_TRANSFER_ENCODING);
  89. /**
  90. * Return the boundary string.
  91. * @return the boundary string
  92. */
  93. public static String getBoundary() {
  94. return BOUNDARY;
  95. }
  96. /**
  97. * Return the name of this part.
  98. * @return The name.
  99. */
  100. public abstract String getName();
  101. /**
  102. * Returns the content type of this part.
  103. * @return the content type, or <code>null</code> to exclude the content type header
  104. */
  105. public abstract String getContentType();
  106. /**
  107. * Return the character encoding of this part.
  108. * @return the character encoding, or <code>null</code> to exclude the character
  109. * encoding header
  110. */
  111. public abstract String getCharSet();
  112. /**
  113. * Return the transfer encoding of this part.
  114. * @return the transfer encoding, or <code>null</code> to exclude the transfer encoding header
  115. */
  116. public abstract String getTransferEncoding();
  117. /**
  118. * Write the start to the specified output stream
  119. * @param out The output stream
  120. * @throws IOException If an IO problem occurs.
  121. */
  122. protected void sendStart(OutputStream out) throws IOException {
  123. LOG.trace("enter sendStart(OutputStream out)");
  124. out.write(EXTRA_BYTES);
  125. out.write(BOUNDARY_BYTES);
  126. out.write(CRLF_BYTES);
  127. }
  128. /**
  129. * Write the content disposition header to the specified output stream
  130. *
  131. * @param out The output stream
  132. * @throws IOException If an IO problem occurs.
  133. */
  134. protected void sendDispositionHeader(OutputStream out) throws IOException {
  135. LOG.trace("enter sendDispositionHeader(OutputStream out)");
  136. out.write(CONTENT_DISPOSITION_BYTES);
  137. out.write(QUOTE_BYTES);
  138. out.write(EncodingUtil.getAsciiBytes(getName()));
  139. out.write(QUOTE_BYTES);
  140. }
  141. /**
  142. * Write the content type header to the specified output stream
  143. * @param out The output stream
  144. * @throws IOException If an IO problem occurs.
  145. */
  146. protected void sendContentTypeHeader(OutputStream out) throws IOException {
  147. LOG.trace("enter sendContentTypeHeader(OutputStream out)");
  148. String contentType = getContentType();
  149. if (contentType != null) {
  150. out.write(CRLF_BYTES);
  151. out.write(CONTENT_TYPE_BYTES);
  152. out.write(EncodingUtil.getAsciiBytes(contentType));
  153. String charSet = getCharSet();
  154. if (charSet != null) {
  155. out.write(CHARSET_BYTES);
  156. out.write(EncodingUtil.getAsciiBytes(charSet));
  157. }
  158. }
  159. }
  160. /**
  161. * Write the content transfer encoding header to the specified
  162. * output stream
  163. *
  164. * @param out The output stream
  165. * @throws IOException If an IO problem occurs.
  166. */
  167. protected void sendTransferEncodingHeader(OutputStream out) throws IOException {
  168. LOG.trace("enter sendTransferEncodingHeader(OutputStream out)");
  169. String transferEncoding = getTransferEncoding();
  170. if (transferEncoding != null) {
  171. out.write(CRLF_BYTES);
  172. out.write(CONTENT_TRANSFER_ENCODING_BYTES);
  173. out.write(EncodingUtil.getAsciiBytes(transferEncoding));
  174. }
  175. }
  176. /**
  177. * Write the end of the header to the output stream
  178. * @param out The output stream
  179. * @throws IOException If an IO problem occurs.
  180. */
  181. protected void sendEndOfHeader(OutputStream out) throws IOException {
  182. LOG.trace("enter sendEndOfHeader(OutputStream out)");
  183. out.write(CRLF_BYTES);
  184. out.write(CRLF_BYTES);
  185. }
  186. /**
  187. * Write the data to the specified output stream
  188. * @param out The output stream
  189. * @throws IOException If an IO problem occurs.
  190. */
  191. protected abstract void sendData(OutputStream out) throws IOException;
  192. /**
  193. * Return the length of the main content
  194. *
  195. * @return long The length.
  196. * @throws IOException If an IO problem occurs
  197. */
  198. protected abstract long lengthOfData() throws IOException;
  199. /**
  200. * Write the end data to the output stream.
  201. * @param out The output stream
  202. * @throws IOException If an IO problem occurs.
  203. */
  204. protected void sendEnd(OutputStream out) throws IOException {
  205. LOG.trace("enter sendEnd(OutputStream out)");
  206. out.write(CRLF_BYTES);
  207. }
  208. /**
  209. * Write all the data to the output stream.
  210. * If you override this method make sure to override
  211. * #length() as well
  212. *
  213. * @param out The output stream
  214. * @throws IOException If an IO problem occurs.
  215. */
  216. public void send(OutputStream out) throws IOException {
  217. LOG.trace("enter send(OutputStream out)");
  218. sendStart(out);
  219. sendDispositionHeader(out);
  220. sendContentTypeHeader(out);
  221. sendTransferEncodingHeader(out);
  222. sendEndOfHeader(out);
  223. sendData(out);
  224. sendEnd(out);
  225. }
  226. /**
  227. * Return the full length of all the data.
  228. * If you override this method make sure to override
  229. * #send(OutputStream) as well
  230. *
  231. * @return long The length.
  232. * @throws IOException If an IO problem occurs
  233. */
  234. public long length() throws IOException {
  235. LOG.trace("enter length()");
  236. ByteArrayOutputStream overhead = new ByteArrayOutputStream();
  237. sendStart(overhead);
  238. sendDispositionHeader(overhead);
  239. sendContentTypeHeader(overhead);
  240. sendTransferEncodingHeader(overhead);
  241. sendEndOfHeader(overhead);
  242. sendEnd(overhead);
  243. return overhead.size() + lengthOfData();
  244. }
  245. /**
  246. * Return a string representation of this object.
  247. * @return A string representation of this object.
  248. * @see java.lang.Object#toString()
  249. */
  250. public String toString() {
  251. return this.getName();
  252. }
  253. /**
  254. * Write all parts and the last boundary to the specified output stream
  255. *
  256. * @param out The output stream
  257. * @param parts The array of parts to be sent
  258. *
  259. * @throws IOException If an IO problem occurs.
  260. */
  261. public static void sendParts(OutputStream out, final Part[] parts)
  262. throws IOException {
  263. LOG.trace("enter sendParts(OutputStream out, Parts[])");
  264. if (parts == null) {
  265. throw new IllegalArgumentException("Parts may not be null");
  266. }
  267. for (int i = 0; i < parts.length; i++) {
  268. parts[i].send(out);
  269. }
  270. out.write(EXTRA_BYTES);
  271. out.write(BOUNDARY_BYTES);
  272. out.write(EXTRA_BYTES);
  273. out.write(CRLF_BYTES);
  274. }
  275. /**
  276. * Return the total sum of all parts and that of the last boundary
  277. *
  278. * @param parts The array of parts
  279. *
  280. * @return the total length
  281. *
  282. * @throws IOException If an IO problem occurs.
  283. */
  284. public static long getLengthOfParts(final Part[] parts)
  285. throws IOException {
  286. LOG.trace("getLengthOfParts(Parts[])");
  287. if (parts == null) {
  288. throw new IllegalArgumentException("Parts may not be null");
  289. }
  290. long total = 0;
  291. for (int i = 0; i < parts.length; i++) {
  292. total += parts[i].length();
  293. }
  294. total += EXTRA_BYTES.length;
  295. total += BOUNDARY_BYTES.length;
  296. total += EXTRA_BYTES.length;
  297. total += CRLF_BYTES.length;
  298. return total;
  299. }
  300. }