01: /*
02: * $Id: BlobImageResource.java 460022 2006-03-28 11:50:28Z jcompagner $ $Revision: 460022 $ $Date: 2006-03-28 13:50:28 +0200 (Tue, 28 Mar 2006) $
03: *
04: * ==============================================================================
05: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
06: * use this file except in compliance with the License. You may obtain a copy of
07: * the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14: * License for the specific language governing permissions and limitations under
15: * the License.
16: */
17: package wicket.markup.html.image.resource;
18:
19: import java.io.ByteArrayOutputStream;
20: import java.io.IOException;
21: import java.io.InputStream;
22: import java.sql.Blob;
23: import java.sql.SQLException;
24: import java.util.Locale;
25:
26: import wicket.WicketRuntimeException;
27: import wicket.util.io.Streams;
28:
29: /**
30: * An ImageResource subclass for dynamic images that come from database BLOB
31: * fields. Subclasses override getBlob() to provide the image data to send back
32: * to the user. A given subclass may decide how to produce this data and
33: * whether/how to buffer it.
34: *
35: * @author Eelco Hillenius
36: */
37: public abstract class BlobImageResource extends DynamicImageResource {
38: /**
39: * Construct.
40: * @param locale
41: */
42: public BlobImageResource(Locale locale) {
43: super (locale);
44: }
45:
46: /**
47: * Construct.
48: * @param format
49: * @param locale
50: */
51: public BlobImageResource(String format, Locale locale) {
52: super (format, locale);
53: }
54:
55: /**
56: * Construct.
57: * @param format
58: */
59: public BlobImageResource(String format) {
60: super (format);
61: }
62:
63: /**
64: * Construct.
65: */
66: public BlobImageResource() {
67: }
68:
69: /**
70: * @see wicket.markup.html.image.resource.DynamicImageResource#getImageData()
71: */
72: protected byte[] getImageData() {
73: try {
74: Blob blob = getBlob();
75: if (blob != null) {
76: InputStream in = blob.getBinaryStream();
77: ByteArrayOutputStream out = new ByteArrayOutputStream();
78: Streams.copy(in, out);
79: return out.toByteArray();
80: }
81: return new byte[0];
82: } catch (SQLException e) {
83: throw new WicketRuntimeException(
84: "Error while reading image data", e);
85: } catch (IOException e) {
86: throw new WicketRuntimeException(
87: "Error while reading image data", e);
88: }
89: }
90:
91: /**
92: * Gets the BLOB (Binary Large OBject) that holds the raw image data.
93: *
94: * @return the BLOB
95: */
96: protected abstract Blob getBlob();
97: }
|