You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by jo...@apache.org on 2008/09/03 01:55:30 UTC

svn commit: r691437 - in /incubator/shindig/trunk/java/gadgets/src: main/java/org/apache/shindig/gadgets/ main/java/org/apache/shindig/gadgets/rewrite/ main/java/org/apache/shindig/gadgets/servlet/ main/java/org/apache/shindig/gadgets/spec/ test/java/o...

Author: johnh
Date: Tue Sep  2 16:55:29 2008
New Revision: 691437

URL: http://svn.apache.org/viewvc?rev=691437&view=rev
Log:
Making GadgetSpec just about immutable (save type=html with href=<url> -- GadgetSpec.setHrefContent()).

At a high level this CL simply adds setContent() to Gadget while removing it from GadgetSpec. The rest covers
all the downstream plumbing. For instance, because BasicGadgetSpecFactory no longer rewrites content, it also can't cache
that content. Thus, a caching layer is added in CachingContentRewriterRegistry. This may (hopefully?) even prove unnecessary,
but is added to be conservative with CPU associated with rewriting.

In addition, ContentRewriter.rewriteGadget() is added to simplify the implementation of rewriting and to consolidate
all rewriting logic in the Registry rather than distributing it in several places. The "standard" implementation of
this method is provided in BasicContentRewriterRegistry. We'll probably have to add a method for rewriting http/makeRequest
content at some point as well.


Added:
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/CachingContentRewriterRegistry.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/AppendRewriter.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/CachingContentRewriterRegistryTest.java
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/Gadget.java
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServer.java
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/ViewContentFetcher.java
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/BasicContentRewriterRegistry.java
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ContentRewriterRegistry.java
    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/main/java/org/apache/shindig/gadgets/spec/GadgetSpec.java
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/View.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/BasicGadgetSpecFactoryTest.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetServerTest.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTest.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTestFixture.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/BasicContentRewriterRegistryTest.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/UrlGeneratorTest.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/GadgetSpecTest.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ViewTest.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=691437&r1=691436&r2=691437&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 Tue Sep  2 16:55:29 2008
@@ -23,8 +23,6 @@
 import org.apache.shindig.gadgets.http.HttpFetcher;
 import org.apache.shindig.gadgets.http.HttpRequest;
 import org.apache.shindig.gadgets.http.HttpResponse;
-import org.apache.shindig.gadgets.rewrite.ContentRewriter;
-import org.apache.shindig.gadgets.rewrite.ContentRewriterRegistry;
 import org.apache.shindig.gadgets.spec.GadgetSpec;
 import org.apache.shindig.gadgets.spec.View;
 
@@ -48,7 +46,6 @@
   static final Logger logger = Logger.getLogger(BasicGadgetSpecFactory.class.getName());
 
   private final HttpFetcher fetcher;
-  private final ContentRewriterRegistry rewriterRegistry;
   private final ExecutorService executor;
 
   @Override
@@ -114,30 +111,26 @@
           throw new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT,
                                     "Unable to retrieve remote gadget content.");
         }
-        if (rewriterRegistry != null) {
-          String content = v.getContent();
-          for (ContentRewriter rewriter : rewriterRegistry.getRewriters()) {
-            content = rewriter.rewriteGadgetView(spec, content, "text/html");
-          }
-          v.setRewrittenContent(content);
-        }
       }
     }
 
+    // Annotate this spec instance with the expiration time (as a Long) associated
+    // with its retrieval. This enables CachingContentRewriterRegistry to properly
+    // cache rewritten content generated from Gadgets based on the spec.
+    spec.setAttribute(GadgetSpec.EXPIRATION_ATTRIB, new Long(response.getCacheExpiration()));
+    
     return new FetchedObject<GadgetSpec>(spec, response.getCacheExpiration());
   }
 
   @Inject
   public BasicGadgetSpecFactory(HttpFetcher fetcher,
                                 CacheProvider cacheProvider,
-                                ContentRewriterRegistry rewriterRegistry,
                                 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) {
     super(cacheProvider, gadgetSpecCacheCapacity, minTtl, maxTtl);
     this.fetcher = fetcher;
-    this.rewriterRegistry = rewriterRegistry;
     this.executor = executor;
   }
 }

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=691437&r1=691436&r2=691437&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 Tue Sep  2 16:55:29 2008
@@ -68,6 +68,11 @@
   public LocaleSpec getLocale() {
     return spec.getModulePrefs().getLocale(context.getLocale());
   }
+  
+  private final View currentView;
+  public View getCurrentView() {
+	  return currentView;
+  }
 
   /**
    * Attempts to extract the "current" view for this gadget.
@@ -75,7 +80,7 @@
    * @param config The container configuration; used to look for any view name
    *        aliases for the container specified in the context.
    */
-  public View getView(ContainerConfig config) {
+  View getView(ContainerConfig config) {
     String viewName = context.getView();
     View view = spec.getView(viewName);
     if (view == null) {
@@ -101,11 +106,33 @@
     }
     return view;
   }
+  
+  private String content;
+  public String getContent() {
+    return content;
+  }
+  
+  public void setContent(String newContent) {
+    this.content = newContent;
+  }
 
+  private ContainerConfig containerConfig;
+  public ContainerConfig getContainerConfig() {
+    return containerConfig;
+  }
+  
   public Gadget(GadgetContext context, GadgetSpec spec,
-      Collection<JsLibrary> jsLibraries) {
+      Collection<JsLibrary> jsLibraries, ContainerConfig containerConfig) {
     this.context = context;
     this.spec = spec;
     this.jsLibraries = jsLibraries;
+    this.containerConfig = containerConfig;
+    this.currentView = getView(this.containerConfig);
+    if (this.currentView != null) {
+      // View might be invalid or associated with no content (type=URL)
+      this.content = this.currentView.getContent();
+    } else {
+      this.content = null;
+    }
   }
 }
