abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1173444 - in /abdera/abdera2: activities/src/main/java/org/apache/abdera2/activities/extra/ activities/src/main/java/org/apache/abdera2/activities/io/gson/ activities/src/main/java/org/apache/abdera2/activities/model/objects/ common/src/ma...
Date Wed, 21 Sep 2011 00:48:28 GMT
Author: jmsnell
Date: Wed Sep 21 00:48:28 2011
New Revision: 1173444

URL: http://svn.apache.org/viewvc?rev=1173444&view=rev
Log:
Extension BinaryObject type + example

Added:
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/BinaryObject.java
  (with props)
    abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java
  (with props)
Modified:
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/io/gson/BaseAdapter.java
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/io/gson/GsonIO.java
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/FileObject.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/date/Duration.java

Added: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/BinaryObject.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/BinaryObject.java?rev=1173444&view=auto
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/BinaryObject.java
(added)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/BinaryObject.java
Wed Sep 21 00:48:28 2011
@@ -0,0 +1,86 @@
+package org.apache.abdera2.activities.extra;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.activation.DataHandler;
+import javax.activation.MimeType;
+
+import org.apache.abdera2.activities.io.gson.Properties;
+import org.apache.abdera2.activities.io.gson.Property;
+import org.apache.abdera2.activities.model.objects.FileObject;
+import org.apache.abdera2.common.anno.Name;
+import org.apache.commons.codec.binary.Base64InputStream;
+import org.apache.commons.codec.binary.Base64OutputStream;
+
+/**
+ * The BinaryObject is an extension object type that allows 
+ * for an Activity Streams object representing arbitrary 
+ * Base64 encoded data. It is an extension of the FileObject 
+ * type so it inherits the fileUrl and mimeType properties
+ * to communicate the permanent url of the data and the 
+ * MIME media type. The content property is used to contain
+ * the Base64 encoded data. This object is particularly 
+ * useful in conjunction with the "attachments" property 
+ * to attach arbitrary binary resources to another ASObject.
+ * For instance, attaching a VCALENDAR file to an Event Object.
+ */
+@Name("binary")
+@Properties({
+  @Property(name="mimeType",to=MimeType.class)
+})
+public class BinaryObject extends FileObject {
+  
+  private static final long serialVersionUID = 5120608845229587281L;
+
+  public BinaryObject() {
+    setMimeType("application/octet-stream");
+  }
+  
+  public BinaryObject(String displayName) {
+    this();
+    setDisplayName(displayName);
+  }
+    
+  public void setContent(DataHandler data) throws IOException {
+    setContent(data.getInputStream());
+    setMimeType(data.getContentType());
+  }
+  
+  public void setContent(InputStream data) throws IOException {
+    ByteArrayOutputStream out = 
+      new ByteArrayOutputStream();
+    Base64OutputStream bout = 
+      new Base64OutputStream(out,true,0,null);
+    byte[] d = new byte[100];
+    int r = -1;
+    while((r = data.read(d)) > -1) { 
+      bout.write(d, 0, r);
+      bout.flush();
+    }
+    bout.close();
+    String c = new String(out.toByteArray(),"UTF-8");
+    super.setContent(c);
+  }
+  
+  public InputStream getInputStream() throws IOException {
+    String content = super.getContent();
+    if (content == null) return null;
+    ByteArrayInputStream in = 
+      new ByteArrayInputStream(
+        content.getBytes("UTF-8"));
+    Base64InputStream bin = 
+      new Base64InputStream(in);
+    return bin;
+  }
+  
+  public void setContent(byte[] data) throws IOException {
+    setContent(new ByteArrayInputStream(data));
+  }
+  
+  public void setContent(byte[] data, int s, int e) throws IOException {
+    setContent(new ByteArrayInputStream(data,s,e));
+  }
+}

