My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians


marți, 8 decembrie 2015

OmniFaces 2.2 FileServlet Examples

If you never heard about the BalusC FileServlet then it is time to find out about it! Starting with OmniFaces 2.2, the FileServlet was slightly refactored, rewritten and modernized with:

- fast NIO stuff instead of legacy RandomAccessFile;
- properly deals with ETag, If-None-Match and If-Modified-Since caching requests;
- properly deals with Range and If-Range ranging requests (RFC7233), which is required by most media players and by web browsers
                    proper audio/video streaming
          a proper resume of an paused download
          download accelerators to be able to request smaller parts simultaneously

This servlet is ideal for large files like media files placed outside the web application and you can't use the default servlet.

You can easily exploit FileServlet by extending this class and overriding the getFile(HttpServletRequest) method to return the desired file. The OmniFaces Showcase also provide an example of implementation, listed below (I just adjusted the path to point, D:\media\):

public class MediaFileServlet extends FileServlet {
 private File folder;

 public void init() throws ServletException {
  folder = new File("D:\\media\\");

 protected File getFile(HttpServletRequest request) throws IllegalArgumentException {
  String pathInfo = request.getPathInfo();
  if (pathInfo == null || pathInfo.isEmpty() || "/".equals(pathInfo)) {
      throw new IllegalArgumentException();

  return new File(folder, pathInfo);

If you want to trigger a HTTP 400 "Bad Request" error, simply throw IllegalArgumentException. If you want to trigger a HTTP 404 "Not Found" error, simply return null, or a non-existent file. Moreover, you can shape this Servlet behavior by overriding the following methods:

- long getExpireTime(HttpServletRequest, File) - By default, the resource may be cached by the client before it expires for 30 days (in seconds). Via this method you can alter this value. Pass in the involved request and file and return the desired number of seconds.

- String getContentType(HttpServletRequest request, File file) - The default implementation delegates File.getName() to ServletContext.getMimeType(String) with a fallback default value of application/octet-stream. If you want to alter this behavior pass in the involved request and file and return the desired content type (e.g. for PNG, add return "image/png";).

- boolean isAttachment(HttpServletRequest request, String contentType) - By default, it returns true if the content type does not start with text or image, and the Accept request header is either null or does not match the given content type. If you want to alter this behavior pass in the involved request and content type and return the desired value. For example, some browsers tend to download a video instead of playing it (e.g. Google Chrome). In order to prevent this, you can override this method to return false (this will cause FileServlet to add in response headers the Content-Disposition as inline instead of attachment:

protected boolean isAttachment(HttpServletRequest request, String contentType) {
 return false;

Example of loading external images/videos/pdfs

Let's suppose that we have several images/videos and pdf documents in a folder named media under D: disk. So, the path for an image named, cartoon.png will be, D:\media\cartoon.png. We want to load these resources in a web application via pure HTML and JSF tags. Our application uses the /faces/* prefix mapping for faces servlet.

Example of loading images (complete application here)

Load the image, D:\media\cartoon.png

HTML:              <img src="#{request.contextPath}/faces/media/cartoon.png"/>

JSF:                  <h:graphicImage url="faces/media/cartoon.png"/>
              <h:graphicImage value="faces/media/cartoon.png"/>

PrimeFaces:       <p:graphicImage url="faces/media/cartoon.png"/>
              <p:graphicImage value="faces/media/cartoon.png"/>

Example of loading videos (complete application here)

Load the video, D:\media\movie.mp4

HTML:            <video width="400" controls="controls">
              <source src="#{request.contextPath}/faces/media/movie.mp4" type="video/mp4"/>    
              <source src="#{request.contextPath}/faces/media/movie.ogg" type="video/ogg"/>                      
              Your browser does not support HTML5 video.

PrimeFaces:      <p:media value="faces/media/movie.mp4" width="400">                        
              <f:param name="autoplay" value="false" />    

Example of loading pdf (complete application here)

Load the video, D:\media\sample.pdf

HTML:              <object width="400" height="500" type="application/pdf"
               <p>Insert your error message here, if the PDF cannot be displayed.</p>

PrimeFaces:       <p:media value="faces/media/sample.pdf" width="400" height="500" />          

Niciun comentariu :

Trimiteți un comentariu


Postări populare

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors