You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by et...@apache.org on 2008/02/19 04:15:57 UTC
svn commit: r628973 - in /incubator/shindig/trunk/java/gadgets/src:
main/java/org/apache/shindig/gadgets/
main/java/org/apache/shindig/gadgets/http/
test/java/org/apache/shindig/gadgets/
Author: etnu
Date: Mon Feb 18 19:15:29 2008
New Revision: 628973
URL: http://svn.apache.org/viewvc?rev=628973&view=rev
Log:
Committed SHINDIG-72 and updated RpcServlet to return metadata in the response body.
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetSpec.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetSpecParser.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/JsonRpcRequest.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetSpecParserTest.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetSpecTestFixture.java
Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java?rev=628973&r1=628972&r2=628973&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java Mon Feb 18 19:15:29 2008
@@ -179,16 +179,18 @@
return baseSpec.getAuthorEmail();
}
- // TODO: make this URI?
- public String getScreenshot() {
+ public URI getScreenshot() {
return baseSpec.getScreenshot();
}
- // TODO: make this URI?
- public String getThumbnail() {
+ public URI getThumbnail() {
return baseSpec.getThumbnail();
}
+ public List<String> getCategories() {
+ return baseSpec.getCategories();
+ }
+
public List<LocaleSpec> getLocaleSpecs() {
return new ArrayList<LocaleSpec>(baseSpec.getLocaleSpecs());
}
@@ -289,7 +291,7 @@
public String getContentData() {
return getContentData(null);
}
-
+
/**
* @param view ID of the view whose content to retrieve
* @return Gadget contents for the given view with all substitutions applied
Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetSpec.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetSpec.java?rev=628973&r1=628972&r2=628973&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetSpec.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetSpec.java Mon Feb 18 19:15:29 2008
@@ -27,15 +27,16 @@
*/
public interface GadgetSpec {
public static final String DEFAULT_VIEW = "default";
-
+
public String getTitle();
public URI getTitleURI();
public String getDirectoryTitle();
public String getDescription();
public String getAuthor();
public String getAuthorEmail();
- public String getScreenshot();
- public String getThumbnail();
+ public URI getScreenshot();
+ public URI getThumbnail();
+ public List<String> getCategories();
public static interface LocaleSpec {
public Locale getLocale();
@@ -99,9 +100,9 @@
* @throws IllegalStateException if contentType is not HTML.
*/
public String getContentData();
-
+
/**
- * @param view Identifier of the desired view to retrieve.
+ * @param view Identifier of the desired view to retrieve.
* @return The HTML content for the specified view of this gadget spec,
* or null if no such view was defined.
* @throws IllegalStateException if contentType is not HTML.
Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetSpecParser.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetSpecParser.java?rev=628973&r1=628972&r2=628973&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetSpecParser.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetSpecParser.java Mon Feb 18 19:15:29 2008
@@ -35,6 +35,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -47,6 +48,8 @@
*/
public class GadgetSpecParser {
+ private static final String[] CATEGORY_ATTRS = {"category", "category2"};
+
/**
* Parses the raw input XML and returns a new GadgetSpec with the processed
* content.
@@ -127,13 +130,18 @@
}
spec.title = title.getNodeValue();
- Node titleUrl = attrs.getNamedItem("title_url");
- if (null != titleUrl) {
- try {
- spec.titleUrl = new URI(titleUrl.getNodeValue());
- } catch (URISyntaxException e) {
- throw new SpecParserException(
- "Malformed \"title_url\": " + titleUrl.getNodeValue());
+ spec.titleUrl = getUriAttributeOrNull(attrs, "title_url");
+ spec.description = getAttributeOrNull(attrs, "description");
+ spec.directoryTitle = getAttributeOrNull(attrs, "directory_title");
+ spec.author = getAttributeOrNull(attrs, "author");
+ spec.authorEmail = getAttributeOrNull(attrs, "author_email");
+ spec.thumbnail = getUriAttributeOrNull(attrs, "thumbnail");
+ spec.screenshot = getUriAttributeOrNull(attrs, "screenshot");
+
+ for (String attrName : CATEGORY_ATTRS) {
+ String attr = getAttributeOrNull(attrs, attrName);
+ if (attr != null) {
+ spec.categories.add(attr);
}
}
@@ -148,6 +156,30 @@
// TODO: Icon parsing
}
+ private String getAttributeOrNull(NamedNodeMap attrs, String attrName) {
+ Node attr = attrs.getNamedItem(attrName);
+ if (null != attr) {
+ return attr.getNodeValue();
+ }
+ return null;
+ }
+
+ private URI getUriAttributeOrNull(NamedNodeMap attrs, String attrName)
+ throws SpecParserException {
+ Node attr = attrs.getNamedItem(attrName);
+ if (null != attr) {
+ try {
+ return new URI(attr.getNodeValue());
+ } catch (URISyntaxException e) {
+ // TODO: This is really not a great way to ensure valid URL's. All
+ // kinds of invalid URL's are parsed as valid URI's.
+ throw new SpecParserException(
+ "Malformed \"" + attrName + "\": " + attr.getNodeValue());
+ }
+ }
+ return null;
+ }
+
/**
* Processes the <Locale> section of the spec.
*
@@ -362,11 +394,12 @@
private List<String> preloads = new ArrayList<String>();
private Map<String, FeatureSpec> requires
= new HashMap<String, FeatureSpec>();
- private String screenshot;
- private String thumbnail;
+ private URI screenshot;
+ private URI thumbnail;
private String title;
private URI titleUrl;
private List<UserPref> userPrefs = new ArrayList<UserPref>();
+ private List<String> categories = new ArrayList<String>();
public GadgetSpec copy() {
// TODO: actually clone this thing.
@@ -387,6 +420,7 @@
spec.title = title;
spec.titleUrl = titleUrl;
spec.userPrefs = new ArrayList<UserPref>(userPrefs);
+ spec.categories = new ArrayList<String>(categories);
return spec;
}
@@ -476,11 +510,11 @@
return requires;
}
- public String getScreenshot() {
+ public URI getScreenshot() {
return screenshot;
}
- public String getThumbnail() {
+ public URI getThumbnail() {
return thumbnail;
}
@@ -536,6 +570,10 @@
public List<UserPref> getUserPrefs() {
return userPrefs;
+ }
+
+ public List<String> getCategories() {
+ return categories;
}
public ContentType getContentType() {
Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/JsonRpcRequest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/JsonRpcRequest.java?rev=628973&r1=628972&r2=628973&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/JsonRpcRequest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/JsonRpcRequest.java Mon Feb 18 19:15:29 2008
@@ -28,9 +28,11 @@
import org.json.JSONException;
import org.json.JSONObject;
+import java.net.URI;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
+import java.util.Set;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
@@ -79,10 +81,9 @@
outGadget.getContentType().toString().toLowerCase());
// Features.
- gadgetJson.put("features", new JSONArray());
- for (String feature : outGadget.getRequires().keySet()) {
- gadgetJson.append("features", feature);
- }
+ Set<String> feats = outGadget.getRequires().keySet();
+ String[] features = feats.toArray(new String[feats.size()]);
+ gadgetJson.put("features", features);
JSONObject prefs = new JSONObject();
@@ -93,12 +94,47 @@
.put("type", pref.getDataType().toString().toLowerCase())
.put("default", pref.getDefaultValue())
.put("enumValues", pref.getEnumValues());
+ prefs.put(pref.getName(), up);
}
gadgetJson.put("userPrefs", prefs);
// Content
String iframeUrl = servletState.getIframeUrl(outGadget, options);
gadgetJson.put("content", iframeUrl);
+
+ // Extended spec data.
+ String directoryTitle = outGadget.getDirectoryTitle();
+ if (directoryTitle != null) {
+ gadgetJson.put("directoryTitle", directoryTitle);
+ }
+
+ URI thumbnail = outGadget.getThumbnail();
+ if (thumbnail != null) {
+ gadgetJson.put("thumbnail", thumbnail.toString());
+ }
+
+ URI screenshot = outGadget.getScreenshot();
+ if (screenshot != null) {
+ gadgetJson.put("screenshot", screenshot.toString());
+ }
+
+ String author = outGadget.getAuthor();
+ if (author != null) {
+ gadgetJson.put("author", author);
+ }
+
+ String authorEmail = outGadget.getAuthorEmail();
+ if (authorEmail != null) {
+ gadgetJson.put("authorEmail", authorEmail);
+ }
+
+ // Categories
+ List<String> cats = outGadget.getCategories();
+ if (cats != null) {
+ String[] categories = cats.toArray(new String[cats.size()]);
+ gadgetJson.put("categories", outGadget.getCategories().toArray());
+ }
+
out.append("gadgets", gadgetJson);
} catch (InterruptedException e) {
throw new RpcException("Incomplete processing", e);
Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetSpecParserTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetSpecParserTest.java?rev=628973&r1=628972&r2=628973&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetSpecParserTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetSpecParserTest.java Mon Feb 18 19:15:29 2008
@@ -42,25 +42,35 @@
}
}
- public void testBasicGadget() throws Exception {
+ private GadgetSpec parse(String specXml) throws Exception {
BasicGadgetId id = new BasicGadgetId();
id.uri = new URI("http://example.org/text.xml");
id.moduleId = 1;
+ return parser.parse(id, specXml);
+ }
+
+ private void assertParseException(String specXml) throws Exception {
+ try {
+ GadgetSpec spec = parse(specXml);
+ fail();
+ } catch (SpecParserException ex) {
+ // expected
+ }
+ }
+
+ public void testBasicGadget() throws Exception {
String xml = "<?xml version=\"1.0\"?>" +
"<Module>" +
"<ModulePrefs title=\"Hello, world!\"/>" +
"<Content type=\"html\">Hello!</Content>" +
"</Module>";
- GadgetSpec spec = parser.parse(id, xml);
+ GadgetSpec spec = parse(xml);
assertEquals("Hello!", spec.getContentData());
assertEquals("Hello, world!", spec.getTitle());
}
public void testEnumParsing() throws Exception {
- BasicGadgetId id = new BasicGadgetId();
- id.uri = new URI("http://example.org/text.xml");
- id.moduleId = 1;
String xml = "<?xml version=\"1.0\"?>" +
"<Module>" +
"<ModulePrefs title=\"Test Enum\">" +
@@ -71,7 +81,7 @@
"</ModulePrefs>" +
"<Content type=\"html\">Hello!</Content>" +
"</Module>";
- GadgetSpec spec = parser.parse(id, xml);
+ GadgetSpec spec = parse(xml);
List<GadgetSpec.UserPref> prefs = spec.getUserPrefs();
@@ -85,5 +95,40 @@
assertEquals("Zero", enumValues.get("0"));
assertEquals("One", enumValues.get("1"));
+ }
+
+ public void testNonCanonicalMetadata() throws Exception {
+ String desc = "World greetings";
+ String dirTitle = "Dir title";
+ String cat = "hello";
+ String cat2 = "hello2";
+ String thumb = "http://foo.com/bar.xml";
+ String xml = "<?xml version=\"1.0\"?>" +
+ "<Module>" +
+ "<ModulePrefs title=\"Hello, world!\"" +
+ " description=\"" + desc + "\"" +
+ " directory_title=\"" + dirTitle + "\"" +
+ " thumbnail=\"" + thumb + "\"" +
+ " category=\"" + cat + "\" category2=\"" + cat2 + "\"/>" +
+ "<Content type=\"html\">Hello!</Content>" +
+ "</Module>";
+ GadgetSpec spec = parse(xml);
+
+ assertEquals(desc, spec.getDescription());
+ assertEquals(dirTitle, spec.getDirectoryTitle());
+ assertEquals(thumb, spec.getThumbnail().toString());
+ assertEquals(2, spec.getCategories().size());
+ assertEquals(cat, spec.getCategories().get(0));
+ assertEquals(cat2, spec.getCategories().get(1));
+ }
+
+ public void testIllegalUris() throws Exception {
+ String xml = "<?xml version=\"1.0\"?>" +
+ "<Module>" +
+ "<ModulePrefs title=\"Hello, world!\"" +
+ " thumbnail=\"foo.com#bar#png\"/>" +
+ "<Content type=\"html\">Hello!</Content>" +
+ "</Module>";
+ assertParseException(xml);
}
}
Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetSpecTestFixture.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetSpecTestFixture.java?rev=628973&r1=628972&r2=628973&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetSpecTestFixture.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetSpecTestFixture.java Mon Feb 18 19:15:29 2008
@@ -87,10 +87,10 @@
public Map<String, FeatureSpec> getRequires() {
return new HashMap<String, FeatureSpec>();
}
- public String getScreenshot() {
+ public URI getScreenshot() {
return null;
}
- public String getThumbnail() {
+ public URI getThumbnail() {
return null;
}
public String getTitle() {
@@ -101,6 +101,9 @@
}
public List<UserPref> getUserPrefs() {
return new LinkedList<UserPref>();
+ }
+ public List<String> getCategories() {
+ return new LinkedList<String>();
}
};