add an Xmpp Avatar Retriever and a HttpClient avatar retriever.
authorDa Risk <darisk972@gmail.com>
Tue, 05 Oct 2010 23:53:21 +0200
changeset 803 8a3a48e85b63
parent 802 20308ad77837
child 804 eaecdcf75b02
add an Xmpp Avatar Retriever and a HttpClient avatar retriever.
src/com/beem/project/beem/BeemService.java
src/com/beem/project/beem/smack/avatar/AvatarExtension.java
src/com/beem/project/beem/smack/avatar/AvatarMetadataExtension.java
src/com/beem/project/beem/smack/avatar/AvatarProvider.java
src/com/beem/project/beem/smack/avatar/AvatarRetrieverFactory.java
src/com/beem/project/beem/smack/avatar/HttpClientAvatarRetriever.java
src/com/beem/project/beem/smack/avatar/XmppAvatarRetriever.java
--- a/src/com/beem/project/beem/BeemService.java	Wed Sep 22 14:09:54 2010 +0200
+++ b/src/com/beem/project/beem/BeemService.java	Tue Oct 05 23:53:21 2010 +0200
@@ -86,6 +86,7 @@
 import com.beem.project.beem.utils.BeemConnectivity;
 import com.beem.project.beem.utils.Status;
 import com.beem.project.beem.smack.avatar.AvatarMetadataProvider;
