You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wookie.apache.org by kr...@apache.org on 2012/03/08 10:03:49 UTC
svn commit: r1298307 - in /incubator/wookie/trunk/connector/java:
src-test/org/apache/wookie/tests/connector/framework/impl/
src/org/apache/wookie/connector/framework/
Author: krispopat
Date: Thu Mar 8 09:03:48 2012
New Revision: 1298307
URL: http://svn.apache.org/viewvc?rev=1298307&view=rev
Log:
Updated Java connector framework to reflect xml changes in the widgets/xml as described in WOOKIE-32. Also updated test
Modified:
incubator/wookie/trunk/connector/java/src-test/org/apache/wookie/tests/connector/framework/impl/TestWookieConnectorService.java
incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/AbstractWookieConnectorService.java
incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/IWookieConnectorService.java
incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/Widget.java
incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/WookieConnectorService.java
Modified: incubator/wookie/trunk/connector/java/src-test/org/apache/wookie/tests/connector/framework/impl/TestWookieConnectorService.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/java/src-test/org/apache/wookie/tests/connector/framework/impl/TestWookieConnectorService.java?rev=1298307&r1=1298306&r2=1298307&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/java/src-test/org/apache/wookie/tests/connector/framework/impl/TestWookieConnectorService.java (original)
+++ incubator/wookie/trunk/connector/java/src-test/org/apache/wookie/tests/connector/framework/impl/TestWookieConnectorService.java Thu Mar 8 09:03:48 2012
@@ -67,7 +67,7 @@ public class TestWookieConnectorService
if (instance == null) {
try {
new URL("http://localhost:8080/wookie").openStream();
- instance = new TestWookieConnectorService("http://localhost:8080/wookie", "TEST", "myshareddata");
+ instance = new TestWookieConnectorService("http://localhost:8080/wookie", "57x7addiQCmrGNUvJ7avvyEoTIM.eq.", "myshareddata");
} catch (ConnectException e) {
// assume localhost is not running so run against bombax
instance = new TestWookieConnectorService("http://bombax.oucs.ox.ac.uk:8888/wookie", "TEST", "myshareddata");
@@ -76,4 +76,5 @@ public class TestWookieConnectorService
return instance;
}
+
}
Modified: incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/AbstractWookieConnectorService.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/AbstractWookieConnectorService.java?rev=1298307&r1=1298306&r2=1298307&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/AbstractWookieConnectorService.java (original)
+++ incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/AbstractWookieConnectorService.java Thu Mar 8 09:03:48 2012
@@ -36,314 +36,389 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
+public abstract class AbstractWookieConnectorService implements IWookieConnectorService {
+
+ private static final Logger logger = LoggerFactory.getLogger(AbstractWookieConnectorService.class);
+
+ WookieServerConnection conn;
+
+ WidgetInstances instances = new WidgetInstances();
-public abstract class AbstractWookieConnectorService implements
- IWookieConnectorService {
- private static final Logger logger = LoggerFactory.getLogger(AbstractWookieConnectorService.class);
- WookieServerConnection conn;
- WidgetInstances instances = new WidgetInstances();
-
- public AbstractWookieConnectorService(String url, String apiKey, String sharedDataKey) throws WookieConnectorException {
- setConnection(new WookieServerConnection(url, apiKey, sharedDataKey));
- }
-
- /**
- * Creates a WookieConnectorService that has not yet been initialised to connect
- * to a specific server.
- */
- protected AbstractWookieConnectorService() {
- super();
- }
-
- public void setConnection(WookieServerConnection newConn) {
- logger.debug("Setting wookie connection to " + conn);
- this.conn = newConn;
- }
-
- public WookieServerConnection getConnection() {
- return this.conn;
- }
-
- /**
- * Get or create an instance of a widget.
- *
- * @param widget
- * @return the ID of the widget instance
- * @throws IOException
- * @throws SimalRepositoryException
- *
- */
- public WidgetInstance getOrCreateInstance(Widget widget) throws IOException,
- WookieConnectorException {
- return getOrCreateInstance(widget.identifier);
- }
-
- public void setPropertyForInstance(WidgetInstance instance, String propertyType, String fName, String fValue)
- throws WookieConnectorException{
- String queryString;
- try {
- if(!propertyType.equals("setpublicproperty") && !propertyType.equals("setpersonalproperty")){
- logger.error("Incorrect requestId parameter. Must be either 'setpublicproperty' or 'setprivateproperty'");
- throw new Exception();
- }
- queryString = new String("?requestid=" + propertyType + "&api_key=");
- queryString+=(URLEncoder.encode(getConnection().getApiKey(), "UTF-8"));
- queryString+=("&shareddatakey=");
- queryString+=(URLEncoder.encode(getConnection().getSharedDataKey(), "UTF-8"));
- queryString+=("&userid=");
- queryString+=(URLEncoder.encode(getCurrentUser().getLoginName(), "UTF-8"));
- queryString+=("&widgetid=");
- queryString+=(URLEncoder.encode(instance.getId(), "UTF-8"));
- queryString+=("&propertyname=");
- queryString+=(URLEncoder.encode(fName, "UTF-8"));
- queryString+=("&propertyvalue=");
- queryString+=(URLEncoder.encode(fValue, "UTF-8"));
- }
- catch (UnsupportedEncodingException e) {
- throw new WookieConnectorException("Must support UTF-8 encoding", e);
- }
- catch (Exception e) {
- throw new WookieConnectorException("Cannot set property type:" + fName + " using requestId " + propertyType, e);
- }
- URL url = null;
- try {
- url = new URL(conn.getURL() + "/WidgetServiceServlet" + queryString);
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- if (conn.getResponseCode() > 200) throw new IOException(conn.getResponseMessage());
- }
- catch (Exception e) {
- throw new WookieConnectorException("Unable to set property ", e);
- }
- }
-
- public WidgetInstance getOrCreateInstance(String guid) throws IOException,
- WookieConnectorException {
- URL url;
- WidgetInstance instance;
- try {
- StringBuilder postdata = new StringBuilder("api_key=");
- postdata.append(URLEncoder.encode(getConnection().getApiKey(), "UTF-8"));
- postdata.append("&shareddatakey=");
- postdata.append(URLEncoder.encode(getConnection().getSharedDataKey(), "UTF-8"));
- postdata.append("&userid=");
- postdata.append(URLEncoder.encode(getCurrentUser().getLoginName(), "UTF-8"));
- postdata.append("&widgetid=");
- postdata.append(URLEncoder.encode(guid, "UTF-8"));
-
- logger.debug("Makeing Wookie REST query using: " + postdata);
-
- url = new URL(conn.getURL() + "/widgetinstances");
- URLConnection urlConn = url.openConnection();
- urlConn.setDoOutput(true);
- OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
- wr.write(postdata.toString());
- wr.flush();
-
- InputStream is = urlConn.getInputStream();
-
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- instance = parseInstance(guid, db.parse(is));
-
- instances.put(instance);
-
- wr.close();
- is.close();
-
- addParticipant(instance, getCurrentUser());
- } catch (MalformedURLException e) {
- throw new RuntimeException("URL for supplied Wookie Server is malformed",
- e);
- } catch (ParserConfigurationException e) {
- throw new RuntimeException("Unable to configure XML parser", e);
- } catch (SAXException e) {
- throw new RuntimeException("Problem parsing XML from Wookie Server", e);
- }
-
- return instance;
- }
-
- /**
- * Parse an XML document returned from the Wookie server that describes a widget instance.
- *
- * @param widgetId the identifier of the widget this document represents
- * @param xml description of the instance as returned by the widget server when the widget was instantiated.
- *
- * @return the identifier for this instance
- */
- public WidgetInstance parseInstance(String widgetId, Document xml) {
- Element rootEl = xml.getDocumentElement();
- String url = rootEl.getElementsByTagName("url").item(0).getTextContent();
- String title = rootEl.getElementsByTagName("title").item(0).getTextContent();
- String height = rootEl.getElementsByTagName("height").item(0).getTextContent();
- String width = rootEl.getElementsByTagName("width").item(0).getTextContent();
- WidgetInstance instance = new WidgetInstance(url, widgetId, title, height, width);
- return instance;
- }
-
- /**
- * @refactor At time of writing the REST API for adding a participant is broken so we are
- * using the non-REST approach. The code for REST API is commented out and should be used
- * in the future.
- */
- public void addParticipant(WidgetInstance widget, User user) throws WookieConnectorException {
- StringBuilder postdata;
- try {
- postdata = new StringBuilder("api_key=");
- postdata.append(URLEncoder.encode(getConnection().getApiKey(), "UTF-8"));
- postdata.append("&shareddatakey=");
- postdata.append(URLEncoder.encode(getConnection().getSharedDataKey(), "UTF-8"));
- postdata.append("&userid=");
- postdata.append(URLEncoder.encode(getCurrentUser().getLoginName(), "UTF-8"));
- postdata.append("&widgetid=");
- postdata.append(URLEncoder.encode(widget.getId(), "UTF-8"));
- postdata.append("&participant_id=");
- postdata.append(URLEncoder.encode(user.getLoginName(), "UTF-8"));
- postdata.append("&participant_display_name=");
- postdata.append(URLEncoder.encode(user.getScreenName(), "UTF-8"));
- postdata.append("&participant_thumbnail_url=");
- postdata.append(URLEncoder.encode(user.getThumbnailUrl(), "UTF-8"));
- } catch (UnsupportedEncodingException e) {
- throw new WookieConnectorException("Must support UTF-8 encoding", e);
- }
-
- URL url = null;
- try {
- url = new URL(conn.getURL() + "/participants");
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setDoOutput(true);
- OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
- wr.write(postdata.toString());
- wr.flush();
- if (conn.getResponseCode() > 201) throw new IOException(conn.getResponseMessage());
- } catch (MalformedURLException e) {
- throw new WookieConnectorException("Participants rest URL is incorrect: " + url, e);
- } catch (IOException e) {
- StringBuilder sb = new StringBuilder("Problem adding a participant. ");
- sb.append("URL: ");
- sb.append(url);
- sb.append(" data: ");
- sb.append(postdata);
- throw new WookieConnectorException(sb.toString(), e);
- }
- }
-
- /**
- * Get a set of all the available widgets in the server. If there is an error
- * communicating with the server return an empty set, or the set received so
- * far in order to allow the application to proceed. The application should
- * display an appropriate message in this case.
- *
- * @return
- * @throws SimalException
- */
- public HashMap<String, Widget> getAvailableWidgets()
- throws WookieConnectorException {
- HashMap<String, Widget> widgets = new HashMap<String, Widget>();
- try {
- InputStream is = new URL(conn.getURL() + "/widgets?all=true").openStream();
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document widgetsDoc = db.parse(is);
-
- Element root = widgetsDoc.getDocumentElement();
- NodeList widgetList = root.getElementsByTagName("widget");
- for (int idx = 0; idx < widgetList.getLength(); idx = idx + 1) {
- Element widgetEl = (Element) widgetList.item(idx);
- String id = widgetEl.getAttribute("identifier");
- if (widgets.containsKey(id)) {
- break;
- }
- String title = widgetEl.getElementsByTagName("title").item(0).getTextContent();
- String description = widgetEl.getElementsByTagName("description").item(0).getTextContent();
- Node iconEl = widgetEl.getElementsByTagName("icon").item(0);
- URL iconURL;
- if (iconEl != null) {
- iconURL = new URL(iconEl.getTextContent());
- } else {
- iconURL = new URL("http://www.oss-watch.ac.uk/images/logo2.gif");
- }
- Widget widget = new Widget(id, title, description, iconURL);
- widgets.put(id, widget);
- }
- } catch (ParserConfigurationException e) {
- throw new WookieConnectorException("Unable to create XML parser", e);
- } catch (MalformedURLException e) {
- throw new WookieConnectorException("URL for Wookie is malformed", e);
- } catch (IOException e) {
- // return an empty set, or the set received so far in order to allow
- // the application to proceed. The application should display an
- // appropriate message in this case.
- return widgets;
- } catch (SAXException e) {
- throw new WookieConnectorException(
- "Unable to parse the response from Wookie", e);
- }
- return widgets;
- }
-
- public WidgetInstances getInstances() {
- return instances;
- }
-
- /**
- * Get the array of users for a widget instance
- * @param instance
- * @return an array of users
- * @throws WookieConnectorException
- */
- public User[] getUsers(WidgetInstance instance) throws WookieConnectorException{
- String queryString;
- try {
- queryString = new String("?api_key=");
- queryString+=(URLEncoder.encode(getConnection().getApiKey(), "UTF-8"));
- queryString+=("&shareddatakey=");
- queryString+=(URLEncoder.encode(getConnection().getSharedDataKey(), "UTF-8"));
- queryString+=("&userid=");
- queryString+=(URLEncoder.encode(getCurrentUser().getLoginName(), "UTF-8"));
- queryString+=("&widgetid=");
- queryString+=(URLEncoder.encode(instance.getId(), "UTF-8"));
- } catch (UnsupportedEncodingException e) {
- throw new WookieConnectorException("Must support UTF-8 encoding", e);
- }
-
- URL url = null;
- try {
- url = new URL(conn.getURL() + "/participants"+queryString);
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- InputStream is = conn.getInputStream();
- if (conn.getResponseCode() > 200) throw new IOException(conn.getResponseMessage());
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document widgetsDoc = db.parse(is);
- Element root = widgetsDoc.getDocumentElement();
- NodeList participantsList = root.getElementsByTagName("participant");
- if (participantsList == null || participantsList.getLength() == 0) return new User[0];
- User[] users = new User[participantsList.getLength()];
- for (int idx = 0; idx < participantsList.getLength(); idx = idx + 1) {
- Element participantEl = (Element) participantsList.item(idx);
- String id = participantEl.getAttribute("id");
- String name = participantEl.getAttribute("display_name");
- String thumbnail = participantEl.getAttribute("thumbnail_url");
- User user = new User(id,name, thumbnail);
- users[idx] = user;
- }
- return users;
- } catch (MalformedURLException e) {
- throw new WookieConnectorException("Participants rest URL is incorrect: " + url, e);
- } catch (IOException e) {
- StringBuilder sb = new StringBuilder("Problem getting participants. ");
- sb.append("URL: ");
- sb.append(url);
- sb.append(" data: ");
- sb.append(queryString);
- throw new WookieConnectorException(sb.toString(), e);
- } catch (ParserConfigurationException e) {
- throw new WookieConnectorException("Problem parsing data: " + url, e);
- } catch (SAXException e) {
- throw new WookieConnectorException("Problem parsing data: " + url, e);
- }
- }
+ public AbstractWookieConnectorService(String url, String apiKey,
+ String sharedDataKey) throws WookieConnectorException {
+ setConnection(new WookieServerConnection(url, apiKey, sharedDataKey));
+ }
+
+
+
+
+ /**
+ * Creates a WookieConnectorService that has not yet been initialised to
+ * connect to a specific server.
+ */
+ protected AbstractWookieConnectorService() {
+ super();
+ }
+
+
+ public void setConnection(WookieServerConnection newConn) {
+ logger.debug("Setting wookie connection to " + conn);
+ this.conn = newConn;
+ }
+
+
+ public WookieServerConnection getConnection() {
+ return this.conn;
+ }
+
+ /**
+ * Get or create an instance of a widget.
+ *
+ * @param widget
+ * @return the ID of the widget instance
+ * @throws IOException
+ * @throws SimalRepositoryException
+ *
+ */
+ public WidgetInstance getOrCreateInstance(Widget widget) throws IOException, WookieConnectorException {
+ return getOrCreateInstance(widget.identifier);
+ }
+
+ public void setPropertyForInstance(WidgetInstance instance,
+ String propertyType, String fName,
+ String fValue) throws WookieConnectorException {
+ String queryString;
+ try {
+ if (!propertyType.equals("setpublicproperty") && !propertyType.equals("setpersonalproperty")) {
+ logger.error("Incorrect requestId parameter. Must be either 'setpublicproperty' or 'setprivateproperty'");
+ throw new Exception();
+ }
+ queryString = new String("?requestid=" + propertyType + "&api_key=");
+ queryString += (URLEncoder.encode(getConnection().getApiKey(),"UTF-8"));
+ queryString += ("&shareddatakey=");
+ queryString += (URLEncoder.encode(getConnection().getSharedDataKey(), "UTF-8"));
+ queryString += ("&userid=");
+ queryString += (URLEncoder.encode(getCurrentUser().getLoginName(), "UTF-8"));
+ queryString += ("&widgetid=");
+ queryString += (URLEncoder.encode(instance.getId(), "UTF-8"));
+ queryString += ("&propertyname=");
+ queryString += (URLEncoder.encode(fName, "UTF-8"));
+ queryString += ("&propertyvalue=");
+ queryString += (URLEncoder.encode(fValue, "UTF-8"));
+ }
+ catch (UnsupportedEncodingException e) {
+ throw new WookieConnectorException("Must support UTF-8 encoding", e);
+ }
+ catch (Exception e) {
+ throw new WookieConnectorException("Cannot set property type:" + fName + " using requestId " + propertyType, e);
+ }
+ URL url = null;
+ try {
+ url = new URL(conn.getURL() + "/WidgetServiceServlet" + queryString);
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ if (conn.getResponseCode() > 200) {
+ throw new IOException(conn.getResponseMessage());
+ }
+ }
+ catch (Exception e) {
+ throw new WookieConnectorException("Unable to set property ", e);
+ }
+ }
+
+ public WidgetInstance getOrCreateInstance(String guid) throws IOException, WookieConnectorException {
+ URL url;
+ WidgetInstance instance;
+ try {
+ StringBuilder postdata = new StringBuilder("api_key=");
+ postdata.append(URLEncoder.encode(getConnection().getApiKey(), "UTF-8"));
+ postdata.append("&shareddatakey=");
+ postdata.append(URLEncoder.encode(getConnection().getSharedDataKey(), "UTF-8"));
+ postdata.append("&userid=");
+ postdata.append(URLEncoder.encode(getCurrentUser().getLoginName(), "UTF-8"));
+ postdata.append("&widgetid=");
+ postdata.append(URLEncoder.encode(guid, "UTF-8"));
+
+ logger.debug("Makeing Wookie REST query using: " + postdata);
+
+ url = new URL(conn.getURL() + "/widgetinstances");
+ URLConnection urlConn = url.openConnection();
+ urlConn.setDoOutput(true);
+ OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
+ wr.write(postdata.toString());
+
+ wr.flush();
+
+ instance = parseInstance(guid, getURLDoc(url));
+ instances.put(instance);
+
+ wr.close();
+
+ addParticipant(instance, getCurrentUser());
+
+ }
+ catch (MalformedURLException e) {
+ throw new RuntimeException(
+ "URL for supplied Wookie Server is malformed", e);
+ }
+ catch (ParserConfigurationException e) {
+ throw new RuntimeException("Unable to configure XML parser", e);
+ }
+ catch (SAXException e) {
+ throw new RuntimeException(
+ "Problem parsing XML from Wookie Server", e);
+ }
+
+ return instance;
+ }
+
+ /**
+ * Parse an XML document returned from the Wookie server that describes a
+ * widget instance.
+ *
+ * @param widgetId
+ * the identifier of the widget this document represents
+ * @param xml
+ * description of the instance as returned by the widget server
+ * when the widget was instantiated.
+ *
+ * @return the identifier for this instance
+ */
+ public WidgetInstance parseInstance(String widgetId, Document xml) {
+ Element rootEl = xml.getDocumentElement();
+ String url = getNodeTextContent(rootEl, "url");
+ String title = getNodeTextContent(rootEl, "title");
+ String height = getNodeTextContent(rootEl, "height");
+ String width = getNodeTextContent(rootEl, "width");
+ WidgetInstance instance = new WidgetInstance(url, widgetId, title, height, width);
+ return instance;
+ }
+
+ /**
+ * @refactor At time of writing the REST API for adding a participant is
+ * broken so we are using the non-REST approach. The code for REST
+ * API is commented out and should be used in the future.
+ */
+ public void addParticipant(WidgetInstance widget, User user)
+ throws WookieConnectorException {
+ StringBuilder postdata;
+ try {
+ postdata = new StringBuilder("api_key=");
+ postdata.append(URLEncoder.encode(getConnection().getApiKey(), "UTF-8"));
+ postdata.append("&shareddatakey=");
+ postdata.append(URLEncoder.encode(getConnection().getSharedDataKey(), "UTF-8"));
+ postdata.append("&userid=");
+ postdata.append(URLEncoder.encode(getCurrentUser().getLoginName(), "UTF-8"));
+ postdata.append("&widgetid=");
+ postdata.append(URLEncoder.encode(widget.getId(), "UTF-8"));
+ postdata.append("&participant_id=");
+ postdata.append(URLEncoder.encode(user.getLoginName(), "UTF-8"));
+ postdata.append("&participant_display_name=");
+ postdata.append(URLEncoder.encode(user.getScreenName(), "UTF-8"));
+ postdata.append("&participant_thumbnail_url=");
+ postdata.append(URLEncoder.encode(user.getThumbnailUrl(), "UTF-8"));
+ }
+ catch (UnsupportedEncodingException e) {
+ throw new WookieConnectorException("Must support UTF-8 encoding", e);
+ }
+
+ URL url = null;
+ try {
+ url = new URL(conn.getURL() + "/participants");
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setDoOutput(true);
+ OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
+ wr.write(postdata.toString());
+ wr.flush();
+ if (conn.getResponseCode() > 201) {
+ throw new IOException(conn.getResponseMessage());
+ }
+ }
+ catch (MalformedURLException e) {
+ throw new WookieConnectorException( "Participants rest URL is incorrect: " + url, e);
+ }
+ catch (IOException e) {
+ StringBuilder sb = new StringBuilder( "Problem adding a participant. ");
+ sb.append("URL: ");
+ sb.append(url);
+ sb.append(" data: ");
+ sb.append(postdata);
+ throw new WookieConnectorException(sb.toString(), e);
+ }
+ }
+
+
+ /**
+ * Get a set of all the available widgets in the server. If there is an
+ * error communicating with the server return an empty set, or the set
+ * received so far in order to allow the application to proceed. The
+ * application should display an appropriate message in this case.
+ *
+ * @return
+ * @throws WookieConnectorException
+ */
+ public HashMap<String, Widget> getAvailableWidgets() throws WookieConnectorException {
+
+ HashMap<String, Widget> widgets = new HashMap<String, Widget>();
+
+ try {
+ URL url = new URL(conn.getURL() + "/widgets?all=true");
+
+ Document widgetsDoc = getURLDoc(url);
+
+ Element root = widgetsDoc.getDocumentElement();
+ NodeList widgetList = root.getElementsByTagName("widget");
+ for (int idx = 0; idx < widgetList.getLength(); idx = idx + 1) {
+ Element widgetEl = (Element) widgetList.item(idx);
+ String id = widgetEl.getAttribute("id");
+ String width = widgetEl.getAttribute("width");
+ String height = widgetEl.getAttribute("height");
+ String version = widgetEl.getAttribute("version");
+ if (widgets.containsKey(id)) {
+ break;
+ }
+ String name = getNodeTextContent(widgetEl, "name" );
+ String description = getNodeTextContent(widgetEl, "description" );
+ String license = getNodeTextContent(widgetEl, "license" );
+ String author = getNodeTextContent(widgetEl, "author" );
+ Element iconEl = (Element) widgetEl.getElementsByTagName("icon").item(0);
+ URL iconURL;
+ if (iconEl != null) {
+ iconURL = new URL(iconEl.getAttribute("src"));
+ }
+ else {
+ iconURL = new URL("http://www.oss-watch.ac.uk/images/logo2.gif");
+ }
+
+ Widget widget = new Widget(id, name,
+ description, iconURL,
+ width, height,
+ version, author,
+ license);
+
+ widgets.put(id, widget);
+ }
+ }
+ catch (ParserConfigurationException e) {
+ throw new WookieConnectorException("Unable to create XML parser", e);
+ }
+ catch (MalformedURLException e) {
+ throw new WookieConnectorException("URL for Wookie is malformed", e);
+ }
+ catch (IOException e) {
+ // return an empty set, or the set received so far in order to allow
+ // the application to proceed. The application should display an
+ // appropriate message in this case.
+ return widgets;
+ }
+ catch (SAXException e) {
+ throw new WookieConnectorException(
+ "Unable to parse the response from Wookie", e);
+ }
+ return widgets;
+ }
+
+
+
+
+ public WidgetInstances getInstances() {
+ return instances;
+ }
+
+ /**
+ * Get the array of users for a widget instance
+ *
+ * @param instance
+ * @return an array of users
+ * @throws WookieConnectorException
+ */
+ public User[] getUsers(WidgetInstance instance) throws WookieConnectorException {
+ String queryString;
+ try {
+ queryString = new String("?api_key=");
+ queryString += (URLEncoder.encode(getConnection().getApiKey(), "UTF-8"));
+ queryString += ("&shareddatakey=");
+ queryString += (URLEncoder.encode(getConnection().getSharedDataKey(), "UTF-8"));
+ queryString += ("&userid=");
+ queryString += (URLEncoder.encode(getCurrentUser().getLoginName(), "UTF-8"));
+ queryString += ("&widgetid=");
+ queryString += (URLEncoder.encode(instance.getId(), "UTF-8"));
+ }
+ catch (UnsupportedEncodingException e) {
+ throw new WookieConnectorException("Must support UTF-8 encoding", e);
+ }
+
+ URL url = null;
+ try {
+ url = new URL(conn.getURL() + "/participants" + queryString);
+ Document usersDoc = getURLDoc(url);
+ Element root = usersDoc.getDocumentElement();
+ NodeList participantsList = root.getElementsByTagName("participant");
+ if (participantsList == null || participantsList.getLength() == 0) {
+ return new User[0];
+ }
+ User[] users = new User[participantsList.getLength()];
+ for (int idx = 0; idx < participantsList.getLength(); idx = idx + 1) {
+ Element participantEl = (Element) participantsList.item(idx);
+ String id = participantEl.getAttribute("id");
+ String name = participantEl.getAttribute("display_name");
+ String thumbnail = participantEl.getAttribute("thumbnail_url");
+ User user = new User(id, name, thumbnail);
+ users[idx] = user;
+ }
+ return users;
+ }
+ catch (MalformedURLException e) {
+ throw new WookieConnectorException(
+ "Participants rest URL is incorrect: " + url, e);
+ }
+ catch (IOException e) {
+ StringBuilder sb = new StringBuilder("Problem getting participants. ");
+ sb.append("URL: ");
+ sb.append(url);
+ sb.append(" data: ");
+ sb.append(queryString);
+ throw new WookieConnectorException(sb.toString(), e);
+ }
+ catch (ParserConfigurationException e) {
+ throw new WookieConnectorException("Problem parsing data: " + url, e);
+ }
+ catch (SAXException e) {
+ throw new WookieConnectorException("Problem parsing data: " + url, e);
+ }
+ }
+
+
+ private String getNodeTextContent(Element e, String subElementName ) {
+ NodeList nl = e.getElementsByTagName("description");
+ if ( nl.getLength() > 0 ) {
+ Node n = nl.item(0);
+ if ( n != null ) {
+ return n.getTextContent();
+ }
+ }
+ return "";
+ }
+
+ /**
+ * Gets the input stream and parses it to a document
+ *
+ * @param url
+ * @return Document
+ * @throws IOException
+ * @throws ParserConfigurationException
+ * @throws SAXException
+ */
+ private Document getURLDoc(URL url) throws IOException, ParserConfigurationException, SAXException {
+ HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
+ InputStream is = httpConnection.getInputStream();
+ if (httpConnection.getResponseCode() > 200) {
+ throw new IOException(httpConnection.getResponseMessage());
+ }
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ DocumentBuilder docb = dbf.newDocumentBuilder();
+ Document parsedDoc = docb.parse(is);
+ is.close();
+ return parsedDoc;
+ }
-
}
Modified: incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/IWookieConnectorService.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/IWookieConnectorService.java?rev=1298307&r1=1298306&r2=1298307&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/IWookieConnectorService.java (original)
+++ incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/IWookieConnectorService.java Thu Mar 8 09:03:48 2012
@@ -114,6 +114,9 @@ public interface IWookieConnectorService
*/
public HashMap<String, Widget> getAvailableWidgets()
throws WookieConnectorException;
+
+
+
/**
* Get all the instances of widgets that are currently managed by this service.
*
Modified: incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/Widget.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/Widget.java?rev=1298307&r1=1298306&r2=1298307&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/Widget.java (original)
+++ incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/Widget.java Thu Mar 8 09:03:48 2012
@@ -22,18 +22,32 @@ import java.net.URL;
* A client side representation of a widget.
*
* @refactor this duplicates data stored in the Widget bean on the server side.
+ *
+ * @refactor additional properties
*/
public class Widget {
+
String identifier;
- String title;
+ String name;
String description;
URL icon;
+ String width;
+ String height;
+ String version;
+ String author;
+ String license;
- public Widget(String identifier, String title, String description, URL icon) {
+ public Widget(String identifier, String name, String description, URL icon,
+ String width, String height, String version, String author, String license) {
this.identifier = identifier;
- this.title = title;
+ this.name = name;
this.description = description;
this.icon = icon;
+ this.width = width;
+ this.height = height;
+ this.version = version;
+ this.author = author;
+ this.license = license;
}
/**
@@ -49,8 +63,8 @@ public class Widget {
* Get the human readable title of this widget.
* @return
*/
- public String getTitle() {
- return title;
+ public String getName() {
+ return name;
}
/**
@@ -69,4 +83,47 @@ public class Widget {
public String getDescription() {
return description;
}
+
+public String getWidth() {
+ return width;
+}
+
+public void setWidth(String width) {
+ this.width = width;
+}
+
+public String getHeight() {
+ return height;
+}
+
+public void setHeight(String height) {
+ this.height = height;
+}
+
+public String getVersion() {
+ return version;
+}
+
+public void setVersion(String version) {
+ this.version = version;
+}
+
+public String getAuthor() {
+ return author;
+}
+
+public void setAuthor(String author) {
+ this.author = author;
+}
+
+public String getLicense() {
+ return license;
+}
+
+public void setLicense(String license) {
+ this.license = license;
+}
+
+
+
}
Modified: incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/WookieConnectorService.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/WookieConnectorService.java?rev=1298307&r1=1298306&r2=1298307&view=diff
==============================================================================
--- incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/WookieConnectorService.java (original)
+++ incubator/wookie/trunk/connector/java/src/org/apache/wookie/connector/framework/WookieConnectorService.java Thu Mar 8 09:03:48 2012
@@ -13,6 +13,8 @@
*/
package org.apache.wookie.connector.framework;
+import java.util.HashMap;
+
/**
* An implementation of the WookieConnectorService for use by Wookie itself.
@@ -64,6 +66,7 @@ public class WookieConnectorService exte
private User getSecondTestUser() {
return new User("testuser2", "Second Test User");
}
+
}