Skip to content

Commit

Permalink
Support serving favicon.
Browse files Browse the repository at this point in the history
Change resource loade to load bytes, not strings.
  • Loading branch information
shaeberling committed Sep 3, 2017
1 parent 0aa2f50 commit 1be6359
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class PolymerRequest implements Request {

private static final String POLYMER_ROOT = "WEB-INF/polymer-app";
private static final Set<String> 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;

Expand All @@ -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<String> content = mResourceLoader.load(POLYMER_ROOT + url);
Optional<byte[]> content = mResourceLoader.load(POLYMER_ROOT + url);
if (content.isPresent()) {
responder.respond(content.get(), fromFilename(url));
}
Expand Down
10 changes: 10 additions & 0 deletions appengine/src/main/java/org/retrostore/request/Responder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,34 +36,36 @@ public class DefaultResourceLoader implements ResourceLoader {
private static final Logger LOG = Logger.getLogger("DefaultResourceLoader");

@Override
public Optional<String> load(String filename) {
public Optional<byte[]> 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);
}
return Optional.absent();
}

@Override
public Optional<String> loadUrl(String urlStr) {
public Optional<byte[]> 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) {
LOG.log(Level.SEVERE, "Cannot read URL", e);
}
return Optional.absent();
}

private byte[] toBytes(InputStream input) throws IOException {
ByteArrayOutputStream data = new ByteArrayOutputStream();
ByteStreams.copy(input, data);
return data.toByteArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public PolymerDebugLoader(String polymerServer, ResourceLoader fallbackResourceL
}

@Override
public Optional<String> load(String filename) {
public Optional<byte[]> 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<String> resource = loadUrl(url);
Optional<byte[]> resource = loadUrl(url);
if (!resource.isPresent()) {
LOG.warning(
String.format("Polymer debug resource cannot be loaded: '%s'. Falling back.", url));
Expand All @@ -59,7 +59,7 @@ public Optional<String> load(String filename) {
}

@Override
public Optional<String> loadUrl(String url) {
public Optional<byte[]> loadUrl(String url) {
return mFallbackResourceLoader.loadUrl(url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/
public interface ResourceLoader {
/** Loads a file with the given name. */
Optional<String> load(String filename);
Optional<byte[]> load(String filename);

/** Loads the given URL. */
Optional<String> loadUrl(String url);
Optional<byte[]> loadUrl(String url);
}

0 comments on commit 1be6359

Please sign in to comment.