\ No newline at end of file

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServer.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServer.java?rev=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServer.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/GadgetServer.java Tue Sep  2 16:55:29 2008
@@ -17,11 +17,13 @@
  */
 package org.apache.shindig.gadgets;
 
+import org.apache.shindig.common.ContainerConfig;
 import org.apache.shindig.common.uri.Uri;
 import org.apache.shindig.gadgets.http.ContentFetcherFactory;
 import org.apache.shindig.gadgets.http.HttpRequest;
 import org.apache.shindig.gadgets.http.HttpResponse;
 import org.apache.shindig.gadgets.oauth.OAuthArguments;
+import org.apache.shindig.gadgets.rewrite.ContentRewriterRegistry;
 import org.apache.shindig.gadgets.spec.Auth;
 import org.apache.shindig.gadgets.spec.Feature;
 import org.apache.shindig.gadgets.spec.GadgetSpec;
@@ -51,6 +53,8 @@
   private final ExecutorService executor;
   private final GadgetFeatureRegistry registry;
   private final GadgetBlacklist blacklist;
+  private final ContainerConfig containerConfig;
+  private final ContentRewriterRegistry rewriterRegistry;
 
   private ContentFetcherFactory preloadFetcherFactory;
   private GadgetSpecFactory specFactory;
@@ -60,46 +64,41 @@
   public GadgetServer(ExecutorService executor,
       GadgetFeatureRegistry registry,
       GadgetBlacklist blacklist,
+      ContainerConfig containerConfig,
+      ContentRewriterRegistry rewriterRegistry,
       ContentFetcherFactory preloadFetcherFactory,
       GadgetSpecFactory specFactory,
       MessageBundleFactory bundleFactory) {
     this.executor = executor;
     this.registry = registry;
     this.blacklist = blacklist;
+    this.containerConfig = containerConfig;
+    this.rewriterRegistry = rewriterRegistry;
     this.preloadFetcherFactory = preloadFetcherFactory;
     this.specFactory = specFactory;
     this.bundleFactory = bundleFactory;
   }
-
+  
   /**
-   * Process a single gadget.
+   * Process a single gadget. Creates a gadget from a retrieved
+   * GadgetSpec and context object. Performs rewriting, then message
+   * bundle substitution and feature processing.
    *
-   * @param context
-   * @return The processed gadget.
+   * @param context Gadget request context.
+   * @return The processed gadget, ready for consumption.
    * @throws GadgetException
    */
   public Gadget processGadget(GadgetContext context) throws GadgetException {
     if (blacklist.isBlacklisted(context.getUrl())) {
       throw new GadgetException(GadgetException.Code.BLACKLISTED_GADGET);
     }
-    return createGadgetFromSpec(specFactory.getGadgetSpec(context), context);
-  }
-
-  /**
-   * Creates a Gadget from the specified gadget spec and context objects.
-   * This performs message bundle substitution as well as feature processing.
-   *
-   * @param spec
-   * @param context
-   * @return The final Gadget, ready for consumption.
-   * @throws GadgetException
-   */
-  private Gadget createGadgetFromSpec(GadgetSpec spec, GadgetContext context)
-      throws GadgetException {
+    // Retrieve the GadgetSpec for the given context.
+    GadgetSpec spec = specFactory.getGadgetSpec(context);
+    
+    // Create substituted GadgetSpec object, including message bundle substitutions.
     MessageBundle bundle
         = bundleFactory.getBundle(spec, context.getLocale(), context.getIgnoreCache());
     String dir = bundle.getLanguageDirection();
-
     Substitutions substituter = new Substitutions();
     substituter.addSubstitutions(
         Substitutions.Type.MESSAGE, bundle.getMessages());
@@ -108,10 +107,16 @@
         Integer.toString(context.getModuleId()));
     UserPrefSubstituter.addSubstitutions(
         substituter, spec, context.getUserPrefs());
-    spec = spec.substitute(substituter, !context.getIgnoreCache());
-
+    spec = spec.substitute(substituter);
+    
     Collection<JsLibrary> jsLibraries = getLibraries(spec, context);
-    Gadget gadget = new Gadget(context, spec, jsLibraries);
+    Gadget gadget = new Gadget(context, spec, jsLibraries, containerConfig);
+    
+    // Perform rewriting operations on the Gadget.
+    if (rewriterRegistry != null) {
+      rewriterRegistry.rewriteGadget(context, gadget);
+    }
+
     startPreloads(gadget);
     return gadget;
   }

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/ViewContentFetcher.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/ViewContentFetcher.java?rev=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/ViewContentFetcher.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/ViewContentFetcher.java Tue Sep  2 16:55:29 2008
@@ -61,11 +61,9 @@
                                   "Unable to retrieve gadget content. HTTP error " +
                                   response.getHttpStatusCode());
       } else {
-        view.setContent(response.getResponseAsString());
-
-        // Reset the href since the content is now inline; a non-null href will
-        // indicate a failed retrieval attempt.
-        view.setHref(null);
+        // Href is reset by setHrefContent call since content is now inline;
+        // a non-null href indicates a failed retrieval attempt.
+        view.setHrefContent(response.getResponseAsString());
       }
     } catch (GadgetException e) {
       logger.info("Failed to retrieve content at "  + view.getHref());

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/BasicContentRewriterRegistry.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/BasicContentRewriterRegistry.java?rev=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/BasicContentRewriterRegistry.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/BasicContentRewriterRegistry.java Tue Sep  2 16:55:29 2008
@@ -23,6 +23,10 @@
 import java.util.LinkedList;
 import java.util.List;
 
+import org.apache.shindig.gadgets.Gadget;
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.GadgetException;
+
 /**
  * Registry into which is injected a single rewriter, which
  * bootstraps the rewriters list. This enables modularization
@@ -49,5 +53,25 @@
       rewriters.add(rewriter);
     }
   }
+  
+  /** {@inheritDoc} */
+  public boolean rewriteGadget(GadgetContext context, Gadget gadget) throws GadgetException {
+    String content = gadget.getContent();
+    String originalContent = content;
+    
+    if (originalContent == null) {
+      return false;
+    }
+    
+    for (ContentRewriter rewriter : getRewriters()) {
+      String rewritten = rewriter.rewriteGadgetView(gadget.getSpec(), content, "text/html");
+      if (rewritten != null) {
+        content = rewritten;
+      }
+    }
+    
+    gadget.setContent(content);
+    return !originalContent.equals(content);
+  }
 
 }

