diff --git a/appengine/src/main/java/org/retrostore/request/PolymerRequest.java b/appengine/src/main/java/org/retrostore/request/PolymerRequest.java index 941397b..3910586 100644 --- a/appengine/src/main/java/org/retrostore/request/PolymerRequest.java +++ b/appengine/src/main/java/org/retrostore/request/PolymerRequest.java @@ -32,7 +32,7 @@ public class PolymerRequest implements Request { private static final String POLYMER_ROOT = "WEB-INF/polymer-app"; private static final Set FORWARD = Sets.newHashSet("/bower_components", - "/images", "/src", "/service-worker.js", "/manifest.json", "/index.html"); + "/favicon", "/images", "/src", "/service-worker.js", "/manifest.json", "/index.html"); private final ResourceLoader mResourceLoader; @@ -53,9 +53,14 @@ public boolean serveUrl(RequestData requestData, url = "/index.html"; } + // Re-write the favicon path in case the browser is requesting it. + if (url.equals("/favicon.ico")) { + url = "/favicon/favicon.ico"; + } + for (String path : FORWARD) { if (url.startsWith(path)) { - Optional content = mResourceLoader.load(POLYMER_ROOT + url); + Optional content = mResourceLoader.load(POLYMER_ROOT + url); if (content.isPresent()) { responder.respond(content.get(), fromFilename(url)); } diff --git a/appengine/src/main/java/org/retrostore/request/Responder.java b/appengine/src/main/java/org/retrostore/request/Responder.java index 733139b..346ed3e 100644 --- a/appengine/src/main/java/org/retrostore/request/Responder.java +++ b/appengine/src/main/java/org/retrostore/request/Responder.java @@ -67,6 +67,16 @@ public void respond(String content, ContentType contentType) { } } + /** Respond with the given content text and type. */ + public void respond(byte[] content, ContentType contentType) { + try { + mResponse.setContentType(contentType.str); + mResponse.getOutputStream().write(content); + } catch (IOException ex) { + LOG.log(Level.SEVERE, "Cannot serve data", ex); + } + } + /** Converts the given object into JSON and sends it. */ public void respondJson(Object object) { try { diff --git a/appengine/src/main/java/org/retrostore/request/ScreenshotRequest.java b/appengine/src/main/java/org/retrostore/request/ScreenshotRequest.java index 54c59d3..dceb466 100644 --- a/appengine/src/main/java/org/retrostore/request/ScreenshotRequest.java +++ b/appengine/src/main/java/org/retrostore/request/ScreenshotRequest.java @@ -18,7 +18,6 @@ import com.google.appengine.api.blobstore.BlobKey; import com.google.appengine.api.images.Image; -import com.google.appengine.api.images.ImagesService; import com.google.appengine.api.images.ImagesServiceFactory; import com.google.appengine.api.images.ServingUrlOptions; import com.google.common.base.Optional; diff --git a/appengine/src/main/java/org/retrostore/resources/DefaultResourceLoader.java b/appengine/src/main/java/org/retrostore/resources/DefaultResourceLoader.java index 9334c32..ffd861a 100644 --- a/appengine/src/main/java/org/retrostore/resources/DefaultResourceLoader.java +++ b/appengine/src/main/java/org/retrostore/resources/DefaultResourceLoader.java @@ -16,15 +16,14 @@ package org.retrostore.resources; -import com.google.common.base.Charsets; import com.google.common.base.Optional; -import com.google.common.io.CharStreams; +import com.google.common.io.ByteStreams; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Level; @@ -37,15 +36,13 @@ public class DefaultResourceLoader implements ResourceLoader { private static final Logger LOG = Logger.getLogger("DefaultResourceLoader"); @Override - public Optional load(String filename) { + public Optional load(String filename) { // TODO: Cache! // TODO: Debug mode for local reloading try { File file = new File(filename); LOG.info("Loading file: " + file.getAbsolutePath()); - InputStream fileStream = new FileInputStream(file); - String content = CharStreams.toString(new InputStreamReader(fileStream, Charsets.UTF_8)); - return Optional.of(content); + return Optional.of(toBytes(new FileInputStream(file))); } catch (IOException e) { LOG.log(Level.SEVERE, "Cannot load file.", e); } @@ -53,13 +50,11 @@ public Optional load(String filename) { } @Override - public Optional loadUrl(String urlStr) { + public Optional loadUrl(String urlStr) { try { URL url = new URL(urlStr); InputStream is = url.openStream(); - String content = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8)); - is.close(); - return Optional.of(content); + return Optional.of(toBytes(is)); } catch (MalformedURLException e) { LOG.log(Level.SEVERE, "Invalid URL", e); } catch (IOException e) { @@ -67,4 +62,10 @@ public Optional loadUrl(String urlStr) { } return Optional.absent(); } + + private byte[] toBytes(InputStream input) throws IOException { + ByteArrayOutputStream data = new ByteArrayOutputStream(); + ByteStreams.copy(input, data); + return data.toByteArray(); + } } diff --git a/appengine/src/main/java/org/retrostore/resources/PolymerDebugLoader.java b/appengine/src/main/java/org/retrostore/resources/PolymerDebugLoader.java index e458065..826165f 100644 --- a/appengine/src/main/java/org/retrostore/resources/PolymerDebugLoader.java +++ b/appengine/src/main/java/org/retrostore/resources/PolymerDebugLoader.java @@ -40,14 +40,14 @@ public PolymerDebugLoader(String polymerServer, ResourceLoader fallbackResourceL } @Override - public Optional load(String filename) { + public Optional load(String filename) { if (!filename.startsWith(POLYMER_ROOT)) { LOG.warning(String.format("Not a polymer file, falling back for '%s'.", filename)); return mFallbackResourceLoader.load(filename); } String url = mPolymerServer + filename.substring(POLYMER_ROOT.length()); - Optional resource = loadUrl(url); + Optional resource = loadUrl(url); if (!resource.isPresent()) { LOG.warning( String.format("Polymer debug resource cannot be loaded: '%s'. Falling back.", url)); @@ -59,7 +59,7 @@ public Optional load(String filename) { } @Override - public Optional loadUrl(String url) { + public Optional loadUrl(String url) { return mFallbackResourceLoader.loadUrl(url); } } diff --git a/appengine/src/main/java/org/retrostore/resources/ResourceLoader.java b/appengine/src/main/java/org/retrostore/resources/ResourceLoader.java index adbbd19..e515692 100644 --- a/appengine/src/main/java/org/retrostore/resources/ResourceLoader.java +++ b/appengine/src/main/java/org/retrostore/resources/ResourceLoader.java @@ -23,8 +23,8 @@ */ public interface ResourceLoader { /** Loads a file with the given name. */ - Optional load(String filename); + Optional load(String filename); /** Loads the given URL. */ - Optional loadUrl(String url); + Optional loadUrl(String url); }