Propchange: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/BinaryObject.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java?rev=1173444&r1=1173443&r2=1173444&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java
(original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java
Wed Sep 21 00:48:28 2011
@@ -64,7 +64,8 @@ public class Extra {
       TvEpisodeObject.class,
       TvSeasonObject.class,
       TvSeriesObject.class,
-      VersionObject.class);
+      VersionObject.class,
+      BinaryObject.class);
   }
   
   /**

Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/io/gson/BaseAdapter.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/io/gson/BaseAdapter.java?rev=1173444&r1=1173443&r2=1173444&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/io/gson/BaseAdapter.java
(original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/io/gson/BaseAdapter.java
Wed Sep 21 00:48:28 2011
@@ -21,7 +21,6 @@ import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
-import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
@@ -86,24 +85,24 @@ public class BaseAdapter 
   private void initPropMap() {
     map.put("verb",Verb.class);
     map.put("url",IRI.class);
-    map.put("fileurl", IRI.class);
+    map.put("fileUrl", IRI.class);
     map.put("gadget", IRI.class);
     map.put("updated", Date.class);
     map.put("published", Date.class);
     map.put("lang", Lang.class);
     map.put("icon", MediaLink.class);
     map.put("image", MediaLink.class);
-    map.put("totalitems", Integer.class);
+    map.put("totalItems", Integer.class);
     map.put("duration", Integer.class);
     map.put("height", Integer.class);
     map.put("location", PlaceObject.class);
     map.put("mood", Mood.class);
     map.put("address", Address.class);
     map.put("stream", MediaLink.class);
-    map.put("fullimage", MediaLink.class);
-    map.put("endtime", Date.class);
-    map.put("starttime", Date.class);
-    map.put("mimetype", MimeType.class);
+    map.put("fullImage", MediaLink.class);
+    map.put("endTime", Date.class);
+    map.put("startTime", Date.class);
+    map.put("mimeType", MimeType.class);
     map.put("rating", Double.class);
     map.put("position", IsoPosition.class);
     map.put("etag", EntityTag.class);
@@ -242,7 +241,7 @@ public class BaseAdapter 
     }
     for (Entry<String,JsonElement> entry : obj.entrySet()) {
       String name = entry.getKey();
-      Class<?> _class = map.get(name.toLowerCase(Locale.US));
+      Class<?> _class = map.get(name);
       JsonElement val = entry.getValue();
       if (val.isJsonPrimitive()) {
         if (_class != null) {

Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/io/gson/GsonIO.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/io/gson/GsonIO.java?rev=1173444&r1=1173443&r2=1173444&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/io/gson/GsonIO.java
(original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/io/gson/GsonIO.java
Wed Sep 21 00:48:28 2011
@@ -24,6 +24,8 @@ import java.io.Reader;
 import java.io.Writer;
 import java.util.Date;
 
+import javax.activation.MimeType;
+
 import org.apache.abdera2.common.date.Duration;
 import org.apache.abdera2.common.date.Interval;
 import org.apache.abdera2.common.geo.IsoPosition;
@@ -91,6 +93,7 @@ public class GsonIO extends IO {
     gb.registerTypeAdapter(IsoPosition.class, new PositionAdapter());
     gb.registerTypeAdapter(EntityTag.class, new EntityTagAdapter());
     gb.registerTypeAdapter(Template.class, new TemplateAdapter());
+    gb.registerTypeAdapter(MimeType.class, new MimeTypeAdapter());
     for(TypeAdapter<?> adapter : adapters) {
       if (adapter instanceof GsonTypeAdapter) {
         gb.registerTypeAdapter(

Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/FileObject.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/FileObject.java?rev=1173444&r1=1173443&r2=1173444&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/FileObject.java
(original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/FileObject.java
Wed Sep 21 00:48:28 2011
@@ -17,6 +17,9 @@
  */
 package org.apache.abdera2.activities.model.objects;
 
+import javax.activation.MimeType;
+import javax.activation.MimeTypeParseException;
+
 import org.apache.abdera2.activities.model.ASObject;
 import org.apache.abdera2.common.anno.Name;
 import org.apache.abdera2.common.iri.IRI;
@@ -42,4 +45,20 @@ public class FileObject 
   public void setFileUrl(IRI fileUrl) {
     setProperty(FILEURL, fileUrl);
   }
