/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.wicket.request.resource;

import java.io.IOException;
import java.io.InputStream;

import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.util.io.ByteArrayOutputStream;
import org.apache.wicket.util.io.Streams;
import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
import org.apache.wicket.util.resource.WebExternalResourceStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Resource served from a file relative to the context root.
 * 
 * @author almaw
 */
public class ContextRelativeResource extends AbstractResource
{
	private static final long serialVersionUID = 1L;

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

	private final String path;

	/**
	 * Construct.
	 * 
	 * @param pathRelativeToContextRoot
	 */
	public ContextRelativeResource(String pathRelativeToContextRoot)
	{
		if (pathRelativeToContextRoot == null)
		{
			throw new IllegalArgumentException("Cannot have null path for ContextRelativeResource.");
		}

		// Make sure there is a leading '/'.
		if (!pathRelativeToContextRoot.startsWith("/"))
		{
			pathRelativeToContextRoot = "/" + pathRelativeToContextRoot;
		}
		path = pathRelativeToContextRoot;
	}

	@Override
	protected ResourceResponse newResourceResponse(final Attributes attributes)
	{
		final ResourceResponse resourceResponse = new ResourceResponse();

		if (resourceResponse.dataNeedsToBeWritten(attributes))
		{
			final WebExternalResourceStream webExternalResourceStream = new WebExternalResourceStream(
				path);

			resourceResponse.setContentType(webExternalResourceStream.getContentType());
			resourceResponse.setLastModified(webExternalResourceStream.lastModifiedTime().toDate());
			resourceResponse.setFileName(path);
			resourceResponse.setWriteCallback(new WriteCallback()
			{
				@Override
				public void writeData(final Attributes attributes)
				{
					InputStream inputStream = null;
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
					try
					{
						inputStream = webExternalResourceStream.getInputStream();
						Streams.copy(inputStream, baos);
						attributes.getResponse().write(baos.toByteArray());
					}
					catch (ResourceStreamNotFoundException rsnfx)
					{
						throw new WicketRuntimeException(rsnfx);
					}
					catch (IOException iox)
					{
						throw new WicketRuntimeException(iox);
					}
					finally
					{
						try
						{
							Streams.close(inputStream);
						}
						catch (IOException iox)
						{
							if (log.isDebugEnabled())
							{
								log.debug(
									"An error occurred while closing the input stream to the external resource",
									iox);
							}
						}

						try
						{
							Streams.close(baos);
						}
						catch (IOException iox)
						{
							if (log.isDebugEnabled())
							{
								log.debug(
									"An error occurred while closing the temporary output stream",
									iox);
							}
						}
					}
				}
			});
		}

		return resourceResponse;
	}
}

@@ -995,32 +995,74 @@
 ayOutputStream;%0A
+import org.apache.wicket.util.io.IOUtils;%0A
 import org.apach
@@ -3278,537 +3278,76 @@
 %09%09%09%09
-try%0A%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09Streams.close(inputStream);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09catch (IOException iox)%0A%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09if (log.isDebugEnabled())%0A%09%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09%09log.debug(%0A%09%09%09%09%09%09%09%09%09%22An error occurred while closing the input stream to the external resource%22,%0A%09%09%09%09%09%09%09%09%09iox);%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D%0A%0A%09%09%09%09%09%09try%0A%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09Streams.close(baos);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09catch (IOException iox)%0A%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09if (log.isDebugEnabled())%0A%09%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09%09log.debug(%0A%09%09%09%09%09%09%09%09%09%22An error occurred while closing the temporary output stream%22,%0A%09%09%09%09%09%09%09%09%09iox);%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D
+IOUtils.closeQuietly(inputStream);%0A%09%09%09%09%09%09IOUtils.closeQuietly(baos);
 %0A%09%09%09

Use IOUtils.closeQuietly() for less noisy code