Author: scottbw
Date: Fri Mar 12 11:00:25 2010
New Revision: 922213
URL: http://svn.apache.org/viewvc?rev=922213&view=rev
Log:
Made a number of improvements to Shindig integration: (1) moved gadget-widget
conversion into a GadgetAdapter class that implements W3CWidget and takes a
JSON object as its constructor, (2) gadget import generates a W3CWidget model
object and doesn't do any persistence itself, this is left to the WidgetFactory
class as is usual for other widget imports, (3) Doesn't import duplicate
gadgets, (4) updated unit tests. See WOOKIE-52 for details.
Added:
incubator/wookie/trunk/src/org/apache/wookie/util/gadgets/GadgetAdapter.java
Modified:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/integration/GadgetUtilsTest.java
incubator/wookie/trunk/src/org/apache/wookie/WidgetAdminServlet.java
incubator/wookie/trunk/src/org/apache/wookie/messages.properties
incubator/wookie/trunk/src/org/apache/wookie/util/gadgets/GadgetUtils.java
Modified:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/integration/GadgetUtilsTest.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/integration/GadgetUtilsTest.java?rev=922213&r1=922212&r2=922213&view=diff
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/integration/GadgetUtilsTest.java
(original)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/integration/GadgetUtilsTest.java
Fri Mar 12 11:00:25 2010
@@ -19,15 +19,13 @@ import static org.junit.Assert.*;
import org.apache.wookie.beans.Widget;
import org.apache.wookie.tests.helpers.MockHttpServletRequest;
import org.apache.wookie.util.gadgets.GadgetUtils;
+import org.apache.wookie.w3c.W3CWidget;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
-import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
-import junit.framework.TestCase;
-
/**
* Tests for Gadget utility methods
*
@@ -46,6 +44,8 @@ public class GadgetUtilsTest {
private static String TEST_SERVICE_URL_VALID;
private static String TEST_SERVICE_URL_INVALID;
private static String TEST_GADGET_URL_VALID;
+ private static String TEST_GADGET_URL_VALID2;
+ private static String TEST_GADGET_URL_VALID3;
private static String TEST_GADGET_URL_INVALID;
private static String TEST_GADGET_URL_MALFORMED;
@@ -60,9 +60,13 @@ public class GadgetUtilsTest {
TEST_METADATA_INVALID =
"{\"gadgets\"[{\"showInDirectory\":false,\"width\":0,\"title\":\"hello world
example\",\"singleton\":false,\"categories\":[\"\",\"\"],\"views\":{\"default\":{\"preferredWidth\":0,\"preferredHeight\":0,\"type\":\"html\",\"quirks\":true}},\"screenshot\":\"\",\"links\":{},\"thumbnail\":\"\",\"authorLink\":\"\",\"height\":0,\"scaling\":false,\"moduleId\":1,\"features\":[],\"showStats\":false,\"authorPhoto\":\"\",\"scrolling\":false,\"url\":\"http://www.google.com/ig/modules/hello.xml\",\"titleUrl\":\"\",\"iframeUrl\":\"/gadgets/ifr?container=default&mid=1&v=db18c863f15d5d1e758a91f2a44881b4&lang=en&country=US&view=default&url=http%3A%2F%2Fwww.google.com%2Fig%2Fmodules%2Fhello.xml\",\"userPrefs\":{}}]}";
TEST_METADATA_NO_GADGETS = "{\"gadgets\":[]}";
- TEST_SERVICE_URL_VALID =
"http://localhost:8080/wookie/gadgets/metadata";
+ TEST_SERVICE_URL_VALID =
"http://localhost:8080/gadgets/metadata";
TEST_SERVICE_URL_INVALID =
"http://localhost:8080/shindig/gadgets/madeupname";
+
TEST_GADGET_URL_VALID =
"http://www.google.com/ig/modules/hello.xml";
+ TEST_GADGET_URL_VALID2 =
"http://hosting.gmodules.com/ig/gadgets/file/112581010116074801021/hamster.xml";
+ TEST_GADGET_URL_VALID3 =
"http://www.google.com/ig/modules/facebook.xml";
+
TEST_GADGET_URL_INVALID =
"http://localhost:8080/gadgets/madeupname";
TEST_GADGET_URL_MALFORMED =
"ttp://www.google.com/ig/modules/hello.xml";
@@ -78,11 +82,12 @@ public class GadgetUtilsTest {
request.setServerName("localhost");
request.setServerPort(8080);
request.setParameter("url", TEST_GADGET_URL_VALID);
-
+
try {
- Widget widget = GadgetUtils.createWidget(request);
+ W3CWidget widget = GadgetUtils.createWidget(request);
assertNotNull(widget);
} catch (Exception e) {
+ e.printStackTrace();
fail("Couldn't create widget from valid request");
}
}
@@ -96,7 +101,7 @@ public class GadgetUtilsTest {
request.setServerPort(8080);
request.setParameter("url", TEST_GADGET_URL_INVALID);
@SuppressWarnings("unused")
- Widget widget = GadgetUtils.createWidget(request);
+ W3CWidget widget = GadgetUtils.createWidget(request);
// Uh-oh! No exception was thrown so we
// better make this test fail!
fail("should've thrown an exception!");
@@ -111,7 +116,7 @@ public class GadgetUtilsTest {
request.setServerPort(8888);
request.setParameter("url", TEST_GADGET_URL_VALID);
@SuppressWarnings("unused")
- Widget widget = GadgetUtils.createWidget(request);
+ W3CWidget widget = GadgetUtils.createWidget(request);
// Uh-oh! No exception was thrown so we
// better make this test fail!
fail("should've thrown an exception!");
@@ -203,7 +208,7 @@ public class GadgetUtilsTest {
@Test(expected = Exception.class)
public void getWidgetWithErrorMetadata() throws Exception {
@SuppressWarnings("unused")
- Widget widget = GadgetUtils.getWidget(GadgetUtils.getMetadata(
+ W3CWidget widget =
GadgetUtils.getWidget(GadgetUtils.getMetadata(
TEST_SERVICE_URL_VALID,
TEST_GADGET_URL_INVALID), SHINDIG);
// Uh-oh! No exception was thrown so we
// better make this test fail!
@@ -223,10 +228,10 @@ public class GadgetUtilsTest {
@Test
public void getWidgetWithValidMetadataAndDefaults() {
- Widget widget = null;
+ W3CWidget widget = null;
try {
widget = GadgetUtils.getWidget(TEST_METADATA_VALID,
SHINDIG);
- assertEquals(widget.getHeight().intValue(), 200);
+ assertEquals(320, widget.getHeight().intValue(), 200);
assertEquals(widget.getWidth().intValue(), 320);
} catch (Exception e) {
fail("Create widget from basic valid metadata -
defaults not used");
@@ -235,7 +240,7 @@ public class GadgetUtilsTest {
@Test
public void getWidgetWithValidMetadataAndOverrides() {
- Widget widget = null;
+ W3CWidget widget = null;
try {
widget =
GadgetUtils.getWidget(TEST_METADATA_SET_HEIGHT_WIDTH,
SHINDIG);
@@ -249,7 +254,7 @@ public class GadgetUtilsTest {
@Test(expected = Exception.class)
public void getWidgetWithEmptyURL() throws Exception {
@SuppressWarnings("unused")
- Widget widget = GadgetUtils.getWidget(TEST_METADATA_URL_EMPTY,
SHINDIG);
+ W3CWidget widget =
GadgetUtils.getWidget(TEST_METADATA_URL_EMPTY, SHINDIG);
// Uh-oh! No exception was thrown so we
// better make this test fail!
fail("should've thrown an exception!");
@@ -258,7 +263,7 @@ public class GadgetUtilsTest {
@Test(expected = Exception.class)
public void getWidgetWithMissingURL() throws Exception {
@SuppressWarnings("unused")
- Widget widget = GadgetUtils.getWidget(TEST_METADATA_URL_NULL,
SHINDIG);
+ W3CWidget widget =
GadgetUtils.getWidget(TEST_METADATA_URL_NULL, SHINDIG);
// Uh-oh! No exception was thrown so we
// better make this test fail!
fail("should've thrown an exception!");
@@ -267,7 +272,7 @@ public class GadgetUtilsTest {
@Test(expected = Exception.class)
public void getWidgetWithInvalidJSON() throws Exception {
@SuppressWarnings("unused")
- Widget widget = GadgetUtils.getWidget(TEST_METADATA_INVALID,
SHINDIG);
+ W3CWidget widget = GadgetUtils.getWidget(TEST_METADATA_INVALID,
SHINDIG);
// Uh-oh! No exception was thrown so we
// better make this test fail!
fail("should've thrown an exception!");
Modified: incubator/wookie/trunk/src/org/apache/wookie/WidgetAdminServlet.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/WidgetAdminServlet.java?rev=922213&r1=922212&r2=922213&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/WidgetAdminServlet.java
(original)
+++ incubator/wookie/trunk/src/org/apache/wookie/WidgetAdminServlet.java Fri
Mar 12 11:00:25 2010
@@ -430,19 +430,19 @@ public class WidgetAdminServlet extends
private void registerOperation(HttpServletRequest request,
Configuration properties, HttpSession session){
Messages localizedMessages =
LocaleHandler.localizeMessages(request);
- Widget widget = null;
session.setAttribute("metadata", null); //$NON-NLS-1$
try {
- widget = GadgetUtils.createWidget(request);
- if (widget.save()){
+ W3CWidget widgetModel =
GadgetUtils.createWidget(request);
+ if(!Widget.exists(widgetModel.getIdentifier())){
+ WidgetFactory.addNewWidget(widgetModel);
session.setAttribute("metadata",
localizedMessages.getString("WidgetAdminServlet.16")); //$NON-NLS-1$
//$NON-NLS-2$
} else {
- session.setAttribute("metadata",
localizedMessages.getString("WidgetAdminServlet.17")); //$NON-NLS-1$
//$NON-NLS-2$
+ session.setAttribute("metadata",
localizedMessages.getString("WidgetAdminServlet.17")); //$NON-NLS-1$
//$NON-NLS-2$
}
} catch (Exception e1) {
+ e1.printStackTrace();
session.setAttribute("metadata",
localizedMessages.getString("WidgetAdminServlet.18")); //$NON-NLS-1$
//$NON-NLS-2$
}
-
}
private void listAPIKeysOperation(HttpSession session,
HttpServletRequest request){
Modified: incubator/wookie/trunk/src/org/apache/wookie/messages.properties
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/messages.properties?rev=922213&r1=922212&r2=922213&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/messages.properties (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/messages.properties Fri Mar 12
11:00:25 2010
@@ -15,7 +15,7 @@ WidgetAdminServlet.13=There was a proble
WidgetAdminServlet.14=There was a problem removing the widget from the system.
WidgetAdminServlet.15=Widget types were successfully set
WidgetAdminServlet.16=Successfully created a new entry for this gadget
-WidgetAdminServlet.17=Couldn't create a new entry for this gadget
+WidgetAdminServlet.17=This gadget is already available
WidgetAdminServlet.18=There was a problem registering this gadget
WidgetAdminServlet.19=Widget was successfully imported into the system.
WidgetAdminServlet.20=Widget was successfully updated in the system.
Added:
incubator/wookie/trunk/src/org/apache/wookie/util/gadgets/GadgetAdapter.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/util/gadgets/GadgetAdapter.java?rev=922213&view=auto
==============================================================================
---
incubator/wookie/trunk/src/org/apache/wookie/util/gadgets/GadgetAdapter.java
(added)
+++
incubator/wookie/trunk/src/org/apache/wookie/util/gadgets/GadgetAdapter.java
Fri Mar 12 11:00:25 2010
@@ -0,0 +1,214 @@
+/*
+ * Licensed 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.
+ */
+package org.apache.wookie.util.gadgets;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.wookie.w3c.IAccessEntity;
+import org.apache.wookie.w3c.IAuthorEntity;
+import org.apache.wookie.w3c.IContentEntity;
+import org.apache.wookie.w3c.IDescriptionEntity;
+import org.apache.wookie.w3c.IFeatureEntity;
+import org.apache.wookie.w3c.IIconEntity;
+import org.apache.wookie.w3c.ILicenseEntity;
+import org.apache.wookie.w3c.INameEntity;
+import org.apache.wookie.w3c.IPreferenceEntity;
+import org.apache.wookie.w3c.IW3CXMLConfiguration;
+import org.apache.wookie.w3c.W3CWidget;
+import org.apache.wookie.w3c.exceptions.BadManifestException;
+import org.apache.wookie.w3c.impl.AuthorEntity;
+import org.apache.wookie.w3c.impl.ContentEntity;
+import org.apache.wookie.w3c.impl.DescriptionEntity;
+import org.apache.wookie.w3c.impl.IconEntity;
+import org.apache.wookie.w3c.impl.NameEntity;
+import org.apache.wookie.w3c.util.LocalizationUtils;
+import org.jdom.Element;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/**
+ * An adapter class used for building a W3C Widget model using Gadget metadata
+ */
+public class GadgetAdapter implements W3CWidget {
+
+ private String fIdentifier;
+ private String fVersion;
+ private Integer fHeight;
+ private Integer fWidth;
+ private ArrayList<INameEntity> fNamesList;
+ private ArrayList<IDescriptionEntity> fDescriptionsList;
+ private IAuthorEntity fAuthor;
+ private ArrayList<IIconEntity> fIconsList;
+ private ArrayList<IContentEntity> fContentList;
+
+ /**
+ * The default Gadget icon
+ */
+ private static final String DEFAULT_ICON =
"/wookie/shared/images/defaultwidget.png";
+
+ public GadgetAdapter(JSONObject gadget, String shindig) throws
Exception {
+ if (gadget.has("errors")) throw new Exception("Invalid gadget -
Shindig error");
+ if (!gadget.has("url")) throw new Exception("Invalid gadget -
URL missing");
+ if (gadget.getString("url") == null ||
gadget.getString("url").equals("")) throw new Exception("Invalid gadget -
Invalid URL");
+ try {
+ @SuppressWarnings("unused")
+ URL url = new URL(gadget.getString("url"));
+ } catch (Exception e) {
+ throw new Exception("Invalid gadget - invalid URL");
+ }
+
+ // Start file
+ // We should be able to use the "iframeUrl" property here, but
+ // it isn't very reliable at generating a usable value, so we
construct
+ // a very basic URL instead
+ // FIXME we need to use real locales in these URLs
+ this.fContentList = new ArrayList<IContentEntity>();
+ String url =
(shindig+"/gadgets/ifr?url="+gadget.getString("url")+"&lang=en&country=UK&view=home");
+ ContentEntity content = new ContentEntity(url,
"UTF-8",IW3CXMLConfiguration.DEFAULT_MEDIA_TYPE);
+ fContentList.add(content);
+
+ // Identifier
+ this.fIdentifier = gadget.getString("url");
+
+ // Height and Width
+ fHeight = 200;
+ fWidth = 320;
+ if (gadget.has("height")) if (gadget.getInt("height") != 0)
fHeight = gadget.getInt("height");
+ if (gadget.has("width")) if (gadget.getInt("width") != 0)
fWidth = gadget.getInt("width");
+
+ // Author
+ String authorName = getValue(gadget, "author", "Unknown
Author");
+ AuthorEntity author = new AuthorEntity(authorName, null, null);
+ fAuthor = author;
+
+ // Name
+ this.fNamesList = new ArrayList<INameEntity>();
+ String title = getValue(gadget, "title", "Untitled Gadget");
+ // Override the title with directory title if present (this is
intended for gallery use)
+ title = getValue(gadget, "directory_title", title);
+ NameEntity name = new NameEntity();
+ name.setName(title);
+ this.fNamesList.add(name);
+
+ // Description
+ this.fDescriptionsList = new ArrayList<IDescriptionEntity>();
+ String description = getValue(gadget, "description", "Google
Gadget");
+ DescriptionEntity desc = new DescriptionEntity();
+ desc.setDescription(description);
+ this.fDescriptionsList.add(desc);
+
+ // Icons
+ this.fIconsList = new ArrayList<IIconEntity>();
+ String icon = getValue(gadget,"thumbnail", DEFAULT_ICON);
+ IconEntity iconEntity = new IconEntity(icon,null,null);
+ this.fIconsList.add(iconEntity);
+ }
+
+ private static String getValue(JSONObject gadget, String property,
String defaultValue) throws JSONException{
+ if (gadget.has(property)){
+ if (gadget.getString(property)!=null){
+ if
(!(gadget.getString(property).trim()).equals("")){
+ return gadget.getString(property);
+ }
+ }
+ }
+ return defaultValue;
+ }
+
+ public String getViewModes() {
+ return null;
+ }
+
+ public String getVersion() {
+ return fVersion;
+ }
+
+ public List<IPreferenceEntity> getPrefences(){
+ return new ArrayList<IPreferenceEntity>();
+
+ }
+
+ public List<IFeatureEntity> getFeatures(){
+ return new ArrayList<IFeatureEntity>();
+
+ }
+
+ public List<IAccessEntity> getAccessList(){
+ return new ArrayList<IAccessEntity>();
+
+ }
+
+ public String getAuthor(){
+ if (fAuthor == null) return null;
+ return fAuthor.getAuthorName();
+ }
+
+ public String getAuthorEmail() {
+ if (fAuthor == null) return null;
+ return fAuthor.getEmail();
+ }
+
+ public String getAuthorHref() {
+ if (fAuthor == null) return null;
+ return fAuthor.getHref();
+ }
+
+ public List<IContentEntity> getContentList() {
+ return fContentList;
+ }
+
+ public List<IDescriptionEntity> getDescriptions(){
+ return fDescriptionsList;
+ }
+
+ public List<INameEntity> getNames() {
+ return fNamesList;
+ }
+
+ public List<IIconEntity> getIconsList() {
+ return fIconsList;
+ }
+
+ public List<ILicenseEntity> getLicensesList() {
+ return new ArrayList<ILicenseEntity>();
+ }
+
+ public String getIdentifier() {
+ return fIdentifier;
+ }
+
+ public Integer getHeight() {
+ return fHeight;
+ }
+
+ public Integer getWidth() {
+ return fWidth;
+ }
+
+ public void fromXML(Element element) throws BadManifestException {
+ }
+
+ public String getXMLTagName() {
+ return null;
+ }
+
+ public String getLocalName(String locale) {
+ INameEntity name =
(INameEntity)LocalizationUtils.getLocalizedElement(fNamesList.toArray(new
INameEntity[fNamesList.size()]), new String[]{locale});
+ if (name != null) return name.getName();
+ return IW3CXMLConfiguration.UNKNOWN;
+ }
+
+}
Modified:
incubator/wookie/trunk/src/org/apache/wookie/util/gadgets/GadgetUtils.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/util/gadgets/GadgetUtils.java?rev=922213&r1=922212&r2=922213&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/util/gadgets/GadgetUtils.java
(original)
+++ incubator/wookie/trunk/src/org/apache/wookie/util/gadgets/GadgetUtils.java
Fri Mar 12 11:00:25 2010
@@ -27,13 +27,11 @@ import org.apache.commons.httpclient.Htt
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
-import org.apache.wookie.beans.Name;
-import org.apache.wookie.beans.Description;
-import org.apache.wookie.beans.StartFile;
-import org.apache.wookie.beans.Widget;
-import org.apache.wookie.beans.WidgetIcon;
+import org.apache.log4j.Logger;
+import org.apache.wookie.w3c.W3CWidget;
import org.json.JSONArray;
import org.json.JSONObject;
+
/**
* Utilities for working with a Shindig Gadget Server, particularly for
parsing gadget metadata
*
@@ -42,15 +40,17 @@ import org.json.JSONObject;
*/
public class GadgetUtils {
+ static Logger _logger = Logger.getLogger(GadgetUtils.class.getName());
+
/**
* Relative URL of the Shindig service
*/
- public static final String SHINDIG_SERVICE = "/wookie";
-
+ public static final String SHINDIG_SERVICE = "/";
+
/**
* Relative URL of the Shindig metadata service
*/
- public static final String METADATA_SERVICE =
"/wookie/gadgets/metadata";
+ public static final String METADATA_SERVICE = "/gadgets/metadata";
/**
* The default Container ID
@@ -58,17 +58,12 @@ public class GadgetUtils {
private static final String CONTAINER_ID = "wookie";
/**
- * The default Gadget icon
- */
- private static final String DEFAULT_ICON =
"/wookie/shared/images/defaultwidget.png";
-
- /**
- * Create a new Widget from the request supplied
+ * Create a new W3CWidget from the request supplied
* @param request the registration request, with a url parameter for
the desired widget
* @return the widget
* @throws MalformedURLException
*/
- public static Widget createWidget(HttpServletRequest request) throws
Exception{
+ public static W3CWidget createWidget(HttpServletRequest request) throws
Exception{
String svc = GadgetUtils.METADATA_SERVICE;
String shindig = GadgetUtils.SHINDIG_SERVICE;
try {
@@ -76,9 +71,8 @@ public class GadgetUtils {
if (properties.getString("widget.metadata.url")!=null)
svc = properties.getString("widget.metadata.url");
if (properties.getString("widget.shindig.url")!=null)
shindig = properties.getString("widget.shindig.url");
} catch (Exception e) {
- e.printStackTrace();
- // Problem with the servlet context; we'll just let it
go for now
- // TODO log this error
+ // Problem with the servlet context; we'll just let it
go for now and use the defaults
+ _logger.warn("problem configuring the gadget metadata
service from widgetserver.properties - using defaults");
}
URL gadgetService = new URL(request.getScheme() ,
request.getServerName() ,
@@ -90,10 +84,10 @@ public class GadgetUtils {
* Create a new widget based on the gadget url supplied, and the
metadata service url supplied
* @param service the metadata service
* @param url the gadget url
- * @return the Widget instance
+ * @return the W3CWidget instance
* @throws Exception
*/
- private static Widget createWidget(String service, String url, String
shindig) throws Exception{
+ private static W3CWidget createWidget(String service, String url,
String shindig) throws Exception{
return getWidget(getMetadata(service,url), shindig);
}
@@ -107,28 +101,25 @@ public class GadgetUtils {
* @throws Exception if the service does not exist, the service
responds with a HTTP error, or the widget URL is malformed
*/
public static String getMetadata(String service,String url) throws
Exception{
-
+
try {
new URL(url);
} catch (MalformedURLException m) {
throw new Exception("Gadget URL was malformed");
}
-
- String request = new String();
- request =
"{\"context\":{\"country\":\"US\",\"language\":\"en\",\"view\":\"default\",\"container\":\""+CONTAINER_ID+"\"},\"gadgets\":[{\"url\":\""+url+"\",\"moduleId\":1}]}";
- StringRequestEntity req = null;
- req = new StringRequestEntity(request, "text/x-json","UTF-8");
- String response = "";
- PostMethod post = new PostMethod(service);
+
try {
+ String request =
"{\"context\":{\"country\":\"US\",\"language\":\"en\",\"view\":\"home\",\"container\":\""+CONTAINER_ID+"\"},\"gadgets\":[{\"url\":\""+url+"\",\"moduleId\":1}]}";
+ StringRequestEntity req = null;
+ req = new StringRequestEntity(request,
"text/x-json","UTF-8");
+ PostMethod post = new PostMethod(service);
post.setRequestEntity(req);
post.setRequestHeader("Content-Length",
String.valueOf(req.getContentLength()));
- response = executeMethod(post);
+ return executeMethod(post);
} catch (Exception e) {
- e.printStackTrace();
throw new Exception("There was a problem connecting to
the Shindig metadata service");
}
- return response;
+
}
/**
@@ -165,116 +156,19 @@ public class GadgetUtils {
}
/**
- * Marshall JSON metadata into a new Widget instance
+ * Marshall JSON metadata into a new W3CWidget instance
* @param metadata JSON-formatted String containing Gadget metadata
- * @return a Widget instance
+ * @param shindig the URL of the shindig service
+ * @return a W3CWidget instance
* @throws Exception if metadata cannot create a valid widget
*/
- public static Widget getWidget(String metadata, String shindig) throws
Exception{
+ public static W3CWidget getWidget(String metadata, String shindig)
throws Exception{
JSONObject gadget = null;
- Widget widget = null;
JSONObject response = new JSONObject(metadata);
JSONArray gadgets = response.getJSONArray("gadgets");
- if (gadgets.length() > 0){
- gadget = gadgets.getJSONObject(0);
- if (gadget.has("errors")) throw new Exception("Invalid
gadget - Shindig error");
-
- widget = new Widget();
- // Defaults
- String title = "Untitled Gadget";
- int height = 200;
- int width = 320;
- String author = "Unknown Author";
- String description = "Google Gadget";
- String icon = DEFAULT_ICON;
-
- if (!gadget.has("url")) throw new Exception("Invalid
gadget - URL missing");
- if (gadget.getString("url") == null ||
gadget.getString("url").equals("")) throw new Exception("Invalid gadget -
Invalid URL");
- try {
- @SuppressWarnings("unused")
- URL url = new URL(gadget.getString("url"));
- } catch (Exception e) {
- throw new Exception("Invalid gadget - invalid
URL");
- }
-
- // Use the URL as the GUID
- widget.setGuid(gadget.getString("url"));
-
- // We should be able to use the "iframeUrl" property
here, but
- // it isn't very reliable at generating a usable value,
so we construct
- // a very basic URL instead
- StartFile sf = new StartFile();
- sf.setWidget(widget);
- // FIXME we need to use real locales in these URLs
-
sf.setUrl(shindig+"/gadgets/ifr?url="+gadget.getString("url")+"&lang=en&country=UK");
-
- if (gadget.has("height")) if (gadget.getInt("height")
!= 0) height = gadget.getInt("height");
- if (gadget.has("width")) if (gadget.getInt("width") !=
0) width = gadget.getInt("width");
-
- if (gadget.has("author")){
- if (gadget.getString("author")!=null){
- if
(!(gadget.getString("author").trim()).equals("")){
- author =
gadget.getString("author");
- }
- }
- }
-
- if (gadget.has("title")){
- if (gadget.getString("title")!=null){
- if
(!(gadget.getString("title").trim()).equals("")){
- title =
gadget.getString("title");
- }
- }
- }
-
- // Override the title with directory title if present
(this is intended for gallery use)
- if (gadget.has("directory_title")){
- if (gadget.getString("directory_title")!=null){
- if
(!(gadget.getString("directory_title").trim()).equals("")){
- title =
gadget.getString("directory_title");
- }
- }
- }
-
- if (gadget.has("description")){
- if (gadget.getString("description")!=null){
- if
(!(gadget.getString("description").trim()).equals("")){
- title =
gadget.getString("description");
- }
- }
- }
-
- if (gadget.has("thumbnail")){
- if (gadget.getString("thumbnail")!=null){
- if
(!(gadget.getString("thumbnail").trim()).equals("")){
- icon =
gadget.getString("thumbnail");
- }
- }
- }
-
- widget.setMaximize(false);
- widget.setHeight(height);
- widget.setWidth(width);
- Name name = new Name();
- name.setWidget(widget);
- name.setName(title);
- Description desc = new Description();
- desc.setWidget(widget);
- desc.setContent(description);
- WidgetIcon wicon = new WidgetIcon();
- wicon.setSrc(icon);
- wicon.setWidget(widget);
- widget.setWidgetAuthor(author);
-
- widget.save();
- sf.save();
- name.save();
- wicon.save();
- desc.save();
-
- }
- return widget;
+ if (gadgets.length()==0) return null;
+ gadget = gadgets.getJSONObject(0);
+ return new GadgetAdapter(gadget, shindig);
}
-
}