Added: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/CachingContentRewriterRegistry.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/CachingContentRewriterRegistry.java?rev=691437&view=auto
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/CachingContentRewriterRegistry.java (added)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/CachingContentRewriterRegistry.java Tue Sep  2 16:55:29 2008
@@ -0,0 +1,137 @@
+/*
+ * 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.rewrite;
+
+import java.util.List;
+import java.util.logging.Logger;
+
+import org.apache.shindig.common.cache.CacheProvider;
+import org.apache.shindig.gadgets.CachingWebRetrievalFactory;
+import org.apache.shindig.gadgets.Gadget;
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.GadgetException;
+import org.apache.shindig.gadgets.rewrite.BasicContentRewriterRegistry;
+import org.apache.shindig.gadgets.rewrite.ContentRewriter;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
+
+import com.google.inject.Inject;
+import com.google.inject.name.Named;
+
+/**
+ * Implementation of a content rewriter registry that delegates to
+ * {@code BasicContentRewriterRegistry} for base operations, but also
+ * provides a layer of caching atop that.
+ */
+public class CachingContentRewriterRegistry
+    extends CachingWebRetrievalFactory<Gadget, CachingContentRewriterRegistry.QueryPair, String>
+    implements ContentRewriterRegistry {
+  
+  static final Logger logger = Logger.getLogger(CachingContentRewriterRegistry.class.getName());
+  private final BasicContentRewriterRegistry baseRegistry;
+  
+  @Inject
+  public CachingContentRewriterRegistry(ContentRewriter firstRewriter,
+      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) {
+    // Cache configuration values are currently identical to those for the spec
+    // cache in BasicGadgetSpecFactory for backward compatibility (ensuring that if
+    // caching is set up for specs, it's set up for rewritten content as well)
+    // TODO: create separate rewritten-content config values.
+    super(cacheProvider, capacity, minTtl, maxTtl);
+    baseRegistry = new BasicContentRewriterRegistry(firstRewriter);
+  }
+
+  @Override
+  protected String getCacheKeyFromQueryObj(QueryPair qp) {
+    // Cache by URI + View. Since we always append a view, there should be no
+    // key conflicts associated with this operation.
+    return qp.gadget.getSpec().getUrl().toString() + "#v=" + qp.gadget.getCurrentView().getName();
+  }
+
+  @Override
+  protected Logger getLogger() {
+    return logger;
+  }
+
+  @Override
+  protected FetchedObject<Gadget> retrieveRawObject(QueryPair qp,
+      boolean ignoreCache) throws GadgetException {
+    // Always attempt to rewrite the inbound gadget object.
+    // Even if that fails, the non-rewritten Gadget should be cached,
+    // to avoid superfluous rewrites later.
+    baseRegistry.rewriteGadget(qp.context, qp.gadget);
+    
+    // Expiration time of 30 minutes by default. This number is arbitrary.
+    // TODO: Make this value configurable.
+    long expiration = System.currentTimeMillis() + (1000 * 60 * 30);
+    Object expirationObj = qp.gadget.getSpec().getAttribute(GadgetSpec.EXPIRATION_ATTRIB);
+    if (expirationObj instanceof Long) {
+      expiration = (Long)expirationObj;
+    }
+    
+    return new FetchedObject<Gadget>(createGadgetCopy(qp.gadget), expiration);
+  }
+  
+  private Gadget createGadgetCopy(Gadget source) {
+    // We need to make a copy of the gadget we cache to avoid
+    // the first instance of the gadget being directly written into
+    // the cache, since it might go on to be modified further in
+    // the processing pipeline (changes which would be inherited by
+    // all subsequent requests that have a cache hit).
+    Gadget gadget = new Gadget(source.getContext(), source.getSpec(),
+        source.getJsLibraries(), source.getContainerConfig());
+    gadget.setContent(source.getContent());
+    return gadget;
+  }
+  
+  /** {@inheritDoc} */
+  public List<ContentRewriter> getRewriters() {
+    return baseRegistry.getRewriters();
+  }
+
+  /** {@inheritDoc} */
+  public boolean rewriteGadget(GadgetContext context, Gadget gadget)
+      throws GadgetException {
+    Gadget cached = doCachedFetch(new QueryPair(context, gadget), context.getIgnoreCache());
+    // At present, the output of rewriting is just the string contained within
+    // the Gadget object. Thus, a successful cache hit results in copying over the
+    // rewritten value to the input gadget object.
+    // TODO: Clean up the ContentRewriter interface so rewriting "output" is clearer.
+    // TODO: If necessary later, copy other modified contents to Gadget object.
+    if (cached != null) {
+      gadget.setContent(cached.getContent());
+      return true;
+    }
+    return baseRegistry.rewriteGadget(context, gadget);
+  }
+  
+  public void appendRewriter(ContentRewriter rewriter) {
+    baseRegistry.appendRewriter(rewriter);
+  }
+  
+  public static class QueryPair {
+    private GadgetContext context;
+    private Gadget gadget;
+    private QueryPair(GadgetContext context, Gadget gadget) {
+      this.context = context;
+      this.gadget = gadget;
+    }
+  }
+}

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ContentRewriterRegistry.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ContentRewriterRegistry.java?rev=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ContentRewriterRegistry.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/ContentRewriterRegistry.java Tue Sep  2 16:55:29 2008
@@ -21,10 +21,23 @@
 
 import java.util.List;
 
