Reviewers: metaweta,

Description:
Changes CajolingService to take a UriCallback object which rewrites uris

Please review this at http://codereview.appspot.com/141042

Affected files:
  M     src/com/google/caja/service/CajolingService.java


Index: src/com/google/caja/service/CajolingService.java
===================================================================
--- src/com/google/caja/service/CajolingService.java    (revision 3825)
+++ src/com/google/caja/service/CajolingService.java    (working copy)
@@ -47,17 +47,40 @@
  * @author [email protected] (Jasvir Nagra)
  */
 public class CajolingService extends HttpServlet {
+  private DEFAULT_HOST = "http://caja.appspot.com/cajoler";;
+
   private List<ContentHandler> handlers = new Vector<ContentHandler>();
   private ContentTypeCheck typeCheck = new LooseContentTypeCheck();
-  private String host = "http://caja.appspot.com/cajoler";;
+  private String host;
+  private UriCallback cb;

   public CajolingService(BuildInfo buildInfo) {
-    registerHandlers(buildInfo);
+    registerHandlers(buildInfo, DEFAULT_HOST);
   }

   public CajolingService(BuildInfo buildInfo, String host) {
+    this(buildInfo, host, new UriCallback() {
+      public Reader retrieve(ExternalReference extref, String mimeType)
+          throws UriCallbackException {
+        try {
+          FetchedData data = fetch(extref.getUri());
+          return new InputStreamReader(
+ new ByteArrayInputStream(data.getContent()), data.getCharSet());
+        } catch (IOException ex) {
+          throw new UriCallbackException(extref, ex);
+        }
+      }
+
+      public URI rewrite(ExternalReference extref, String mimeType) {
+        return null;
+      }
+    });
+  }
+
+ public CajolingService(BuildInfo buildInfo, String host, UriCallback cb) {
     this.host = host;
     registerHandlers(buildInfo);
+    this.cb = cb;
   }

   /**
@@ -208,27 +231,11 @@
   }

   public void registerHandlers(BuildInfo buildInfo) {
-    UriCallback retriever = new UriCallback() {
-      public Reader retrieve(ExternalReference extref, String mimeType)
-          throws UriCallbackException {
-        try {
-          FetchedData data = fetch(extref.getUri());
-          return new InputStreamReader(
- new ByteArrayInputStream(data.getContent()), data.getCharSet());
-        } catch (IOException ex) {
-          throw new UriCallbackException(extref, ex);
-        }
-      }
-
-      public URI rewrite(ExternalReference extref, String mimeType) {
-        return null;
-      }
-    };
     handlers.add(new JsHandler(buildInfo));
     handlers.add(new ImageHandler());
-    handlers.add(new GadgetHandler(buildInfo, retriever));
+    handlers.add(new GadgetHandler(buildInfo, cb));
     handlers.add(new InnocentHandler());
-    handlers.add(new HtmlHandler(buildInfo, host, retriever));
+    handlers.add(new HtmlHandler(buildInfo, host, cb));
   }

   protected FetchedData fetch(URI uri) throws IOException {


Reply via email to