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;
018
019import java.io.IOException;
020
021import org.apache.wicket.WicketRuntimeException;
022import org.apache.wicket.markup.ComponentTag;
023import org.apache.wicket.markup.html.WebComponent;
024import org.apache.wicket.model.IModel;
025import org.apache.wicket.request.resource.PackageResourceReference;
026import org.apache.wicket.util.image.ImageUtil;
027import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
028
029/**
030 * The inline image is used to embed the complete image content within a HTML document.
031 *
032 * @author Tobias Soloschenko
033 * @since 6.20.0
034 * 
035 */
036public class InlineImage extends WebComponent
037{
038
039        private static final long serialVersionUID = 1L;
040
041        private PackageResourceReference packageResourceReference;
042
043        /**
044         * Creates an inline image
045         *
046         * @param id
047         *            the id
048         * @param packageResourceReference
049         *            the package resource reference of the image
050         */
051        public InlineImage(String id, PackageResourceReference packageResourceReference)
052        {
053                this(id, null, packageResourceReference);
054        }
055
056        /**
057         * Creates an inline image
058         *
059         * @param id
060         *            the id
061         * @param model
062         *            the model of the inline image
063         * @param packageResourceReference
064         *            the package resource reference of the image
065         */
066        public InlineImage(String id, IModel<?> model, PackageResourceReference packageResourceReference)
067        {
068                super(id, model);
069                this.packageResourceReference = packageResourceReference;
070        }
071
072        /**
073         * Renders the complete image tag with the base64 encoded content.
074         */
075        @Override
076        protected void onComponentTag(ComponentTag tag)
077        {
078                super.onComponentTag(tag);
079
080                checkComponentTag(tag, "img");
081
082                try
083                {
084                        tag.put("src", ImageUtil.createBase64EncodedImage(packageResourceReference, false));
085                }
086                catch (ResourceStreamNotFoundException e)
087                {
088                        throw new WicketRuntimeException("Couldn't find the resource stream", e);
089                }
090                catch (IOException e)
091                {
092                        throw new WicketRuntimeException("Error while reading the resource stream", e);
093                }
094        }
095}