Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved url generation into container config. PHP code is still doing t…

…his from separate config files, but as we move more configuration into container config instead of ad hoc configuration mechanisms we can reduce this.

git-svn-id: https://svn.apache.org/repos/asf/incubator/shindig/trunk@702981 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit 036cb38273f1d24ac81228b2241ab7c2217fee30 1 parent 1387354
Kevin Brown authored
View
11 config/container.js
@@ -60,6 +60,17 @@
// DNS domain on which gadgets should render.
"gadgets.lockedDomainSuffix" : "-a.example.com:8080",
+// Various urls generated throughout the code base.
+// iframeBaseUri will automatically have the host inserted
+// if locked domain is enabled and the implementation supports it.
+// query parameters will be added.
+"gadgets.iframeBaseUri" : "/gadgets/ifr",
+
+// jsUriTemplate will have %host% and %js% substituted.
+// No locked domain special cases, but jsUriTemplate must
+// never conflict with a lockedDomainSuffix.
+"gadgets.jsUriTemplate" : "http://%host%/gadgets/js/%js%",
+
// Use an insecure security token by default
"gadgets.securityTokenType" : "insecure",
View
2  java/common/conf/shindig.properties
@@ -1,8 +1,6 @@
shindig.features.default=res://features/features.txt
shindig.containers.default=res://containers/default/container.js
shindig.blacklist.file=
-shindig.urls.iframe.prefix=/gadgets/ifr
-shindig.urls.js.prefix=http://%host%/gadgets/js/%js%
shindig.oauth.state-key=
shindig.signing.key-name=
shindig.signing.key-file=
View
44 java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultUrlGenerator.java
@@ -18,6 +18,7 @@
*/
package org.apache.shindig.gadgets;
+import org.apache.shindig.common.ContainerConfig;
import org.apache.shindig.common.uri.Uri;
import org.apache.shindig.common.uri.UriBuilder;
import org.apache.shindig.common.util.HashUtil;
@@ -25,11 +26,12 @@
import org.apache.shindig.gadgets.spec.UserPref;
import org.apache.shindig.gadgets.spec.View;
+import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Singleton;
-import com.google.inject.name.Named;
import java.util.Collection;
+import java.util.Map;
import java.util.regex.Pattern;
/**
@@ -42,17 +44,26 @@
*/
@Singleton
public class DefaultUrlGenerator implements UrlGenerator {
- private final static Pattern ALLOWED_FEATURE_NAME = Pattern.compile("[0-9a-zA-Z\\.\\-]+");
- private final String jsPrefix;
- private final String iframePrefix;
+ protected static final Pattern ALLOWED_FEATURE_NAME = Pattern.compile("[0-9a-zA-Z\\.\\-]+");
+ protected static final String IFRAME_URI_PARAM = "gadgets.iframeBaseUri";
+ protected static final String JS_URI_PARAM = "gadgets.jsUriTemplate";
private final String jsChecksum;
+ private final Map<String, Uri> iframeBaseUris;
+ private final Map<String, String> jsUriTemplates;
+ private final LockedDomainService lockedDomainService;
@Inject
- public DefaultUrlGenerator(@Named("shindig.urls.iframe.prefix") String iframePrefix,
- @Named("shindig.urls.js.prefix") String jsPrefix,
+ public DefaultUrlGenerator(ContainerConfig containerConfig,
+ LockedDomainService lockedDomainService,
GadgetFeatureRegistry registry) {
- this.iframePrefix = iframePrefix;
- this.jsPrefix = jsPrefix;
+ iframeBaseUris = Maps.newHashMap();
+ jsUriTemplates = Maps.newHashMap();
+ for (String container : containerConfig.getContainers()) {
+ iframeBaseUris.put(container, Uri.parse(containerConfig.get(container, IFRAME_URI_PARAM)));
+ jsUriTemplates.put(container, containerConfig.get(container, JS_URI_PARAM));
+ }
+
+ this.lockedDomainService = lockedDomainService;
StringBuilder jsBuf = new StringBuilder();
for (GadgetFeature feature : registry.getAllFeatures()) {
@@ -61,9 +72,15 @@ public DefaultUrlGenerator(@Named("shindig.urls.iframe.prefix") String iframePre
}
}
jsChecksum = HashUtil.checksum(jsBuf.toString().getBytes());
+
}
public String getBundledJsUrl(Collection<String> features, GadgetContext context) {
+ String jsPrefix = jsUriTemplates.get(context.getContainer());
+ if (jsPrefix == null) {
+ return "";
+ }
+
return jsPrefix.replace("%host%", context.getHost())
.replace("%js%", getBundledJsParam(features, context));
}
@@ -114,7 +131,16 @@ public String getIframeUrl(Gadget gadget) {
case HTML:
default:
// TODO: Locked domain support.
- uri = new UriBuilder(Uri.parse(iframePrefix));
+ Uri iframeBaseUri = iframeBaseUris.get(context.getContainer());
+ if (iframeBaseUri != null) {
+ uri = new UriBuilder(iframeBaseUri);
+ } else {
+ uri = new UriBuilder();
+ }
+ String host = lockedDomainService.getLockedDomainForGadget(spec, context.getContainer());
+ if (host != null) {
+ uri.setAuthority(host);
+ }
break;
}
View
67 java/gadgets/src/test/java/org/apache/shindig/gadgets/DefaultUrlGeneratorTest.java
@@ -18,8 +18,12 @@
*/
package org.apache.shindig.gadgets;
+import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.isA;
+import org.apache.shindig.common.ContainerConfig;
+import org.apache.shindig.common.ContainerConfigException;
import org.apache.shindig.common.uri.Uri;
import org.apache.shindig.gadgets.spec.GadgetSpec;
@@ -32,6 +36,7 @@
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -40,8 +45,8 @@
* Tests for DefaultUrlGenerator.
*/
public class DefaultUrlGeneratorTest extends GadgetTestFixture {
- private static final String IFR_PREFIX = "shindig/eye-frame";
- private static final String JS_PREFIX = "http://%host%/get-together/livescript/%js%";
+ private static final String IFR_BASE = "/gadgets/eye-frame";
+ private static final String JS_BASE = "http://%host%/get-together/livescript/%js%";
private static final String SPEC_URL = "http://example.org/gadget.xml";
private static final String TYPE_URL_HREF_HOST = "opensocial.org";
private static final String TYPE_URL_HREF_PATH = "/app/foo";
@@ -55,12 +60,12 @@
private static final int MODULE_ID = 3435;
private final GadgetContext context = mock(GadgetContext.class);
+ private final LockedDomainService lockedDomainService = mock(LockedDomainService.class);
+ private FakeContainerConfig config;
private DefaultUrlGenerator realUrlGenerator;
@Override
public void setUp() throws Exception {
- realUrlGenerator = new DefaultUrlGenerator(IFR_PREFIX, JS_PREFIX, registry);
-
expect(context.getContainer()).andReturn(CONTAINER).anyTimes();
expect(context.getUrl()).andReturn(URI.create(SPEC_URL)).anyTimes();
Map<String, String> prefMap = Maps.newHashMap();
@@ -70,6 +75,11 @@ public void setUp() throws Exception {
expect(context.getLocale()).andReturn(Locale.getDefault()).anyTimes();
expect(context.getModuleId()).andReturn(MODULE_ID).anyTimes();
expect(context.getView()).andReturn(VIEW).anyTimes();
+
+ config = new FakeContainerConfig();
+ config.properties.put(DefaultUrlGenerator.IFRAME_URI_PARAM, IFR_BASE);
+ config.properties.put(DefaultUrlGenerator.JS_URI_PARAM, JS_BASE);
+ realUrlGenerator = new DefaultUrlGenerator(config, lockedDomainService, registry);
}
public void testGetBundledJsParam() throws Exception {
@@ -141,7 +151,35 @@ public void testGetIframeUrlTypeHtml() throws Exception {
Uri iframeUrl = Uri.parse(realUrlGenerator.getIframeUrl(gadget));
- assertEquals(IFR_PREFIX, iframeUrl.getPath());
+ assertEquals(IFR_BASE, iframeUrl.getPath());
+ assertEquals(CONTAINER, iframeUrl.getQueryParameter("container"));
+ assertEquals(UP_VALUE, iframeUrl.getQueryParameter("up_" + UP_NAME));
+ assertEquals(Integer.toString(MODULE_ID), iframeUrl.getQueryParameter("mid"));
+ assertEquals(VIEW, iframeUrl.getQueryParameter("view"));
+ }
+
+ public void testGetIframeUrlTypeHtmlWithLockedDomain() throws Exception {
+ String xml
+ = "<Module>" +
+ " <ModulePrefs title='test'/>" +
+ " <Content type='html'/>" +
+ " <UserPref name='" + UP_NAME + "' datatype='string'/>" +
+ "</Module>";
+ GadgetSpec spec = new GadgetSpec(Uri.parse(SPEC_URL), xml);
+
+ expect(lockedDomainService.getLockedDomainForGadget(isA(GadgetSpec.class), eq(CONTAINER)))
+ .andReturn("locked.example.org");
+ replay();
+
+ Gadget gadget = new Gadget()
+ .setContext(context)
+ .setSpec(spec)
+ .setCurrentView(spec.getView("default"));
+
+ Uri iframeUrl = Uri.parse(realUrlGenerator.getIframeUrl(gadget));
+
+ assertEquals("locked.example.org", iframeUrl.getAuthority());
+ assertEquals(IFR_BASE, iframeUrl.getPath());
assertEquals(CONTAINER, iframeUrl.getQueryParameter("container"));
assertEquals(UP_VALUE, iframeUrl.getQueryParameter("up_" + UP_NAME));
assertEquals(Integer.toString(MODULE_ID), iframeUrl.getQueryParameter("mid"));
@@ -157,6 +195,7 @@ public void testGetIframeUrlTypeUrl() throws Exception {
"</Module>";
GadgetSpec spec = new GadgetSpec(Uri.parse(SPEC_URL), xml);
replay();
+
Gadget gadget = new Gadget()
.setContext(context)
.setSpec(spec)
@@ -171,4 +210,22 @@ public void testGetIframeUrlTypeUrl() throws Exception {
StringAssert.assertContains("up_" + UP_NAME + '=' + UP_VALUE, iframeUrl.getQuery());
StringAssert.assertContains("mid=" + MODULE_ID, iframeUrl.getQuery());
}
+
+ private static class FakeContainerConfig extends ContainerConfig {
+ private final Map<String, String> properties = Maps.newHashMap();
+
+ public FakeContainerConfig() throws ContainerConfigException {
+ super(null);
+ }
+
+ @Override
+ public String get(String container, String property) {
+ return properties.get(property);
+ }
+
+ @Override
+ public Collection<String> getContainers() {
+ return Arrays.asList(CONTAINER);
+ }
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.