Index: java/org/apache/commons/httpclient/methods/multipart/FilePart.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java,v retrieving revision 1.14 diff -u -r1.14 FilePart.java --- java/org/apache/commons/httpclient/methods/multipart/FilePart.java 16 Mar 2003 12:05:03 -0000 1.14 +++ java/org/apache/commons/httpclient/methods/multipart/FilePart.java 26 Sep 2003 02:36:10 -0000 @@ -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 null the + * {@link #DEFAULT_CONTENT_TYPE default} is used + * @param charset the charset encoding for this part, if null 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 null the + * {@link #DEFAULT_CONTENT_TYPE default} is used + * @param charset the charset encoding for this part, if null the + * {@link #DEFAULT_CHARSET default} is used * * @throws FileNotFoundException if the file 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 null the + * {@link #DEFAULT_CONTENT_TYPE default} is used + * @param charset the charset encoding for this part, if null the + * {@link #DEFAULT_CHARSET default} is used * * @throws FileNotFoundException if the file 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; } /** Index: java/org/apache/commons/httpclient/methods/multipart/Part.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/Part.java,v retrieving revision 1.10 diff -u -r1.10 Part.java --- java/org/apache/commons/httpclient/methods/multipart/Part.java 4 Apr 2003 02:37:03 -0000 1.10 +++ java/org/apache/commons/httpclient/methods/multipart/Part.java 26 Sep 2003 02:36:11 -0000 @@ -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 null 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 null 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 null 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 Index: java/org/apache/commons/httpclient/methods/multipart/StringPart.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/StringPart.java,v retrieving revision 1.7 diff -u -r1.7 StringPart.java --- java/org/apache/commons/httpclient/methods/multipart/StringPart.java 6 Apr 2003 22:31:54 -0000 1.7 +++ java/org/apache/commons/httpclient/methods/multipart/StringPart.java 26 Sep 2003 02:36:12 -0000 @@ -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 null + * 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; } + } Index: src/java/org/apache/commons/httpclient/methods/multipart/PartBase.java =================================================================== RCS file: src/java/org/apache/commons/httpclient/methods/multipart/PartBase.java diff -N src/java/org/apache/commons/httpclient/methods/multipart/PartBase.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/java/org/apache/commons/httpclient/methods/multipart/PartBase.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,179 @@ +/* + * $Header: $ + * $Revision: $ + * $Date: $ + * + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + * [Additional notices, if required by prior licensing conditions] + * + */ + +package org.apache.commons.httpclient.methods.multipart; + + +/** + * Provides setters and getters for the basic Part properties. + * + * @author Michael Becke + */ +public abstract class PartBase extends Part { + + /** Name of the file part. */ + private String name; + + /** Content type of the file part. */ + private String contentType; + + /** Content encoding of the file part. */ + private String charSet; + + /** The transfer encoding. */ + private String transferEncoding; + + /** + * Constructor. + * + * @param name The name of the part + * @param contentType The content type, or null + * @param charset The character encoding, or null + * @param transferEncoding The transfer encoding, or null + */ + public PartBase(String name, String contentType, String charSet, String transferEncoding) { + + if (name == null) { + throw new IllegalArgumentException("Name must not be null"); + } + this.name = name; + this.contentType = contentType; + this.charSet = charSet; + this.transferEncoding = transferEncoding; + } + + /** + * Returns the name. + * @return The name. + * @see org.apache.commons.httpclient.methods.multipart.Part#getName() + */ + public String getName() { + return this.name; + } + + /** + * Returns 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; + } + + /** + * Returns the transfer encoding of this part. + * @return String The name. + */ + public String getTransferEncoding() { + return transferEncoding; + } + + /** + * Sets the character encoding. + * + * @param charSet the character encoding, or null to exclude the character + * encoding header + */ + public void setCharSet(String charSet) { + this.charSet = charSet; + } + + /** + * Sets the content type. + * + * @param contentType the content type, or null to exclude the content type header + */ + public void setContentType(String contentType) { + this.contentType = contentType; + } + + /** + * Sets the part name. + * + * @param name + */ + public void setName(String name) { + if (name == null) { + throw new IllegalArgumentException("Name must not be null"); + } + this.name = name; + } + + /** + * Sets the transfer encoding. + * + * @param transferEncoding the transfer encoding, or null to exclude the + * transfer encoding header + */ + public void setTransferEncoding(String transferEncoding) { + this.transferEncoding = transferEncoding; + } + +}