You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by md...@apache.org on 2004/01/12 22:42:36 UTC
cvs commit: jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/spi MultipartFormDataProvider.java MultipartServiceProvider.java
mdiggory 2004/01/12 13:42:36
Added: codec-multipart/src/java/org/apache/commons/codec/multipart
StringPart.java Part.java FileSource.java
Source.java MultipartCodec.java FilePart.java
ByteArraySource.java MultipartCodecFactory.java
codec-multipart/src/test/org/apache/commons/codec/multipart
MultipartTest.java
codec-multipart build.xml project.xml
codec-multipart/src/java/org/apache/commons/codec/multipart/util
AbstractPart.java Constants.java
codec-multipart/src/java/org/apache/commons/codec/multipart/spi
MultipartFormDataProvider.java
MultipartServiceProvider.java
Log:
Initial Commit of codec-multipart codebase.
Revision Changes Path
1.1 jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/StringPart.java
Index: StringPart.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/StringPart.java,v 1.1 2004/01/12 21:42:35 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:35 $
*
* ====================================================================
*
* 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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart;
import java.io.OutputStream;
import java.io.IOException;
import org.apache.commons.codec.multipart.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Simple string parameter for multipart encoding.
*
* @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a>
* @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
* @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
*
*/
public class StringPart extends AbstractPart {
/** Log object for this class. */
private static final Log LOG = LogFactory.getLog(StringPart.class);
/** Default content encoding of string parameters. */
public static final String DEFAULT_CONTENT_TYPE = "text/plain";
/** Default charset of string parameters*/
public static final String DEFAULT_CHARSET = "US-ASCII";
/** Default transfer encoding of string parameters*/
public static final String DEFAULT_TRANSFER_ENCODING = "8bit";
/** Contents of this StringPart. */
private byte[] content;
/** 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, if <code>null</code>
* the {@link #DEFAULT_CHARSET default} is used
*/
public StringPart(String name, String value, String 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");
}
if (value.indexOf(0) != -1) {
// See RFC 2048, 2.8. "8bit Data"
throw new IllegalArgumentException("NULs may not be present in string parts");
}
this.value = value;
}
/**
* Constructor.
*
* @param name The name of the part
* @param value the string to post
*/
public StringPart(String name, String value) {
this(name, value, null);
}
/**
* 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 = Constants.getContentBytes(value, getCharSet());
}
return content;
}
/**
* Writes the data to the given OutputStream.
* @param out the OutputStream to write to
* @throws IOException if there is a write error
*/
public void writeTo(OutputStream out) throws IOException {
LOG.trace("enter sendData(OutputStream)");
out.write(getContent());
}
/**
* Return the length of the data.
* @return The length of the data.
* @throws IOException If an IO problem occurs
* @see org.apache.commons.codec.multipart.OldPart#length()
*/
public long length() throws IOException {
LOG.trace("enter length()");
return getContent().length;
}
/* (non-Javadoc)
* @see org.apache.commons.codec.multipart.BasePart#setCharSet(java.lang.String)
*/
public void setCharSet(String charSet) {
super.setCharSet(charSet);
this.content = null;
}
}
1.1 jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/Part.java
Index: Part.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/Part.java,v 1.1 2004/01/12 21:42:35 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:35 $
*
* ====================================================================
*
* 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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart;
import java.io.IOException;
import java.io.OutputStream;
/**
* Parts need only be defined in an Interface.
* This interface defines the basic methods neccessary
* to access the name, type and content of a "Part" in
* a multipart request.
*
* @author Mark Diggory
*/
public interface Part {
/**
* Return the name of this part.
* @return The name.
*/
public abstract String getName();
/**
* Returns the content type of this part.
* @return String The name.
*/
public abstract String getContentType();
/**
* Return the character encoding of this part.
* @return String The name.
*/
public abstract String getCharSet();
/**
* Return the transfer encoding of this part.
* @return the transfer encoding, or <code>null</code> to exclude the transfer encoding header
*/
public abstract String getTransferEncoding();
/**
* Write the data to the specified output stream
* @param out The output stream
* @throws IOException If an IO problem occurs.
*/
public abstract void writeTo(OutputStream out) throws IOException;
/**
* Return the length of the main content
*
* @return long The length.
* @throws IOException If an IO problem occurs
*/
public abstract long length() throws IOException;
}
1.1 jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/FileSource.java
Index: FileSource.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/FileSource.java,v 1.1 2004/01/12 21:42:35 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:35 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* A Source that reads from a File.
*
* @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
* @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
*/
public class FileSource implements Source {
/** File part file. */
private File file = null;
/** File part file name. */
private String fileName = null;
/**
* Constructor for FileSource.
*
* @param file the FilePart source File.
*
* @throws FileNotFoundException if the file does not exist or
* cannot be read
*/
public FileSource(File file) throws FileNotFoundException {
this.file = file;
if (file != null) {
if (!file.isFile()) {
throw new FileNotFoundException("File is not a normal file.");
}
if (!file.canRead()) {
throw new FileNotFoundException("File is not readable.");
}
this.fileName = file.getName();
}
}
/**
* Constructor for FileSource.
*
* @param fileName the file name of the FilePart
* @param file the source File for the FilePart
*
* @throws FileNotFoundException if the file does not exist or
* cannot be read
*/
public FileSource(String fileName, File file)
throws FileNotFoundException {
this(file);
if (fileName != null) {
this.fileName = fileName;
}
}
/**
* Return the length of the file
* @return the length of the file.
* @see Source#getLength()
*/
public long getLength() {
if (this.file != null) {
return this.file.length();
} else {
return 0;
}
}
/**
* Return the current filename
* @return the filename.
* @see Source#getFileName()
*/
public String getFileName() {
return (fileName == null) ? "noname" : fileName;
}
/**
* Return a new {@link FileInputStream} for the current filename.
* @return the new input stream.
* @throws IOException If an IO problem occurs.
* @see Source#createInputStream()
*/
public InputStream createInputStream() throws IOException {
if (this.file != null) {
return new FileInputStream(this.file);
} else {
return new ByteArrayInputStream(new byte[] {});
}
}
}
1.1 jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/Source.java
Index: Source.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/Source.java,v 1.1 2004/01/12 21:42:35 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:35 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart;
import java.io.IOException;
import java.io.InputStream;
/**
* An interface for providing access to data when encoding MultiPart content.
*
* @see FilePart
*
* @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
* @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
*
*/
public interface Source {
/**
* Gets the number of bytes contained in this source.
*
* @return a value >= 0
*/
public abstract long getLength();
/**
* Gets the name of the file this source represents.
*
* @return the fileName used for posting a MultiPart file part
*/
public abstract String getFileName();
/**
* Gets a new InputStream for reading this source. This method can be
* called more than once and should therefore return a new stream every
* time.
*
* @return a new InputStream
*
* @throws IOException if an error occurs when creating the InputStream
*/
public abstract InputStream createInputStream() throws IOException;
}
1.1 jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/MultipartCodec.java
Index: MultipartCodec.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/MultipartCodec.java,v 1.1 2004/01/12 21:42:35 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:35 $
*
* ====================================================================
*
* 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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart;
import java.io.IOException;
import java.io.OutputStream;
/**
* MultipartCodec provides the basic interface to deterimine the
* size of multipart encoded content and to also write that
* content to an OutputStream.
*
* @author Mark Diggory
*/
public interface MultipartCodec {
/**
* Write all parts and the last boundary to the specified output stream
* @param out The output stream
* @param parts The array of parts to be sent
* @throws IOException If an IO problem occurs.
*/
public abstract void writeTo(OutputStream out, final Part[] parts)
throws IOException;
/**
* Return the total sum of all parts and that of the last boundary
* @param parts The array of parts
* @return the total length
* @throws IOException If an IO problem occurs.
*/
public abstract long lengthEncoded(final Part[] parts)
throws IOException;
}
1.1 jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/FilePart.java
Index: FilePart.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/FilePart.java,v 1.1 2004/01/12 21:42:35 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:35 $
*
* ====================================================================
*
* 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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.codec.multipart.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This class implements a part of a Multipart post object that
* consists of a file.
*
* @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a>
* @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
* @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
* @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
* @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
*
*/
public class FilePart extends AbstractPart {
/** Default content encoding of file attachments. */
public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
/** Default charset of file attachments. */
public static final String DEFAULT_CHARSET = Constants.DEFAULT_CONTENT_CHARSET;
/** Default transfer encoding of file attachments. */
public static final String DEFAULT_TRANSFER_ENCODING = "binary";
/** 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=";
/** Attachment's file name as a byte array */
protected static final byte[] FILE_NAME_BYTES =
Constants.getAsciiBytes(FILE_NAME);
/** Source of the file part. */
private Source source;
/**
* FilePart Constructor.
*
* @param name the name for this part
* @param partSource the source 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, Source partSource, String contentType, String charset) {
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");
}
if (partSource.getLength() < 0) {
throw new IllegalArgumentException("Source length must be >= 0");
}
this.source = partSource;
}
/**
* FilePart Constructor.
*
* @param name the name for this part
* @param partSource the source for this part
*/
public FilePart(String name, Source partSource) {
this(name, partSource, null, null);
}
/**
* FilePart Constructor.
*
* @param name the name of the file part
* @param file the file to post
*
* @throws FileNotFoundException if the <i>file</i> is not a normal
* file or if it is not readable.
*/
public FilePart(String name, File file)
throws FileNotFoundException {
this(name, new FileSource(file), null, null);
}
/**
* FilePart Constructor.
*
* @param name the name of the file part
* @param file the file to post
* @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.
*/
public FilePart(String name, File file, String contentType, String charset)
throws FileNotFoundException {
this(name, new FileSource(file), contentType, charset);
}
/**
* FilePart Constructor.
*
* @param name the name of the file part
* @param fileName the file name
* @param file the file to post
*
* @throws FileNotFoundException if the <i>file</i> is not a normal
* file or if it is not readable.
*/
public FilePart(String name, String fileName, File file)
throws FileNotFoundException {
this(name, new FileSource(fileName, file), null, null);
}
/**
* FilePart Constructor.
*
* @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 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.
*/
public FilePart(String name, String fileName, File file, String contentType, String charset)
throws FileNotFoundException {
this(name, new FileSource(fileName, file), contentType, charset);
}
/**
* Write the data in "source" to the specified stream.
* @param out The output stream.
* @throws IOException if an IO problem occurs.
* @see org.apache.commons.codec.multipart.OldPart#writeTo(OutputStream)
*/
public void writeTo(OutputStream out) throws IOException {
LOG.trace("enter sendData(OutputStream out)");
if (length() == 0) {
// this file contains no data, so there is nothing to send.
// we don't want to create a zero length buffer as this will
// cause an infinite loop when reading.
LOG.debug("No data to send.");
return;
}
byte[] tmp = new byte[4096];
InputStream instream = source.createInputStream();
try {
int len;
while ((len = instream.read(tmp)) >= 0) {
out.write(tmp, 0, len);
}
} finally {
// we're done with the stream, close it
instream.close();
}
}
/**
* Return the length of the data.
* @return The length.
* @throws IOException if an IO problem occurs
* @see org.apache.commons.codec.multipart.OldPart#lengthOfData()
*/
public long length() throws IOException {
LOG.trace("enter length()");
return source.getLength();
}
}
1.1 jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/ByteArraySource.java
Index: ByteArraySource.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/ByteArraySource.java,v 1.1 2004/01/12 21:42:35 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:35 $
*
* ====================================================================
*
* 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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* A Source that reads from a byte array. This class should be used when
* the data to post is already loaded into memory.
*
* @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
* @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
*/
public class ByteArraySource implements Source {
/** Name of the source file. */
private String fileName;
/** Byte array of the source file. */
private byte[] bytes;
/**
* Constructor for ByteArraySource.
*
* @param fileName the name of the file these bytes represent
* @param bytes the content of this part
*/
public ByteArraySource(String fileName, byte[] bytes) {
this.fileName = fileName;
this.bytes = bytes;
}
/**
* @see Source#getLength()
*/
public long getLength() {
return bytes.length;
}
/**
* @see Source#getFileName()
*/
public String getFileName() {
return fileName;
}
/**
* @see Source#createInputStream()
*/
public InputStream createInputStream() throws IOException {
return new ByteArrayInputStream(bytes);
}
}
1.1 jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/MultipartCodecFactory.java
Index: MultipartCodecFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/MultipartCodecFactory.java,v 1.1 2004/01/12 21:42:35 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:35 $
*
* ====================================================================
*
* 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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart;
import org.apache.commons.codec.multipart.spi.MultipartFormDataProvider;
import org.apache.commons.codec.multipart.spi.MultipartServiceProvider;
/**
* MultipartCodecFactory is the starting point to begin working with a
* MultipartCodec. It provides basic methods to instantiate any MultipartCodec
* for which a service provider is maintained.
*
* @author Mark Diggory
*/
public class MultipartCodecFactory {
/* non-constructor*/
private MultipartCodecFactory(){
}
/* TODO: write discovery mechanisms? */
public static MultipartCodec newInstance(){
return new MultipartFormDataProvider();
}
/* TODO: write discovery mechanisms? */
public static MultipartCodec newInstance(Class clazz) throws InstantiationException, IllegalAccessException{
if(clazz.isAssignableFrom(MultipartServiceProvider.class)){
return (MultipartCodec)clazz.newInstance();
}else{
throw new InstantiationException("Not a valid MultipartServiceProvider");
}
}
}
1.1 jakarta-commons-sandbox/codec-multipart/src/test/org/apache/commons/codec/multipart/MultipartTest.java
Index: MultipartTest.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/test/org/apache/commons/codec/multipart/MultipartTest.java,v 1.1 2004/01/12 21:42:35 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:35 $
*
* ====================================================================
*
* 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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.apache.commons.codec.BinaryDecoder;
import org.apache.commons.codec.BinaryEncoder;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
*/
public class MultipartTest extends TestCase{
/** Log object for this class. */
private static final Log LOG = LogFactory.getLog(MultipartTest.class);
/**
* No-arg constructor.
*/
public MultipartTest() {
super();
}
public void testBasicEncodeDecode() throws Exception {
/*
Part[] parts = new Part[]{
new StringPart("test1name", "test1value"),
new StringPart("test2name", "test2value"),
new FilePart("test3name", new File("test")),
new FilePart("test4name","test4filename",new File("test"))
};
ByteArrayOutputStream out = new ByteArrayOutputStream();
MultipartCodecFactory codec = new MultipartCodecFactory();
String plain = "Hello there!";
codec.encodeTo(parts,out);
System.out.print(new String(out.toByteArray()));
assertEquals("Basic URL encoding test",
"Hello+there%21", encoded);
*/
}
}
1.1 jakarta-commons-sandbox/codec-multipart/build.xml
Index: build.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<!--
build.xml generated by maven from project.xml version 0.1
on date January 12 2004, time 1600
-->
<project default="jar" name="commons-codec-multipart" basedir=".">
<property name="defaulttargetdir" value="target">
</property>
<property name="libdir" value="target/lib">
</property>
<property name="classesdir" value="target/classes">
</property>
<property name="testclassesdir" value="target/test-classes">
</property>
<property name="testreportdir" value="target/test-reports">
</property>
<property name="distdir" value="dist">
</property>
<property name="javadocdir" value="dist/docs/api">
</property>
<property name="final.name" value="commons-codec-multipart-0.1">
</property>
<target name="init" description="o Initializes some properties">
<mkdir dir="${libdir}">
</mkdir>
<condition property="noget">
<equals arg2="only" arg1="${build.sysclasspath}">
</equals>
</condition>
</target>
<target name="compile" description="o Compile the code" depends="get-deps">
<mkdir dir="${classesdir}">
</mkdir>
<javac destdir="${classesdir}" deprecation="true" debug="true" optimize="false" excludes="**/package.html">
<src>
<pathelement location="C:\eclipse\workspace\codec-multipart\src\java">
</pathelement>
</src>
<classpath>
<fileset dir="${libdir}">
<include name="*.jar">
</include>
</fileset>
</classpath>
</javac>
<copy todir="${testclassesdir}">
<fileset dir="C:\eclipse\workspace\codec-multipart\src\test">
<include name="**/*.xml">
</include>
</fileset>
</copy>
</target>
<target name="jar" description="o Create the jar" depends="compile,test">
<jar jarfile="target/${final.name}.jar" excludes="**/package.html" basedir="${classesdir}">
</jar>
</target>
<target name="clean" description="o Clean up the generated directories">
<delete dir="${defaulttargetdir}">
</delete>
<delete dir="${distdir}">
</delete>
</target>
<target name="dist" description="o Create a distribution" depends="jar, javadoc">
<mkdir dir="dist">
</mkdir>
<copy todir="dist">
<fileset dir="${defaulttargetdir}" includes="*.jar">
</fileset>
<fileset dir="${basedir}" includes="LICENSE*, README*">
</fileset>
</copy>
</target>
<target name="test" description="o Run the test cases" if="test.failure" depends="internal-test">
<fail message="There were test failures.">
</fail>
</target>
<target name="internal-test" depends="compile-tests">
<mkdir dir="${testreportdir}">
</mkdir>
<junit dir="./" failureproperty="test.failure" printSummary="yes" fork="true" haltonerror="true">
<sysproperty key="basedir" value=".">
</sysproperty>
<formatter type="xml">
</formatter>
<formatter usefile="false" type="plain">
</formatter>
<classpath>
<fileset dir="${libdir}">
<include name="*.jar">
</include>
</fileset>
<pathelement path="${testclassesdir}">
</pathelement>
<pathelement path="${classesdir}">
</pathelement>
</classpath>
<batchtest todir="${testreportdir}">
<fileset dir="C:\eclipse\workspace\codec-multipart\src\test">
<include name="**/*Test.java">
</include>
<exclude name="**/*AbstractTest.java">
</exclude>
</fileset>
</batchtest>
</junit>
</target>
<target name="compile-tests" depends="compile">
<mkdir dir="${testclassesdir}">
</mkdir>
<javac destdir="${testclassesdir}" deprecation="true" debug="true" optimize="false" excludes="**/package.html">
<src>
<pathelement location="C:\eclipse\workspace\codec-multipart\src\test">
</pathelement>
</src>
<classpath>
<fileset dir="${libdir}">
<include name="*.jar">
</include>
</fileset>
<pathelement path="${classesdir}">
</pathelement>
</classpath>
</javac>
</target>
<target name="javadoc" description="o Generate javadoc" depends="jar">
<mkdir dir="${javadocdir}">
</mkdir>
<tstamp>
<format pattern="2004-yyyy" property="year">
</format>
</tstamp>
<property name="copyright" value="Copyright &copy; Apache Software Foundation. All Rights Reserved.">
</property>
<property name="title" value="Codec-Multipart 0.1 API">
</property>
<javadoc use="true" private="true" destdir="${javadocdir}" author="true" version="true" sourcepath="C:\eclipse\workspace\codec-multipart\src\java" packagenames="org.apache.commons.*.*">
<classpath>
<fileset dir="${libdir}">
<include name="*.jar">
</include>
</fileset>
<pathelement location="target/${final.name}.jar">
</pathelement>
</classpath>
</javadoc>
</target>
<target name="get-deps" unless="noget" depends="init">
<get dest="${libdir}/junit-3.8.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/junit/jars/junit-3.8.1.jar">
</get>
<get dest="${libdir}/commons-logging-1.0.3.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-logging/jars/commons-logging-1.0.3.jar">
</get>
<get dest="${libdir}/commons-codec-1.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-codec/jars/commons-codec-1.1.jar">
</get>
<get dest="${libdir}/junit-3.8.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/junit/jars/junit-3.8.1.jar">
</get>
<get dest="${libdir}/ant-1.5.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/ant/jars/ant-1.5.jar">
</get>
<get dest="${libdir}/ant-optional-1.5.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/ant/jars/ant-optional-1.5.jar">
</get>
</target>
<target name="install-maven">
<get dest="${user.home}/maven-install-latest.jar" usetimestamp="true" src="${repo}/maven/maven-install-latest.jar">
</get>
<unjar dest="${maven.home}" src="${user.home}/maven-install-latest.jar">
</unjar>
</target>
</project>
1.1 jakarta-commons-sandbox/codec-multipart/project.xml
Index: project.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<project>
<extend>../project.xml</extend>
<name>Codec-Multipart</name>
<id>commons-codec-multipart</id>
<currentVersion>0.1</currentVersion>
<organization>
<name>Apache Software Foundation</name>
<url>http://www.apache.org</url>
<logo>http://jakarta.apache.org/images/jakarta-logo.gif</logo>
</organization>
<inceptionYear>2004</inceptionYear>
<shortDescription>Simple encoders and decoders</shortDescription>
<description>
Common Multipart Mime Codecs for future addition to Commons Codec.
</description>
<versions/>
<branches/>
<developers>
<developer>
<name>Mark Diggory</name>
<id>mdiggory</id>
<email>mdiggory at apache.org</email>
</developer>
<developer>
<name>Your Name Here</name>
<id>yName</id>
<email>yName at apache.org</email>
</developer>
</developers>
<dependencies>
<dependency>
<id>junit</id>
<version>3.8.1</version>
<url>http://www.junit.org/</url>
</dependency>
<dependency>
<id>commons-logging</id>
<version>1.0.3</version>
<url>http://jakarta.apache.org/commons/logging.html</url>
</dependency>
<dependency>
<id>commons-codec</id>
<version>1.1</version>
<url>http://jakarta.apache.org/commons/codec/</url>
</dependency>
</dependencies>
<issueTrackingUrl>http://nagoya.apache.org/bugzilla/buglist.cgi?bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&email1=&emailtype1=substring&emailassigned_to1=1&email2=&emailtype2=substring&emailreporter2=1&bugidtype=include&bug_id=&changedin=&votes=&chfieldfrom=&chfieldto=Now&chfieldvalue=&product=Commons&component=Codec&short_desc=&short_desc_type=allwordssubstr&long_desc=&long_desc_type=allwordssubstr&bug_file_loc=&bug_file_loc_type=allwordssubstr&keywords=&keywords_type=anywords&field0-0-0=noop&type0-0-0=noop&value0-0-0=&cmdtype=doit&newqueryname=&order=Reuse+same+sort+as+last+time</issueTrackingUrl>
<build>
<unitTest>
<includes>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/*AbstractTest.java</exclude>
</excludes>
<resources>
<resource>
<directory>${pom.build.unitTestSourceDirectory}</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</unitTest>
</build>
<reports>
<report>maven-changelog-plugin</report>
<!-- <report>maven-changes-plugin</report> -->
<report>maven-checkstyle-plugin</report>
<report>maven-clover-plugin</report>
<report>maven-developer-activity-plugin</report>
<report>maven-file-activity-plugin</report>
<report>maven-javadoc-plugin</report>
<!-- <report>maven-jdepend-plugin</report> -->
<!-- <report>maven-jellydoc-plugin</report> -->
<report>maven-junit-report-plugin</report>
<report>maven-jxr-plugin</report>
<report>maven-license-plugin</report>
<!-- <report>maven-linkcheck-plugin</report> -->
<report>maven-statcvs-plugin</report>
<!-- <report>maven-tasklist-plugin</report> -->
</reports>
</project>
1.1 jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/util/AbstractPart.java
Index: AbstractPart.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/util/AbstractPart.java,v 1.1 2004/01/12 21:42:35 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:35 $
*
* ====================================================================
*
* 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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart.util;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.codec.multipart.Part;
/**
* Provides setters and getters for the basic Part properties.
*
* @author Michael Becke
* @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
*/
public abstract class AbstractPart implements 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 <code>null</code>
* @param charSet The character encoding, or <code>null</code>
* @param transferEncoding The transfer encoding, or <code>null</code>
*/
public AbstractPart(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.codec.multipart.multipart.OldPart#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 <code>null</code> to exclude the character
* encoding header
*/
public void setCharSet(String charSet) {
this.charSet = charSet;
}
/**
* Sets the content type.
*
* @param contentType the content type, or <code>null</code> 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 <code>null</code> to exclude the
* transfer encoding header
*/
public void setTransferEncoding(String transferEncoding) {
this.transferEncoding = transferEncoding;
}
/**
* Return a string representation of this object.
* @return A string representation of this object.
* @see java.lang.Object#toString()
*/
public String toString() {
return this.getName();
}
/**
* Write the data to the specified output stream
* @param out The output stream
* @throws IOException If an IO problem occurs.
*/
public abstract void writeTo(OutputStream out) throws IOException;
/**
* Return the length of the main content
*
* @return long The length.
* @throws IOException If an IO problem occurs
*/
public abstract long length() throws IOException;
}
1.1 jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/util/Constants.java
Index: Constants.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/util/Constants.java,v 1.1 2004/01/12 21:42:35 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:35 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart.util;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* HTTP content conversion routines.
*
* @author Oleg Kalnichevski
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
* @author <a href="mailto:mdiggory@latte.harvard.edu">Mark Diggory</a>
*/
public class Constants {
/** Character set used to encode HTTP protocol elements */
public static final String HTTP_ELEMENT_CHARSET = "US-ASCII";
/** Default content encoding chatset */
public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
/** Log object for this class. */
private static final Log LOG = LogFactory.getLog(Constants.class);
/**
* Converts the specified string to a byte array of HTTP element characters.
* This method is to be used when encoding content of HTTP elements (such as
* request headers)
*
* @param data the string to be encoded
* @return The resulting byte array.
*/
public static byte[] getBytes(final String data) {
if (data == null) {
throw new IllegalArgumentException("Parameter may not be null");
}
try {
return data.getBytes(HTTP_ELEMENT_CHARSET);
} catch (UnsupportedEncodingException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Unsupported encoding: "
+ HTTP_ELEMENT_CHARSET
+ ". System default encoding used");
}
return data.getBytes();
}
}
/**
* Converts the byte array of HTTP element characters to a string This
* method is to be used when decoding content of HTTP elements (such as
* response headers)
*
* @param data the byte array to be encoded
* @param offset the index of the first byte to encode
* @param length the number of bytes to encode
* @return The resulting string.
*/
public static String getString(final byte[] data, int offset, int length) {
if (data == null) {
throw new IllegalArgumentException("Parameter may not be null");
}
try {
return new String(data, offset, length, HTTP_ELEMENT_CHARSET);
} catch (UnsupportedEncodingException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Unsupported encoding: "
+ HTTP_ELEMENT_CHARSET
+ ". System default encoding used");
}
return new String(data, offset, length);
}
}
/**
* Converts the byte array of HTTP element characters to a string This
* method is to be used when decoding content of HTTP elements (such as
* response headers)
*
* @param data the byte array to be encoded
* @return The resulting string.
*/
public static String getString(final byte[] data) {
return getString(data, 0, data.length);
}
/**
* Converts the specified string to a byte array of HTTP content charachetrs
* This method is to be used when encoding content of HTTP request/response
* If the specified charset is not supported, default HTTP content encoding
* (ISO-8859-1) is applied
*
* @param data the string to be encoded
* @param charset the desired character encoding
* @return The resulting byte array.
*/
public static byte[] getContentBytes(final String data, String charset) {
if (data == null) {
throw new IllegalArgumentException("Parameter may not be null");
}
if ((charset == null) || (charset.equals(""))) {
charset = DEFAULT_CONTENT_CHARSET;
}
try {
return data.getBytes(charset);
} catch (UnsupportedEncodingException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Unsupported encoding: "
+ charset
+ ". HTTP default encoding used");
}
try {
return data.getBytes(DEFAULT_CONTENT_CHARSET);
} catch (UnsupportedEncodingException e2) {
if (LOG.isWarnEnabled()) {
LOG.warn("Unsupported encoding: "
+ DEFAULT_CONTENT_CHARSET
+ ". System encoding used");
}
return data.getBytes();
}
}
}
/**
* Converts the byte array of HTTP content characters to a string This
* method is to be used when decoding content of HTTP request/response If
* the specified charset is not supported, default HTTP content encoding
* (ISO-8859-1) is applied
*
* @param data the byte array to be encoded
* @param offset the index of the first byte to encode
* @param length the number of bytes to encode
* @param charset the desired character encoding
* @return The result of the conversion.
*/
public static String getContentString(
final byte[] data,
int offset,
int length,
String charset
) {
if (data == null) {
throw new IllegalArgumentException("Parameter may not be null");
}
if ((charset == null) || (charset.equals(""))) {
charset = DEFAULT_CONTENT_CHARSET;
}
try {
return new String(data, offset, length, charset);
} catch (UnsupportedEncodingException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Unsupported encoding: "
+ DEFAULT_CONTENT_CHARSET
+ ". Default HTTP encoding used");
}
try {
return new String(data, offset, length, DEFAULT_CONTENT_CHARSET);
} catch (UnsupportedEncodingException e2) {
if (LOG.isWarnEnabled()) {
LOG.warn("Unsupported encoding: "
+ DEFAULT_CONTENT_CHARSET
+ ". System encoding used");
}
return new String(data, offset, length);
}
}
}
/**
* Converts the byte array of HTTP content characters to a string This
* method is to be used when decoding content of HTTP request/response If
* the specified charset is not supported, default HTTP content encoding
* (ISO-8859-1) is applied
*
* @param data the byte array to be encoded
* @param charset the desired character encoding
* @return The result of the conversion.
*/
public static String getContentString(final byte[] data, String charset) {
return getContentString(data, 0, data.length, charset);
}
/**
* Converts the specified string to a byte array of HTTP content characters
* using default HTTP content encoding (ISO-8859-1) This method is to be
* used when encoding content of HTTP request/response
*
* @param data the string to be encoded
* @return The byte array as above.
*/
public static byte[] getContentBytes(final String data) {
return getContentBytes(data, null);
}
/**
* Converts the byte array of HTTP content characters to a string using
* default HTTP content encoding (ISO-8859-1) This method is to be used when
* decoding content of HTTP request/response
*
* @param data the byte array to be encoded
* @param offset the index of the first byte to encode
* @param length the number of bytes to encode
* @return The string representation of the byte array.
*/
public static String getContentString(final byte[] data, int offset, int length) {
return getContentString(data, offset, length, null);
}
/**
* Converts the byte array of HTTP content characters to a string using
* default HTTP content encoding (ISO-8859-1) This method is to be used when
* decoding content of HTTP request/response
*
* @param data the byte array to be encoded
* @return The string representation of the byte array.
*/
public static String getContentString(final byte[] data) {
return getContentString(data, null);
}
/**
* Converts the specified string to byte array of ASCII characters.
*
* @param data the string to be encoded
* @return The string as a byte array.
*/
public static byte[] getAsciiBytes(final String data) {
if (data == null) {
throw new IllegalArgumentException("Parameter may not be null");
}
try {
return data.getBytes("US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("HttpClient requires ASCII support");
}
}
/**
* Converts the byte array of ASCII characters to a string. This method is
* to be used when decoding content of HTTP elements (such as response
* headers)
*
* @param data the byte array to be encoded
* @param offset the index of the first byte to encode
* @param length the number of bytes to encode
* @return The string representation of the byte array
*/
public static String getAsciiString(final byte[] data, int offset, int length) {
if (data == null) {
throw new IllegalArgumentException("Parameter may not be null");
}
try {
return new String(data, offset, length, "US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("HttpClient requires ASCII support");
}
}
/**
* Converts the byte array of ASCII characters to a string. This method is
* to be used when decoding content of HTTP elements (such as response
* headers)
*
* @param data the byte array to be encoded
* @return The string representation of the byte array
*/
public static String getAsciiString(final byte[] data) {
return getAsciiString(data, 0, data.length);
}
}
1.1 jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/spi/MultipartFormDataProvider.java
Index: MultipartFormDataProvider.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/spi/MultipartFormDataProvider.java,v 1.1 2004/01/12 21:42:36 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:36 $
*
* ====================================================================
*
* 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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart.spi;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.codec.multipart.*;
import org.apache.commons.codec.multipart.Part;
import org.apache.commons.codec.multipart.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Implements a multipart/form-data encoded Service Provider.
* This code was originally extracted from the Commons
* HttpClient project.
*
* @author Mark Diggory
*/
public class MultipartFormDataProvider extends MultipartServiceProvider {
/** The Content-Type for multipart/form-data. */
public static final String MIME_TYPE = "multipart/form-data";
/** Log object for this class. */
private static final Log log = LogFactory.getLog(MultipartFormDataProvider.class);
/** The boundary */
protected static final String BOUNDARY =
"----------------314159265358979323846";
/** The boundary as a byte array */
protected static final byte[] BOUNDARY_BYTES =
Constants.getAsciiBytes(BOUNDARY);
/** Carriage return/linefeed */
protected static final String CRLF = "\r\n";
/** Carriage return/linefeed as a byte array */
protected static final byte[] CRLF_BYTES = Constants.getAsciiBytes(CRLF);
/** Content dispostion characters */
protected static final String QUOTE = "\"";
/** Content dispostion as a byte array */
protected static final byte[] QUOTE_BYTES = Constants.getAsciiBytes(QUOTE);
/** Extra characters */
protected static final String EXTRA = "--";
/** Extra characters as a byte array */
protected static final byte[] EXTRA_BYTES = Constants.getAsciiBytes(EXTRA);
/** Content dispostion characters */
protected static final String CONTENT_DISPOSITION =
"Content-Disposition: form-data; name=";
/** Content dispostion as a byte array */
protected static final byte[] CONTENT_DISPOSITION_BYTES =
Constants.getAsciiBytes(CONTENT_DISPOSITION);
/** Content type header */
protected static final String CONTENT_TYPE = "Content-Type: ";
/** Content type header as a byte array */
protected static final byte[] CONTENT_TYPE_BYTES =
Constants.getAsciiBytes(CONTENT_TYPE);
/** Content charset */
protected static final String CHARSET = "; charset=";
/** Content charset as a byte array */
protected static final byte[] CHARSET_BYTES =
Constants.getAsciiBytes(CHARSET);
/** Content type header */
protected static final String CONTENT_TRANSFER_ENCODING =
"Content-Transfer-Encoding: ";
/** Content type header as a byte array */
protected static final byte[] CONTENT_TRANSFER_ENCODING_BYTES =
Constants.getAsciiBytes(CONTENT_TRANSFER_ENCODING);
/**
* Return the total sum of all parts and that of the last boundary
* @param parts The array of parts
* @return the total length
* @throws IOException If an IO problem occurs.
*/
public long lengthEncoded(final Part[] parts)
throws IOException {
log.trace("getLengthOfParts(Parts[])");
if (parts == null) {
throw new IllegalArgumentException("Parts may not be null");
}
long total = 0;
for (int i = 0; i < parts.length; i++) {
total += parts[i].length();
ByteArrayOutputStream overhead = new ByteArrayOutputStream();
writeStart(overhead);
writeDispositionHeader(overhead, parts[i]);
writeContentTypeHeader(overhead, parts[i]);
writeTransferEncodingHeader(overhead, parts[i]);
writeEndOfHeader(overhead);
writeEnd(overhead);
total += overhead.size();
}
total += EXTRA_BYTES.length;
total += BOUNDARY_BYTES.length;
total += EXTRA_BYTES.length;
total += CRLF_BYTES.length;
return total;
}
/**
* Write the content disposition header to the specified output stream
*
* @param out
* The output stream
* @throws IOException
* If an IO problem occurs.
*/
protected static void writeDispositionHeader(OutputStream out, Part part)
throws IOException {
log.trace("enter sendDispositionHeader(OutputStream out)");
out.write(CONTENT_DISPOSITION_BYTES);
out.write(QUOTE_BYTES);
out.write(Constants.getAsciiBytes(part.getName()));
out.write(QUOTE_BYTES);
}
/**
* Write the content type header to the specified output stream
*
* @param out
* The output stream
* @throws IOException
* If an IO problem occurs.
*/
protected static void writeContentTypeHeader(OutputStream out, Part part)
throws IOException {
log.trace("enter sendContentTypeHeader(OutputStream out)");
String contentType = part.getContentType();
if (contentType != null) {
out.write(CRLF_BYTES);
out.write(CONTENT_TYPE_BYTES);
out.write(Constants.getAsciiBytes(contentType));
String charSet = part.getCharSet();
if (charSet != null) {
out.write(CHARSET_BYTES);
out.write(Constants.getAsciiBytes(charSet));
}
}
}
/**
* Write the content transfer encoding header to the specified output
* stream
*
* @param out
* The output stream
* @throws IOException
* If an IO problem occurs.
*/
protected static void writeTransferEncodingHeader(
OutputStream out,
Part part)
throws IOException {
log.trace("enter sendTransferEncodingHeader(OutputStream out)");
String transferEncoding = part.getTransferEncoding();
if (transferEncoding != null) {
out.write(CRLF_BYTES);
out.write(CONTENT_TRANSFER_ENCODING_BYTES);
out.write(Constants.getAsciiBytes(transferEncoding));
}
}
/**
* Write the end of the header to the output stream
*
* @param out
* The output stream
* @throws IOException
* If an IO problem occurs.
*/
protected static void writeEndOfHeader(OutputStream out)
throws IOException {
log.trace("enter sendEndOfHeader(OutputStream out)");
out.write(CRLF_BYTES);
out.write(CRLF_BYTES);
}
/**
* Write all parts and the last boundary to the specified output stream
*
* @param out
* The output stream
* @param parts
* The array of parts to be sent
*
* @throws IOException
* If an IO problem occurs.
*/
public void writeTo(OutputStream out, final Part[] parts)
throws IOException {
log.trace("enter sendParts(OutputStream out, Parts[])");
if (parts == null) {
throw new IllegalArgumentException("Parts may not be null");
}
for (int i = 0; i < parts.length; i++) {
writeStart(out);
writeDispositionHeader(out, parts[i]);
writeContentTypeHeader(out, parts[i]);
writeTransferEncodingHeader(out, parts[i]);
writeEndOfHeader(out);
parts[i].writeTo(out);
writeEnd(out);
}
out.write(EXTRA_BYTES);
out.write(BOUNDARY_BYTES);
out.write(EXTRA_BYTES);
out.write(CRLF_BYTES);
}
/**
* Write the start to the specified output stream
*
* @param out
* The output stream
* @throws IOException
* If an IO problem occurs.
*/
protected static void writeStart(OutputStream out) throws IOException {
log.trace("enter sendStart(OutputStream out)");
out.write(EXTRA_BYTES);
out.write(BOUNDARY_BYTES);
out.write(CRLF_BYTES);
}
/**
* Write the end data to the output stream.
*
* @param out
* The output stream
* @throws IOException
* If an IO problem occurs.
*/
protected static void writeEnd(OutputStream out) throws IOException {
log.trace("enter sendEnd(OutputStream out)");
out.write(CRLF_BYTES);
}
}
1.1 jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/spi/MultipartServiceProvider.java
Index: MultipartServiceProvider.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/codec-multipart/src/java/org/apache/commons/codec/multipart/spi/MultipartServiceProvider.java,v 1.1 2004/01/12 21:42:36 mdiggory Exp $
* $Revision: 1.1 $
* $Date: 2004/01/12 21:42:36 $
*
* ====================================================================
*
* 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
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.codec.multipart.spi;
import org.apache.commons.codec.multipart.MultipartCodec;
/**
* This class will maintain any generic Service Provider methods and any
* define any private methods we wish to keep from the user.
*
* @author Mark Diggory
*/
public abstract class MultipartServiceProvider implements MultipartCodec {
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org