Author: etnu
Date: Tue Aug 26 15:54:34 2008
New Revision: 689283

URL: http://svn.apache.org/viewvc?rev=689283&view=rev
Log:
Initial commit for SHINDIG-518.

Renderer will become the new primary code path for rendering gadgets, 
eventually replacing GadgetRenderingTask.

This is obviously not a full implementation, and it only supports the basic 
proxied content retrieval. Many more patches to follow.


Added:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Renderer.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/RenderingException.java
    
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/RendererTest.java

Added: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Renderer.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Renderer.java?rev=689283&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Renderer.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Renderer.java
 Tue Aug 26 15:54:34 2008
@@ -0,0 +1,77 @@
+/*
+ * 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;
+
+import org.apache.shindig.common.uri.Uri;
+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.spec.GadgetSpec;
+import org.apache.shindig.gadgets.spec.View;
+
+import com.google.inject.Inject;
+
+/**
+ * Handles producing output markup for a gadget based on the provided context.
+ */
+public class Renderer {
+  private final GadgetSpecFactory gadgetSpecFactory;
+  private final HttpFetcher httpFetcher;
+
+  @Inject
+  public Renderer(GadgetSpecFactory gadgetSpecFactory, HttpFetcher 
httpFetcher) {
+    this.gadgetSpecFactory = gadgetSpecFactory;
+    this.httpFetcher = httpFetcher;
+  }
+
+  /**
+   * Render the gadget into a string by performing the following steps:
+   *
+   * - Retrieve gadget specification information (GadgetSpec, MessageBundle, 
etc.)
+   *
+   * - Fetch any preloaded data needed to handle the request, as handled by 
Preloader.
+   *
+   * - Perform rewriting operations on the output content, handled by Rewriter.
+   *
+   * @param context The context for the gadget rendering operation.
+   * @return The rendered gadget content
+   * @throws RenderingException if any issues arise that prevent rendering.
+   */
+  public String render(GadgetContext context) throws RenderingException {
+    try {
+      GadgetSpec spec = gadgetSpecFactory.getGadgetSpec(context);
+      View view = spec.getView(context.getView());
+      if (view.getType() == View.ContentType.URL) {
+        throw new RenderingException("Attempted to render a url-type gadget.");
+      }
+
+      // TODO: Add current url to GadgetContext to support transitive proxying.
+
+      if (view.getHref() == null) {
+        return view.getContent();
+      } else {
+        HttpRequest request = new HttpRequest(Uri.fromJavaUri(view.getHref()));
+        HttpResponse response = httpFetcher.fetch(request);
+        return response.getResponseAsString();
+      }
+    } catch (GadgetException e) {
+      throw new RenderingException(e);
+    }
+  }
+}

Added: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/RenderingException.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/RenderingException.java?rev=689283&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/RenderingException.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/RenderingException.java
 Tue Aug 26 15:54:34 2008
@@ -0,0 +1,39 @@
+/*
+ * 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;
+
+/**
+ * Exceptions thrown during gadget rendering.
+ *
+ * These execeptions will usually translate directly into an end-user error 
message, so they should
+ * be easily localizable.
+ */
+public class RenderingException extends Exception {
+  public RenderingException(Throwable t) {
+    super(t);
+  }
+
+  public RenderingException(String message) {
+    super(message);
+  }
+
+  public RenderingException(String message, Throwable t) {
+    super(message, t);
+  }
+}

Added: 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/RendererTest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/RendererTest.java?rev=689283&view=auto
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/RendererTest.java
 (added)
+++ 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/RendererTest.java
 Tue Aug 26 15:54:34 2008
@@ -0,0 +1,115 @@
+/*
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.shindig.common.uri.Uri;
+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.spec.GadgetSpec;
+
+import com.google.common.collect.Maps;
+
+import org.junit.Test;
+
+import java.net.URI;
+import java.util.Map;
+
+/**
+ * Tests for Renderer
+ */
+public class RendererTest {
+  private static final Uri SPEC_URL = 
Uri.parse("http://example.org/gadget.xml";);
+  private static final String BASIC_HTML_CONTENT = "Hello, World!";
+  private static final String PROXIED_HTML_CONTENT = "Hello, Universe!";
+  private static final Uri PROXIED_HTML_HREF = 
Uri.parse("http://example.org/proxied.php";);
+  private static final String GADGET =
+      "<Module>" +
+      " <ModulePrefs title='foo'/>" +
+      " <Content view='html' type='html'>" + BASIC_HTML_CONTENT + "</Content>" 
+
+      " <Content view='proxied' type='html' href='" + PROXIED_HTML_HREF + 
"'/>" +
+      " <Content view='url' type='url' 
href='http://example.org/always/an/error.html'/>" +
+      "</Module>";
+
+  private final FakeHttpFetcher httpFetcher = new FakeHttpFetcher();
+
+  private final Renderer renderer = new Renderer(new FakeGadgetSpecFactory(), 
httpFetcher);
+
+  private GadgetContext makeContext(final String view, final Uri specUrl) {
+    return new GadgetContext() {
+      @Override
+      public URI getUrl() {
+        return specUrl.toJavaUri();
+      }
+
+      @Override
+      public String getView() {
+        return view;
+      }
+    };
+  }
+
+  @Test
+  public void renderPlainTypeHtml() throws Exception {
+    String content = renderer.render(makeContext("html", SPEC_URL));
+    assertEquals(BASIC_HTML_CONTENT, content);
+  }
+
+  @Test
+  public void renderProxiedTypeHtml() throws Exception {
+    httpFetcher.responses.put(PROXIED_HTML_HREF, new 
HttpResponse(PROXIED_HTML_CONTENT));
+    String content = renderer.render(makeContext("proxied", SPEC_URL));
+    assertEquals(PROXIED_HTML_CONTENT, content);
+  }
+
+  @Test(expected = RenderingException.class)
+  public void renderTypeUrl() throws RenderingException {
+    renderer.render(makeContext("url", SPEC_URL));
+  }
+
+  @Test(expected = RenderingException.class)
+  public void renderInvalidUrl() throws RenderingException {
+    renderer.render(makeContext("url", Uri.parse("doesnotexist")));    
+  }
+
+  private static class FakeGadgetSpecFactory implements GadgetSpecFactory {
+    public GadgetSpec getGadgetSpec(GadgetContext context) throws 
GadgetException {
+      return new GadgetSpec(context.getUrl(), GADGET);
+    }
+
+    public GadgetSpec getGadgetSpec(URI uri, boolean ignoreCache) {
+      throw new UnsupportedOperationException();
+    }
+  }
+
+  private static class FakeHttpFetcher implements HttpFetcher {
+    private final Map<Uri, HttpResponse> responses = Maps.newHashMap();
+
+    public HttpResponse fetch(HttpRequest request) throws GadgetException {
+      HttpResponse response = responses.get(request.getUri());
+      if (response == null) {
+        throw new 
GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT,
+            "Unknown gadget: " + request.getUri());
+      }
+      return response;
+    }
+  }
+}


Reply via email to