+import org.apache.shindig.gadgets.Gadget;
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.GadgetException;
+
 @ImplementedBy(BasicContentRewriterRegistry.class)
 public interface ContentRewriterRegistry {
   /**
    * @return An immutable list of all content rewriters
    */
   public List<ContentRewriter> getRewriters();
+  
+  /**
+   * Rewrites a {@code Gadget} object given the registered rewriters.
+   * @param context Context for gadget rewriting
+   * @param gadget Gadget object to rewrite
+   * @return True if rewriting occurred
+   * @throws GadgetException Potentially passed through from rewriters
+   */
+  public boolean rewriteGadget(GadgetContext context, Gadget gadget) throws GadgetException;
 }

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=691437&r1=691436&r2=691437&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 Sep  2 16:55:29 2008
@@ -159,7 +159,7 @@
    * Renders a successfully processed gadget.
    */
   private void outputGadget(Gadget gadget) throws IOException, GadgetException {
-    View view = gadget.getView(containerConfig);
+    View view = gadget.getCurrentView();
     if (view == null) {
       throw new GadgetException(GadgetException.Code.UNKNOWN_VIEW_SPECIFIED,
           "No appropriate view could be found for gadget: " + gadget.getSpec().getUrl());
@@ -307,7 +307,9 @@
           .append("\n-->\n</script>");
     }
 
-    String content = view.getContent();
+    // Content to output now comes from the Gadget, which is processed,
+    // rather than the Gadget's View, a sub-component of immutable GadgetSpec
+    String content = gadget.getContent();
     for (GadgetContentFilter filter : filters) {
       content = filter.filter(content);
     }

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=691437&r1=691436&r2=691437&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 Tue Sep  2 16:55:29 2008
@@ -18,7 +18,6 @@
  */
 package org.apache.shindig.gadgets.servlet;
 
-import org.apache.shindig.common.ContainerConfig;
 import org.apache.shindig.common.util.HashUtil;
 import org.apache.shindig.common.util.Utf8UrlCoder;
 import org.apache.shindig.gadgets.Gadget;
@@ -47,7 +46,6 @@
   private final String jsPrefix;
   private final String iframePrefix;
   private final String jsChecksum;
-  private final ContainerConfig containerConfig;
   private final static Pattern ALLOWED_FEATURE_NAME
       = Pattern.compile("[0-9a-zA-Z\\.\\-]+");
 
@@ -100,7 +98,7 @@
     GadgetContext context = gadget.getContext();
     GadgetSpec spec = gadget.getSpec();
     String url = context.getUrl().toString();
-    View view = gadget.getView(containerConfig);
+    View view = gadget.getCurrentView();
     View.ContentType type;
     if (view == null) {
       type = View.ContentType.HTML;
@@ -159,11 +157,9 @@
   @Inject
   public UrlGenerator(@Named("shindig.urls.iframe.prefix") String iframePrefix,
                       @Named("shindig.urls.js.prefix") String jsPrefix,
-                      GadgetFeatureRegistry registry,
-                      ContainerConfig containerConfig) {
+                      GadgetFeatureRegistry registry) {
     this.iframePrefix = iframePrefix;
     this.jsPrefix = jsPrefix;
-    this.containerConfig = containerConfig;
 
     StringBuilder jsBuf = new StringBuilder();
     for (GadgetFeature feature : registry.getAllFeatures()) {

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/GadgetSpec.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/GadgetSpec.java?rev=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/GadgetSpec.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/GadgetSpec.java Tue Sep  2 16:55:29 2008
@@ -41,6 +41,7 @@
 public class GadgetSpec {
   public static final String DEFAULT_VIEW = "default";
   public static final Locale DEFAULT_LOCALE = new Locale("all", "ALL");
+  public static final String EXPIRATION_ATTRIB = "expiration";
 
   /**
    * The url for this gadget spec.
@@ -114,7 +115,7 @@
    * @param substituter
    * @return The substituted spec.
    */
-  public GadgetSpec substitute(Substitutions substituter, boolean rewrite) {
+  public GadgetSpec substitute(Substitutions substituter) {
     GadgetSpec spec = new GadgetSpec(this);
     spec.modulePrefs = modulePrefs.substitute(substituter);
     if (userPrefs.isEmpty()) {
@@ -128,7 +129,7 @@
     }
     Map<String, View> viewMap = new HashMap<String, View>(views.size());
     for (View view : views.values()) {
-     viewMap.put(view.getName(), view.substitute(substituter, rewrite));
+      viewMap.put(view.getName(), view.substitute(substituter));
     }
     spec.views = Collections.unmodifiableMap(viewMap);
 

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/View.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/View.java?rev=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/View.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/View.java Tue Sep  2 16:55:29 2008
@@ -64,10 +64,7 @@
   public URI getHref() {
     return href;
   }
-  public void setHref(URI href) {
-    this.href = href;
-  }
-
+  
   /**
    * Content@quirks
    */
@@ -101,8 +98,18 @@
   public String getContent() {
     return content;
   }
-  public void setContent(String content) {
+  
+  /**
+   * Set content for a type=html, href=URL style gadget.
+   * This is the last bastion of GadgetSpec mutability,
+   * and should only be used for the described case.
+   * Call nulls out href in order to indicate content was
+   * successfully retrieved.
+   * @param content New gadget content retrieved from href.
+   */
+  public void setHrefContent(String content) {
     this.content = content;
+    this.href = null;
   }
 
   /**
@@ -120,13 +127,9 @@
    * @param substituter
    * @return The substituted view.
    */
-  public View substitute(Substitutions substituter, boolean rewrite) {
+  public View substitute(Substitutions substituter) {
     View view = new View(this);
-    if (rewrite && rewrittenContent != null) {
-      view.content = substituter.substituteString(null, rewrittenContent);
-    } else {
-      view.content = substituter.substituteString(null, content);
-    }
+    view.content = substituter.substituteString(null, content);
     view.href = substituter.substituteUri(null, href);
     return view;
   }
@@ -229,17 +232,4 @@
       return "url".equals(value) ? URL : HTML;
     }
   }
-
-  //
-  // Decorations
-  //
-  private String rewrittenContent;
-
-  public String getRewrittenContent() {
-    return rewrittenContent;
-  }
-
-  public void setRewrittenContent(String rewrittenContent) {
-    this.rewrittenContent = rewrittenContent;
-  }
 }
\ No newline at end of file

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=691437&r1=691436&r2=691437&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 Tue Sep  2 16:55:29 2008
@@ -26,17 +26,12 @@
 import org.apache.shindig.gadgets.http.HttpRequest;
 import org.apache.shindig.gadgets.http.HttpResponse;
 import org.apache.shindig.gadgets.http.HttpResponseBuilder;
-import org.apache.shindig.gadgets.rewrite.BasicContentRewriterRegistry;
-import org.apache.shindig.gadgets.rewrite.ContentRewriter;
-import org.apache.shindig.gadgets.rewrite.ContentRewriterRegistry;
 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 static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
 import org.junit.Test;
 
 import java.net.URI;
@@ -85,14 +80,11 @@
   private final static ExecutorService FAKE_EXECUTOR = new TestExecutorService();
 
   private final HttpFetcher fetcher = EasyMock.createNiceMock(HttpFetcher.class);
-  private final CaptureRewriter rewriter = new CaptureRewriter();
-  private final ContentRewriterRegistry rewriterRegistry =
-      new BasicContentRewriterRegistry(rewriter);
   
   private final CacheProvider cacheProvider = new DefaultCacheProvider();
 
   private final BasicGadgetSpecFactory specFactory
-      = new BasicGadgetSpecFactory(fetcher, cacheProvider, rewriterRegistry, FAKE_EXECUTOR, 5, -1000, 1000);
+      = new BasicGadgetSpecFactory(fetcher, cacheProvider, FAKE_EXECUTOR, 5, -1000, 1000);
 
   @Test
   public void specFetched() throws Exception {
@@ -104,8 +96,6 @@
     GadgetSpec spec = specFactory.getGadgetSpec(SPEC_URL.toJavaUri(), true);
 
     assertEquals(LOCAL_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getContent());
-    assertEquals(LOCAL_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getRewrittenContent());
-    assertTrue("Content not rewritten.", rewriter.rewroteView);
   }
 
   @Test
@@ -118,8 +108,6 @@
     GadgetSpec spec = specFactory.getGadgetSpec(NO_CACHE_CONTEXT);
 
     assertEquals(LOCAL_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getContent());
-    assertEquals(LOCAL_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getRewrittenContent());
-    assertTrue("Content not rewritten.", rewriter.rewroteView);
   }
 
   @Test
@@ -139,8 +127,6 @@
     GadgetSpec spec = specFactory.getGadgetSpec(SPEC_URL.toJavaUri(), false);
 
     assertEquals(ALT_LOCAL_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getContent());
-    assertEquals(ALT_LOCAL_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getRewrittenContent());
-    assertTrue("Content not rewritten.", rewriter.rewroteView);
   }
 
   @Test
@@ -159,8 +145,6 @@
     GadgetSpec spec = specFactory.getGadgetSpec(SPEC_URL.toJavaUri(), false);
 
     assertEquals(ALT_LOCAL_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getContent());
-    assertEquals(ALT_LOCAL_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getRewrittenContent());
-    assertTrue("Content not rewritten.", rewriter.rewroteView);
   }
 
   @Test
@@ -176,8 +160,6 @@
     GadgetSpec spec = specFactory.getGadgetSpec(SPEC_URL.toJavaUri(), true);
 
     assertEquals(REMOTE_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getContent());
-    assertEquals(REMOTE_CONTENT, spec.getView(GadgetSpec.DEFAULT_VIEW).getRewrittenContent());
-    assertTrue("Content not rewritten.", rewriter.rewroteView);
   }
 
   @Test
@@ -191,8 +173,6 @@
 
     assertEquals(REMOTE_URL.toJavaUri(), spec.getView(GadgetSpec.DEFAULT_VIEW).getHref());
     assertEquals("", spec.getView(GadgetSpec.DEFAULT_VIEW).getContent());
-    assertEquals(null, spec.getView(GadgetSpec.DEFAULT_VIEW).getRewrittenContent());
-    assertFalse("Content was rewritten for type=url.", rewriter.rewroteView);
   }
 
   @Test(expected = GadgetException.class)
@@ -225,17 +205,4 @@
 
     specFactory.getGadgetSpec(SPEC_URL.toJavaUri(), true);
   }
