/**
 * 
 */
package brix.plugin.site.resource;
import java.io.InputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.wicket.IRequestTarget;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.model.IModel;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.util.string.Strings;
import org.apache.wicket.util.time.Time;
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());
    
        Date lastModified = node.getLastModified();        
        response.setLastModifiedTime(Time.valueOf(lastModified));        
        
        try
        {   
        	HttpServletRequest r = ((WebRequest)requestCycle.getRequest()).getHttpServletRequest();
        	String since = r.getHeader("If-Modified-Since");
        	if (!save && since != null) 
        	{
        		Date d = new Date(r.getDateHeader("If-Modified-Since"));        	        		
        		        	
        		// the weird toString comparison is to prevent comparing milliseconds
        		if (d.after(lastModified) || d.toString().equals(lastModified.toString()))
        		{        	
        			response.setContentLength(node.getContentLength());
        			response.getHttpServletResponse().setStatus(HttpServletResponse.SC_NOT_MODIFIED);        			
        			return;
        				
        		}        		
        	}
        	String fileName = node.getName();
        	long length = node.getContentLength();
        	HttpServletRequest httpServletRequest = ((WebRequest)requestCycle.getRequest()).getHttpServletRequest();
        	HttpServletResponse httpServletResponse = response.getHttpServletResponse();
        	InputStream stream = node.getDataAsStream(); 
        	
            new Streamer(length, stream, fileName, save, httpServletRequest, httpServletResponse).stream();
        }
        catch (Exception 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";

}

@@ -274,24 +274,76 @@
 uestCycle;%0D%0A
+import org.apache.wicket.RestartResponseException;%0D%0A
 import org.a
@@ -644,16 +644,166 @@
 .Brix;%0D%0A
+import brix.web.nodepage.ForbiddenPage;%0D%0Aimport brix.auth.Action;%0D%0Aimport brix.plugin.site.SitePlugin;%0D%0Aimport brix.plugin.site.auth.SiteNodeAction;%0D%0A
 import b
@@ -1678,32 +1678,34 @@
 tObject();%0D%0A
+%0D%0A
         
 %0D%0A        We
@@ -1684,32 +1684,195 @@
 t();%0D%0A%0D%0A        
+if (!SitePlugin.get().canViewNode(node, Action.Context.PRESENTATION))%0D%0A        %7B%0D%0A            throw new RestartResponseException(ForbiddenPage.class);%0D%0A        %7D%0D%0A
 %0D%0A        WebRes

Add missing check for authenticated resources.