package com.cineplexnotifier.rest;

import java.io.IOException;
import java.net.URL;

import javax.ejb.EJB;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.apache.commons.io.IOUtils;
import org.jboss.resteasy.annotations.cache.Cache;

import com.cineplexnotifier.data.MovieRepository;

@Path("images")
@Produces("image/jpeg")
public class ImageResouce {
	
	@EJB
	private MovieRepository dao;
	
	@GET
	@Path("thumbnail/{cineplexKey}")
	@Cache
	public byte[] getThumbnail(@PathParam("cineplexKey") String key) throws IOException {
		URL remoteUrl = new URL(dao.selectByCineplexKey(key).getThumbnailImageUrl());
		return IOUtils.toByteArray(remoteUrl.openStream());
	}

}

@@ -57,16 +57,101 @@
 eption;%0A
+import java.io.InputStream;%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;%0A
 import j
@@ -163,16 +163,16 @@
 et.URL;%0A
-
 %0Aimport 
@@ -293,16 +293,98 @@
 roduces;
+%0Aimport javax.ws.rs.RedirectionException;%0Aimport javax.ws.rs.core.Response.Status;
 %0A%0Aimport
@@ -586,17 +586,142 @@
 souce %7B%0A
-%09
+%0A%09public static final String MISSING_THUMBNAIL_URL = %22http://mediafiles.cineplex.com/Cineplex2013/postermissing_230x341.jpg%22;%0A
 %0A%09@EJB%0A%09
@@ -753,10 +753,9 @@
 ao;%0A
-%09
 %0A
+
 %09@GE
@@ -886,16 +886,39 @@
 tion
- %7B%0A%09%09URL
+, URISyntaxException %7B%0A%09%09String
  rem
@@ -930,16 +930,8 @@
 l = 
-new URL(
 dao.
@@ -981,63 +981,405 @@
 rl()
-);%0A%09%09return IOUtils.toByteArray(remoteUrl.openStream())
+;%0A%0A%09%09// Do not deliver the not found images from our server as they contain%0A%09%09// the Cineplex logo%0A%09%09if (MISSING_THUMBNAIL_URL.equals(remoteUrl)) %7B%0A%09%09%09throw new RedirectionException(Status.MOVED_PERMANENTLY, new URI(remoteUrl));%0A%09%09%7D%0A%0A%09%09URL remote = new URL(remoteUrl);%0A%09%09InputStream stream = remote.openStream();%0A%09%09byte%5B%5D r = IOUtils.toByteArray(stream);%0A%09%09IOUtils.closeQuietly(stream);%0A%09%09return r
 ;%0A%09%7D

handle Cineplex logo in 404