Author: mgrigorov
Date: Tue May 10 09:39:19 2011
New Revision: 1101380
URL: http://svn.apache.org/viewvc?rev=1101380&view=rev
Log:
WICKET-3693 Enhance ByteArrayResource for dynamic byte arrays
Make ByteArrayResource work either with static byte array (passed to the
constructor) or generate it dynamically with #getData(Attributes)
Update javadocs in DynamicImageResource.
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/DynamicImageResource.java
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java?rev=1101380&r1=1101379&r2=1101380&view=diff
==============================================================================
---
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java
(original)
+++
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/ByteArrayResource.java
Tue May 10 09:39:19 2011
@@ -16,9 +16,14 @@
*/
package org.apache.wicket.request.resource;
+import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.util.time.Time;
/**
+ * An {@link IResource} for byte arrays. The byte array can be static - passed
to the constructor,
+ * or dynamic - by overriding
+ * {@link #getData(org.apache.wicket.request.resource.IResource.Attributes)}
+ *
* @author Matej Knopp
*/
public class ByteArrayResource extends AbstractResource
@@ -28,8 +33,8 @@ public class ByteArrayResource extends A
/** the content type. */
private final String contentType;
- /** binary data. */
- private final byte[] array;
+ /** the binary data. */
+ private byte[] array;
/** the time that this resource was last modified; same as construction
time. */
private final Time lastModified = Time.now();
@@ -37,6 +42,18 @@ public class ByteArrayResource extends A
private final String filename;
/**
+ * Creates a {@link ByteArrayResource} 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 ByteArrayResource(final String contentType)
+ {
+ this(contentType, null, null);
+ }
+
+ /**
* Creates a Resource from the given byte array with its content type
*
* @param contentType
@@ -80,7 +97,13 @@ public class ByteArrayResource extends A
response.setContentType(contentType);
response.setLastModified(lastModified);
- response.setContentLength(array.length);
+
+ final byte[] data = getData(attributes);
+ if (data == null)
+ {
+ throw new WicketRuntimeException("ByteArrayResource's
data cannot be 'null'.");
+ }
+ response.setContentLength(data.length);
if (response.dataNeedsToBeWritten(attributes))
{
@@ -99,7 +122,7 @@ public class ByteArrayResource extends A
@Override
public void writeData(final Attributes
attributes)
{
- attributes.getResponse().write(array);
+ attributes.getResponse().write(data);
}
});
@@ -108,4 +131,17 @@ public class ByteArrayResource extends A
return response;
}
+
+ /**
+ * 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)
+ {
+ return array;
+ }
}
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/DynamicImageResource.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/DynamicImageResource.java?rev=1101380&r1=1101379&r2=1101380&view=diff
==============================================================================
---
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/DynamicImageResource.java
(original)
+++
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/DynamicImageResource.java
Tue May 10 09:39:19 2011
@@ -19,11 +19,11 @@ package org.apache.wicket.request.resour
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.util.Date;
import javax.imageio.ImageIO;
import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.util.lang.Args;
import org.apache.wicket.util.time.Time;
/**
@@ -74,6 +74,7 @@ public abstract class DynamicImageResour
*/
public synchronized final void setFormat(String format)
{
+ Args.notNull(format, "format");
this.format = format;
}
@@ -113,10 +114,11 @@ public abstract class DynamicImageResour
/**
* Get image data for our dynamic image resource. If the subclass
regenerates the data, it
- * should set the lastModifiedTime when it does so. This ensures that
image caching works
- * correctly.
+ * should set the {@link
DynamicImageResource#setLastModifiedTime(Time)} when it does so. This
+ * ensures that image caching works correctly.
*
* @param attributes
+ * the context bringing the request, response and the
parameters
*
* @return The image data for this dynamic image
*/