+  
+  public MimeType getMimeType() {
+    return getProperty("mimeType");
+  }
+  
+  public void setMimeType(MimeType mimeType) {
+    setProperty("mimeType", mimeType);
+  }
+  
+  public void setMimeType(String mimeType) {
+    try {
+      setProperty("mimeType", new MimeType(mimeType));
+    } catch (MimeTypeParseException e) {
+      throw new org.apache.abdera2.common.mediatype.MimeTypeParseException(e);
+    }
+  }
 }

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/date/Duration.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/date/Duration.java?rev=1173444&r1=1173443&r2=1173444&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/date/Duration.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/date/Duration.java Wed Sep
21 00:48:28 2011
@@ -8,6 +8,7 @@ package org.apache.abdera2.common.date;
  */
 public class Duration {
 
+  private final boolean rev;
   private final double y, m, w, d, h, i, s;
 
   public Duration(double s) {
@@ -35,6 +36,11 @@ public class Duration {
   }
   
   public Duration(double y, double m, double w, double d, double h, double i, double s) {
+    this(false,y,m,w,d,h,i,s);
+  }
+  
+  public Duration(boolean rev, double y, double m, double w, double d, double h, double i,
double s) {
+    this.rev = rev;
     this.y = Math.max(0, y);
     this.m = Math.max(0, m);
     this.w = Math.max(0, w);
@@ -56,6 +62,10 @@ public class Duration {
       throw new IllegalArgumentException();
   }
   
+  public boolean reversed() {
+    return rev;
+  }
+  
   public double years() {
     return y;
   }
@@ -97,6 +107,7 @@ public class Duration {
     result = prime * result + (int) (temp ^ (temp >>> 32));
     temp = Double.doubleToLongBits(m);
     result = prime * result + (int) (temp ^ (temp >>> 32));
+    result = prime * result + (rev ? 1231 : 1237);
     temp = Double.doubleToLongBits(s);
     result = prime * result + (int) (temp ^ (temp >>> 32));
     temp = Double.doubleToLongBits(w);
@@ -123,6 +134,8 @@ public class Duration {
       return false;
     if (Double.doubleToLongBits(m) != Double.doubleToLongBits(other.m))
       return false;
+    if (rev != other.rev)
+      return false;
     if (Double.doubleToLongBits(s) != Double.doubleToLongBits(other.s))
       return false;
     if (Double.doubleToLongBits(w) != Double.doubleToLongBits(other.w))
@@ -133,7 +146,10 @@ public class Duration {
   }
 
   public String toString() {
-    StringBuilder buf = new StringBuilder("P");
+    StringBuilder buf = new StringBuilder();
+    
+    if (rev) buf.append('-');
+    buf.append('P');
     
     if (y > 0) 
       buf.append(is_smallest(m,w,d,h,i,s)&&is_frac(y)?Double.toString(y):Integer.toString((int)y))
@@ -181,8 +197,13 @@ public class Duration {
   
   public static Duration parse(String val) {
     double y = 0, m = 0, w = 0, d = 0, h = 0, i = 0, s = 0;
+    boolean rev = false;
     if (val == null) return null;
     int p = 0, l = val.length();
+    if (val.charAt(p) == '-') {
+      rev = true;
+      p++;
+    }
     if (val.charAt(p) != 'P' && val.charAt(p) != 'p')
       throw new IllegalArgumentException();
     int r = ++p;
@@ -204,7 +225,7 @@ public class Duration {
       p = r+1;
       r = p;
     }
-    return new Duration(y,m,w,d,h,i,s);
+    return new Duration(rev,y,m,w,d,h,i,s);
   }
   
   private static boolean digit(char c) {

Added: abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java?rev=1173444&view=auto
==============================================================================
--- abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java
(added)
+++ abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java
Wed Sep 21 00:48:28 2011
@@ -0,0 +1,50 @@
+package org.apache.abdera2.examples.activities;
+
+import java.io.InputStream;
+import java.io.StringReader;
+import java.net.URL;
+
+import javax.activation.DataHandler;
+
+import org.apache.abdera2.activities.extra.BinaryObject;
+import org.apache.abdera2.activities.extra.Extra;
+import org.apache.abdera2.activities.model.IO;
+import org.apache.abdera2.activities.model.objects.BadgeObject;
+
+/**
+ * Illustrates the extension "binary" objectType... this can be useful, 
+ * for instance, when attaching arbitrary base64 encoded data to an 
+ * object (e.g in the "attachments" property)
+ */
+public class BinaryDataObjectExample {
+
+  public static void main(String... args) throws Exception {
+    
+    IO io = IO.get();
+    Extra.initExtras(io);
+    
+    URL url = BinaryDataObjectExample.class.getResource("/info.png");
+    DataHandler dataHandler = new DataHandler(url); 
+    
+    BinaryObject dataObject = new BinaryObject();
+    dataObject.setContent(dataHandler);
+    
+    BadgeObject badge = new BadgeObject();
+    badge.addAttachment(dataObject);
+    
+    // check the round trip //
+    StringReader sr = new StringReader(io.write(badge));
+    
+    badge = io.readObject(sr);
+    
+    dataObject = (BinaryObject) badge.getAttachments().iterator().next();
+    
+    InputStream in = dataObject.getInputStream(); 
+    byte[] buf = new byte[100];
+    int r = -1;
+    while((r = in.read(buf)) > -1)
+      System.out.write(buf,0,r);
+    
+  }
+  
+}

Propchange: abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message