From 27c0416d8f31c30fa402a3444a4cfe6f81349b4a Mon Sep 17 00:00:00 2001 From: mandlm Date: Tue, 29 Dec 2015 20:05:06 +0100 Subject: [PATCH] Serve images from external directory via servlet --- ...vice.java => CurrentImageNameService.java} | 6 ++-- .../client/CurrentImageNameServiceAsync.java | 8 ++++++ .../client/CurrentImageURLServiceAsync.java | 8 ------ .../mandlm/fotostream/client/FotoStream.java | 9 ++---- ....java => CurrentImageNameServiceImpl.java} | 10 +++---- .../fotostream/server/ImageServlet.java | 28 +++++++++++++++++++ war/WEB-INF/web.xml | 18 +++++++++--- 7 files changed, 61 insertions(+), 26 deletions(-) rename src/net/molez/mandlm/fotostream/client/{CurrentImageURLService.java => CurrentImageNameService.java} (54%) create mode 100644 src/net/molez/mandlm/fotostream/client/CurrentImageNameServiceAsync.java delete mode 100644 src/net/molez/mandlm/fotostream/client/CurrentImageURLServiceAsync.java rename src/net/molez/mandlm/fotostream/server/{CurrentImageURLServiceImpl.java => CurrentImageNameServiceImpl.java} (83%) create mode 100644 src/net/molez/mandlm/fotostream/server/ImageServlet.java diff --git a/src/net/molez/mandlm/fotostream/client/CurrentImageURLService.java b/src/net/molez/mandlm/fotostream/client/CurrentImageNameService.java similarity index 54% rename from src/net/molez/mandlm/fotostream/client/CurrentImageURLService.java rename to src/net/molez/mandlm/fotostream/client/CurrentImageNameService.java index f4e1c2e..8a0ff81 100644 --- a/src/net/molez/mandlm/fotostream/client/CurrentImageURLService.java +++ b/src/net/molez/mandlm/fotostream/client/CurrentImageNameService.java @@ -3,8 +3,8 @@ package net.molez.mandlm.fotostream.client; import com.google.gwt.user.client.rpc.RemoteService; import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; -@RemoteServiceRelativePath("currentImageURL") -public interface CurrentImageURLService extends RemoteService +@RemoteServiceRelativePath("currentImageName") +public interface CurrentImageNameService extends RemoteService { - String getCurrentImageURL(); + String getCurrentImageName(); } diff --git a/src/net/molez/mandlm/fotostream/client/CurrentImageNameServiceAsync.java b/src/net/molez/mandlm/fotostream/client/CurrentImageNameServiceAsync.java new file mode 100644 index 0000000..2a77ac4 --- /dev/null +++ b/src/net/molez/mandlm/fotostream/client/CurrentImageNameServiceAsync.java @@ -0,0 +1,8 @@ +package net.molez.mandlm.fotostream.client; + +import com.google.gwt.user.client.rpc.AsyncCallback; + +public interface CurrentImageNameServiceAsync +{ + void getCurrentImageName(AsyncCallback callback); +} diff --git a/src/net/molez/mandlm/fotostream/client/CurrentImageURLServiceAsync.java b/src/net/molez/mandlm/fotostream/client/CurrentImageURLServiceAsync.java deleted file mode 100644 index 65e6f91..0000000 --- a/src/net/molez/mandlm/fotostream/client/CurrentImageURLServiceAsync.java +++ /dev/null @@ -1,8 +0,0 @@ -package net.molez.mandlm.fotostream.client; - -import com.google.gwt.user.client.rpc.AsyncCallback; - -public interface CurrentImageURLServiceAsync -{ - void getCurrentImageURL(AsyncCallback callback); -} diff --git a/src/net/molez/mandlm/fotostream/client/FotoStream.java b/src/net/molez/mandlm/fotostream/client/FotoStream.java index 45cd3a5..6dab343 100644 --- a/src/net/molez/mandlm/fotostream/client/FotoStream.java +++ b/src/net/molez/mandlm/fotostream/client/FotoStream.java @@ -12,7 +12,7 @@ import com.google.gwt.user.client.ui.RootPanel; public class FotoStream implements EntryPoint { - private final CurrentImageURLServiceAsync imageURLService = GWT.create(CurrentImageURLService.class); + private final CurrentImageNameServiceAsync imageNameService = GWT.create(CurrentImageNameService.class); public void onModuleLoad() { @@ -49,7 +49,7 @@ public class FotoStream implements EntryPoint @Override public void run() { - imageURLService.getCurrentImageURL(new AsyncCallback() + imageNameService.getCurrentImageName(new AsyncCallback() { @Override public void onFailure(Throwable caught) @@ -60,10 +60,7 @@ public class FotoStream implements EntryPoint @Override public void onSuccess(String result) { - if (result != image.getUrl()) - { - image.setUrl(result); - } + image.setUrl("image?name=" + result); } }); } diff --git a/src/net/molez/mandlm/fotostream/server/CurrentImageURLServiceImpl.java b/src/net/molez/mandlm/fotostream/server/CurrentImageNameServiceImpl.java similarity index 83% rename from src/net/molez/mandlm/fotostream/server/CurrentImageURLServiceImpl.java rename to src/net/molez/mandlm/fotostream/server/CurrentImageNameServiceImpl.java index db85f5a..8a668aa 100644 --- a/src/net/molez/mandlm/fotostream/server/CurrentImageURLServiceImpl.java +++ b/src/net/molez/mandlm/fotostream/server/CurrentImageNameServiceImpl.java @@ -9,10 +9,10 @@ import java.util.Random; import com.google.gwt.user.server.rpc.RemoteServiceServlet; -import net.molez.mandlm.fotostream.client.CurrentImageURLService; +import net.molez.mandlm.fotostream.client.CurrentImageNameService; @SuppressWarnings("serial") -public class CurrentImageURLServiceImpl extends RemoteServiceServlet implements CurrentImageURLService +public class CurrentImageNameServiceImpl extends RemoteServiceServlet implements CurrentImageNameService { private List imageFiles = new ArrayList(); @@ -44,7 +44,7 @@ public class CurrentImageURLServiceImpl extends RemoteServiceServlet implements }); } - public String getCurrentImageURL() + public String getCurrentImageName() { int newImageSeconds = 60; @@ -60,11 +60,11 @@ public class CurrentImageURLServiceImpl extends RemoteServiceServlet implements if (latestFileDate.before(new Date(new Date().getTime() - newImageSeconds * 1000))) { - return imageFiles.get(new Random().nextInt(imageFiles.size())).getPath(); + return imageFiles.get(new Random().nextInt(imageFiles.size())).getName(); } else { - return latestFile.getPath(); + return latestFile.getName(); } } } diff --git a/src/net/molez/mandlm/fotostream/server/ImageServlet.java b/src/net/molez/mandlm/fotostream/server/ImageServlet.java new file mode 100644 index 0000000..f4161e3 --- /dev/null +++ b/src/net/molez/mandlm/fotostream/server/ImageServlet.java @@ -0,0 +1,28 @@ +package net.molez.mandlm.fotostream.server; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; + +import javax.imageio.ImageIO; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@SuppressWarnings("serial") +public class ImageServlet extends HttpServlet +{ + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + String imageName = request.getParameter("name"); + File f = new File("img/" + imageName); + + response.setContentType("image/jpeg"); + OutputStream out = response.getOutputStream(); + + ImageIO.write(ImageIO.read(f), "jpg", out); + out.close(); + } +} diff --git a/war/WEB-INF/web.xml b/war/WEB-INF/web.xml index 003a6da..cd7c9c7 100644 --- a/war/WEB-INF/web.xml +++ b/war/WEB-INF/web.xml @@ -8,13 +8,23 @@ - currentImageURLServlet - net.molez.mandlm.fotostream.server.CurrentImageURLServiceImpl + currentImageNameServlet + net.molez.mandlm.fotostream.server.CurrentImageNameServiceImpl - currentImageURLServlet - /fotostream/currentImageURL + currentImageNameServlet + /fotostream/currentImageName + + + + ImageServlet + net.molez.mandlm.fotostream.server.ImageServlet + + + + ImageServlet + /image