-
-  private static class CaptureRewriter implements ContentRewriter {
-    private boolean rewroteView = false;
-
-    public HttpResponse rewrite(HttpRequest request, HttpResponse original) {
-      return original;
-    }
-
-    public String rewriteGadgetView(GadgetSpec spec, String original, String mimeType) {
-      rewroteView = true;
-      return original;
-    }
-  }
 }

Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetServerTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetServerTest.java?rev=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetServerTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetServerTest.java Tue Sep  2 16:55:29 2008
@@ -77,6 +77,11 @@
       = "<messagebundle>" +
         "  <msg name=\"title\">TITLE</msg>" +
        "</messagebundle>";
+  
+  @Override
+  protected void setUp() throws Exception {
+    rewriter.resetWasRewritten();
+  }
 
   public void testGadgetSpecLookup() throws Exception {
     HttpRequest req = new HttpRequest(SPEC_URL);
@@ -89,6 +94,7 @@
     verify();
     assertEquals("GadgetServerTest",
         gadget.getSpec().getModulePrefs().getTitle());
+    assertTrue(rewriter.viewWasRewritten());
   }
 
   public void testGadgetSpecLookupWithFetcherFailure() throws Exception {
@@ -132,6 +138,7 @@
     assertEquals("TITLE", gadget.getSpec().getModulePrefs().getTitle());
     assertEquals("BODY",
         gadget.getSpec().getView(GadgetSpec.DEFAULT_VIEW).getContent());
+    assertTrue(rewriter.viewWasRewritten());
   }
 
   public void testBundledSubstitutionsDone() throws Exception {
@@ -160,6 +167,7 @@
     assertEquals("TITLE", gadget.getSpec().getModulePrefs().getTitle());
     assertEquals("BODY",
         gadget.getSpec().getView(GadgetSpec.DEFAULT_VIEW).getContent());
+    assertTrue(rewriter.viewWasRewritten());
   }
 
   public void testPreloadsFetched() throws Exception {
@@ -185,6 +193,7 @@
 
     assertEquals(preloadData, gadget.getPreloadMap().values().iterator().next()
                                     .get().getResponseAsString());
+    assertTrue(rewriter.viewWasRewritten());
   }
 
   public void testPreloadViewMatch() throws Exception {
@@ -221,6 +230,7 @@
     Gadget gadget = gadgetServer.processGadget(context);
 
     assertTrue(gadget.getPreloadMap().size() == 1);
+    assertTrue(rewriter.viewWasRewritten());
   }
 
   public void testPreloadAntiMatch() throws Exception {
@@ -257,6 +267,7 @@
 
     Gadget gadget = gadgetServer.processGadget(context);
     assertTrue(gadget.getPreloadMap().isEmpty());
+    assertTrue(rewriter.viewWasRewritten());
   }
 
   public void testNoSignedPreloadWithoutToken() throws Exception {
@@ -306,6 +317,7 @@
 
     Gadget gadget = gadgetServer.processGadget(BASIC_CONTEXT);
     assertTrue(gadget.getPreloadMap().size() == 1);
+    assertTrue(rewriter.viewWasRewritten());
   }
 
   public void testOAuthPreload() throws Exception {
@@ -332,6 +344,7 @@
 
     Gadget gadget = gadgetServer.processGadget(BASIC_CONTEXT);
     assertTrue(gadget.getPreloadMap().size() == 1);
+    assertTrue(rewriter.viewWasRewritten());
   }
 
   public void testBlacklistedGadget() throws Exception {

Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTest.java?rev=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTest.java Tue Sep  2 16:55:29 2008
@@ -73,7 +73,7 @@
 
     spec = new GadgetSpec(URI.create(SPEC_URL), SPEC_XML);
     libraries = Arrays.asList(lib);
-    gadget = new Gadget(context, spec, libraries);
+    gadget = new Gadget(context, spec, libraries, config);
   }
 
   @Test

Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTestFixture.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTestFixture.java?rev=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTestFixture.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/GadgetTestFixture.java Tue Sep  2 16:55:29 2008
@@ -26,10 +26,12 @@
 import org.apache.shindig.common.util.FakeTimeSource;
 import org.apache.shindig.gadgets.http.ContentFetcherFactory;
 import org.apache.shindig.gadgets.http.HttpFetcher;
