package com.antonytrupe.server;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.SortDirection;
@SuppressWarnings("serial")
public class ImageServlet extends HttpServlet {
private static final Logger LOGGER = Logger.getLogger(ImageServlet.class
.getName());
public final static BlobKey getBlobKeyByFileName(final String fileName) {
Filter nameFilter = new FilterPredicate("filename",
FilterOperator.EQUAL, fileName);
final Query query = new Query("__BlobInfo__").setFilter(nameFilter);
query.addSort("creation", SortDirection.DESCENDING);
final DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
final PreparedQuery pq = datastore.prepare(query);
final List<Entity> entList = pq.asList(FetchOptions.Builder
.withLimit(1));
if (entList.size() > 0) {
final String blobKeyString = entList.get(0).getKey().getName();
return new BlobKey(blobKeyString);
}
return null;
}
private final BlobstoreService blobstoreService = BlobstoreServiceFactory
.getBlobstoreService();
@Override
public void doGet(final HttpServletRequest request,
final HttpServletResponse response) throws IOException {
// number of seconds
final int cacheAge = 60 * 60;
final long expiry = new Date().getTime() + cacheAge * 1000;
response.setDateHeader("Expires", expiry);
response.setHeader("Cache-Control", "max-age=" + cacheAge);
response.setHeader("Pragma", "cache");
// System.out.println(request.getPathInfo());
// BlobKey blobKey = new BlobKey(request.getParameter("blob-key"));
final String[] parts = request.getPathInfo().split("/");
final String fileName = parts[1];
// //////////////
final BlobKey blobKey = getBlobKeyByFileName(fileName);
// //////////////
if (blobKey == null || blobKey.equals("undefined")
|| blobKey.equals("") || blobKey.equals("null")) {
// return a not found error
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
LOGGER.warning("No blob found by filename " + fileName);
} else {
final BlobInfo blobInfo = new BlobInfoFactory()
.loadBlobInfo(blobKey);
if (blobInfo != null && blobInfo.getSize() > 0) {
// process blob
this.blobstoreService.serve(blobKey, response);
} else {
// return a not found error
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
LOGGER.warning("No blob found by filename " + fileName);
}
}
}
}