+import com.beem.project.beem.smack.avatar.AvatarProvider;
 import com.beem.project.beem.smack.caps.CapsProvider;
 
 /**
@@ -363,6 +364,7 @@
 
 	//PEP avatar
 	pm.addExtensionProvider("metadata", "urn:xmpp:avatar:metadata", new AvatarMetadataProvider());
+	pm.addExtensionProvider("data", "urn:xmpp:avatar:data", new AvatarProvider());
 
 //         PEPProvider pep  = new PEPProvider();
 //         AvatarMetadataProvider avaMeta  = new AvatarMetadataProvider();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/beem/project/beem/smack/avatar/AvatarExtension.java	Tue Oct 05 23:53:21 2010 +0200
@@ -0,0 +1,109 @@
+/*
+    BEEM is a videoconference application on the Android Platform.
+
+    Copyright (C) 2009 by Frederic-Charles Barthelery,
+                          Jean-Manuel Da Silva,
+                          Nikita Kozlov,
+                          Philippe Lago,
+                          Jean Baptiste Vergely,
+                          Vincent Veronis.
+
+    This file is part of BEEM.
+
+    BEEM is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    BEEM is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with BEEM.  If not, see <http://www.gnu.org/licenses/>.
+
+    Please send bug reports with examples or suggestions to
+    contact@beem-project.com or http://dev.beem-project.com/
+
+    Epitech, hereby disclaims all copyright interest in the program "Beem"
+    written by Frederic-Charles Barthelery,
+               Jean-Manuel Da Silva,
+               Nikita Kozlov,
+               Philippe Lago,
+               Jean Baptiste Vergely,
+               Vincent Veronis.
+
+    Nicolas Sadirac, November 26, 2009
+    President of Epitech.
+
+    Flavien Astraud, November 26, 2009
+    Head of the EIP Laboratory.
+
+*/
+package com.beem.project.beem.smack.avatar;
+
+import org.jivesoftware.smack.util.Base64;
+import org.jivesoftware.smack.packet.PacketExtension;
+
+/**
+ * PacketExtension to represent the Avatar data.
+ * XML namespace urn:xmpp:avatar:data
+ *
+ */
+public class AvatarExtension implements PacketExtension {
+
+    private String mData;
+
+    /**
+     * Create an AvatarExtension.
+     */
+    public AvatarExtension(final String base64) {
+	mData = base64;
+    }
+
+    /**
+     * Create an AvatarExtension.
+     */
+    public AvatarExtension(final byte[] data) {
+	mData = Base64.encodeBytes(data);
+    }
+
+    /**
+     * Get the avatar data as a Base64 string.
+     *
+     * @return a base64 string.
+     */
+    public String getBase64() {
+	return mData;
+    }
+
+    /**
+     * Get the avatar data.
+     *
+     * @return the decoded data
+     */
+    public byte[] getData() {
+	return Base64.decode(mData);
+    }
+
+    @Override
+    public String getElementName() {
+	return "data";
+    }
+
+    @Override
+    public String getNamespace() {
+	return "urn:xmpp:avatar:data";
+    }
+
+    @Override
+    public String toXML() {
+	StringBuilder builder = new StringBuilder("<data xmlns=\"");
+	builder.append(getNamespace()).append("\">");
+	builder.append(mData);
+	builder.append("</data>");
+	return builder.toString();
+    }
+
+}
--- a/src/com/beem/project/beem/smack/avatar/AvatarMetadataExtension.java	Wed Sep 22 14:09:54 2010 +0200
+++ b/src/com/beem/project/beem/smack/avatar/AvatarMetadataExtension.java	Tue Oct 05 23:53:21 2010 +0200
@@ -94,7 +94,7 @@
     public String toXML() {
 	StringBuilder builder = new StringBuilder("<metadata xmlns=\"");
 	builder.append(getNamespace()).append("\">");
-	for (Info info : infos) {
+	for (Info info : mInfos) {
 	    builder.append(info.toXML());
 	}
 	builder.append("</metadata>");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/beem/project/beem/smack/avatar/AvatarProvider.java	Tue Oct 05 23:53:21 2010 +0200
@@ -0,0 +1,82 @@
+/*
+    BEEM is a videoconference application on the Android Platform.
+
+    Copyright (C) 2009 by Frederic-Charles Barthelery,
+                          Jean-Manuel Da Silva,
+                          Nikita Kozlov,
+                          Philippe Lago,
+                          Jean Baptiste Vergely,
+                          Vincent Veronis.
+
+    This file is part of BEEM.
+
+    BEEM is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    BEEM is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with BEEM.  If not, see <http://www.gnu.org/licenses/>.
+
+    Please send bug reports with examples or suggestions to
+    contact@beem-project.com or http://dev.beem-project.com/
+
+    Epitech, hereby disclaims all copyright interest in the program "Beem"
+    written by Frederic-Charles Barthelery,
+               Jean-Manuel Da Silva,
+               Nikita Kozlov,
+               Philippe Lago,
+               Jean Baptiste Vergely,
+               Vincent Veronis.
+
+    Nicolas Sadirac, November 26, 2009
+    President of Epitech.
+
+    Flavien Astraud, November 26, 2009
+    Head of the EIP Laboratory.
+
+*/
+package com.beem.project.beem.smack.avatar;
+
+import org.jivesoftware.smack.packet.PacketExtension;
+import org.jivesoftware.smack.provider.PacketExtensionProvider;
+import org.xmlpull.v1.XmlPullParser;
+
+/**
+ * A PacketExtensionProvider to parse the Avatar data.
+ * XML namespace urn:xmpp:avatar:data
+ */
+public class AvatarProvider implements PacketExtensionProvider {
+
+    /**
+     * Creates a new AvatarProvider.
+     * ProviderManager requires that every PacketExtensionProvider has a public, no-argument constructor
+     */
+    public AvatarProvider() {
+    }
+
+    @Override
+    public PacketExtension parseExtension(XmlPullParser parser)
+	throws Exception {
+	AvatarMetadataExtension metadata = new AvatarMetadataExtension();
+	boolean done = false;
+	StringBuilder buffer = new StringBuilder();
+	while (!done) {
+	    int eventType = parser.getEventType();
+	    if (eventType == XmlPullParser.START_TAG) {
+		if ("data".equals(parser.getName())) {
+		    String data = parser.nextText();
+		    AvatarExtension avatar = new AvatarExtension(data);
+		    return avatar;
+		}
+	    }
+	    parser.next();
+	}
+	return null;
+    }
+}
--- a/src/com/beem/project/beem/smack/avatar/AvatarRetrieverFactory.java	Wed Sep 22 14:09:54 2010 +0200
+++ b/src/com/beem/project/beem/smack/avatar/AvatarRetrieverFactory.java	Tue Oct 05 23:53:21 2010 +0200
@@ -45,6 +45,9 @@
 
 import com.beem.project.beem.smack.avatar.AvatarMetadataExtension.Info;
 import org.jivesoftware.smack.Connection;
+// API level 8
+//import android.net.http.AndroidHttpClient;
+import org.apache.http.client.HttpClient;
 
 /**
  * A factory for AvatarRetriever.
@@ -68,8 +71,10 @@
     public static AvatarRetriever getRetriever(Connection con, String from, Info info) {
 	String url = info.getUrl();
 	if (url != null) {
-	    return new HttpAvatarRetriever(url);
+	    // return new HttpAvatarRetriever(url);
+	    // HttpClient client = AndroidHttpClient.newInstance("Beem");
+	    return new HttpClientAvatarRetriever(url);
 	}
-	return null;
+	return new XmppAvatarRetriever(con, from, info.getId());
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/beem/project/beem/smack/avatar/HttpClientAvatarRetriever.java	Tue Oct 05 23:53:21 2010 +0200
@@ -0,0 +1,113 @@
+/*
+    BEEM is a videoconference application on the Android Platform.
+
+    Copyright (C) 2009 by Frederic-Charles Barthelery,
+                          Jean-Manuel Da Silva,
+                          Nikita Kozlov,
+                          Philippe Lago,
+                          Jean Baptiste Vergely,
+                          Vincent Veronis.
+
+    This file is part of BEEM.
+
+    BEEM is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    BEEM is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with BEEM.  If not, see <http://www.gnu.org/licenses/>.
+
+    Please send bug reports with examples or suggestions to
+    contact@beem-project.com or http://dev.beem-project.com/
+
+    Epitech, hereby disclaims all copyright interest in the program "Beem"
+    written by Frederic-Charles Barthelery,
+               Jean-Manuel Da Silva,
+               Nikita Kozlov,
+               Philippe Lago,
+               Jean Baptiste Vergely,
+               Vincent Veronis.
+
+    Nicolas Sadirac, November 26, 2009
+    President of Epitech.
+
+    Flavien Astraud, November 26, 2009
+    Head of the EIP Laboratory.
+
+*/
+package com.beem.project.beem.smack.avatar;
+
+import java.io.InputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.URL;
+import org.apache.http.client.HttpClient;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpEntity;
+
+/**
+ * An AvatarRetriever which retrieve the avatar over HTTP using the Apache HttpClient.
+ */
+public  class HttpClientAvatarRetriever implements AvatarRetriever {
+
+    private String mUrl;
+    private HttpClient mClient;
+
+    /**
+     * Create a HttpAvatarRetriever.
+     *
+     * @param client the custom HttpClient to use to downlowad
+     * @param url the url of the avatar to download.
+     */
+    public HttpClientAvatarRetriever(final HttpClient client, final String url) {
+	mUrl = url;
+	mClient = client;
+    }
+
+    /**
+     * Create a HttpAvatarRetriever.
+     *
+     * @param url the url of the avatar to download.
+     */
+    public HttpClientAvatarRetriever(final String url) {
+	mUrl= url;
+	mClient = new DefaultHttpClient();
+    }
+
+    @Override
+    public byte[] getAvatar() throws IOException {
+	HttpUriRequest request;
+	try {
+	    request = new HttpGet(mUrl);
+	} catch(IllegalArgumentException e) {
+	    IOException ioe = new IOException("Invalid url " +mUrl);
+	    ioe.initCause(e);
+	    throw ioe;
+	}
+	HttpResponse response = mClient.execute(request);
+	HttpEntity entity = response.getEntity();
+	InputStream in = entity.getContent();
+	ByteArrayOutputStream os = new ByteArrayOutputStream();
+	try {
+	    byte[] data = new byte[1024];
+	    int nbread;
+	    while ((nbread = in.read(data)) != -1) {
+		os.write(data, 0, nbread);
+	    }
+	} finally {
+	    in.close();
+	    os.close();
+	}
+	return os.toByteArray();
+    }
+
+}
--- a/src/com/beem/project/beem/smack/avatar/XmppAvatarRetriever.java	Wed Sep 22 14:09:54 2010 +0200
+++ b/src/com/beem/project/beem/smack/avatar/XmppAvatarRetriever.java	Tue Oct 05 23:53:21 2010 +0200
@@ -51,6 +51,7 @@
 import org.jivesoftware.smackx.pubsub.Node;
 import org.jivesoftware.smackx.pubsub.LeafNode;
 import org.jivesoftware.smackx.pubsub.Item;
+import org.jivesoftware.smackx.pubsub.PayloadItem;
 
 /**
  * An AvatarRetriever which retrieve the avatar over the XMPP connection.
@@ -76,6 +77,9 @@
 		LeafNode lnode = (LeafNode) node;
 		List<Item> items = lnode.getItems(Arrays.asList(mId));
 		// TODO the rest ^^
+		PayloadItem<AvatarExtension> item = (PayloadItem<AvatarExtension>) items.get(0);
+		AvatarExtension avatar = item.getPayload();
+		return avatar.getData();
 	    }
 	} catch (XMPPException e) {
 	    return null;