Commits

Anonymous committed ace988a

Extract ContentSourceFactory from StaticContentRouter

  • Participants
  • Parent commits 3d4d9f9

Comments (0)

Files changed (10)

chempound-webapp/src/main/java/net/chempound/webapp/CssContentRouter.java

 package net.chempound.webapp;
 
-import net.chempound.config.ChempoundConfiguration;
+import net.chempound.webapp.content.ContentSourceFactory;
 import net.chempound.webapp.plugin.CachedStaticContentRouter;
 import net.chempound.webapp.plugins.CssPlugin;
 import net.chempound.webapp.plugins.CssRegistry;
     private final CssRegistry cssRegistry;
 
     @Inject
-    public CssContentRouter(final Context context, final ChempoundConfiguration configuration, final CssRegistry cssRegistry) throws IOException {
-        super("css", context, configuration);
+    public CssContentRouter(final Context context, final ContentSourceFactory contentSourceFactory, final CssRegistry cssRegistry) throws IOException {
+        super("css", context, contentSourceFactory);
         this.cssRegistry = cssRegistry;
     }
 

chempound-webapp/src/main/java/net/chempound/webapp/CssRouter.java

-package net.chempound.webapp;
-
-import net.chempound.config.ChempoundConfiguration;
-import net.chempound.webapp.plugin.CachedStaticContentRouter;
-import net.chempound.webapp.plugins.CssRegistry;
-import org.restlet.Context;
-
-import javax.inject.Inject;
-import java.io.IOException;
-
-/**
- * @author Sam Adams
- */
-public class CssRouter extends CachedStaticContentRouter {
-
-    private final CssRegistry cssRegistry;
-
-    @Inject
-    public CssRouter(final Context context, final ChempoundConfiguration configuration, final CssRegistry cssRegistry) throws IOException {
-        super("css", context, configuration);
-        this.cssRegistry = cssRegistry;
-
-        doAttachRoutes();
-    }
-
-    @Override
-    protected void doAttachRoutes() throws IOException {
-        attachRoute("css", CssRouter.class, "/net/chempound/web/css/");
-    }
-
-}

chempound-webapp/src/main/java/net/chempound/webapp/DefaultChempoundWebModule.java

 import com.google.inject.multibindings.MapBinder;
 import com.google.inject.multibindings.Multibinder;
 import net.chempound.services.FreemarkerService;
+import net.chempound.webapp.content.ClasspathContentSourceFactory;
+import net.chempound.webapp.content.ContentSourceFactory;
 import net.chempound.webapp.output.ContentType;
 import net.chempound.webapp.plugin.DefaultCssRegistry;
 import net.chempound.webapp.plugin.DefaultPluginRegistry;
         bind(FreemarkerService.class).to(DefaultFreemarkerService.class);
         bind(TemplateLoaderFactory.class).to(ClasspathTemplateLoaderFactory.class);
 
+        bind(ContentSourceFactory.class).to(ClasspathContentSourceFactory.class);
         bind(PluginRegistry.class).to(DefaultPluginRegistry.class);
         bind(CssRegistry.class).to(DefaultCssRegistry.class);
         bind(SkinRegistry.class).to(DefaultSkinRegistry.class);

chempound-webapp/src/main/java/net/chempound/webapp/SkinContentRouter.java

 package net.chempound.webapp;
 
-import net.chempound.config.ChempoundConfiguration;
+import net.chempound.webapp.content.ContentSourceFactory;
 import net.chempound.webapp.plugin.CachedStaticContentRouter;
 import net.chempound.webapp.plugins.Skin;
 import net.chempound.webapp.plugins.SkinRegistry;
     private final SkinRegistry skinRegistry;
 
     @Inject
-    public SkinContentRouter(final Context context, final ChempoundConfiguration configuration, final SkinRegistry skinRegistry) throws IOException {
-        super("skin", context, configuration);
+    public SkinContentRouter(final Context context, final ContentSourceFactory contentSourceFactory, final SkinRegistry skinRegistry) throws IOException {
+        super("skin", context, contentSourceFactory);
         this.skinRegistry = skinRegistry;
     }
 

chempound-webapp/src/main/java/net/chempound/webapp/StaticContentRouter.java

 package net.chempound.webapp;
 
-import net.chempound.config.ChempoundConfiguration;
+import net.chempound.webapp.content.ContentSourceFactory;
 import net.chempound.webapp.plugin.CachedStaticContentRouter;
 import org.restlet.Context;
 
 public class StaticContentRouter extends CachedStaticContentRouter {
 
     @Inject
-    public StaticContentRouter(final Context context, final ChempoundConfiguration configuration) throws IOException {
-        super("static", context, configuration);
+    public StaticContentRouter(final Context context, final ContentSourceFactory contentSourceFactory) throws IOException {
+        super("static", context, contentSourceFactory);
     }
 
     @Override

chempound-webapp/src/main/java/net/chempound/webapp/content/ClasspathContentSourceFactory.java

+package net.chempound.webapp.content;
+
+import org.restlet.data.LocalReference;
+
+import java.io.IOException;
+
+public class ClasspathContentSourceFactory implements ContentSourceFactory {
+
+    @Override
+    public LocalReference createReference(final String contentType, final String id, final Class<?> context, final String path) throws IOException {
+        return LocalReference.createClapReference(LocalReference.CLAP_CLASS, path);
+    }
+}

chempound-webapp/src/main/java/net/chempound/webapp/content/ContentSourceFactory.java

+package net.chempound.webapp.content;
+
+import org.restlet.data.LocalReference;
+
+import java.io.IOException;
+
+public interface ContentSourceFactory {
+
+    LocalReference createReference(String contentType, String id, Class<?> context, String path) throws IOException;
+
+}

chempound-webapp/src/main/java/net/chempound/webapp/content/FileCacheContentSourceFactory.java

+package net.chempound.webapp.content;
+
+import net.chempound.config.ChempoundConfiguration;
+import net.chempound.webapp.FileExtractor;
+import org.apache.commons.io.FileUtils;
+import org.restlet.data.LocalReference;
+
+import javax.inject.Singleton;
+import java.io.File;
+import java.io.IOException;
+
+@Singleton
+public class FileCacheContentSourceFactory implements ContentSourceFactory {
+
+    private final File cachePath;
+
+    public FileCacheContentSourceFactory(final ChempoundConfiguration config) {
+        this.cachePath = config.getCacheDirectory();
+    }
+
+    @Override
+    public LocalReference createReference(final String contentType, final String id, final Class<?> contentContext, final String contentPath) throws IOException {
+        final File root = new File(cachePath, contentType + "/" + id + "/");
+        FileUtils.forceMkdir(root);
+        FileExtractor.extract(contentContext, contentPath, root);
+        return LocalReference.createFileReference(root);
+    }
+}

chempound-webapp/src/main/java/net/chempound/webapp/plugin/CachedStaticContentRouter.java

 package net.chempound.webapp.plugin;
 
-import net.chempound.config.ChempoundConfiguration;
-import net.chempound.webapp.FileExtractor;
-import org.apache.commons.io.FileUtils;
+import net.chempound.webapp.content.ContentSourceFactory;
 import org.restlet.Context;
-import org.restlet.data.LocalReference;
 import org.restlet.resource.Directory;
 import org.restlet.routing.Router;
 import org.restlet.routing.Template;
 
 import javax.inject.Inject;
-import java.io.File;
 import java.io.IOException;
 
 /**
  */
 public abstract class CachedStaticContentRouter extends Router {
 
-    private final ChempoundConfiguration configuration;
     private final String cacheContext;
+    private final ContentSourceFactory contentSourceFactory;
 
     @Inject
-    public CachedStaticContentRouter(final String cacheContext, final Context context, final ChempoundConfiguration configuration) {
+    public CachedStaticContentRouter(final String cacheContext, final Context context, final ContentSourceFactory contentSourceFactory) {
         super(context);
         this.cacheContext = cacheContext;
-        this.configuration = configuration;
+        this.contentSourceFactory = contentSourceFactory;
 
         setDefaultMatchingMode(Template.MODE_STARTS_WITH);
     }
     }
 
     private Directory createDirectory(final String id, final Class<?> contentContext,final String contentPath) throws IOException {
-        final Directory directory = new Directory(getContext(), getReference(id, contentContext, contentPath));
+        final Directory directory = new Directory(getContext(), contentSourceFactory.createReference(cacheContext, id, contentContext, contentPath));
         directory.setListingAllowed(true);
         directory.setNegotiatingContent(false);
         return directory;
     }
-
-    private LocalReference getReference(final String id, final Class<?> contentContext,final String contentPath) throws IOException {
-        final File root = new File(configuration.getWorkspace(), "cache/" + cacheContext + "/" + id + "/");
-        FileUtils.forceMkdir(root);
-        FileExtractor.extract(contentContext, contentPath, root);
-        return LocalReference.createFileReference(root);
-    }
-
 }

chempound-webapp/src/main/java/net/chempound/webapp/plugin/PluginRouter.java

 package net.chempound.webapp.plugin;
 
-import net.chempound.config.ChempoundConfiguration;
+import net.chempound.webapp.content.ContentSourceFactory;
 import net.chempound.webapp.plugins.ChempoundWebPlugin;
 import net.chempound.webapp.plugins.PluginRegistry;
 import org.restlet.Context;
     private final PluginRegistry registry;
 
     @Inject
-    public PluginRouter(final Context context, final PluginRegistry registry, final ChempoundConfiguration configuration) throws IOException {
-        super("plugin", context, configuration);
+    public PluginRouter(final Context context, final PluginRegistry registry, final ContentSourceFactory contentSourceFactory) throws IOException {
+        super("plugin", context, contentSourceFactory);
         this.registry = registry;
     }