001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.markup.html.image.resource;
018
019import java.io.ByteArrayOutputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.sql.Blob;
023import java.sql.SQLException;
024
025import org.apache.wicket.WicketRuntimeException;
026import org.apache.wicket.request.resource.DynamicImageResource;
027import org.apache.wicket.util.io.Streams;
028
029
030/**
031 * An ImageResource subclass for dynamic images that come from database BLOB fields. Subclasses
032 * override getBlob() to provide the image data to send back to the user. A given subclass may
033 * decide how to produce this data and whether/how to buffer it.
034 * 
035 * @author Eelco Hillenius
036 */
037public abstract class BlobImageResource extends DynamicImageResource
038{
039        /**
040         * 
041         */
042        private static final long serialVersionUID = 1L;
043
044        /**
045         * Construct.
046         * 
047         * @param format
048         */
049        public BlobImageResource(String format)
050        {
051                super(format);
052        }
053
054        /**
055         * Construct.
056         */
057        public BlobImageResource()
058        {
059        }
060
061        @Override
062        protected byte[] getImageData(Attributes attributes)
063        {
064                try
065                {
066                        Blob blob = getBlob(attributes);
067                        if (blob != null)
068                        {
069                                InputStream in = blob.getBinaryStream();
070                                ByteArrayOutputStream out = new ByteArrayOutputStream();
071                                Streams.copy(in, out);
072                                return out.toByteArray();
073                        }
074                        return new byte[0];
075                }
076                catch (SQLException e)
077                {
078                        throw new WicketRuntimeException("Error while reading image data", e);
079                }
080                catch (IOException e)
081                {
082                        throw new WicketRuntimeException("Error while reading image data", e);
083                }
084        }
085
086        /**
087         * Gets the BLOB (Binary Large OBject) that holds the raw image data.
088         *
089         * @param attributes
090         *      the current web attributes (request, response, parameters)
091         * @return the BLOB
092         */
093        protected abstract Blob getBlob(Attributes attributes);
094}