![JAR search and dependency download from the Maven repository](/logo.png)
org.omnifaces.cdi.GraphicImageBean Maven / Gradle / Ivy
/*
* Copyright OmniFaces
*
* Licensed 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
*
* https://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.omnifaces.cdi;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.Date;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Stereotype;
import javax.inject.Named;
import org.omnifaces.component.output.GraphicImage;
import org.omnifaces.el.ExpressionInspector;
import org.omnifaces.el.MethodReference;
import org.omnifaces.resourcehandler.DefaultResourceHandler;
import org.omnifaces.resourcehandler.DynamicResource;
import org.omnifaces.resourcehandler.GraphicResource;
import org.omnifaces.resourcehandler.GraphicResourceHandler;
/**
*
* Stereo type that designates a bean with one or more methods returning byte[]
or InputStream
* as a named application scoped bean specifically for serving graphic images via <o:graphicImage>
* component or #{of:graphicImageURL()}
EL functions.
*
* import org.omnifaces.cdi.GraphicImageBean;
*
* @GraphicImageBean
* public class Images {
*
* @Inject
* private ImageService service;
*
* public byte[] get(Long id) {
* return service.getContent(id);
* }
*
* }
*
*
* When using @Named @ApplicationScoped
instead, serving graphic images via a JSF page will continue to
* work, but when the server restarts, then hotlinking/bookmarking will stop working until the JSF page referencing the
* same bean method is requested for the first time. This is caused by a security restriction which should prevent users
* from invoking arbitrary bean methods by manipulating the URL. The @GraphicImageBean
basically enables
* endusers to invoke any public method returning a byte[]
or InputStream
on the bean by just
* a HTTP GET request.
*
*
Usage
*
* You can use #{of:graphicImageURL()}
EL functions to generate URLs referring the
* @GraphicImageBean
bean, optionally with the image type
and lastModified
* arguments. Below are some usage examples:
*
* <ui:repeat value="#{bean.products}" var="product">
*
* <!-- Basic, using default type and last modified. -->
* <a href="#{of:graphicImageURL('images.full(product.imageId)')}">
* <o:graphicImage value="#{images.thumb(product.imageId)}" />
* </a>
*
* <!-- With specified type and default last modified. -->
* <a href="#{of:graphicImageURLWithType('images.full(product.imageId)', 'png')}">
* <o:graphicImage value="#{images.thumb(product.imageId)}" type="png" />
* </a>
*
* <!-- With specified type and last modified. -->
* <a href="#{of:graphicImageURLWithTypeAndLastModified('images.full(product.imageId)', 'png', product.lastModified)}">
* <o:graphicImage value="#{images.thumb(product.imageId)}" type="png" lastModified="#{product.lastModified}" />
* </a>
* </ui:repeat>
*
*
* Note that in the #{of:graphicImageURL()}
EL functions the expression string represents the same value as
* you would use in <o:graphicImage>
and that it must be a quoted string. Any nested quotes can be
* escaped with backslash.
*
* The type
argument/attribute is the image type represented as file extension. E.g. "webp", "jpg", "png", "gif",
* "ico", "svg", "bmp", "tiff", etc. When unspecified then the content type will default to "image"
* without any subtype. This should work for most images in most browsers. This may however fail on newer images or in
* older browsers. In that case, you can explicitly specify the image type via the type
argument/attribute
* which must represent a valid file extension.
*
* The lastModified
argument/attribute is the "last modified" timestamp, can be {@link Long} or
* {@link Date}, or otherwise an attempt will be made to parse it as {@link Long}. When unspecified, then the "default
* resource maximum age" as set in either the Mojarra specific context parameter
* com.sun.faces.defaultResourceMaxAge
or MyFaces specific context parameter
* org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES
will be used, else a default of 1 week will be assumed.
*
* @since 2.5
* @author Bauke Scholtz
* @see GraphicImage
* @see GraphicResource
* @see DynamicResource
* @see GraphicResourceHandler
* @see DefaultResourceHandler
* @see ExpressionInspector
* @see MethodReference
*/
@Inherited
@Documented
@Stereotype
@Named
@ApplicationScoped
@Retention(RUNTIME)
@Target(TYPE)
public @interface GraphicImageBean {
//
}