You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by ca...@apache.org on 2008/10/17 14:40:34 UTC
svn commit: r705584 -
/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/multipart/AbstractMultipartCollectionAdapter.java
Author: calavera
Date: Fri Oct 17 05:40:34 2008
New Revision: 705584
URL: http://svn.apache.org/viewvc?rev=705584&view=rev
Log:
Multipart creation implementation supports draft 04:
http://atompub-mulitpart-spec.googlecode.com/svn/trunk/draft-gregorio-atompub-multipart-04-from-3.diff.html
Modified:
incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/multipart/AbstractMultipartCollectionAdapter.java
Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/multipart/AbstractMultipartCollectionAdapter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/multipart/AbstractMultipartCollectionAdapter.java?rev=705584&r1=705583&r2=705584&view=diff
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/multipart/AbstractMultipartCollectionAdapter.java (original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/multipart/AbstractMultipartCollectionAdapter.java Fri Oct 17 05:40:34 2008
@@ -46,6 +46,12 @@
public abstract class AbstractMultipartCollectionAdapter extends AbstractCollectionAdapter
implements MultipartRelatedCollectionInfo {
+ private static final String CONTENT_TYPE_HEADER = "content-type";
+ private static final String CONTENT_ID_HEADER = "content-id";
+ private static final String START_PARAM = "start";
+ private static final String TYPE_PARAM = "type";
+ private static final String BOUNDARY_PARAM = "boundary";
+
protected Map<String, String> accepts;
public String[] getAccepts(RequestContext request) {
@@ -57,7 +63,7 @@
RequestContext request) throws IOException, ParseException,
MessagingException {
- String boundary = request.getContentType().getParameter("boundary");
+ String boundary = request.getContentType().getParameter(BOUNDARY_PARAM);
if (boundary == null) {
throw new IllegalArgumentException("multipart/related stream invalid, boundary parameter is missing.");
@@ -65,11 +71,13 @@
boundary = "--" + boundary;
- String type = request.getContentType().getParameter("type");
+ String type = request.getContentType().getParameter(TYPE_PARAM);
if (!(type != null && MimeTypeHelper.isAtom(type))) {
throw new ParseException("multipart/related stream invalid, type parameter should be "
+ Constants.ATOM_MEDIA_TYPE);
}
+
+ String start = request.getContentType().getParameter(START_PARAM);
PushbackInputStream pushBackInput = new PushbackInputStream(request.getInputStream(), 2);
pushBackInput.unread("\r\n".getBytes());
@@ -77,31 +85,56 @@
MultipartInputStream multipart = new MultipartInputStream(pushBackInput, boundary.getBytes());
multipart.skipBoundary();
-
- //get the media link entry
- Map<String, String> entryHeaders = getHeaders(multipart);
- if (!(entryHeaders.get("content-type") != null &&
- MimeTypeHelper.isAtom(entryHeaders.get("content-type")))) {
- throw new ParseException("multipart/related stream invalid, media link entry content-type is missing");
+
+ Document<Entry> entry = null;
+ Map<String, String> entryHeaders = new HashMap<String, String>();
+ InputStream data = null;
+ Map<String, String> dataHeaders = new HashMap<String, String>();
+
+ Map<String, String> headers = getHeaders(multipart);
+
+ //check if the first boundary is the media link entry
+ if (start == null || start.length() == 0 || (headers.containsKey(CONTENT_ID_HEADER) && start.equals(headers.get(CONTENT_ID_HEADER)))
+ || (headers.containsKey(CONTENT_TYPE_HEADER) && MimeTypeHelper.isAtom(headers.get(CONTENT_TYPE_HEADER))) ) {
+ entry = getEntry(multipart, request);
+ entryHeaders.putAll(headers);
+ } else {
+ data = getDataInputStream(multipart);
+ dataHeaders.putAll(headers);
}
- Document<Entry> entry = getEntry(multipart, request);
multipart.skipBoundary();
- //get the media resource
- Map<String, String> dataHeaders = getHeaders(multipart);
- if (dataHeaders.get("content-type") == null) {
- throw new ParseException("multipart/related stream invalid, data content-type is missing");
- }
- if (!isContentTypeAccepted(dataHeaders.get("content-type"), request)) {
- throw new ParseException("multipart/related stream invalid, content-type not accepted into a multipart file");
+ headers = getHeaders(multipart);
+
+ if (start != null && (headers.containsKey(CONTENT_ID_HEADER) && start.equals(headers.get(CONTENT_ID_HEADER)))
+ && (headers.containsKey(CONTENT_TYPE_HEADER) && MimeTypeHelper.isAtom(headers.get(CONTENT_TYPE_HEADER))) ) {
+ entry = getEntry(multipart, request);
+ entryHeaders.putAll(headers);
+ } else {
+ data = getDataInputStream(multipart);
+ dataHeaders.putAll(headers);
}
- InputStream data = getDataInputStream(multipart);
+ checkMultipartContent(entry, dataHeaders, request);
return new MultipartRelatedPost(entry, data, entryHeaders, dataHeaders);
}
+ private void checkMultipartContent(Document<Entry> entry, Map<String, String> dataHeaders,
+ RequestContext request) throws ParseException {
+ if (entry == null) {
+ throw new ParseException("multipart/related stream invalid, media link entry is missing");
+ }
+ if (!dataHeaders.containsKey(CONTENT_TYPE_HEADER)) {
+ throw new ParseException("multipart/related stream invalid, data content-type is missing");
+ }
+ if (!isContentTypeAccepted(dataHeaders.get(CONTENT_TYPE_HEADER), request)) {
+ throw new ParseException("multipart/related stream invalid, content-type "
+ + dataHeaders.get(CONTENT_TYPE_HEADER) + " not accepted into this multipart file");
+ }
+ }
+
private Map<String, String> getHeaders(MultipartInputStream multipart) throws IOException, MessagingException {
Map<String, String> mapHeaders = new HashMap<String, String>();
moveToHeaders(multipart);