You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by mb...@apache.org on 2003/10/10 06:18:35 UTC
cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart PartBase.java Part.java StringPart.java FilePart.java
mbecke 2003/10/09 21:18:35
Modified: httpclient/src/java/org/apache/commons/httpclient/methods/multipart
Part.java StringPart.java FilePart.java
Added: httpclient/src/java/org/apache/commons/httpclient/methods/multipart
PartBase.java
Log:
Adds support for disabling transfer-encoding and content-type headers in multi-part posts.
PR: 23431
Submitted by: Michael Becke
Reviewed by: Oleg Kalnichevski
Revision Changes Path
1.11 +9 -11 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/Part.java
Index: Part.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/Part.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- Part.java 4 Apr 2003 02:37:03 -0000 1.10
+++ Part.java 10 Oct 2003 04:18:35 -0000 1.11
@@ -153,25 +153,26 @@
/**
* Return the name of this part.
- * @return String The name.
+ * @return The name.
*/
public abstract String getName();
/**
- * Return the content type of this part.
- * @return String The name.
+ * Returns the content type of this part.
+ * @return the content type, or <code>null</code> to exclude the content type header
*/
public abstract String getContentType();
/**
* Return the character encoding of this part.
- * @return String The name.
+ * @return the character encoding, or <code>null</code> to exclude the character
+ * encoding header
*/
public abstract String getCharSet();
/**
* Return the transfer encoding of this part.
- * @return String The name.
+ * @return the transfer encoding, or <code>null</code> to exclude the transfer encoding header
*/
public abstract String getTransferEncoding();
@@ -206,7 +207,6 @@
* @param out The output stream
* @throws IOException If an IO problem occurs.
*/
-
protected void sendContentTypeHeader(OutputStream out) throws IOException {
LOG.trace("enter sendContentTypeHeader(OutputStream out)");
String contentType = getContentType();
@@ -229,7 +229,6 @@
* @param out The output stream
* @throws IOException If an IO problem occurs.
*/
-
protected void sendTransferEncodingHeader(OutputStream out) throws IOException {
LOG.trace("enter sendTransferEncodingHeader(OutputStream out)");
String transferEncoding = getTransferEncoding();
@@ -324,7 +323,6 @@
public String toString() {
return this.getName();
}
-
/**
* Write all parts and the last boundary to the specified output stream
1.8 +39 -54 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/StringPart.java
Index: StringPart.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/StringPart.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- StringPart.java 6 Apr 2003 22:31:54 -0000 1.7
+++ StringPart.java 10 Oct 2003 04:18:35 -0000 1.8
@@ -79,7 +79,7 @@
*
* @since 2.0
*/
-public class StringPart extends Part {
+public class StringPart extends PartBase {
/** Log object for this class. */
private static final Log LOG = LogFactory.getLog(StringPart.class);
@@ -93,33 +93,28 @@
/** Default transfer encoding of string parameters*/
public static final String DEFAULT_TRANSFER_ENCODING = "8bit";
- /** Name of this StringPart. */
- private String name;
-
/** Contents of this StringPart. */
private byte[] content;
-
- /** Charset of this StringPart. */
- private String charset;
+ /** The String value of this part. */
+ private String value;
+
/**
* Constructor.
*
* @param name The name of the part
* @param value the string to post
- * @param charset the charset to be used to encode the string
+ * @param charset the charset to be used to encode the string, if <code>null</code>
+ * the {@link #DEFAULT_CHARSET default} is used
*/
public StringPart(String name, String value, String charset) {
- LOG.trace("enter StringPart(String, String, String)");
- if (name == null) {
- throw new IllegalArgumentException("Name may not be null");
- }
- this.name = name;
- if (charset != null) {
- this.charset = charset;
- } else {
- this.charset = DEFAULT_CHARSET;
- }
+
+ super(
+ name,
+ DEFAULT_CONTENT_TYPE,
+ charset == null ? DEFAULT_CHARSET : charset,
+ DEFAULT_TRANSFER_ENCODING
+ );
if (value == null) {
throw new IllegalArgumentException("Value may not be null");
}
@@ -127,7 +122,7 @@
// See RFC 2048, 2.8. "8bit Data"
throw new IllegalArgumentException("NULs may not be present in string parts");
}
- this.content = HttpConstants.getContentBytes(value, this.charset);
+ this.value = value;
}
/**
@@ -139,37 +134,18 @@
public StringPart(String name, String value) {
this(name, value, null);
}
-
- /**
- * Return the name of this part.
- * @return the name of this StringPart.
- */
- public String getName() {
- return name;
- }
-
- /**
- * Return the content type of this part.
- * @return String The name.
- */
- public String getContentType() {
- return DEFAULT_CONTENT_TYPE;
- }
-
- /**
- * Return the character encoding of this part.
- * @return String The name.
- */
- public String getCharSet() {
- return this.charset;
- }
-
+
/**
- * Return the transfer encoding of this part.
- * @return String The name.
- */
- public String getTransferEncoding() {
- return DEFAULT_TRANSFER_ENCODING;
+ * Gets the content in bytes. Bytes are lazily created to allow the charset to be changed
+ * after the part is created.
+ *
+ * @return the content in bytes
+ */
+ private byte[] getContent() {
+ if (content == null) {
+ content = HttpConstants.getContentBytes(value, getCharSet());
+ }
+ return content;
}
/**
@@ -179,7 +155,7 @@
*/
protected void sendData(OutputStream out) throws IOException {
LOG.trace("enter sendData(OutputStream)");
- out.write(this.content);
+ out.write(getContent());
}
/**
@@ -190,6 +166,15 @@
*/
protected long lengthOfData() throws IOException {
LOG.trace("enter lengthOfData()");
- return this.content.length;
+ return getContent().length;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.httpclient.methods.multipart.BasePart#setCharSet(java.lang.String)
+ */
+ public void setCharSet(String charSet) {
+ super.setCharSet(charSet);
+ this.content = null;
}
+
}
1.15 +24 -65 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java
Index: FilePart.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- FilePart.java 16 Mar 2003 12:05:03 -0000 1.14
+++ FilePart.java 10 Oct 2003 04:18:35 -0000 1.15
@@ -87,7 +87,7 @@
* @since 2.0
*
*/
-public class FilePart extends Part {
+public class FilePart extends PartBase {
/** Default content encoding of file attachments. */
public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
@@ -101,7 +101,6 @@
/** Log object for this class. */
private static final Log LOG = LogFactory.getLog(FilePart.class);
-
/** Attachment's file name */
protected static final String FILE_NAME = "; filename=";
@@ -109,32 +108,28 @@
protected static final byte[] FILE_NAME_BYTES =
HttpConstants.getAsciiBytes(FILE_NAME);
- /** Name of the file part. */
- private String name;
-
/** Source of the file part. */
private PartSource source;
-
- /** Content type of the file part. */
- private String contentType;
-
- /** Content encoding of the file part. */
- private String charset;
/**
* FilePart Constructor.
*
* @param name the name for this part
* @param partSource the source for this part
- * @param contentType the content type for this part
- * @param charset the charset encoding for this part
+ * @param contentType the content type for this part, if <code>null</code> the
+ * {@link #DEFAULT_CONTENT_TYPE default} is used
+ * @param charset the charset encoding for this part, if <code>null</code> the
+ * {@link #DEFAULT_CHARSET default} is used
*/
public FilePart(String name, PartSource partSource, String contentType, String charset) {
- LOG.trace("enter FilePart(String, PartSource, String, String)");
- if (name == null) {
- throw new IllegalArgumentException("Name may not be null");
- }
- this.name = name;
+
+ super(
+ name,
+ contentType == null ? DEFAULT_CONTENT_TYPE : contentType,
+ charset == null ? DEFAULT_CHARSET : charset,
+ DEFAULT_TRANSFER_ENCODING
+ );
+
if (partSource == null) {
throw new IllegalArgumentException("Source may not be null");
}
@@ -142,12 +137,6 @@
throw new IllegalArgumentException("Source length must be >= 0");
}
this.source = partSource;
- if (contentType != null) {
- this.contentType = contentType;
- } else {
- this.contentType = DEFAULT_CONTENT_TYPE;
- }
- this.charset = charset;
}
/**
@@ -179,8 +168,10 @@
*
* @param name the name of the file part
* @param file the file to post
- * @param contentType the content type for the file
- * @param charset the charset encoding of the file
+ * @param contentType the content type for this part, if <code>null</code> the
+ * {@link #DEFAULT_CONTENT_TYPE default} is used
+ * @param charset the charset encoding for this part, if <code>null</code> the
+ * {@link #DEFAULT_CHARSET default} is used
*
* @throws FileNotFoundException if the <i>file</i> is not a normal
* file or if it is not readable.
@@ -211,8 +202,10 @@
* @param name the name of the file part
* @param fileName the file name
* @param file the file to post
- * @param contentType the content type for the file
- * @param charset the charset encoding of the file
+ * @param contentType the content type for this part, if <code>null</code> the
+ * {@link #DEFAULT_CONTENT_TYPE default} is used
+ * @param charset the charset encoding for this part, if <code>null</code> the
+ * {@link #DEFAULT_CHARSET default} is used
*
* @throws FileNotFoundException if the <i>file</i> is not a normal
* file or if it is not readable.
@@ -220,40 +213,6 @@
public FilePart(String name, String fileName, File file, String contentType, String charset)
throws FileNotFoundException {
this(name, new FilePartSource(fileName, file), contentType, charset);
- }
-
- /**
- * Return the name.
- * @return The name.
- * @see org.apache.commons.httpclient.methods.multipart.Part#getName()
- */
- public String getName() {
- return this.name;
- }
-
- /**
- * Return the content type of this part.
- * @return String The name.
- */
- public String getContentType() {
- return this.contentType;
- }
-
- /**
- * Return the character encoding of this part.
- * @return String The name.
- */
- public String getCharSet() {
- return this.charset;
- }
-
- /**
- * Return the transfer encoding of this part.
- * @return String The name.
- */
-
- public String getTransferEncoding() {
- return DEFAULT_TRANSFER_ENCODING;
}
/**
1.2 +179 -0 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/PartBase.java
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org