Author: lryan
Date: Tue Jul 29 14:59:32 2008
New Revision: 680856

URL: http://svn.apache.org/viewvc?rev=680856&view=rev
Log:
Fix problem with non-inline JS being included out-of-order

Added:
    
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/RestfulRequestItemTest.java
      - copied, changed from r679466, 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/RequestItemTest.java
Modified:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/servlet/GadgetRenderingTask.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=680856&r1=680855&r2=680856&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
 Tue Jul 29 14:59:32 2008
@@ -19,6 +19,8 @@
 
 package org.apache.shindig.gadgets.servlet;
 
+import com.google.inject.Inject;
+
 import org.apache.shindig.common.ContainerConfig;
 import org.apache.shindig.common.SecurityToken;
 import org.apache.shindig.common.SecurityTokenDecoder;
@@ -40,9 +42,6 @@
 import org.apache.shindig.gadgets.spec.ModulePrefs;
 import org.apache.shindig.gadgets.spec.Preload;
 import org.apache.shindig.gadgets.spec.View;
-
-import com.google.inject.Inject;
-
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -70,30 +69,45 @@
  * Represents a single rendering task
  */
 public class GadgetRenderingTask {
+
   protected static final int DEFAULT_CACHE_TTL = 60 * 5;
+
   protected static final String CAJA_PARAM = "caja";
+
   protected static final String LIBS_PARAM_NAME = "libs";
+
   protected static final Logger logger
       = Logger.getLogger("org.apache.shindig.gadgets");
-  protected static final String STRICT_MODE_DOCTYPE = "<!DOCTYPE HTML PUBLIC 
\"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\";>";
+
+  protected static final String STRICT_MODE_DOCTYPE
+      = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" 
\"http://www.w3.org/TR/html4/strict.dtd\";>";
 
   private HttpServletRequest request;
+
   private HttpServletResponse response;
+
   private final GadgetServer server;
+
   private final MessageBundleFactory messageBundleFactory;
+
   private final GadgetFeatureRegistry registry;
+
   private final ContainerConfig containerConfig;
+
   private final UrlGenerator urlGenerator;
+
   private final SecurityTokenDecoder tokenDecoder;
+
   private GadgetContext context;
+
   private final List<GadgetContentFilter> filters;
+
   private final LockedDomainService domainLocker;
+
   private String container = null;
 
   /**
    * Processes a single rendering request and produces output html or errors.
-   *
-   * @throws IOException
    */
   public void process(HttpServletRequest request, HttpServletResponse response)
       throws IOException {
@@ -111,7 +125,7 @@
 
     if (!"http".equals(url.getScheme()) && !"https".equals(url.getScheme())) {
       response.sendError(HttpServletResponse.SC_BAD_REQUEST,
-                         "Unsupported scheme (must be http or https).");
+          "Unsupported scheme (must be http or https).");
       return;
     }
 
@@ -140,18 +154,14 @@
 
   /**
    * Renders a successfully processed gadget.
-   *
-   * @param gadget
-   * @throws IOException
-   * @throws GadgetException
    */
   private void outputGadget(Gadget gadget) throws IOException, GadgetException 
{
     View view = gadget.getView(containerConfig);
     if (view == null) {
-        throw new GadgetException(GadgetException.Code.UNKNOWN_VIEW_SPECIFIED,
-            "No appropriate view could be found for gadget: " + 
gadget.getSpec().getUrl());
+      throw new GadgetException(GadgetException.Code.UNKNOWN_VIEW_SPECIFIED,
+          "No appropriate view could be found for gadget: " + 
gadget.getSpec().getUrl());
     }
-    switch(view.getType()) {
+    switch (view.getType()) {
       case HTML:
         outputHtmlGadget(gadget, view);
         break;
@@ -164,9 +174,7 @@
   /**
    * Redirect a type=html gadget to a locked domain if necessary.
    *
-   * @param gadget
    * @return true if the request was handled, false if the request can proceed
-   * @throws IOException
    */
   private boolean mustRedirectToLockedDomain(Gadget gadget)
       throws IOException {
@@ -183,9 +191,9 @@
         gadgetUrl, container);
     String redir =
         request.getScheme() + "://" +
-        required +
-        request.getServletPath() + '?' +
-        request.getQueryString();
+            required +
+            request.getServletPath() + '?' +
+            request.getQueryString();
     logger.info("Redirecting gadget " + context.getUrl() + " from domain " +
         host + " to domain " + redir);
     response.sendRedirect(redir);
@@ -195,11 +203,6 @@
 
   /**
    * Handles type=html gadget output.
-   *
-   * @param gadget
-   * @param view
-   * @throws IOException
-   * @throws GadgetException
    */
   private void outputHtmlGadget(Gadget gadget, View view)
       throws IOException, GadgetException {
@@ -217,11 +220,11 @@
     // TODO: Substitute gadgets.skins values in here.
     String boilerPlate
         = "<html><head><style type=\"text/css\">" +
-          "body,td,div,span,p{font-family:arial,sans-serif;}" +
-          "a {color:#0000cc;}a:visited {color:#551a8b;}" +
-          "a:active {color:#ff0000;}" +
-          "body{margin: 0px;padding: 0px;background-color:white;}" +
-          "</style></head>";
+        "body,td,div,span,p{font-family:arial,sans-serif;}" +
+        "a {color:#0000cc;}a:visited {color:#551a8b;}" +
+        "a:active {color:#ff0000;}" +
+        "body{margin: 0px;padding: 0px;background-color:white;}" +
+        "</style></head>";
     markup.append(boilerPlate);
     LocaleSpec localeSpec = gadget.getSpec().getModulePrefs().getLocale(
         gadget.getContext().getLocale());
@@ -229,11 +232,10 @@
       markup.append("<body>");
     } else {
       markup.append("<body dir=\"")
-            .append(localeSpec.getLanguageDirection())
-            .append("\">");
+          .append(localeSpec.getLanguageDirection())
+          .append("\">");
     }
 
-    StringBuilder externJs = new StringBuilder();
     StringBuilder inlineJs = new StringBuilder();
     String externFmt = "<script src=\"%s\"></script>";
     String forcedLibs = request.getParameter("libs");
@@ -262,7 +264,12 @@
     // Inline any libs that weren't forced
     for (JsLibrary library : gadget.getJsLibraries()) {
       if (library.getType().equals(JsLibrary.Type.URL)) {
-        externJs.append(String.format(externFmt, library.getContent()));
+        if (inlineJs.length() > 0) {
+          markup.append("<script><!--\n").append(inlineJs)
+              .append("\n-->\n</script>");
+          inlineJs.setLength(0);
+        }
+        markup.append(String.format(externFmt, library.getContent()));
       } else {
         if (!libs.contains(library.getFeature())) {
           // already pulled this file in from the shared contents.
@@ -293,11 +300,7 @@
 
     if (inlineJs.length() > 0) {
       markup.append("<script><!--\n").append(inlineJs)
-            .append("\n-->\n</script>");
-    }
-
-    if (externJs.length() > 0) {
-      markup.append(externJs);
+          .append("\n-->\n</script>");
     }
 
     String content = view.getContent();
@@ -306,8 +309,8 @@
     }
 
     markup.append(content)
-          .append("<script>gadgets.util.runOnLoadHandlers();</script>")
-          .append("</body></html>");
+        .append("<script>gadgets.util.runOnLoadHandlers();</script>")
+        .append("</body></html>");
     if (context.getIgnoreCache()) {
       HttpUtil.setCachingHeaders(response, 0);
     } else if (request.getParameter("v") != null) {
@@ -323,12 +326,8 @@
 
   /**
    * Outputs a url type gadget by redirecting.
-   *
-   * @param gadget
-   * @param view
-   * @throws IOException
    */
-  private void outputUrlGadget(Gadget gadget,  View view) throws IOException {
+  private void outputUrlGadget(Gadget gadget, View view) throws IOException {
     // TODO: generalize this as injectedArgs on Gadget object
 
     // Preserve existing query string parameters.
@@ -344,20 +343,20 @@
 
     try {
       href = new URI(href.getScheme(),
-                     href.getUserInfo(),
-                     href.getHost(),
-                     href.getPort(),
-                     href.getPath(),
-                     null,
-                     null);
+          href.getUserInfo(),
+          href.getHost(),
+          href.getPort(),
+          href.getPath(),
+          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());
+          e.getMessage());
     }
     // Necessary to avoid double-URL-encoding of the JavaScript bundle portion 
of the query.
     StringBuilder redirectHref = new StringBuilder(href.toString());
-    if (query.toString() != null ) {
+    if (query.toString() != null) {
       redirectHref.append('?');
       redirectHref.append(query.toString());
     }
@@ -369,12 +368,8 @@
   }
 
   /**
-   * Displays errors for failed GadgetRendering.
-   * Also sets the caching headers to insure that our server
-   * is not hurt by failing backends when nocache is not set.
-   *
-   * @param error
-   * @throws IOException
+   * Displays errors for failed GadgetRendering. Also sets the caching headers 
to insure that our
+   * server is not hurt by failing backends when nocache is not set.
    */
   private void outputErrors(GadgetException error) throws IOException {
     // Log the errors here for now. We might want different severity levels
@@ -396,18 +391,15 @@
 
   /**
    * Appends libs to the query string.
-   * @param libs
-   * @param query
    */
   private void appendLibsToQuery(Set<String> libs, StringBuilder query) {
     query.append('&')
-         .append(LIBS_PARAM_NAME)
-         .append('=')
-         .append(Utf8UrlCoder.encode(urlGenerator.getBundledJsParam(libs, 
context)));
+        .append(LIBS_PARAM_NAME)
+        .append('=')
+        .append(Utf8UrlCoder.encode(urlGenerator.getBundledJsParam(libs, 
context)));
   }
 
   /**
-   * @param req
    * @return Whether or not to use caja.
    */
   protected boolean getUseCaja(HttpServletRequest req) {
@@ -418,17 +410,16 @@
   /**
    * Appends javascript configuration to the bottom of an existing script 
block.
    *
-   * Appends special configuration for gadgets.util.hasFeature and
-   * gadgets.util.getFeatureParams to the output js.
+   * Appends special configuration for gadgets.util.hasFeature and 
gadgets.util.getFeatureParams to
+   * the output js.
    *
-   * This can't be handled via the normal configuration mechanism because it is
-   * something that varies per request.
+   * This can't be handled via the normal configuration mechanism because it 
is something that
+   * varies per request.
    *
    * Only explicitly <Require>'d and <Optional> features will be added.
    *
-   * @param gadget
    * @param reqs The features you require.
-   * @param js Existing js, to which the configuration will be appended.
+   * @param js   Existing js, to which the configuration will be appended.
    * @throws GadgetException if there is a problem with the gadget auth token
    */
   private void appendJsConfig(Gadget gadget, Set<String> reqs,
@@ -467,11 +458,7 @@
   }
 
   /**
-   * Appends data from <Preload> elements to make them available to
-   * gadgets.io.
-   *
-   * @param gadget
-   * @param inlineJs
+   * Appends data from <Preload> elements to make them available to gadgets.io.
    */
   private void appendPreloads(Gadget gadget, StringBuilder inlineJs) {
     // Output preloads. We will allow the gadget render to continue
@@ -485,11 +472,11 @@
         // Use raw param as key as URL may have to be decoded
         JSONObject jsonEntry = new JSONObject();
         jsonEntry.put("body", response.getResponseAsString())
-                 .put("rc", response.getHttpStatusCode());
+            .put("rc", response.getHttpStatusCode());
         resp.put(entry.getKey().getHref().toString(), jsonEntry);
       } catch (JSONException e) {
         logger.log(
-            Level.INFO,"Error outputting preload for " + preload.getHref(), e);
+            Level.INFO, "Error outputting preload for " + preload.getHref(), 
e);
       } catch (InterruptedException e) {
         logger.log(
             Level.INFO, "Error scheduling preload for " + preload.getHref(), 
e);
@@ -499,10 +486,12 @@
       }
     }
     inlineJs.append("gadgets.io.preloaded_ = ").append(resp.toString())
-            .append(";\n");
+        .append(";\n");
   }
 
-  /** Gets the container for the current request. */
+  /**
+   * Gets the container for the current request.
+   */
   private String getContainerForRequest() {
     if (container != null) {
       return container;
@@ -521,8 +510,7 @@
   /**
    * Validates that the parent parameter was acceptable.
    *
-   * @return True if the parent parameter is valid for the current
-   *     container.
+   * @return True if the parent parameter is valid for the current container.
    */
   private boolean validateParent() {
     String container = getContainerForRequest();
@@ -559,12 +547,12 @@
 
   @Inject
   public GadgetRenderingTask(GadgetServer server,
-                             MessageBundleFactory messageBundleFactory,
-                             GadgetFeatureRegistry registry,
-                             ContainerConfig containerConfig,
-                             UrlGenerator urlGenerator,
-                             SecurityTokenDecoder tokenDecoder,
-                             LockedDomainService lockedDomainService) {
+      MessageBundleFactory messageBundleFactory,
+      GadgetFeatureRegistry registry,
+      ContainerConfig containerConfig,
+      UrlGenerator urlGenerator,
+      SecurityTokenDecoder tokenDecoder,
+      LockedDomainService lockedDomainService) {
     this.server = server;
     this.messageBundleFactory = messageBundleFactory;
     this.registry = registry;

Copied: 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/RestfulRequestItemTest.java
 (from r679466, 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/RequestItemTest.java)
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/RestfulRequestItemTest.java?p2=incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/RestfulRequestItemTest.java&p1=incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/RequestItemTest.java&r1=679466&r2=680856&rev=680856&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/RequestItemTest.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/RestfulRequestItemTest.java
 Tue Jul 29 14:59:32 2008
@@ -17,25 +17,25 @@
  */
 package org.apache.shindig.social.opensocial.service;
 
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+import junit.framework.TestCase;
+
 import org.apache.shindig.common.SecurityToken;
 import org.apache.shindig.common.testing.FakeGadgetToken;
-import org.apache.shindig.social.opensocial.service.RequestItem;
 import org.apache.shindig.social.opensocial.spi.GroupId;
 import org.apache.shindig.social.opensocial.spi.PersonService;
 import org.apache.shindig.social.opensocial.spi.UserId;
 
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
-import junit.framework.TestCase;
-
 import java.util.Map;
 
-public class RequestItemTest extends TestCase {
+public class RestfulRequestItemTest extends TestCase {
 
   public void testParseUrl() throws Exception {
     String path = "/people/john.doe/@self";
 
-    RequestItem request = new RequestItem();
+    RestfulRequestItem request = new RestfulRequestItem();
     request.setUrl(path + "?fields=huey,dewey,louie");
 
     request.putUrlParamsIntoParameters();
@@ -44,7 +44,7 @@
     assertEquals("huey,dewey,louie", request.getParameters().get("fields"));
 
     // Try it without any params
-    request = new RequestItem();
+    request = new RestfulRequestItem();
     request.setUrl(path);
 
     request.putUrlParamsIntoParameters();
@@ -58,7 +58,7 @@
     Map<String, String> params = Maps.newHashMap();
     SecurityToken token = null;
     String method = "method";
-    RequestItem request = new RequestItem();
+    RestfulRequestItem request = new RestfulRequestItem();
 
     request.setUrl(url);
     assertEquals(url, request.getUrl());
@@ -74,7 +74,7 @@
   }
 
   public void testGetAppId() throws Exception {
-    RequestItem request = new RequestItem();
+    RestfulRequestItem request = new RestfulRequestItem();
 
     request.setParameters(Maps.immutableMap("appId", "100"));
     assertEquals("100", request.getAppId());
@@ -86,21 +86,21 @@
   }
 
   public void testGetUser() throws Exception {
-    RequestItem request = new RequestItem();
+    RestfulRequestItem request = new RestfulRequestItem();
 
     request.setParameters(Maps.immutableMap("userId", "@owner"));
     assertEquals(UserId.Type.owner, request.getUser().getType());
   }
 
   public void testGetGroup() throws Exception {
-    RequestItem request = new RequestItem();
+    RestfulRequestItem request = new RestfulRequestItem();
 
     request.setParameters(Maps.immutableMap("groupId", "@self"));
     assertEquals(GroupId.Type.self, request.getGroup().getType());
   }
 
   public void testStartIndex() throws Exception {
-    RequestItem request = new RequestItem();
+    RestfulRequestItem request = new RestfulRequestItem();
 
     request.setParameters(Maps.<String, String>immutableMap("startIndex", 
null));
     assertEquals(0, request.getStartIndex());
@@ -110,7 +110,7 @@
   }
 
   public void testCount() throws Exception {
-    RequestItem request = new RequestItem();
+    RestfulRequestItem request = new RestfulRequestItem();
 
     request.setParameters(Maps.<String, String>immutableMap("count", null));
     assertEquals(20, request.getCount());
@@ -120,7 +120,7 @@
   }
 
   public void testOrderBy() throws Exception {
-    RequestItem request = new RequestItem();
+    RestfulRequestItem request = new RestfulRequestItem();
 
     request.setParameters(Maps.<String, String>immutableMap("orderBy", null));
     assertEquals(PersonService.SortOrder.topFriends, request.getOrderBy());
@@ -130,7 +130,7 @@
   }
 
   public void testFilterBy() throws Exception {
-    RequestItem request = new RequestItem();
+    RestfulRequestItem request = new RestfulRequestItem();
 
     request.setParameters(Maps.<String, String>immutableMap("filterBy", null));
     assertEquals(PersonService.FilterType.all, request.getFilterBy());
@@ -140,7 +140,7 @@
   }
 
   public void testFields() throws Exception {
-    RequestItem request = new RequestItem();
+    RestfulRequestItem request = new RestfulRequestItem();
 
     request.setParameters(Maps.<String, String>immutableMap("fields", null));
     assertEquals(Sets.<String>newHashSet(), request.getFields());
@@ -149,4 +149,10 @@
     assertEquals(Sets.newHashSet("happy", "sad", "grumpy"), 
request.getFields());
   }
 
+  public void testRouteFromParameter() throws Exception {
+    assertEquals("path", RestfulRequestItem.getServiceFromPath("/path"));
+    assertEquals("path", RestfulRequestItem.getServiceFromPath("/path/fun"));
+    assertEquals("path", 
RestfulRequestItem.getServiceFromPath("/path/fun/yes"));
+  }
+
 }
\ No newline at end of file


Reply via email to