/**
 * 
 */
package brix.plugin.site.resource;

import java.io.IOException;

import org.apache.wicket.IRequestTarget;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.model.IModel;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.util.string.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import brix.Brix;
import brix.jcr.wrapper.BrixFileNode;
import brix.jcr.wrapper.BrixNode;

public class ResourceRequestTarget implements IRequestTarget
{
    private final IModel<BrixNode> node;
    private final Boolean save;

    public ResourceRequestTarget(IModel<BrixNode> node)
    {
        super();
        this.node = node;
        this.save = null;
    }

    public ResourceRequestTarget(IModel<BrixNode> node, boolean save)
    {
        super();
        this.node = node;
        this.save = save;
    }

    public void detach(RequestCycle requestCycle)
    {
        node.detach();
    }

    public void respond(RequestCycle requestCycle)
    {
        boolean save = (this.save != null) ? this.save : Strings.isTrue(requestCycle.getRequest()
                .getParameter(SAVE_PARAMETER));

        BrixFileNode node = (BrixFileNode)this.node.getObject();

        WebResponse response = (WebResponse)requestCycle.getResponse();

        response.setContentType(node.getMimeType());

        if (save)
        {
            response.setAttachmentHeader(node.getName());
        }

        try
        {
            node.writeData(requestCycle.getResponse().getOutputStream());
        }
        catch (IOException e)
        {
            log.error("Error writing resource data to content", e);
        }
    }

    private static final Logger log = LoggerFactory.getLogger(ResourceRequestTarget.class);

    public static final String SAVE_PARAMETER = Brix.NS_PREFIX + "save";

}
@@ -62,22 +62,66 @@
 ava.
-io.IOException
+util.Date;%0D%0A%0D%0Aimport javax.servlet.http.HttpServletRequest
 ;%0D%0A%0D
@@ -243,16 +243,68 @@
 Model;%0D%0A
+import org.apache.wicket.protocol.http.WebRequest;%0D%0A
 import o
@@ -395,16 +395,58 @@
 rings;%0D%0A
+import org.apache.wicket.util.time.Time;%0D%0A
 import o
@@ -1401,24 +1401,32 @@
 tObject();%0D%0A
+        
 %0D%0A        We
@@ -1566,32 +1566,41 @@
 save)%0D%0A        %7B
+        %09
 %0D%0A            re
@@ -1659,32 +1659,797 @@
  %7D%0D%0A
-%0D%0A        try%0D%0A        %7B
+        %0D%0A        response.setLastModifiedTime(Time.valueOf(node.getLastModified()));        %0D%0A        %0D%0A        try%0D%0A        %7B   %0D%0A        %09HttpServletRequest r = ((WebRequest)requestCycle.getRequest()).getHttpServletRequest();%0D%0A        %09String since = r.getHeader(%22If-Modified-Since%22);%0D%0A        %09if (!save && since != null) %0D%0A        %09%7B%0D%0A        %09%09Date d = new Date(r.getDateHeader(%22If-Modified-Since%22));        %09%0D%0A        %09%09Date m = node.getLastModified();%0D%0A        %09%09        %09%0D%0A        %09%09// the weird toString comparison is to prevent comparing milliseconds%0D%0A        %09%09if (d.after(m) %7C%7C d.toString().equals(m.toString()))%0D%0A        %09%09%7B        %09%0D%0A        %09%09%09response.getHttpServletResponse().setStatus(304);        %09%09%09%0D%0A        %09%09%09return;%0D%0A        %09%09%09%09%0D%0A        %09%09%7D        %09%09%0D%0A        %09%7D
 %0D%0A  
@@ -2551,10 +2551,8 @@
 ch (
-IO
 Exce
@@ -2833,8 +2833,10 @@
 e%22;%0D%0A%0D%0A%7D
+%0D%0A

check for If-Modified-Since header