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.RemoteServiceRelativePath;
@RemoteServiceRelativePath("currentImageURL")
public interface CurrentImageURLService extends RemoteService
@RemoteServiceRelativePath("currentImageName")
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
{
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<String>()
imageNameService.getCurrentImageName(new AsyncCallback<String>()
{
@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);
}
});
}

View File

@ -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<File> imageFiles = new ArrayList<File>();
@ -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();
}
}
}

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 -->
<servlet>
<servlet-name>currentImageURLServlet</servlet-name>
<servlet-class>net.molez.mandlm.fotostream.server.CurrentImageURLServiceImpl</servlet-class>
<servlet-name>currentImageNameServlet</servlet-name>
<servlet-class>net.molez.mandlm.fotostream.server.CurrentImageNameServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>currentImageURLServlet</servlet-name>
<url-pattern>/fotostream/currentImageURL</url-pattern>
<servlet-name>currentImageNameServlet</servlet-name>
<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>
<!-- Default page to serve -->