Serve images from external directory via servlet

This commit is contained in:
mandlm 2015-12-29 20:05:06 +01:00
parent 844ea49ed4
commit 27c0416d8f
7 changed files with 61 additions and 26 deletions

View File

@ -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.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath("currentImageURL") @RemoteServiceRelativePath("currentImageName")
public interface CurrentImageURLService extends RemoteService public interface CurrentImageNameService extends RemoteService
{ {
String getCurrentImageURL(); String getCurrentImageName();
} }

View File

@ -0,0 +1,8 @@
package net.molez.mandlm.fotostream.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface CurrentImageNameServiceAsync
{
void getCurrentImageName(AsyncCallback<String> callback);
}

View File

@ -1,8 +0,0 @@
package net.molez.mandlm.fotostream.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface CurrentImageURLServiceAsync
{
void getCurrentImageURL(AsyncCallback<String> callback);
}

View File

@ -12,7 +12,7 @@ import com.google.gwt.user.client.ui.RootPanel;
public class FotoStream implements EntryPoint public class FotoStream implements EntryPoint
{ {
private final CurrentImageURLServiceAsync imageURLService = GWT.create(CurrentImageURLService.class); private final CurrentImageNameServiceAsync imageNameService = GWT.create(CurrentImageNameService.class);
public void onModuleLoad() public void onModuleLoad()
{ {
@ -49,7 +49,7 @@ public class FotoStream implements EntryPoint
@Override @Override
public void run() public void run()
{ {
imageURLService.getCurrentImageURL(new AsyncCallback<String>() imageNameService.getCurrentImageName(new AsyncCallback<String>()
{ {
@Override @Override
public void onFailure(Throwable caught) public void onFailure(Throwable caught)
@ -60,10 +60,7 @@ public class FotoStream implements EntryPoint
@Override @Override
public void onSuccess(String result) public void onSuccess(String result)
{ {
if (result != image.getUrl()) image.setUrl("image?name=" + result);
{
image.setUrl(result);
}
} }
}); });
} }

View File

@ -9,10 +9,10 @@ import java.util.Random;
import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import net.molez.mandlm.fotostream.client.CurrentImageURLService; import net.molez.mandlm.fotostream.client.CurrentImageNameService;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class CurrentImageURLServiceImpl extends RemoteServiceServlet implements CurrentImageURLService public class CurrentImageNameServiceImpl extends RemoteServiceServlet implements CurrentImageNameService
{ {
private List<File> imageFiles = new ArrayList<File>(); private List<File> imageFiles = new ArrayList<File>();
@ -44,7 +44,7 @@ public class CurrentImageURLServiceImpl extends RemoteServiceServlet implements
}); });
} }
public String getCurrentImageURL() public String getCurrentImageName()
{ {
int newImageSeconds = 60; int newImageSeconds = 60;
@ -60,11 +60,11 @@ public class CurrentImageURLServiceImpl extends RemoteServiceServlet implements
if (latestFileDate.before(new Date(new Date().getTime() - newImageSeconds * 1000))) 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 else
{ {
return latestFile.getPath(); return latestFile.getName();
} }
} }
} }

View File

@ -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();
}
}

View File

@ -8,13 +8,23 @@
<!-- Servlets --> <!-- Servlets -->
<servlet> <servlet>
<servlet-name>currentImageURLServlet</servlet-name> <servlet-name>currentImageNameServlet</servlet-name>
<servlet-class>net.molez.mandlm.fotostream.server.CurrentImageURLServiceImpl</servlet-class> <servlet-class>net.molez.mandlm.fotostream.server.CurrentImageNameServiceImpl</servlet-class>
</servlet> </servlet>
<servlet-mapping> <servlet-mapping>
<servlet-name>currentImageURLServlet</servlet-name> <servlet-name>currentImageNameServlet</servlet-name>
<url-pattern>/fotostream/currentImageURL</url-pattern> <url-pattern>/fotostream/currentImageName</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>net.molez.mandlm.fotostream.server.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/image</url-pattern>
</servlet-mapping> </servlet-mapping>
<!-- Default page to serve --> <!-- Default page to serve -->