You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2010/11/27 21:11:21 UTC

svn commit: r1039747 - /wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/ContextRelativeResource.java

Author: mgrigorov
Date: Sat Nov 27 20:11:21 2010
New Revision: 1039747

URL: http://svn.apache.org/viewvc?rev=1039747&view=rev
Log:
Return back ContextRelativeResource.

It was deleted in the early days of 1.5 development.

Added:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/ContextRelativeResource.java   (with props)

Added: wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/ContextRelativeResource.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/ContextRelativeResource.java?rev=1039747&view=auto
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/ContextRelativeResource.java (added)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/ContextRelativeResource.java Sat Nov 27 20:11:21 2010
@@ -0,0 +1,98 @@
+/*
+ * 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;
+
+/**
+ * Resource served from a file relative to the context root.
+ * 
+ * @author almaw
+ */
+public class ContextRelativeResource extends AbstractResource
+{
+	private static final long serialVersionUID = 1L;
+
+	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)
+				{
+					try
+					{
+						InputStream inputStream = webExternalResourceStream.getInputStream();
+						ByteArrayOutputStream baos = new ByteArrayOutputStream();
+						Streams.copy(inputStream, baos);
+						attributes.getResponse().write(baos.toByteArray());
+					}
+					catch (ResourceStreamNotFoundException rsnfx)
+					{
+						throw new WicketRuntimeException(rsnfx);
+					}
+					catch (IOException iox)
+					{
+						throw new WicketRuntimeException(iox);
+					}
+				}
+			});
+		}
+
+		return resourceResponse;
+	}
+}

Propchange: wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/ContextRelativeResource.java
------------------------------------------------------------------------------
    svn:eol-style = native