+import org.apache.shindig.gadgets.http.HttpRequest;
+import org.apache.shindig.gadgets.http.HttpResponse;
 import org.apache.shindig.gadgets.oauth.OAuthFetcher;
 import org.apache.shindig.gadgets.rewrite.BasicContentRewriterRegistry;
 import org.apache.shindig.gadgets.rewrite.ContentRewriter;
-import org.apache.shindig.gadgets.rewrite.NoOpContentRewriter;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
 
 import java.util.concurrent.ExecutorService;
 
@@ -46,19 +48,41 @@
       new BasicMessageBundleFactory(fetcher, cacheProvider, 0, 0L, 0L);
   public final GadgetFeatureRegistry registry;
   public final ContainerConfig containerConfig = mock(ContainerConfig.class);
-  public final ContentRewriter rewriter = new NoOpContentRewriter();
+  public final CaptureRewriter rewriter = new CaptureRewriter();
   public final FakeTimeSource timeSource = new FakeTimeSource();
   public final ExecutorService executor = new TestExecutorService();
   public final GadgetSpecFactory specFactory = new BasicGadgetSpecFactory(
-      fetcher, cacheProvider, new BasicContentRewriterRegistry(null), executor, 0, 0L, 0L);
+      fetcher, cacheProvider, executor, 0, 0L, 0L);
 
   public GadgetTestFixture() {
     try {
       registry = new GadgetFeatureRegistry(null, fetcher);
       gadgetServer = new GadgetServer(executor, registry, blacklist,
+          containerConfig, new BasicContentRewriterRegistry(rewriter),
           fetcherFactory, specFactory, bundleFactory);
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
   }
+  
+  protected static class CaptureRewriter implements ContentRewriter {
+    private boolean rewroteView = false;
+
+    public HttpResponse rewrite(HttpRequest request, HttpResponse original) {
+      return original;
+    }
+
+    public String rewriteGadgetView(GadgetSpec spec, String original, String mimeType) {
+      rewroteView = true;
+      return original;
+    }
+    
+    public boolean viewWasRewritten() {
+      return rewroteView;
+    }
+    
+    protected void resetWasRewritten() {
+      rewroteView = false;
+    }
+  }
 }

