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 2014/05/26 20:26:00 UTC

[1/2] git commit: WICKET-5600 Introduce CharSequenceResource similar to ByteArrayResource

Repository: wicket
Updated Branches:
  refs/heads/master 068d7d596 -> af02fb49a


WICKET-5600 Introduce CharSequenceResource similar to ByteArrayResource

(cherry picked from commit 7b6b888839327f0c0095bd60bb8a067528bc3139)


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/681a6ed9
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/681a6ed9
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/681a6ed9

Branch: refs/heads/master
Commit: 681a6ed90e87b4115388a3a69ca886e7f8e3a7b1
Parents: 068d7d5
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Mon May 26 20:20:39 2014 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Mon May 26 20:22:39 2014 +0200

----------------------------------------------------------------------
 .../request/resource/BaseDataResource.java      | 194 +++++++++++++++++++
 .../request/resource/ByteArrayResource.java     | 110 ++---------
 .../request/resource/CharSequenceResource.java  |  81 ++++++++
 3 files changed, 292 insertions(+), 93 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/681a6ed9/wicket-core/src/main/java/org/apache/wicket/request/resource/BaseDataResource.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/BaseDataResource.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/BaseDataResource.java
new file mode 100644
index 0000000..45fbd32
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/BaseDataResource.java
@@ -0,0 +1,194 @@
+/*
+ * 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.net.URLConnection;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.wicket.request.Response;
+import org.apache.wicket.util.time.Time;
+
+/**
+ * An abstract resource that can deliver static data - passed to the constructor,
+ * or dynamic - returned by {@link #getData(org.apache.wicket.request.resource.IResource.Attributes)}
+ *
+ * @param <T> The type of the data this resource can deliver
+ */
+public abstract class BaseDataResource<T> extends AbstractResource
+{
+	private static final long serialVersionUID = 1L;
+
+	/** the content type */
+	private final String contentType;
+
+	/** the data to deliver */
+	private T data;
+
+	/** the time that this resource was last modified; same as construction time. */
+	private final Time lastModified = Time.now();
+
+	private final String filename;
+
+	/**
+	 * Creates a {@link org.apache.wicket.request.resource.BaseDataResource} which will
+	 * provide its data dynamically with
+	 * {@link #getData(org.apache.wicket.request.resource.IResource.Attributes)}
+	 *
+	 * @param contentType
+	 *            The Content type of the array.
+	 */
+	public BaseDataResource(final String contentType)
+	{
+		this(contentType, null, null);
+	}
+
+	/**
+	 * Creates a Resource from the given data with its content type
+	 *
+	 * @param contentType
+	 *            The Content type of the array.
+	 * @param data
+	 *            The data
+	 */
+	public BaseDataResource(final String contentType, final T data)
+	{
+		this(contentType, data, null);
+	}
+
+	/**
+	 * Creates a Resource from the given data with its content type and filename
+	 *
+	 * @param contentType
+	 *            The Content type of the array.
+	 * @param data
+	 *            The data
+	 * @param filename
+	 *            The filename that will be set as the Content-Disposition header.
+	 */
+	public BaseDataResource(final String contentType, final T data, final String filename)
+	{
+		this.contentType = contentType;
+		this.data = data;
+		this.filename = filename;
+	}
+
+	/**
+	 * Post-configures the given response, e.g. set/override response headers.
+	 *
+	 * @param response
+	 *              The response to configure
+	 * @param attributes
+	 *              The request attributes (web request, web response, parameters)
+	 */
+	protected void configureResponse(final ResourceResponse response, final Attributes attributes)
+	{
+	}
+
+	@Override
+	protected ResourceResponse newResourceResponse(final Attributes attributes)
+	{
+		final ResourceResponse response = new ResourceResponse();
+
+		String contentType = this.contentType;
+
+		if (contentType == null)
+		{
+			if (filename != null)
+			{
+				contentType = URLConnection.getFileNameMap().getContentTypeFor(filename);
+			}
+
+			if (contentType == null)
+			{
+				contentType = "application/octet-stream";
+			}
+		}
+
+		response.setContentType(contentType);
+		response.setLastModified(lastModified);
+
+		final T data = getData(attributes);
+		if (data == null)
+		{
+			response.setError(HttpServletResponse.SC_NOT_FOUND);
+		}
+		else
+		{
+			Long length = getLength(data);
+			if (length != null)
+			{
+				response.setContentLength(length);
+			}
+
+			if (response.dataNeedsToBeWritten(attributes))
+			{
+				if (filename != null)
+				{
+					response.setFileName(filename);
+					response.setContentDisposition(ContentDisposition.ATTACHMENT);
+				}
+				else
+				{
+					response.setContentDisposition(ContentDisposition.INLINE);
+				}
+
+				response.setWriteCallback(new WriteCallback()
+				{
+					@Override
+					public void writeData(final Attributes attributes)
+					{
+						BaseDataResource.this.writeData(attributes.getResponse(), data);
+					}
+				});
+
+				configureResponse(response, attributes);
+			}
+		}
+
+		return response;
+	}
+
+	/**
+	 * Writes the given data to the response
+	 * @param response
+	 *              The response to write to
+	 * @param data
+	 *              The data to write
+	 */
+	protected abstract void writeData(Response response, T data);
+
+	/**
+	 * @param data
+	 *              The data to be written
+	 * @return The length of the data to be written. Used to set "Content-Length" response header
+	 */
+	protected abstract Long getLength(T data);
+
+	/**
+	 * Gets the data for this resource.
+	 * 
+	 * @param attributes
+	 *            the context bringing the request, response and the parameters
+	 * 
+	 * @return The data for this resource
+	 */
+	protected T getData(final Attributes attributes)
+	{
+		return data;
+	}
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/681a6ed9/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java
index fe78ea0..5b645a5 100644
--- a/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java
+++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java
@@ -16,11 +16,7 @@
  */
 package org.apache.wicket.request.resource;
 
-import java.net.URLConnection;
-
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.wicket.util.time.Time;
+import org.apache.wicket.request.Response;
 
 /**
  * An {@link IResource} for byte arrays. The byte array can be static - passed to the constructor,
@@ -29,21 +25,10 @@ import org.apache.wicket.util.time.Time;
  * 
  * @author Matej Knopp
  */
-public class ByteArrayResource extends AbstractResource
+public class ByteArrayResource extends BaseDataResource<byte[]>
 {
 	private static final long serialVersionUID = 1L;
 
-	/** the content type. */
-	private final String contentType;
-
-	/** the binary data. */
-	private byte[] array;
-
-	/** the time that this resource was last modified; same as construction time. */
-	private final Time lastModified = Time.now();
-
-	private final String filename;
-
 	/**
 	 * Creates a {@link ByteArrayResource} which will provide its data dynamically with
 	 * {@link #getData(org.apache.wicket.request.resource.IResource.Attributes)}
@@ -53,7 +38,7 @@ public class ByteArrayResource extends AbstractResource
 	 */
 	public ByteArrayResource(final String contentType)
 	{
-		this(contentType, null, null);
+		super(contentType);
 	}
 
 	/**
@@ -66,7 +51,7 @@ public class ByteArrayResource extends AbstractResource
 	 */
 	public ByteArrayResource(final String contentType, final byte[] array)
 	{
-		this(contentType, array, null);
+		super(contentType, array);
 	}
 
 	/**
@@ -81,89 +66,28 @@ public class ByteArrayResource extends AbstractResource
 	 */
 	public ByteArrayResource(final String contentType, final byte[] array, final String filename)
 	{
-		this.contentType = contentType;
-		this.array = array;
-		this.filename = filename;
+		super(contentType, array, filename);
 	}
 
-	protected void configureResponse(final ResourceResponse response, final Attributes attributes)
+	/*
+		@since Wicket 6.16.0
+		override for backward compatibility
+	 */
+	@Override
+	protected byte[] getData(Attributes attributes)
 	{
+		return super.getData(attributes);
 	}
 
-	/**
-	 * @see org.apache.wicket.request.resource.AbstractResource#newResourceResponse(org.apache.wicket.request.resource.IResource.Attributes)
-	 */
 	@Override
-	protected ResourceResponse newResourceResponse(final Attributes attributes)
+	protected void writeData(Response response, byte[] data)
 	{
-		final ResourceResponse response = new ResourceResponse();
-
-		String contentType = this.contentType;
-
-		if (contentType == null)
-		{
-			if (filename != null)
-			{
-				contentType = URLConnection.getFileNameMap().getContentTypeFor(filename);
-			}
-
-			if (contentType == null)
-			{
-				contentType = "application/octet-stream";
-			}
-		}
-
-
-		response.setContentType(contentType);
-		response.setLastModified(lastModified);
-
-		final byte[] data = getData(attributes);
-		if (data == null)
-		{
-			response.setError(HttpServletResponse.SC_NOT_FOUND);
-		}
-		else
-		{
-			response.setContentLength(data.length);
-
-			if (response.dataNeedsToBeWritten(attributes))
-			{
-				if (filename != null)
-				{
-					response.setFileName(filename);
-					response.setContentDisposition(ContentDisposition.ATTACHMENT);
-				}
-				else
-				{
-					response.setContentDisposition(ContentDisposition.INLINE);
-				}
-
-				response.setWriteCallback(new WriteCallback()
-				{
-					@Override
-					public void writeData(final Attributes attributes)
-					{
-						attributes.getResponse().write(data);
-					}
-				});
-
-				configureResponse(response, attributes);
-			}
-		}
-
-		return response;
+		response.write(data);
 	}
 
-	/**
-	 * Gets the data for this resource.
-	 * 
-	 * @param attributes
-	 *            the context bringing the request, response and the parameters
-	 * 
-	 * @return The byte array data for this resource
-	 */
-	protected byte[] getData(final Attributes attributes)
+	@Override
+	protected Long getLength(byte[] data)
 	{
-		return array;
+		return (long) data.length;
 	}
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/681a6ed9/wicket-core/src/main/java/org/apache/wicket/request/resource/CharSequenceResource.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/CharSequenceResource.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/CharSequenceResource.java
new file mode 100644
index 0000000..eeb9055
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/CharSequenceResource.java
@@ -0,0 +1,81 @@
+/*
+ * 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 org.apache.wicket.request.Response;
+
+/**
+ * An {@link org.apache.wicket.request.resource.IResource} for CharSequences.
+ * The char sequence can be static - passed to the constructor,
+ * or dynamic - by overriding {@link #getData(org.apache.wicket.request.resource.IResource.Attributes)}
+ */
+public class CharSequenceResource extends BaseDataResource<CharSequence>
+{
+	private static final long serialVersionUID = 1L;
+
+	/**
+	 * Creates a {@link org.apache.wicket.request.resource.CharSequenceResource} which will provide its data dynamically with
+	 * {@link #getData(org.apache.wicket.request.resource.IResource.Attributes)}
+	 *
+	 * @param contentType
+	 *            The Content type of the array.
+	 */
+	public CharSequenceResource(final String contentType)
+	{
+		super(contentType);
+	}
+
+	/**
+	 * Creates a Resource from the given char sequence with its content type
+	 *
+	 * @param contentType
+	 *            The Content type of the array.
+	 * @param data
+	 *            The data
+	 */
+	public CharSequenceResource(final String contentType, final CharSequence data)
+	{
+		super(contentType, data);
+	}
+
+	/**
+	 * Creates a Resource from the given char sequence with its content type
+	 *
+	 * @param contentType
+	 *            The Content type of the array.
+	 * @param data
+	 *            The data
+	 * @param filename
+	 *            The filename that will be set as the Content-Disposition header.
+	 */
+	public CharSequenceResource(final String contentType, final CharSequence data, final String filename)
+	{
+		super(contentType, data, filename);
+	}
+
+	@Override
+	protected void writeData(Response response, CharSequence data)
+	{
+		response.write(data);
+	}
+
+	@Override
+	protected Long getLength(CharSequence data)
+	{
+		return (long) data.length();
+	}
+}


[2/2] git commit: Remove not needed method

Posted by mg...@apache.org.
Remove not needed method


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/af02fb49
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/af02fb49
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/af02fb49

Branch: refs/heads/master
Commit: af02fb49ae85c57eb37dd4619efe2abfc5bdd54c
Parents: 681a6ed
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Mon May 26 20:25:23 2014 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Mon May 26 20:25:23 2014 +0200

----------------------------------------------------------------------
 .../apache/wicket/request/resource/ByteArrayResource.java | 10 ----------
 1 file changed, 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/af02fb49/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java
index 5b645a5..259ede6 100644
--- a/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java
+++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java
@@ -69,16 +69,6 @@ public class ByteArrayResource extends BaseDataResource<byte[]>
 		super(contentType, array, filename);
 	}
 
-	/*
-		@since Wicket 6.16.0
-		override for backward compatibility
-	 */
-	@Override
-	protected byte[] getData(Attributes attributes)
-	{
-		return super.getData(attributes);
-	}
-
 	@Override
 	protected void writeData(Response response, byte[] data)
 	{