Hi Luke,

This is a litte off topic for the Tomcat usergroup but here you go.

I have added the following to my web.xml....

<servlet>
   <servlet-name>img</servlet-name>
   
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
   <servlet-name>img</servlet-name>
   <url-pattern>/img/*</url-pattern>
</servlet-mapping>

My img-servlet.xml....

<bean id="imageController" class="com.benshort.catalog.web.ImageController">
   <property name="imageRepository"><ref bean="imageRepo"/></property>
</bean>

<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
   <property name="mappings">
       <props>
           <prop key="/*.*">imageController</prop>
       </props>
   </property>
</bean>

And my image controller....

public class ImageController extends AbstractController
   {
   private SimpleDateFormat mDateFormat = new SimpleDateFormat("EEE,
dd MMM yyyy HH:mm:ss zzz");

   private IImageRepository mIImageRepository;

   public void setImageRepository(IImageRepository ImageRepository)
       {
       mIImageRepository = ImageRepository;
       }

   protected ModelAndView handleRequestInternal(HttpServletRequest
httpServletRequest, HttpServletResponse httpServletResponse) throws
Exception
       {
       final String url = httpServletRequest.getRequestURI();

       final String imageName =
url.substring(url.lastIndexOf("/")+1).toLowerCase();

       Long imageId = Long.parseLong(imageName.substring(0,
imageName.lastIndexOf(".")));

       Image image = mIImageRepository.getImage(imageId);

       String ifModifiedSince =
httpServletRequest.getHeader("if-modified-since");

       if ( ifModifiedSince != null )
           {
           long modifiedDate = mDateFormat.parse(ifModifiedSince).getTime();

           if ( modifiedDate < image.getLastModified() )
               {

httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
               httpServletResponse.setContentLength(0);
               return null;
               }
           }

       httpServletResponse.setContentType(image.getMimeType());
       httpServletResponse.setContentLength(image.getLength());
       // ie seems not to see this header if its not in uppercase :S
       httpServletResponse.setDateHeader("LAST-MODIFIED",
image.getLastModified());
       httpServletResponse.getOutputStream().write(image.getData());
       httpServletResponse.flushBuffer();

       return null;
       }
   }

The controller needs a bit more wrk to handel bad urls etc, but its functional.

Give me a shout if you have any questions.

Regards

Ben


On 9/8/06, Luke McLean <[EMAIL PROTECTED]> wrote:

Hi Ben,  I'm starting out to do exactly the same thing and the web is quite
sketchy on how to do it with Spring.  Would you mind posting or sending the
controller code that you use to return the image please.  I would be very
greatful.

Thanks,
Luke.


benshort wrote:
>
> What I am doing is serving images from a database.
>
>

--
View this message in context: 
http://www.nabble.com/HTTP-304---IF_MODIFIED_SINCE-tf2220040.html#a6203229
Sent from the Tomcat - User forum at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to