Added: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/AppendRewriter.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/AppendRewriter.java?rev=691437&view=auto
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/AppendRewriter.java (added)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/AppendRewriter.java Tue Sep  2 16:55:29 2008
@@ -0,0 +1,46 @@
+/*
+ * 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.rewrite;
+
+import org.apache.shindig.gadgets.http.HttpRequest;
+import org.apache.shindig.gadgets.http.HttpResponse;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
+
+/**
+ * Simple ContentRewriter implementation that appends
+ * some particular String to the given input content.
+ * Used for testing.
+ */
+class AppendRewriter implements ContentRewriter {
+  private final String appender;
+  
+  AppendRewriter(String appender) {
+    this.appender = appender;
+  }
+
+  public HttpResponse rewrite(HttpRequest request, HttpResponse original) {
+    // Does nothing.
+    return null;
+  }
+
+  public String rewriteGadgetView(GadgetSpec spec, String original,
+      String mimeType) {
+    // Appends appender to the end of the input string.
+    return original + appender;
+  }
+}
\ No newline at end of file

Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/BasicContentRewriterRegistryTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/BasicContentRewriterRegistryTest.java?rev=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/BasicContentRewriterRegistryTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/BasicContentRewriterRegistryTest.java Tue Sep  2 16:55:29 2008
@@ -17,6 +17,15 @@
  */
 package org.apache.shindig.gadgets.rewrite;
 
+import org.easymock.classextension.EasyMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.classextension.EasyMock.replay;
+
+import org.apache.shindig.gadgets.Gadget;
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
+import org.apache.shindig.gadgets.spec.View;
+
 import junit.framework.TestCase;
 
 public class BasicContentRewriterRegistryTest extends TestCase {
@@ -47,4 +56,31 @@
     assertSame(cr1, r.getRewriters().get(1));
     assertSame(cr2, r.getRewriters().get(2));
   }
+  
+  public void testRunGadgetRewrites() throws Exception {
+    BasicContentRewriterRegistry r = new BasicContentRewriterRegistry(null);
+    StringBuilder appendFull = new StringBuilder();
+    for (int i = 0; i < 3; ++i) {
+      String appendNew = "-" + i;
+      appendFull.append(appendNew);
+      r.appendRewriter(new AppendRewriter(appendNew));
+    }
+    String inputContent = "foo";
+    String rewrittenContent = inputContent + appendFull.toString();
+    
+    GadgetSpec spec = EasyMock.createNiceMock(GadgetSpec.class);
+    View view = EasyMock.createNiceMock(View.class);
+    expect(view.getName()).andReturn(GadgetSpec.DEFAULT_VIEW).anyTimes();
+    expect(view.getType()).andReturn(View.ContentType.HTML).anyTimes();
+    expect(view.getContent()).andReturn(inputContent).anyTimes();
+    expect(spec.getView(GadgetSpec.DEFAULT_VIEW)).andReturn(view).anyTimes();
+    GadgetContext context = EasyMock.createNiceMock(GadgetContext.class);
+    expect(context.getView()).andReturn(GadgetSpec.DEFAULT_VIEW).anyTimes();
+    replay(context, view, spec);
+    
+    Gadget gadget = new Gadget(context, spec, null, null);
+    assertEquals(inputContent, gadget.getContent());
+    assertTrue(r.rewriteGadget(context, gadget));
+    assertEquals(rewrittenContent, gadget.getContent());
+  }
 }

