You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by el...@apache.org on 2009/08/26 10:25:46 UTC
svn commit: r807922 - in
/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common:
internal/providers/multipart/ model/multipart/
Author: elib
Date: Wed Aug 26 08:25:46 2009
New Revision: 807922
URL: http://svn.apache.org/viewvc?rev=807922&view=rev
Log:
Add more javadoc to multipart
Modified:
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/multipart/InMultiPartProvider.java
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedInMultiPart.java
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedInPart.java
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedOutMultiPart.java
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/InMultiPart.java
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/InPart.java
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/OutMultiPart.java
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/OutPart.java
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/multipart/InMultiPartProvider.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/multipart/InMultiPartProvider.java?rev=807922&r1=807921&r2=807922&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/multipart/InMultiPartProvider.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/multipart/InMultiPartProvider.java Wed Aug 26 08:25:46 2009
@@ -55,7 +55,6 @@
String boundary = mediaType.getParameters().get("boundary");
MultiPartParser mpParser = new MultiPartParser(entityStream,boundary);
InMultiPart imp = new InMultiPart(mpParser);
- imp.setBoundary(boundary);
imp.setProviders(providers);
return imp;
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedInMultiPart.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedInMultiPart.java?rev=807922&r1=807921&r2=807922&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedInMultiPart.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedInMultiPart.java Wed Aug 26 08:25:46 2009
@@ -24,27 +24,44 @@
import java.util.ArrayList;
import java.util.List;
+/**
+ * This class is used to represent inbound MultiPart messages, unlike the {@link InMultiPart}
+ * the {@link BufferedInMultiPart} enable non sequential access to the message's parts
+ * <code>
+ *
+ * @POST
+ * @Consumes( MediaTypeUtils.MULTIPART_FORM_DATA) <br>
+ * public String postMultipart(InMultiPart inMP) throws IOException <br>
+ * { <br>
+ * while(inMP.hasNext()) { <br>
+ * InPart part = inMP.next();<br>
+ * MyClass myOject =part.getBody(MyClass.class, null);<br>
+ * // Do somthing<br>
+ * <br>
+ * .<br>
+ * .<br>
+ * }<br>
+ *
+ */
public class BufferedInMultiPart {
- private List<InPart> parts = new ArrayList<InPart>();
- private InMultiPart imp;
-
- public BufferedInMultiPart(InMultiPart imp) throws IOException{
- this.imp = imp;
- while(imp.hasNext()){
- BufferedInPart cip = new BufferedInPart(imp.next());
- parts.add(cip);
- }
- }
+ private List<InPart> parts = new ArrayList<InPart>();
- public String getBoundary() {
- return imp.getBoundary();
- }
- public int getSize(){
- return parts.size();
- }
-
- public List<InPart> getParts() {
- return parts;
- }
+ public BufferedInMultiPart(InMultiPart imp) throws IOException {
+ while (imp.hasNext()) {
+ BufferedInPart cip = new BufferedInPart(imp.next());
+ parts.add(cip);
+ }
+ }
+
+ /**
+ * return the number of parts in the multipart message
+ */
+ public int getSize() {
+ return parts.size();
+ }
+
+ public List<InPart> getParts() {
+ return parts;
+ }
}
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedInPart.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedInPart.java?rev=807922&r1=807921&r2=807922&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedInPart.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedInPart.java Wed Aug 26 08:25:46 2009
@@ -25,11 +25,17 @@
import java.io.IOException;
import java.io.InputStream;
+/**
+ * An extension to the {@link InPart} class that enables calling of the
+ * {@link BufferedInPart#getBody(Class, java.lang.reflect.Type)} multiple times and in a non sequential order.
+ *
+ * @author elib
+ */
public class BufferedInPart extends InPart {
byte[] content;
public BufferedInPart(InPart ip) throws IOException {
- super(ip.getHeaders(),ip.getProviders());
+ super(ip.getHeaders(), ip.getProviders());
InputStream src = ip.getInputStream();
ByteArrayOutputStream dst = new ByteArrayOutputStream(1024);
byte[] bytes = new byte[1024];
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedOutMultiPart.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedOutMultiPart.java?rev=807922&r1=807921&r2=807922&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedOutMultiPart.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/BufferedOutMultiPart.java Wed Aug 26 08:25:46 2009
@@ -24,7 +24,11 @@
import java.util.Iterator;
import java.util.List;
-
+/**
+ * A simple implementation of the {@link OutMultiPart}
+ * @author elib
+ *
+ */
public class BufferedOutMultiPart extends OutMultiPart {
private List<OutPart> parts = new ArrayList<OutPart>();
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/InMultiPart.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/InMultiPart.java?rev=807922&r1=807921&r2=807922&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/InMultiPart.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/InMultiPart.java Wed Aug 26 08:25:46 2009
@@ -29,9 +29,27 @@
import org.apache.wink.common.RestException;
import org.apache.wink.common.internal.providers.multipart.MultiPartParser;
+/**
+ * This class is used to represent inbound MultiPart messages, it implements
+ * an Iterator to iterate over the message's parts. A typical usage of
+ * InMultiPart looks like this:
+ *
+ * @POST
+ * @Consumes( MediaTypeUtils.MULTIPART_FORM_DATA) <br>
+ * public String postMultipart(InMultiPart inMP) throws IOException <br>
+ * { <br>
+ * while(inMP.hasNext()) { <br>
+ * InPart part = inMP.next();<br>
+ * MyClass myOject =part.getBody(MyClass.class, null);<br>
+ * // Do somthing<br>
+ * <br>
+ * .<br>
+ * .<br>
+ * }<br>
+ */
public class InMultiPart implements Iterator<InPart> {
public final static String SEP = "\n";
- private String boundary = "simple boundary";
+ //private String boundary = "simple boundary";
private MultiPartParser MPParser;
int index = -1;
boolean moved = false;
@@ -50,19 +68,12 @@
MPParser = mim;
}
- public void setBoundary(String boundary) {
- this.boundary = boundary;
- }
-
- public String getBoundary() {
- return boundary;
- }
-
/**
* Returns <tt>true</tt> if the iteration has more elements. (In other
* words, returns <tt>true</tt> if <tt>next</tt> would return an element
- * rather than throwing an exception.) NOTICE: calling the has next will
- * cause the inputStream of the previous part to be invalid
+ * rather than throwing an exception.) <br>
+ * NOTICE: calling the has next will cause the inputStream of the previous
+ * part to be invalid
*
* @return <tt>true</tt> if the iterator has more elements.
*/
@@ -93,6 +104,9 @@
}
+ /**
+ * Not implemented
+ */
public void remove() {
throw new UnsupportedOperationException();
}
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/InPart.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/InPart.java?rev=807922&r1=807921&r2=807922&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/InPart.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/InPart.java Wed Aug 26 08:25:46 2009
@@ -35,87 +35,102 @@
import org.apache.wink.common.internal.CaseInsensitiveMultivaluedMap;
+/**
+ * This class is used to represent a single part in an inbound MultiPart
+ * messages,
+ *
+ * @see InMultiPart
+ */
public class InPart {
- private MultivaluedMap<String, String> headers = new CaseInsensitiveMultivaluedMap<String>();
- private InputStream inputStream;
- private Providers providers;
-
- public Providers getProviders() {
- return providers;
- }
-
- public void setProviders(Providers providers) {
- this.providers = providers;
- }
-
-
- public InPart() {
-
- }
-
-
- public InPart(MultivaluedMap<String, String> headers,Providers providers) {
- super();
- this.headers = headers;
- this.providers = providers;
- }
-
-
- public void setInputStream(InputStream inputStream) {
- this.inputStream = inputStream;
- }
-
- public InputStream getInputStream() {
- return inputStream;
- }
-
- public void setHeaders(MultivaluedMap<String, String> headers) {
- this.headers = headers;
- }
-
- public MultivaluedMap<String, String> getHeaders() {
- return headers;
- }
-
-
- public void addHeader(String name, String value) {
- getHeaders().add(name, value);
- }
-
- public void setContentType(String contentType) {
- getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, contentType);
- }
-
- public String getContentType() {
- return getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
- }
-
- public void setLocationHeader(String location) {
- getHeaders().putSingle("location", location);
- }
-
- public Set<String> getHeadersName() {
- return getHeaders().keySet();
- }
-
- //public abstract InputStream getInputStream();
-
-
- public <T> T getBody(Class<T> type, Type genericType,Providers providers) throws IOException{
- MediaType mt = MediaType.valueOf(getContentType());
- MessageBodyReader<T> reader = providers.getMessageBodyReader(type, genericType, null, mt);
- if(reader == null)
- throw new WebApplicationException(Response.Status.UNSUPPORTED_MEDIA_TYPE);
- return reader.readFrom(type, genericType, null, mt, getHeaders(),getInputStream());
- }
-
-
- public <T> T getBody(Class<T> type, Type genericType) throws IOException{
- return getBody(type, genericType,this.providers);
- }
-
-
-
-
+ private MultivaluedMap<String, String> headers = new CaseInsensitiveMultivaluedMap<String>();
+ private InputStream inputStream;
+ private Providers providers;
+
+ /**
+ * get the providers that are used by the @see InPart#getBody(Class, Type)
+ * method for deserialization of the part
+ *
+ * @return
+ */
+ public Providers getProviders() {
+ return providers;
+ }
+
+ /**
+ * set the providers to be used by the @see InPart#getBody(Class, Type)
+ * method for deserialization of the part
+ *
+ * @return
+ */
+ public void setProviders(Providers providers) {
+ this.providers = providers;
+ }
+
+ public InPart() {
+
+ }
+
+ public InPart(MultivaluedMap<String, String> headers, Providers providers) {
+ super();
+ this.headers = headers;
+ this.providers = providers;
+ }
+
+ protected void setInputStream(InputStream inputStream) {
+ this.inputStream = inputStream;
+ }
+
+ public InputStream getInputStream() {
+ return inputStream;
+ }
+
+ protected void setHeaders(MultivaluedMap<String, String> headers) {
+ this.headers = headers;
+ }
+
+ /**
+ * Get the part's headers
+ *
+ * @return
+ */
+ public MultivaluedMap<String, String> getHeaders() {
+ return headers;
+ }
+
+
+ public String getContentType() {
+ return getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
+ }
+
+ public Set<String> getHeadersName() {
+ return getHeaders().keySet();
+ }
+
+ /**
+ * This method is used for deserialization of the part stream to a <T>
+ * object using the given providers.<br>
+ *
+ * @param <T>
+ * @param type
+ * @param genericType
+ * @param providers
+ * @return
+ * @throws IOException
+ */
+ public <T> T getBody(Class<T> type, Type genericType, Providers providers) throws IOException {
+ MediaType mt = MediaType.valueOf(getContentType());
+ MessageBodyReader<T> reader = providers.getMessageBodyReader(type, genericType, null, mt);
+ if (reader == null)
+ throw new WebApplicationException(Response.Status.UNSUPPORTED_MEDIA_TYPE);
+ return reader.readFrom(type, genericType, null, mt, getHeaders(), getInputStream());
+ }
+
+ /**
+ * This method is used for deserialization of the part stream. It make a
+ * usage of its providers data member for the deserialization <br>
+ */
+ public <T> T getBody(Class<T> type, Type genericType) throws IOException {
+ return getBody(type, genericType, this.providers);
+ }
}
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/OutMultiPart.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/OutMultiPart.java?rev=807922&r1=807921&r2=807922&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/OutMultiPart.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/OutMultiPart.java Wed Aug 26 08:25:46 2009
@@ -26,31 +26,56 @@
import javax.ws.rs.ext.Providers;
-
+/**
+ * An abstract class to generate a MultiPart response, the concept behind this
+ * class been abstract is that there might be simple implementation over a
+ * collection or more complex once like over a database cursor
+ *
+ * @author elib
+ */
public abstract class OutMultiPart {
-
- private String boundary = "simple boundary";
- public final static String SEP = "\n";
-
- public void setBoundary(String boundary) {
- this.boundary = boundary;
- }
- public String getBoundary() {
- return boundary;
- }
- public abstract Iterator<? extends OutPart> getIterator();
-
-
- public void write(OutputStream os, Providers providers) throws IOException{
- Iterator<? extends OutPart> it = getIterator();
- while (it.hasNext()){
- OutPart p = it.next();
- os.write((SEP+"--"+boundary+SEP).getBytes());
- p.writePart(os,providers);
- }
- os.write((SEP+"--"+boundary+"--"+SEP).getBytes());
- }
-
-
+
+ private String boundary = "simple boundary";
+ public final static String SEP = "\n";
+
+ /**
+ * set the boundary to be used to separate between the different parts
+ *
+ * @param boundary
+ */
+ public void setBoundary(String boundary) {
+ this.boundary = boundary;
+ }
+
+ public String getBoundary() {
+ return boundary;
+ }
+
+ /**
+ * An implementation of this method should return an iterator over the
+ * {@link OutPart} of the message, this iterator is used to serialized the
+ * message
+ *
+ * @return
+ */
+ protected abstract Iterator<? extends OutPart> getIterator();
+
+ /**
+ * This method write the multiPart message to the os stream, it make a usage
+ * of the providers for the serialization of the parts
+ *
+ * @param os
+ * @param providers
+ * @throws IOException
+ */
+ public void write(OutputStream os, Providers providers) throws IOException {
+ Iterator<? extends OutPart> it = getIterator();
+ while (it.hasNext()) {
+ OutPart p = it.next();
+ os.write((SEP + "--" + boundary + SEP).getBytes());
+ p.writePart(os, providers);
+ }
+ os.write((SEP + "--" + boundary + "--" + SEP).getBytes());
+ }
}
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/OutPart.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/OutPart.java?rev=807922&r1=807921&r2=807922&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/OutPart.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/model/multipart/OutPart.java Wed Aug 26 08:25:46 2009
@@ -34,15 +34,32 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * This class is used to represent a part in an {@link OutMultiPart} message,
+ * basically it holds the part headers and body
+ *
+ * @author elib
+ */
public class OutPart {
private MultivaluedMap<String, String> headers = new CaseInsensitiveMultivaluedMap<String>();
private Object body;
private static final Logger logger = LoggerFactory.getLogger(OutPart.class);
+ /**
+ * add header to the part
+ *
+ * @param name
+ * @param value
+ */
public void addHeader(String name, String value) {
getHeaders().add(name, value);
}
+ /**
+ * Add the content type header
+ *
+ * @param contentType
+ */
public void setContentType(String contentType) {
getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, contentType);
}
@@ -51,13 +68,24 @@
return getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
}
+ /**
+ * set the location header
+ *
+ * @param location
+ */
public void setLocationHeader(String location) {
getHeaders().putSingle("location", location);
}
-
-
- public void writeBody(OutputStream os, Providers providers) throws IOException {
+ /**
+ * write the part body to the os stream, the providers might be used to
+ * serialize the body
+ *
+ * @param os
+ * @param providers
+ * @throws IOException
+ */
+ protected void writeBody(OutputStream os, Providers providers) throws IOException {
if (getBody() != null) {
MessageBodyWriter writer =
@@ -75,6 +103,13 @@
}
}
+ /**
+ * write the entire part to the os stream, the providers might be used to serialize the body
+ *
+ * @param os
+ * @param providers
+ * @throws IOException
+ */
public void writePart(OutputStream os, Providers providers) throws IOException {
// writeHeaders
for (String name : getHeaders().keySet()) {
@@ -91,6 +126,7 @@
writeBody(os, providers);
}
+
public void setBody(Object body) {
this.body = body;
}