Author: etnu
Date: Wed Oct 1 11:53:51 2008
New Revision: 700867
URL: http://svn.apache.org/viewvc?rev=700867&view=rev
Log:
Removed unneeded external view resolution being done in GadgetSpecFactory. This
is now done in renderer anyway.
Also fixed a potential NPE in renderer when using proxied rendering. This isn't
the correct long term answer here, since this data is supposed to get sent to
the remote site.
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicGadgetSpecFactory.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/HtmlRenderer.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/BasicGadgetSpecFactoryTest.java
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicGadgetSpecFactory.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicGadgetSpecFactory.java?rev=700867&r1=700866&r2=700867&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicGadgetSpecFactory.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicGadgetSpecFactory.java
Wed Oct 1 11:53:51 2008
@@ -25,18 +25,12 @@
import org.apache.shindig.gadgets.http.HttpRequest;
import org.apache.shindig.gadgets.http.HttpResponse;
import org.apache.shindig.gadgets.spec.GadgetSpec;
-import org.apache.shindig.gadgets.spec.View;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.ExecutorService;
import java.util.logging.Logger;
/**
@@ -44,15 +38,24 @@
*/
@Singleton
public class BasicGadgetSpecFactory implements GadgetSpecFactory {
- public static final String RAW_GADGETSPEC_XML_PARAM_NAME = "rawxml";
- public static final URI RAW_GADGET_URI = getRawGadgetUri();
-
+ static final String RAW_GADGETSPEC_XML_PARAM_NAME = "rawxml";
+ static final URI RAW_GADGET_URI = URI.create("http://localhost/raw.xml");
+
static final Logger logger =
Logger.getLogger(BasicGadgetSpecFactory.class.getName());
private final HttpFetcher fetcher;
- private final ExecutorService executor;
private final TtlCache<URI, GadgetSpec> ttlCache;
+ @Inject
+ public BasicGadgetSpecFactory(HttpFetcher fetcher,
+ CacheProvider cacheProvider,
+ @Named("shindig.gadget-spec.cache.capacity")
int capacity,
+ @Named("shindig.gadget-spec.cache.minTTL")
long minTtl,
+ @Named("shindig.gadget-spec.cache.maxTTL")
long maxTtl) {
+ this.fetcher = fetcher;
+ this.ttlCache = new TtlCache<URI, GadgetSpec>(cacheProvider, capacity,
minTtl, maxTtl);
+ }
+
public GadgetSpec getGadgetSpec(GadgetContext context) throws
GadgetException {
String rawxml = context.getParameter(RAW_GADGETSPEC_XML_PARAM_NAME);
if (rawxml != null) {
@@ -63,20 +66,20 @@
}
return getGadgetSpec(context.getUrl(), context.getIgnoreCache());
}
-
+
/**
* Retrieves a gadget specification from the cache or from the Internet.
*/
- public GadgetSpec getGadgetSpec(URI gadgetUri, boolean ignoreCache) throws
GadgetException {
+ public GadgetSpec getGadgetSpec(URI gadgetUri, boolean ignoreCache) throws
GadgetException {
if (ignoreCache) {
return fetchObjectAndCache(gadgetUri, ignoreCache);
}
-
+
TtlCache.CachedObject<GadgetSpec> cached = null;
synchronized(ttlCache) {
cached = ttlCache.getElementWithExpiration(gadgetUri);
}
-
+
if (cached.obj == null || cached.isExpired) {
try {
return fetchObjectAndCache(gadgetUri, ignoreCache);
@@ -89,7 +92,7 @@
}
}
}
-
+
return cached.obj;
}
@@ -105,62 +108,11 @@
"Unable to retrieve gadget xml. HTTP error " +
response.getHttpStatusCode());
}
- GadgetSpec spec = new GadgetSpec(url, response.getResponseAsString());
- // Find the type=HTML views that link to their content externally.
- List<View> hrefViewList = new ArrayList<View>();
- for (View v : spec.getViews().values()) {
- if (v.getType() != View.ContentType.URL && v.getHref() != null) {
- hrefViewList.add(v);
- }
- }
-
- // Retrieve all external view contents simultaneously.
- CountDownLatch latch = new CountDownLatch(hrefViewList.size());
- for (View v : hrefViewList) {
- executor.execute(new ViewContentFetcher(v, latch, fetcher, ignoreCache));
- }
- try {
- latch.await();
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
-
- for (View v : spec.getViews().values()) {
- if (v.getType() != View.ContentType.URL) {
- // A non-null href at this point indicates that the retrieval of remote
- // content has failed.
- if (v.getHref() != null) {
- throw new
GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT,
- "Unable to retrieve remote gadget
content.");
- }
- }
- }
+ GadgetSpec spec = new GadgetSpec(url, response.getResponseAsString());
ttlCache.addElement(url, spec, response.getCacheExpiration());
-
- return spec;
- }
-
- private static URI getRawGadgetUri() {
- try {
- return new URI("http", "localhost", "/raw.xml", null);
- } catch (URISyntaxException e) {
- // Never happens
- }
- return null;
- }
- @Inject
- public BasicGadgetSpecFactory(HttpFetcher fetcher,
- CacheProvider cacheProvider,
- ExecutorService executor,
- @Named("shindig.gadget-spec.cache.capacity")int gadgetSpecCacheCapacity,
- @Named("shindig.gadget-spec.cache.minTTL")long minTtl,
- @Named("shindig.gadget-spec.cache.maxTTL")long maxTtl) {
- this.fetcher = fetcher;
- this.executor = executor;
- this.ttlCache =
- new TtlCache<URI, GadgetSpec>(cacheProvider, gadgetSpecCacheCapacity,
minTtl, maxTtl);
+ return spec;
}
}
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/HtmlRenderer.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/HtmlRenderer.java?rev=700867&r1=700866&r2=700867&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/HtmlRenderer.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/HtmlRenderer.java
Wed Oct 1 11:53:51 2008
@@ -71,13 +71,14 @@
GadgetSpec spec = gadget.getSpec();
Preloads preloads = preloader.preload(context, spec);
+ gadget.setPreloads(preloads);
if (view.getHref() == null) {
- gadget.setPreloads(preloads);
return rewriter.rewriteGadget(gadget, view.getContent());
} else {
// TODO: Add current url to GadgetContext to support transitive
proxying.
HttpRequest request = new HttpRequest(Uri.fromJavaUri(view.getHref()))
+ .setIgnoreCache(context.getIgnoreCache())
.setOAuthArguments(new OAuthArguments(view))
.setAuthType(view.getAuthType())
.setSecurityToken(context.getToken())
Modified:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/BasicGadgetSpecFactoryTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/BasicGadgetSpecFactoryTest.java?rev=700867&r1=700866&r2=700867&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/BasicGadgetSpecFactoryTest.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/BasicGadgetSpecFactoryTest.java
Wed Oct 1 11:53:51 2008
@@ -18,10 +18,13 @@
*/
package org.apache.shindig.gadgets;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.classextension.EasyMock.replay;
+import static org.junit.Assert.assertEquals;
+
import org.apache.shindig.common.cache.CacheProvider;
import org.apache.shindig.common.cache.DefaultCacheProvider;
import org.apache.shindig.common.uri.Uri;
-import org.apache.shindig.common.testing.TestExecutorService;
import org.apache.shindig.gadgets.http.HttpFetcher;
import org.apache.shindig.gadgets.http.HttpRequest;
import org.apache.shindig.gadgets.http.HttpResponse;
@@ -29,13 +32,9 @@
import org.apache.shindig.gadgets.spec.GadgetSpec;
import org.easymock.EasyMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.classextension.EasyMock.replay;
-import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.net.URI;
-import java.util.concurrent.ExecutorService;
/**
* Tests for BasicGadgetSpecFactory
@@ -45,33 +44,27 @@
private final static Uri REMOTE_URL =
Uri.parse("http://example.org/remote.html");
private final static String LOCAL_CONTENT = "Hello, local content!";
private final static String ALT_LOCAL_CONTENT = "Hello, local content!";
- private final static String REMOTE_CONTENT = "Hello, remote content!";
private final static String RAWXML_CONTENT = "Hello, rawxml content!";
private final static String LOCAL_SPEC_XML
- = "<Module>" +
- " <ModulePrefs title='GadgetSpecFactoryTest'/>" +
- " <Content type='html'>" + LOCAL_CONTENT + "</Content>" +
- "</Module>";
+ = "<Module>" +
+ " <ModulePrefs title='GadgetSpecFactoryTest'/>" +
+ " <Content type='html'>" + LOCAL_CONTENT + "</Content>" +
+ "</Module>";
private final static String ALT_LOCAL_SPEC_XML
- = "<Module>" +
- " <ModulePrefs title='GadgetSpecFactoryTest'/>" +
- " <Content type='html'>" + ALT_LOCAL_CONTENT + "</Content>" +
- "</Module>";
- private final static String REMOTE_SPEC_XML
- = "<Module>" +
- " <ModulePrefs title='GadgetSpecFactoryTest'/>" +
- " <Content type='html' href='" + REMOTE_URL + "'/>" +
- "</Module>";
+ = "<Module>" +
+ " <ModulePrefs title='GadgetSpecFactoryTest'/>" +
+ " <Content type='html'>" + ALT_LOCAL_CONTENT + "</Content>" +
+ "</Module>";
private final static String RAWXML_SPEC_XML
- = "<Module>" +
- " <ModulePrefs title='GadgetSpecFactoryTest'/>" +
- " <Content type='html'>" + RAWXML_CONTENT + "</Content>" +
- "</Module>";
+ = "<Module>" +
+ " <ModulePrefs title='GadgetSpecFactoryTest'/>" +
+ " <Content type='html'>" + RAWXML_CONTENT + "</Content>" +
+ "</Module>";
private final static String URL_SPEC_XML
- = "<Module>" +
- " <ModulePrefs title='GadgetSpecFactoryTest'/>" +
- " <Content type='url' href='" + REMOTE_URL + "'/>" +
- "</Module>";
+ = "<Module>" +
+ " <ModulePrefs title='GadgetSpecFactoryTest'/>" +
+ " <Content type='url' href='" + REMOTE_URL + "'/>" +
+ "</Module>";
private final static GadgetContext NO_CACHE_CONTEXT = new GadgetContext() {
@Override
@@ -83,19 +76,19 @@
return SPEC_URL.toJavaUri();
}
};
-
+
private final static GadgetContext RAWXML_GADGET_CONTEXT = new
GadgetContext() {
@Override
public boolean getIgnoreCache() {
// This should be ignored by calling code.
return false;
}
-
+
@Override
public URI getUrl() {
return SPEC_URL.toJavaUri();
}
-
+
@Override
public String getParameter(String param) {
if (param.equals(BasicGadgetSpecFactory.RAW_GADGETSPEC_XML_PARAM_NAME)) {
@@ -104,14 +97,13 @@
return null;
}
};
- private final static ExecutorService FAKE_EXECUTOR = new
TestExecutorService();
private final HttpFetcher fetcher =
EasyMock.createNiceMock(HttpFetcher.class);
-
+
private final CacheProvider cacheProvider = new DefaultCacheProvider();
private final BasicGadgetSpecFactory specFactory
- = new BasicGadgetSpecFactory(fetcher, cacheProvider, FAKE_EXECUTOR, 5,
-1000, 1000);
+ = new BasicGadgetSpecFactory(fetcher, cacheProvider, 5, -1000, 1000);
@Test
public void specFetched() throws Exception {
@@ -136,7 +128,7 @@
assertEquals(LOCAL_CONTENT,
spec.getView(GadgetSpec.DEFAULT_VIEW).getContent());
}
-
+
@Test
public void specFetchedFromParam() throws Exception {
// Set up request as if it's a regular spec request, and ensure that
@@ -145,9 +137,9 @@
HttpResponse response = new HttpResponse(LOCAL_SPEC_XML);
expect(fetcher.fetch(request)).andReturn(response);
replay(fetcher);
-
+
GadgetSpec spec = specFactory.getGadgetSpec(RAWXML_GADGET_CONTEXT);
-
+
assertEquals(RAWXML_CONTENT,
spec.getView(GadgetSpec.DEFAULT_VIEW).getContent());
assertEquals(BasicGadgetSpecFactory.RAW_GADGET_URI, spec.getUrl());
}
@@ -174,7 +166,7 @@
@Test
public void staleSpecReturnedFromCacheOnError() throws Exception {
HttpRequest request = new HttpRequest(SPEC_URL).setIgnoreCache(true);
- HttpRequest retriedRequest = new
HttpRequest(SPEC_URL).setIgnoreCache(false);
+ HttpRequest retriedRequest = new
HttpRequest(SPEC_URL).setIgnoreCache(false);
HttpResponse expiredResponse = new HttpResponseBuilder()
.setResponse(LOCAL_SPEC_XML.getBytes("UTF-8"))
.addHeader("Pragma", "no-cache")
@@ -190,21 +182,6 @@
}
@Test
- public void externalContentFetched() throws Exception {
- HttpRequest request = new HttpRequest(SPEC_URL).setIgnoreCache(true);
- HttpResponse response = new HttpResponse(REMOTE_SPEC_XML);
- HttpRequest viewRequest = new HttpRequest(REMOTE_URL).setIgnoreCache(true);
- HttpResponse viewResponse = new HttpResponse(REMOTE_CONTENT);
- expect(fetcher.fetch(request)).andReturn(response);
- expect(fetcher.fetch(viewRequest)).andReturn(viewResponse);
- replay(fetcher);
-
- GadgetSpec spec = specFactory.getGadgetSpec(SPEC_URL.toJavaUri(), true);
-
- assertEquals(REMOTE_CONTENT,
spec.getView(GadgetSpec.DEFAULT_VIEW).getContent());
- }
-
- @Test
public void typeUrlNotFetchedRemote() throws Exception {
HttpRequest request = new HttpRequest(SPEC_URL).setIgnoreCache(true);
HttpResponse response = new HttpResponse(URL_SPEC_XML);
@@ -227,18 +204,6 @@
}
@Test(expected = GadgetException.class)
- public void badRemoteContentThrows() throws Exception {
- HttpRequest request = new HttpRequest(SPEC_URL).setIgnoreCache(true);
- HttpResponse response = new HttpResponse(REMOTE_SPEC_XML);
- HttpRequest viewRequest = new HttpRequest(REMOTE_URL).setIgnoreCache(true);
- expect(fetcher.fetch(request)).andReturn(response);
- expect(fetcher.fetch(viewRequest)).andReturn(HttpResponse.error());
- replay(fetcher);
-
- specFactory.getGadgetSpec(SPEC_URL.toJavaUri(), true);
- }
-
- @Test(expected = GadgetException.class)
public void throwingFetcherRethrows() throws Exception {
HttpRequest request = new HttpRequest(SPEC_URL).setIgnoreCache(true);
expect(fetcher.fetch(request)).andThrow(