Added: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/CachingContentRewriterRegistryTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/CachingContentRewriterRegistryTest.java?rev=691437&view=auto
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/CachingContentRewriterRegistryTest.java (added)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/rewrite/CachingContentRewriterRegistryTest.java Tue Sep  2 16:55:29 2008
@@ -0,0 +1,80 @@
+/**
+ * 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.rewrite;
+
+import static org.easymock.EasyMock.expect;
+import static org.easymock.classextension.EasyMock.replay;
+
+import org.apache.shindig.common.cache.DefaultCacheProvider;
+import org.apache.shindig.gadgets.Gadget;
+import org.apache.shindig.gadgets.GadgetContext;
+import org.apache.shindig.gadgets.spec.GadgetSpec;
+import org.apache.shindig.gadgets.spec.View;
+import org.easymock.classextension.EasyMock;
+
+import junit.framework.TestCase;
+
+import java.net.URI;
+
+public class CachingContentRewriterRegistryTest extends TestCase {
+  public void testCachedRewriteIsReturned() throws Exception {
+    // Sort of a weird test, but gets the basic idea of caching across.
+    // Perform a rewrite, then update the rewriter in such a way that
+    // would yield a different rewritten result, but ensure that
+    // the result of the rewriter is the old version, which hasn't
+    // yet expired. To ensure no expiry, set expiration date
+    // to the largest possible date.
+    CachingContentRewriterRegistry r = new CachingContentRewriterRegistry(null,
+        new DefaultCacheProvider(), 100, 0, Integer.MAX_VALUE);
+    StringBuilder appendFull = new StringBuilder();
+    for (int i = 0; i < 3; ++i) {
+      String appendNew = "-" + i;
+      appendFull.append(appendNew);
+      r.appendRewriter(new AppendRewriter(appendNew));
+    }
+    String inputContent = "foo";
+    String rewrittenContent = inputContent + appendFull.toString();
+    
+    GadgetSpec spec = EasyMock.createNiceMock(GadgetSpec.class);
+    View view = EasyMock.createNiceMock(View.class);
+    expect(view.getName()).andReturn(GadgetSpec.DEFAULT_VIEW).anyTimes();
+    expect(view.getType()).andReturn(View.ContentType.HTML).anyTimes();
+    expect(view.getContent()).andReturn(inputContent).anyTimes();
+    expect(spec.getView(GadgetSpec.DEFAULT_VIEW)).andReturn(view).anyTimes();
+    expect(spec.getAttribute(GadgetSpec.EXPIRATION_ATTRIB)).andReturn(Long.MAX_VALUE).anyTimes();
+    expect(spec.getUrl()).andReturn(new URI("http://gadget.org/gadget.xml")).anyTimes();
+    GadgetContext context = EasyMock.createNiceMock(GadgetContext.class);
+    expect(context.getView()).andReturn(GadgetSpec.DEFAULT_VIEW).anyTimes();
+    replay(context, view, spec);
+    
+    Gadget gadget = new Gadget(context, spec, null, null);
+    assertEquals(inputContent, gadget.getContent());
+    
+    // Should be rewritten the first time.
+    assertTrue(r.rewriteGadget(context, gadget));
+    assertEquals(rewrittenContent, gadget.getContent());
+    
+    r.appendRewriter(new AppendRewriter("-end"));
+    
+    // Should also be rewritten the second time, but with the previous
+    // expected rewritten content value.
+    Gadget nextGadget = new Gadget(context, spec, null, null);
+    assertTrue(r.rewriteGadget(context, nextGadget));
+    assertEquals(rewrittenContent, nextGadget.getContent());
+  }
+}

Modified: 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=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/UrlGeneratorTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/servlet/UrlGeneratorTest.java Tue Sep  2 16:55:29 2008
@@ -62,7 +62,7 @@
 
   @Override
   public void setUp() throws Exception {
-    realUrlGenerator = new UrlGenerator(IFR_PREFIX, JS_PREFIX, registry, containerConfig);
+    realUrlGenerator = new UrlGenerator(IFR_PREFIX, JS_PREFIX, registry);
 
     expect(context.getContainer()).andReturn(CONTAINER).anyTimes();
     expect(context.getUrl()).andReturn(URI.create(SPEC_URL)).anyTimes();
@@ -129,8 +129,8 @@
           " <UserPref name='" + UP_NAME + "' datatype='string'/>" +
           "</Module>";
     GadgetSpec spec = new GadgetSpec(URI.create(SPEC_URL), xml);
-    Gadget gadget = new Gadget(context, spec, Collections.<JsLibrary>emptyList());
     replay();
+    Gadget gadget = new Gadget(context, spec, Collections.<JsLibrary>emptyList(), containerConfig);
 
     URI iframeUrl = URI.create(realUrlGenerator.getIframeUrl(gadget));
 
@@ -149,9 +149,9 @@
           " <UserPref name='" + UP_NAME + "' datatype='string'/>" +
           "</Module>";
     GadgetSpec spec = new GadgetSpec(URI.create(SPEC_URL), xml);
-    Gadget gadget = new Gadget(context, spec, Collections.<JsLibrary>emptyList());
     replay();
-
+    Gadget gadget = new Gadget(context, spec, Collections.<JsLibrary>emptyList(), containerConfig);
+    
     URI iframeUrl = URI.create(realUrlGenerator.getIframeUrl(gadget));
 
     assertEquals(TYPE_URL_HREF_HOST, iframeUrl.getAuthority());

Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/GadgetSpecTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/GadgetSpecTest.java?rev=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/GadgetSpecTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/GadgetSpecTest.java Tue Sep  2 16:55:29 2008
@@ -104,7 +104,7 @@
     substituter.addSubstitution(Type.USER_PREF, "title", title);
     substituter.addSubstitution(Type.MESSAGE, "content", content);
 
-    GadgetSpec spec = new GadgetSpec(SPEC_URL, xml).substitute(substituter, false);
+    GadgetSpec spec = new GadgetSpec(SPEC_URL, xml).substitute(substituter);
     assertEquals(title, spec.getModulePrefs().getTitle());
     assertEquals(content, spec.getView(GadgetSpec.DEFAULT_VIEW).getContent());
   }

Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ViewTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ViewTest.java?rev=691437&r1=691436&r2=691437&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ViewTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ViewTest.java Tue Sep  2 16:55:29 2008
@@ -150,7 +150,7 @@
     substituter.addSubstitution(Type.MODULE, "ID", "3");
 
     View view = new View("test",
-        Arrays.asList(XmlUtil.parse(xml))).substitute(substituter, false);
+        Arrays.asList(XmlUtil.parse(xml))).substitute(substituter);
     assertEquals("Hello, foo Earthright 3", view.getContent());
   }
 
@@ -166,7 +166,7 @@
     substituter.addSubstitution(Type.MODULE, "ID", "123");
 
     View view = new View("test",
-        Arrays.asList(XmlUtil.parse(xml))).substitute(substituter, false);
+        Arrays.asList(XmlUtil.parse(xml))).substitute(substituter);
     assertEquals("http://up.example.org/123?dir=rtl",
                  view.getHref().toString());
   }