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/07/18 16:45:08 UTC
svn commit: r677918 - in /incubator/abdera/java/trunk/client/src:
main/java/org/apache/abdera/protocol/client/
main/java/org/apache/abdera/protocol/client/util/
test/java/org/apache/abdera/test/client/
test/java/org/apache/abdera/test/client/util/ test...
Author: calavera
Date: Fri Jul 18 07:45:08 2008
New Revision: 677918
URL: http://svn.apache.org/viewvc?rev=677918&view=rev
Log:
added client support in order to allow to send multipart/request objects, it follows the AtomPub multipart spec:
http://atompub-mulitpart-spec.googlecode.com/svn/trunk/draft-gregorio-atompub-multipart-02.txt
Example:
Entry entry = new FOMEntry();
entry.setTitle("my image");
entry.addAuthor("david");
entry.setId("tag:apache.org,2008:234534344");
entry.setSummary("multipart test");
entry.setContent(new IRI("cid:234234@example.com"), "image/png");
new AbderaClient().post("http://localhost/media", entry, new FileInputStream("info.png"));
Added:
incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/MultimediaRelatedRequestEntity.java
incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/util/
incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/util/MultipartRelatedRequestEntityTest.java
incubator/abdera/java/trunk/client/src/test/resources/
incubator/abdera/java/trunk/client/src/test/resources/org/
incubator/abdera/java/trunk/client/src/test/resources/org/apache/
incubator/abdera/java/trunk/client/src/test/resources/org/apache/abdera/
incubator/abdera/java/trunk/client/src/test/resources/org/apache/abdera/test/
incubator/abdera/java/trunk/client/src/test/resources/org/apache/abdera/test/client/
incubator/abdera/java/trunk/client/src/test/resources/org/apache/abdera/test/client/util/
incubator/abdera/java/trunk/client/src/test/resources/org/apache/abdera/test/client/util/info.png (with props)
Modified:
incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbderaClient.java
incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java
Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbderaClient.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbderaClient.java?rev=677918&r1=677917&r2=677918&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbderaClient.java (original)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/AbderaClient.java Fri Jul 18 07:45:08 2008
@@ -32,6 +32,7 @@
import org.apache.abdera.model.Base;
import org.apache.abdera.model.Document;
import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Entry;
import org.apache.abdera.protocol.EntityProvider;
import org.apache.abdera.protocol.Response.ResponseType;
import org.apache.abdera.protocol.client.cache.Cache;
@@ -42,6 +43,7 @@
import org.apache.abdera.protocol.client.util.BaseRequestEntity;
import org.apache.abdera.protocol.client.util.EntityProviderRequestEntity;
import org.apache.abdera.protocol.client.util.MethodHelper;
+import org.apache.abdera.protocol.client.util.MultimediaRelatedRequestEntity;
import org.apache.abdera.protocol.client.util.SimpleSSLProtocolSocketFactory;
import org.apache.abdera.protocol.error.Error;
import org.apache.abdera.protocol.error.ProtocolException;
@@ -236,6 +238,47 @@
}
return execute("POST", uri, new BaseRequestEntity(base, options.isUseChunked()), options);
}
+
+ /**
+ * Sends an HTTP POST request to the specified URI.
+ * It uses the media and entry parameters to create a multipart/related object.
+ * If the contentType is not provided this method tries to get it from the type attribute of the entry content.
+ *
+ * @param uri The request URI
+ * @param entry The entry that will be sent as the first element of the multipart/related object
+ * @param media The media object that will be sent as the second element of the multipart/related object
+ */
+ public ClientResponse post(String uri, Entry entry, InputStream media) {
+ return post(uri, entry, media, getDefaultRequestOptions());
+ }
+
+ /**
+ * Sends an HTTP POST request to the specified URI.
+ * It uses the media and entry parameters to create a multipart/related object.
+ * If the contentType is not provided this method tries to get it from the type attribute of the entry content.
+ *
+ * @param uri The request URI
+ * @param entry The entry that will be sent as the first element of the multipart/related object
+ * @param media The media object that will be sent as the second element of the multipart/related object
+ * @param options The request options
+ */
+ public ClientResponse post(String uri, Entry entry, InputStream media, RequestOptions options) {
+ return post(uri, entry, media, null, options);
+ }
+
+ /**
+ * Sends an HTTP POST request to the specified URI.
+ * It uses the media and entry parameters to create a multipart/related object.
+ *
+ * @param uri The request URI
+ * @param entry The entry that will be sent as the first element of the multipart/related object
+ * @param media The media object that will be sent as the second element of the multipart/related object
+ * @param contentType the content type of the media object
+ * @param options The request options
+ */
+ public ClientResponse post(String uri, Entry entry, InputStream media, String contentType, RequestOptions options) {
+ return execute("POST", uri, new MultimediaRelatedRequestEntity(entry, media, contentType), options);
+ }
/**
* Sends an HTTP PUT request to the specified URI.
Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/MultimediaRelatedRequestEntity.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/MultimediaRelatedRequestEntity.java?rev=677918&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/MultimediaRelatedRequestEntity.java (added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/util/MultimediaRelatedRequestEntity.java Fri Jul 18 07:45:08 2008
@@ -0,0 +1,99 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License. For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.client.util;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.util.MimeTypeHelper;
+import org.apache.commons.httpclient.methods.RequestEntity;
+
+import sun.misc.BASE64Encoder;
+
+public class MultimediaRelatedRequestEntity implements RequestEntity {
+
+ static final int BUFF_SIZE = 1024;
+ static final byte[] buffer = new byte[BUFF_SIZE];
+ private final Entry entry;
+ private final InputStream input;
+ private final String contentType;
+ private String boundary;
+
+ public MultimediaRelatedRequestEntity(Entry entry, InputStream input) {
+ this(entry, input, null);
+ }
+
+ public MultimediaRelatedRequestEntity(Entry entry, InputStream input, String contentType) {
+ this(entry, input, contentType, null);
+ }
+
+ public MultimediaRelatedRequestEntity(Entry entry, InputStream input, String contentType, String boundary) {
+ this.input = input;
+ this.entry = entry;
+ this.contentType = contentType != null?contentType:entry.getContentMimeType().toString() ;
+ this.boundary = boundary != null?boundary:String.valueOf(System.currentTimeMillis());
+ }
+
+ public void writeRequest(OutputStream arg0) throws IOException {
+ DataOutputStream out = new DataOutputStream(arg0);
+ out.writeBytes("--" + boundary + "\r\n");
+ writeEntry(out);
+ writeInput(out);
+ }
+
+ private void writeEntry(DataOutputStream out) throws IOException {
+ out.writeBytes("content-type: " + MimeTypeHelper.getMimeType(entry) + "\r\n\r\n");
+ entry.writeTo(out);
+ out.writeBytes("\r\n" + "--" + boundary + "\r\n");
+ }
+
+ private void writeInput(DataOutputStream out) throws IOException {
+ if (contentType == null) {
+ throw new NullPointerException("media content type can't be null");
+ }
+ out.writeBytes("content-type: " + contentType + "\r\n");
+
+ String contentId = entry.getContentSrc().toString();
+ if (!contentId.matches("cid:.+")) {
+ throw new IllegalArgumentException("entry content source is not a correct content-ID");
+ }
+ out.writeBytes("content-id: <" + contentId.substring(4) + ">\r\n\r\n");
+
+ int end;
+ while ((end = input.read(buffer)) != -1) {
+ out.writeBytes(new BASE64Encoder().encode(buffer));
+ }
+ out.writeBytes("\r\n" + "--" + boundary + "\r\n");
+ }
+
+ public long getContentLength() {
+ return -1;
+ }
+
+ public String getContentType() {
+ return "Multipart/Related; boundary=\"--" + boundary
+ + "\";type=\"" + MimeTypeHelper.getMimeType(entry) + "\"";
+ }
+
+ public boolean isRepeatable() {
+ return true;
+ }
+}
Modified: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java?rev=677918&r1=677917&r2=677918&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java (original)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java Fri Jul 18 07:45:08 2008
@@ -19,6 +19,7 @@
import org.apache.abdera.test.client.app.AppTest;
import org.apache.abdera.test.client.cache.CacheTest;
+import org.apache.abdera.test.client.util.MultipartRelatedRequestEntityTest;
import org.junit.internal.runners.TextListener;
import org.junit.runner.JUnitCore;
@@ -27,7 +28,8 @@
{
JUnitCore runner = new JUnitCore();
runner.addListener(new TextListener(System.out));
- runner.run(CacheTest.class,AppTest.class);
+ runner.run(CacheTest.class,AppTest.class,
+ MultipartRelatedRequestEntityTest.class);
}
}
Added: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/util/MultipartRelatedRequestEntityTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/util/MultipartRelatedRequestEntityTest.java?rev=677918&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/util/MultipartRelatedRequestEntityTest.java (added)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/util/MultipartRelatedRequestEntityTest.java Fri Jul 18 07:45:08 2008
@@ -0,0 +1,97 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License. For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.test.client.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+
+import javax.activation.MimeType;
+
+import junit.framework.Assert;
+
+import org.apache.abdera.i18n.iri.IRI;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.parser.stax.FOMEntry;
+import org.apache.abdera.protocol.client.util.MultimediaRelatedRequestEntity;
+import org.apache.abdera.util.MimeTypeHelper;
+import org.apache.commons.httpclient.methods.RequestEntity;
+import org.junit.Test;
+import org.mortbay.io.WriterOutputStream;
+
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+
+public class MultipartRelatedRequestEntityTest extends Assert {
+
+ @Test
+ public void testMultipartFormat() throws IOException {
+ Entry entry = new FOMEntry();
+ entry.setTitle("my image");
+ entry.addAuthor("david");
+ entry.setId("tag:apache.org,2008:234534344");
+ entry.setSummary("multipart test");
+ entry.setContent(new IRI("cid:234234@example.com"), "image/jpg");
+ RequestEntity request = new MultimediaRelatedRequestEntity(entry, this.getClass().getResourceAsStream("info.png"));
+
+ StringWriter sw = new StringWriter();
+ WriterOutputStream os = new WriterOutputStream(sw);
+ request.writeRequest(os);
+
+ String multipart = sw.toString();
+ //System.out.println(sw.toString());
+ assertTrue(multipart.contains("content-id: <23...@example.com>"));
+ assertTrue(multipart.contains("content-type: image/jpg"));
+ }
+
+ @Test
+ public void testMultimediaRelatedContentType() throws Exception {
+ MimeType type = new MimeType("Multipart/Related;boundary=\"--35245352345sdfg\"");
+ assertTrue(MimeTypeHelper.isMatch("Multipart/Related", type.toString()));
+ assertEquals("--35245352345sdfg", type.getParameter("boundary"));
+ }
+
+ //@Test
+ public void testMultipartEncoding() throws Exception {
+ InputStream input = this.getClass().getResourceAsStream("info.png");
+ int BUFF_SIZE = 1024;
+
+ byte[] line = new byte[BUFF_SIZE];
+ int end;
+ String s = "";
+ while ((end = input.read(line)) != -1) {
+ s += new BASE64Encoder().encode(line);
+ }
+
+ ByteArrayInputStream bi = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(s));
+ File f = new File("info-out.png");
+ if (f.exists()) f.delete();
+ f.createNewFile();
+ FileOutputStream fo = new FileOutputStream(f);
+
+ while ((end = bi.read(line)) != -1) {
+ fo.write(line, 0, end);
+ }
+
+ fo.flush();
+ fo.close();
+ }
+}
Added: incubator/abdera/java/trunk/client/src/test/resources/org/apache/abdera/test/client/util/info.png
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/resources/org/apache/abdera/test/client/util/info.png?rev=677918&view=auto
==============================================================================
Binary file - no diff available.
Propchange: incubator/abdera/java/trunk/client/src/test/resources/org/apache/abdera/test/client/util/info.png
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream