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/06/22 13:05:25 UTC
svn commit: r670338 - in /incubator/shindig/trunk/java/gadgets/src:
main/java/org/apache/shindig/gadgets/servlet/
test/java/org/apache/shindig/gadgets/servlet/
Author: etnu
Date: Sun Jun 22 04:05:25 2008
New Revision: 670338
URL: http://svn.apache.org/viewvc?rev=670338&view=rev
Log:
Applied SHINDIG-397 with some style fixes.
Added:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/UrlGeneratorTest.java
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTask.java
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/UrlGenerator.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTaskTest.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/HttpTestFixture.java
Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTask.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTask.java?rev=670338&r1=670337&r2=670338&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTask.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTask.java Sun Jun 22 04:05:25 2008
@@ -21,6 +21,7 @@
import org.apache.shindig.common.SecurityToken;
import org.apache.shindig.common.SecurityTokenDecoder;
+import org.apache.shindig.common.util.Utf8UrlCoder;
import org.apache.shindig.gadgets.ContainerConfig;
import org.apache.shindig.gadgets.Gadget;
import org.apache.shindig.gadgets.GadgetContentFilter;
@@ -335,6 +336,7 @@
URI href = view.getHref();
String queryStr = href.getQuery();
StringBuilder query = new StringBuilder(queryStr == null ? "" : queryStr);
+ String fragment = href.getFragment();
// TODO: figure out a way to make this work with forced libs.
Set<String> libs
@@ -347,14 +349,24 @@
href.getHost(),
href.getPort(),
href.getPath(),
- query.toString(),
- href.getFragment());
+ null,
+ null);
} catch (URISyntaxException e) {
// Not really ever going to happen; input values are already OK.
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
e.getMessage());
}
- response.sendRedirect(href.toString());
+ // Necessary to avoid double-URL-encoding of the JavaScript bundle portion of the query.
+ StringBuilder redirectHref = new StringBuilder(href.toString());
+ if (query.toString() != null ) {
+ redirectHref.append("?");
+ redirectHref.append(query.toString());
+ }
+ if (fragment != null) {
+ redirectHref.append("#");
+ redirectHref.append(fragment);
+ }
+ response.sendRedirect(redirectHref.toString());
}
/**
@@ -392,7 +404,7 @@
query.append('&')
.append(LIBS_PARAM_NAME)
.append('=')
- .append(urlGenerator.getBundledJsParam(libs, context));
+ .append(Utf8UrlCoder.encode(urlGenerator.getBundledJsParam(libs, context)));
}
/**
Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/UrlGenerator.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/UrlGenerator.java?rev=670338&r1=670337&r2=670338&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/UrlGenerator.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/UrlGenerator.java Sun Jun 22 04:05:25 2008
@@ -19,6 +19,7 @@
package org.apache.shindig.gadgets.servlet;
import org.apache.shindig.common.util.HashUtil;
+import org.apache.shindig.common.util.Utf8UrlCoder;
import org.apache.shindig.gadgets.ContainerConfig;
import org.apache.shindig.gadgets.Gadget;
import org.apache.shindig.gadgets.GadgetContext;
@@ -34,8 +35,6 @@
import com.google.inject.Singleton;
import com.google.inject.name.Named;
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
import java.util.Collection;
import java.util.regex.Pattern;
@@ -67,8 +66,7 @@
* @param context
* @return The bundled js parameter for type=url gadgets.
*/
- public String getBundledJsParam(Collection<String> features,
- GadgetContext context) {
+ public String getBundledJsParam(Collection<String> features, GadgetContext context) {
StringBuilder buf = new StringBuilder();
boolean first = false;
for (String feature : features) {
@@ -81,6 +79,9 @@
buf.append(feature);
}
}
+ if (!first) {
+ buf.append("core");
+ }
buf.append(".js?v=").append(jsChecksum)
.append("&container=").append(context.getContainer())
.append("&debug=").append(context.getDebug() ? "1" : "0");
@@ -98,61 +99,58 @@
StringBuilder buf = new StringBuilder();
GadgetContext context = gadget.getContext();
GadgetSpec spec = gadget.getSpec();
- try {
- String url = context.getUrl().toString();
- View view = gadget.getView(containerConfig);
- View.ContentType type;
- if (view == null) {
- type = View.ContentType.HTML;
- } else {
- type = view.getType();
- }
- switch (type) {
- case URL:
- // type = url
- buf.append(view.getHref());
- if (url.indexOf('?') == -1) {
- buf.append('?');
- } else {
- buf.append('&');
- }
- break;
- case HTML:
- default:
- buf.append(iframePrefix);
- break;
- }
- buf.append("container=").append(context.getContainer());
- if (context.getModuleId() != 0) {
- buf.append("&mid=").append(context.getModuleId());
- }
- if (context.getIgnoreCache()) {
- buf.append("&nocache=1");
- } else {
- buf.append("&v=").append(spec.getChecksum());
- }
+ String url = context.getUrl().toString();
+ View view = gadget.getView(containerConfig);
+ View.ContentType type;
+ if (view == null) {
+ type = View.ContentType.HTML;
+ } else {
+ type = view.getType();
+ }
+ switch (type) {
+ case URL:
+ // type = url
+ buf.append(view.getHref());
+ if (url.indexOf('?') == -1) {
+ buf.append('?');
+ } else {
+ buf.append('&');
+ }
+ break;
+ case HTML:
+ default:
+ buf.append(iframePrefix);
+ break;
+ }
+ buf.append("container=").append(context.getContainer());
+ if (context.getModuleId() != 0) {
+ buf.append("&mid=").append(context.getModuleId());
+ }
+ if (context.getIgnoreCache()) {
+ buf.append("&nocache=1");
+ } else {
+ buf.append("&v=").append(spec.getChecksum());
+ }
- buf.append("&lang=").append(context.getLocale().getLanguage());
- buf.append("&country=").append(context.getLocale().getCountry());
+ buf.append("&lang=").append(context.getLocale().getLanguage());
+ buf.append("&country=").append(context.getLocale().getCountry());
- UserPrefs prefs = context.getUserPrefs();
- for (UserPref pref : gadget.getSpec().getUserPrefs()) {
- String name = pref.getName();
- String value = prefs.getPref(name);
- if (value == null) {
- value = pref.getDefaultValue();
- }
- buf.append("&up_").append(URLEncoder.encode(pref.getName(), "UTF-8"))
- .append('=').append(URLEncoder.encode(value, "UTF-8"));
+ UserPrefs prefs = context.getUserPrefs();
+ for (UserPref pref : gadget.getSpec().getUserPrefs()) {
+ String name = pref.getName();
+ String value = prefs.getPref(name);
+ if (value == null) {
+ value = pref.getDefaultValue();
}
- // add url last to work around browser bugs
- if(!type.equals(View.ContentType.URL)) {
- buf.append("&url=")
- .append(URLEncoder.encode(url, "UTF-8"));
- }
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException("UTF-8 Not supported!", e);
+ buf.append("&up_").append(Utf8UrlCoder.encode(pref.getName()))
+ .append('=').append(Utf8UrlCoder.encode(value));
}
+ // add url last to work around browser bugs
+ if(!type.equals(View.ContentType.URL)) {
+ buf.append("&url=")
+ .append(Utf8UrlCoder.encode(url));
+ }
+
return buf.toString();
}
Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTaskTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTaskTest.java?rev=670338&r1=670337&r2=670338&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTaskTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTaskTest.java Sun Jun 22 04:05:25 2008
@@ -19,9 +19,11 @@
package org.apache.shindig.gadgets.servlet;
import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.isA;
import org.apache.shindig.common.testing.FakeGadgetToken;
+import org.apache.shindig.common.util.Utf8UrlCoder;
import org.apache.shindig.gadgets.ContainerConfig;
import org.apache.shindig.gadgets.Gadget;
import org.apache.shindig.gadgets.GadgetContext;
@@ -57,10 +59,10 @@
final static String ALT_CONTENT = "Goodbye, city.";
final static String SPEC_XML
= "<Module>" +
- "<ModulePrefs title=\"hello\"/>" +
- "<Content type=\"html\" quirks=\"false\">" + CONTENT + "</Content>" +
- "<Content type=\"html\" view=\"quirks\" quirks=\"true\"/>" +
- "<Content type=\"html\" view=\"ALIAS\">" + ALT_CONTENT + "</Content>" +
+ "<ModulePrefs title='hello'/>" +
+ "<Content type='html' quirks='false'>" + CONTENT + "</Content>" +
+ "<Content type='html' view='quirks' quirks='true'/>" +
+ "<Content type='html' view='ALIAS'>" + ALT_CONTENT + "</Content>" +
"</Module>";
final static String LIBS = "dummy:blah";
@@ -96,12 +98,12 @@
expect(fixture.request.getParameter("view")).andReturn(view);
expect(fixture.request.getParameterNames()).andReturn(EMPTY_PARAMS);
expect(fixture.request.getParameter("container")).andReturn(null);
- expect(fixture.request.getHeader("Host")).andReturn("www.example.com");
+ expect(fixture.request.getHeader("Host")).andReturn("www.example.org");
}
private void expectLockedDomainCheck() throws Exception {
expect(lockedDomainService.gadgetCanRender(
- EasyMock.eq("www.example.com"),
+ EasyMock.eq("www.example.org"),
isA(Gadget.class),
EasyMock.eq("default"))).andReturn(true);
}
@@ -164,18 +166,18 @@
private void expectLockedDomainFailure() {
expect(lockedDomainService.gadgetCanRender(
- EasyMock.eq("www.example.com"),
+ EasyMock.eq("www.example.org"),
isA(Gadget.class),
EasyMock.eq("default"))).andReturn(false);
expect(fixture.request.getScheme()).andReturn("http");
expect(fixture.request.getServletPath()).andReturn("/gadgets/ifr");
expect(fixture.request.getQueryString()).andReturn("stuff=foo%20bar");
expect(lockedDomainService.getLockedDomainForGadget(
- SPEC_URL.toString(), "default")).andReturn("locked.example.com");
+ SPEC_URL.toString(), "default")).andReturn("locked.example.org");
}
private void expectSendRedirect() throws Exception {
- response.sendRedirect("http://locked.example.com/gadgets/ifr?stuff=foo%20bar");
+ response.sendRedirect("http://locked.example.org/gadgets/ifr?stuff=foo%20bar");
EasyMock.expectLastCall().once();
}
@@ -208,11 +210,11 @@
public void testAuthTokenInjection_allparams() throws Exception {
expect(fixture.request.getParameter("st")).andReturn("fake-token");
expect(securityTokenDecoder.createToken("fake-token")).andReturn(
- new FakeGadgetToken("updated-token", "{ 'foo' : 'bar' }"));
+ new FakeGadgetToken("updated-token", "{ \"foo\" : \"bar\" }"));
String content = parseBasicGadget(GadgetSpec.DEFAULT_VIEW);
JSONObject auth = parseShindigAuthConfig(content);
assertEquals("updated-token", auth.getString("authToken"));
- assertEquals("{ 'foo' : 'bar' }", auth.getString("trustedJson"));
+ assertEquals("{ \"foo\" : \"bar\" }", auth.getString("trustedJson"));
}
public void testAuthTokenInjection_none() throws Exception {
@@ -261,5 +263,60 @@
fixture.checkCacheControlHeaders(0, true);
}
+ @SuppressWarnings("unchecked")
+ public void testParseUrlGadget() throws Exception {
+ String contentUrl = "http://www.example.org/content.php";
+ String libsUrl = "&libs=";
+ String jsUrl = "core.js?v=12345&container=apache&debug=0";
+ String gadgetXml
+ = "<Module>" +
+ "<ModulePrefs title='hello'/>" +
+ "<Content type='url' href='" + contentUrl + "'/>" +
+ "</Module>";
+
+ expect(fixture.request.getParameter("url")).andReturn(SPEC_URL.toString());
+ expect(fixture.request.getParameter("view")).andReturn(GadgetSpec.DEFAULT_VIEW);
+ expect(fixture.request.getParameterNames()).andReturn(EMPTY_PARAMS);
+ expect(fixture.request.getParameter("container")).andReturn(null);
+ expect(fetcher.fetch(SPEC_REQUEST)).andReturn(new HttpResponse(gadgetXml));
+ expect(urlGenerator.getBundledJsParam(isA(Collection.class), isA(GadgetContext.class)))
+ .andReturn(jsUrl);
+ response.sendRedirect(contentUrl + "?" + libsUrl + Utf8UrlCoder.encode(jsUrl));
+ expectLastCall().once();
+
+ replay();
+ fixture.replay();
+ gadgetRenderer.process(fixture.request, response);
+ }
+
+ @SuppressWarnings("unchecked")
+ public void testParseUrlGadgetWithQueryAndFragment() throws Exception {
+ String contentUrl = "http://www.example.org/content.php";
+ String contentQuery = "?foo=bar";
+ String contentFragment = "#foo";
+ String libsUrl = "&libs=";
+ String jsUrl = "core.js?v=12345&container=apache&debug=0";
+ String gadgetXml
+ = "<Module>" +
+ "<ModulePrefs title='hello'/>" +
+ "<Content type='url' href='" + contentUrl + contentQuery + contentFragment + "'/>" +
+ "</Module>";
+
+ expect(fixture.request.getParameter("url")).andReturn(SPEC_URL.toString());
+ expect(fixture.request.getParameter("view")).andReturn(GadgetSpec.DEFAULT_VIEW);
+ expect(fixture.request.getParameterNames()).andReturn(EMPTY_PARAMS);
+ expect(fixture.request.getParameter("container")).andReturn(null);
+ expect(fetcher.fetch(SPEC_REQUEST)).andReturn(new HttpResponse(gadgetXml));
+ expect(urlGenerator.getBundledJsParam(isA(Collection.class), isA(GadgetContext.class)))
+ .andReturn(jsUrl);
+ response.sendRedirect(contentUrl + contentQuery + libsUrl + Utf8UrlCoder.encode(jsUrl) +
+ contentFragment);
+ expectLastCall().once();
+
+ replay();
+ fixture.replay();
+ gadgetRenderer.process(fixture.request, response);
+ }
+
// TODO: Lots of ugly tests on html content.
}
Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/HttpTestFixture.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/HttpTestFixture.java?rev=670338&r1=670337&r2=670338&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/HttpTestFixture.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/HttpTestFixture.java Sun Jun 22 04:05:25 2008
@@ -24,6 +24,10 @@
import org.apache.shindig.gadgets.rewrite.ContentRewriter;
import org.apache.shindig.gadgets.rewrite.NoOpContentRewriter;
+/**
+ * @deprecated Migrate to ServletTestFixture as possible.
+ */
+@Deprecated
public abstract class HttpTestFixture extends GadgetTestFixture {
public final GadgetRenderingTask gadgetRenderer;
public final JsonRpcHandler jsonRpcHandler;
Added: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/UrlGeneratorTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/UrlGeneratorTest.java?rev=670338&view=auto
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/UrlGeneratorTest.java (added)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/UrlGeneratorTest.java Sun Jun 22 04:05:25 2008
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.shindig.gadgets.servlet;
+
+import static org.easymock.EasyMock.expect;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.shindig.gadgets.ContainerConfig;
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.GadgetFeature;
+import org.apache.shindig.gadgets.GadgetFeatureRegistry;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Tests for UrlGenerator.
+ */
+public class UrlGeneratorTest {
+ private final static String IFR_PREFIX = "shindig/eye-frame?";
+ private final static String JS_PREFIX = "get-together/livescript/";
+
+ private final ServletTestFixture fixture = new ServletTestFixture();
+ private final GadgetFeatureRegistry featureRegistry = fixture.mock(GadgetFeatureRegistry.class);
+ private final ContainerConfig containerConfig = fixture.mock(ContainerConfig.class);
+ private final GadgetContext context = fixture.mock(GadgetContext.class);
+
+ @Before
+ public void setUp() throws Exception {
+ expect(featureRegistry.getAllFeatures()).andReturn(new ArrayList<GadgetFeature>());
+ }
+
+ @Test
+ public void getBundledJsParam() throws Exception {
+ List<String> features = new ArrayList<String>();
+ features.add("foo");
+ features.add("bar");
+ expect(context.getContainer()).andReturn("shindig");
+ expect(context.getDebug()).andReturn(true);
+ fixture.replay();
+
+ UrlGenerator urlGenerator
+ = new UrlGenerator(IFR_PREFIX, JS_PREFIX, featureRegistry, containerConfig);
+ String jsParam = urlGenerator.getBundledJsParam(features, context);
+
+ assertTrue(jsParam.matches("foo:bar\\.js\\?v=[0-9a-zA-Z]*&container=shindig&debug=1"));
+ }
+
+ @Test
+ public void getBundledJsParamWithBadFeatureName() throws Exception {
+ List<String> features = new ArrayList<String>();
+ features.add("foo!");
+ features.add("bar");
+ expect(context.getContainer()).andReturn("opensocial.org");
+ expect(context.getDebug()).andReturn(true);
+ fixture.replay();
+
+ UrlGenerator urlGenerator
+ = new UrlGenerator(IFR_PREFIX, JS_PREFIX, featureRegistry, containerConfig);
+ String jsParam = urlGenerator.getBundledJsParam(features, context);
+
+ assertTrue(jsParam.matches("bar\\.js\\?v=[0-9a-zA-Z]*&container=opensocial.org&debug=1"));
+ }
+
+ @Test
+ public void getBundledJsParamWithNoFeatures() throws Exception {
+ List<String> features = new ArrayList<String>();
+ expect(context.getContainer()).andReturn("apache.org");
+ expect(context.getDebug()).andReturn(false);
+ fixture.replay();
+
+ UrlGenerator urlGenerator
+ = new UrlGenerator(IFR_PREFIX, JS_PREFIX, featureRegistry, containerConfig);
+ String jsParam = urlGenerator.getBundledJsParam(features, context);
+
+ assertTrue(jsParam.matches("core\\.js\\?v=[0-9a-zA-Z]*&container=apache.org&debug=0"));
+ }
+
+ @Test
+ public void getBundledJsUrl() throws Exception {
+ List<String> features = new ArrayList<String>();
+ expect(context.getContainer()).andReturn("myhibebfacekut");
+ expect(context.getDebug()).andReturn(false);
+ fixture.replay();
+
+ UrlGenerator urlGenerator
+ = new UrlGenerator(IFR_PREFIX, JS_PREFIX, featureRegistry, containerConfig);
+ String jsParam = urlGenerator.getBundledJsUrl(features, context);
+
+ assertTrue(
+ jsParam.matches(JS_PREFIX + "core\\.js\\?v=[0-9a-zA-Z]*&container=myhibebfacekut&debug=0"));
+ }
+
+ // TODO: iframe output tests.
+}