/*
 * $Id: SharedResourceRequestTarget.java,v 1.3 2005/12/30 20:20:17 jonathanlocke
 * Exp $ $Revision$ $Date$
 * 
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package wicket.request.target.resource;

import wicket.RequestCycle;
import wicket.Resource;
import wicket.SharedResources;
import wicket.WicketRuntimeException;
import wicket.request.RequestParameters;

/**
 * Default implementation of {@link ISharedResourceRequestTarget}. Target that
 * denotes a shared {@link wicket.Resource}.
 * 
 * @author Eelco Hillenius
 */
public class SharedResourceRequestTarget implements ISharedResourceRequestTarget
{
	/** the key of the resource. */
	private final String resourceKey;

	private final RequestParameters requestParameters;


	/**
	 * Construct.
	 * 
	 * @param resourceKey
	 *            the key of the resource
	 */
	public SharedResourceRequestTarget(String resourceKey)
	{
		this(resourceKey, null);
	}


	/**
	 * Construct.
	 * 
	 * @param resourceKey
	 *            the key of the resource
	 * @param requestParameters
	 */
	public SharedResourceRequestTarget(String resourceKey, RequestParameters requestParameters)
	{
		if (resourceKey == null)
		{
			throw new IllegalArgumentException("Argument resourceKey must be not-null");
		}

		this.resourceKey = resourceKey;
		this.requestParameters = requestParameters;
	}

	/**
	 * Respond by looking up the shared resource and delegating the actual
	 * response to that resource.
	 * 
	 * @see wicket.IRequestTarget#respond(wicket.RequestCycle)
	 */
	public void respond(RequestCycle requestCycle)
	{
		SharedResources sharedResources = requestCycle.getApplication().getSharedResources();
		Resource resource = sharedResources.get(resourceKey);
		if (resource == null)
		{
			throw new WicketRuntimeException("shared resource " + resourceKey + " not found");
		}
		sharedResources.onResourceRequested(resourceKey);

		if (requestParameters != null)
		{
			resource.setParameters(requestParameters.getParameters());
		}

		resource.onResourceRequested();
	}

	/**
	 * @see wicket.IRequestTarget#cleanUp(wicket.RequestCycle)
	 */
	public void cleanUp(RequestCycle requestCycle)
	{
	}

	/**
	 * @see wicket.IRequestTarget#getLock(RequestCycle)
	 */
	public Object getLock(RequestCycle requestCycle)
	{
		return null;
	}

	/**
	 * @see wicket.request.target.resource.ISharedResourceRequestTarget#getResourceKey()
	 */
	public final String getResourceKey()
	{
		return resourceKey;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj)
	{
		if (obj instanceof SharedResourceRequestTarget)
		{
			SharedResourceRequestTarget that = (SharedResourceRequestTarget)obj;
			return resourceKey.equals(that.resourceKey);
		}
		return false;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode()
	{
		int result = "SharedResourceRequestTarget".hashCode();
		result += resourceKey.hashCode();
		return 17 * result;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		return "[SharedResourceRequestTarget@" + hashCode() + " " + resourceKey + "]";
	}
}

@@ -788,16 +788,64 @@
 ource;%0A%0A
+import javax.servlet.http.HttpServletResponse;%0A%0A
 import w
@@ -888,16 +888,40 @@
 source;%0A
+import wicket.Response;%0A
 import w
@@ -981,16 +981,57 @@
 eption;%0A
+import wicket.protocol.http.WebResponse;%0A
 import w
@@ -2426,32 +2426,256 @@
 ce == null)%0A%09%09%7B%0A
+%09%09%09Response response = requestCycle.getResponse();%0A%09%09%09if(response instanceof WebResponse)%0A%09%09%09%7B%0A%09%09%09%09((WebResponse)response).getHttpServletResponse().setStatus(HttpServletResponse.SC_NOT_FOUND);%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%09else%0A%09%09%09%7B%0A%09
 %09%09%09throw new Wic
@@ -2740,24 +2740,29 @@
 ot found%22);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09shared

send a 404 only when a resource is not found instead of throwing an exception