org.apache.myfaces.renderkit.html.util
Class NonBufferingAddResource

java.lang.Object
  extended by org.apache.myfaces.renderkit.html.util.NonBufferingAddResource
All Implemented Interfaces:
org.apache.myfaces.renderkit.html.util.AddResource

public class NonBufferingAddResource
extends Object
implements org.apache.myfaces.renderkit.html.util.AddResource

Since:
1.1.7
Author:
Martin Marinschek

Field Summary
protected  String _contextPath
           
protected  org.apache.commons.logging.Log log
           
protected static String PATH_SEPARATOR
           
 
Fields inherited from interface org.apache.myfaces.renderkit.html.util.AddResource
BODY_END, BODY_ONLOAD, HEADER_BEGIN
 
Constructor Summary
NonBufferingAddResource()
           
 
Method Summary
 void addInlineScriptAtPosition(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourcePosition position, String inlineScript)
           
 void addInlineStyleAtPosition(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourcePosition position, String inlineStyle)
           
 void addJavaScriptAtPosition(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourcePosition position, Class myfacesCustomComponent, String resourceName)
           
 void addJavaScriptAtPosition(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourcePosition position, Class myfacesCustomComponent, String resourceName, boolean defer)
           
 void addJavaScriptAtPosition(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourcePosition position, org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler)
           
 void addJavaScriptAtPosition(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourcePosition position, org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler, boolean defer)
           
 void addJavaScriptAtPosition(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourcePosition position, String uri)
           
 void addJavaScriptAtPosition(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourcePosition position, String uri, boolean defer)
           
 void addJavaScriptAtPositionPlain(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourcePosition position, Class myfacesCustomComponent, String resourceName)
           
 void addJavaScriptHere(javax.faces.context.FacesContext context, Class myfacesCustomComponent, String resourceName)
          Insert a [script src="url"] entry at the current location in the response.
 void addJavaScriptHere(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler)
          Insert a [script src="url"] entry at the current location in the response.
 void addJavaScriptHere(javax.faces.context.FacesContext context, String uri)
          Insert a [script src="url"] entry at the current location in the response.
 void addJavaScriptHerePlain(javax.faces.context.FacesContext context, String uri)
           
 void addJavaScriptToBodyTag(javax.faces.context.FacesContext context, String javascriptEventName, String addedJavaScript)
          Deprecated.  
 void addResourceHere(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler)
           
 void addStyleSheet(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourcePosition position, Class myfacesCustomComponent, String resourceName)
           
 void addStyleSheet(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourcePosition position, org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler)
           
 void addStyleSheet(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourcePosition position, String uri)
           
protected  long getCacheKey(javax.faces.context.FacesContext context)
          Return a value used in the {cacheKey} part of a generated URL for a resource reference.
protected  String getResourceUri(javax.faces.context.FacesContext context, Class resourceLoader, boolean withContextPath)
          Get the Path used to retrieve an resource.
 String getResourceUri(javax.faces.context.FacesContext context, Class myfacesCustomComponent, String resource)
           
 String getResourceUri(javax.faces.context.FacesContext context, Class myfacesCustomComponent, String resource, boolean withContextPath)
           
 String getResourceUri(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler)
          Get the Path used to retrieve an resource.
 String getResourceUri(javax.faces.context.FacesContext context, org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler, boolean withContextPath)
          Get the Path used to retrieve an resource.
 String getResourceUri(javax.faces.context.FacesContext context, String uri)
          Get the Path used to retrieve an resource.
 String getResourceUri(javax.faces.context.FacesContext context, String uri, boolean withContextPath)
          Get the Path used to retrieve an resource.
 boolean hasHeaderBeginInfos()
           
 boolean isResourceUri(javax.servlet.ServletContext servletContext, javax.servlet.http.HttpServletRequest request)
           
 void parseResponse(javax.servlet.http.HttpServletRequest request, String bufferedResponse, javax.servlet.http.HttpServletResponse response)
           
 boolean requiresBuffer()
           
 void responseFinished()
           
 void responseStarted()
           
 void serveResource(javax.servlet.ServletContext context, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
 void setContextPath(String contextPath)
          the context path for the web-app.
You can set the context path only once, every subsequent set will throw an SecurityException
protected  void validateResourceHandler(org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler)
          Verify that the resource handler is acceptable.
protected  void validateResourceLoader(Class resourceloader)
          Given a Class object, verify that the instances of that class implement the ResourceLoader interface.
protected static void writeInlineScript(javax.faces.context.ResponseWriter writer, String inlineScript)
           
protected static void writeInlineStylesheet(javax.faces.context.ResponseWriter writer, String inlineStyle)
           
protected static void writeJavaScriptReference(javax.faces.context.FacesContext context, String resourceUri, boolean encoding, boolean defer)
           
protected static void writeJavaScriptReference(javax.servlet.http.HttpServletResponse response, javax.faces.context.ResponseWriter writer, String resourceUri, boolean encoding, boolean defer)
           
 void writeMyFacesJavascriptBeforeBodyEnd(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
 void writeResponse(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
protected static void writeStyleReference(javax.faces.context.FacesContext context, String resourceUri)
           
protected static void writeStyleReference(javax.servlet.http.HttpServletResponse response, javax.faces.context.ResponseWriter writer, String resourceUri)
           
 void writeWithFullHeader(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PATH_SEPARATOR

protected static final String PATH_SEPARATOR
See Also:
Constant Field Values

_contextPath

protected String _contextPath

log

protected org.apache.commons.logging.Log log
Constructor Detail

NonBufferingAddResource

public NonBufferingAddResource()
Method Detail

setContextPath

public void setContextPath(String contextPath)
the context path for the web-app.
You can set the context path only once, every subsequent set will throw an SecurityException

Specified by:
setContextPath in interface org.apache.myfaces.renderkit.html.util.AddResource

addJavaScriptHere

public void addJavaScriptHere(javax.faces.context.FacesContext context,
                              Class myfacesCustomComponent,
                              String resourceName)
                       throws IOException
Insert a [script src="url"] entry at the current location in the response. The resource is expected to be in the classpath, at the same location as the specified component + "/resource".

Example: when customComponent is class example.Widget, and resourceName is script.js, the resource will be retrieved from "example/Widget/resource/script.js" in the classpath.

Specified by:
addJavaScriptHere in interface org.apache.myfaces.renderkit.html.util.AddResource
Throws:
IOException

addJavaScriptHere

public void addJavaScriptHere(javax.faces.context.FacesContext context,
                              String uri)
                       throws IOException
Insert a [script src="url"] entry at the current location in the response.

Specified by:
addJavaScriptHere in interface org.apache.myfaces.renderkit.html.util.AddResource
Parameters:
uri - is the location of the desired resource, relative to the base directory of the webapp (ie its contextPath).
Throws:
IOException

writeJavaScriptReference

protected static void writeJavaScriptReference(javax.faces.context.FacesContext context,
                                               String resourceUri,
                                               boolean encoding,
                                               boolean defer)
                                        throws IOException
Throws:
IOException

writeJavaScriptReference

protected static void writeJavaScriptReference(javax.servlet.http.HttpServletResponse response,
                                               javax.faces.context.ResponseWriter writer,
                                               String resourceUri,
                                               boolean encoding,
                                               boolean defer)
                                        throws IOException
Throws:
IOException

writeStyleReference

protected static void writeStyleReference(javax.faces.context.FacesContext context,
                                          String resourceUri)
                                   throws IOException
Throws:
IOException

writeStyleReference

protected static void writeStyleReference(javax.servlet.http.HttpServletResponse response,
                                          javax.faces.context.ResponseWriter writer,
                                          String resourceUri)
                                   throws IOException
Throws:
IOException

writeInlineScript

protected static void writeInlineScript(javax.faces.context.ResponseWriter writer,
                                        String inlineScript)
                                 throws IOException
Throws:
IOException

writeInlineStylesheet

protected static void writeInlineStylesheet(javax.faces.context.ResponseWriter writer,
                                            String inlineStyle)
                                     throws IOException
Throws:
IOException

addJavaScriptHerePlain

public void addJavaScriptHerePlain(javax.faces.context.FacesContext context,
                                   String uri)
                            throws IOException
Specified by:
addJavaScriptHerePlain in interface org.apache.myfaces.renderkit.html.util.AddResource
Throws:
IOException

addJavaScriptHere

public void addJavaScriptHere(javax.faces.context.FacesContext context,
                              org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler)
                       throws IOException
Insert a [script src="url"] entry at the current location in the response.

Specified by:
addJavaScriptHere in interface org.apache.myfaces.renderkit.html.util.AddResource
Parameters:
context - The current faces-context
resourceHandler - is an object which specifies exactly how to build the url that is emitted into the script tag. Code which needs to generate URLs in ways that this class does not support by default can implement a custom ResourceHandler.
Throws:
IOException

addResourceHere

public void addResourceHere(javax.faces.context.FacesContext context,
                            org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler)
                     throws IOException
Specified by:
addResourceHere in interface org.apache.myfaces.renderkit.html.util.AddResource
Throws:
IOException

validateResourceHandler

protected void validateResourceHandler(org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler)
Verify that the resource handler is acceptable. Null is not valid, and the getResourceLoaderClass method must return a Class object whose instances implements the ResourceLoader interface.

Parameters:
resourceHandler - handler to check

validateResourceLoader

protected void validateResourceLoader(Class resourceloader)
Given a Class object, verify that the instances of that class implement the ResourceLoader interface.

Parameters:
resourceloader - loader to check

addJavaScriptAtPosition

public void addJavaScriptAtPosition(javax.faces.context.FacesContext context,
                                    org.apache.myfaces.renderkit.html.util.ResourcePosition position,
                                    org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler)
Specified by:
addJavaScriptAtPosition in interface org.apache.myfaces.renderkit.html.util.AddResource

addJavaScriptAtPosition

public void addJavaScriptAtPosition(javax.faces.context.FacesContext context,
                                    org.apache.myfaces.renderkit.html.util.ResourcePosition position,
                                    Class myfacesCustomComponent,
                                    String resourceName)
Specified by:
addJavaScriptAtPosition in interface org.apache.myfaces.renderkit.html.util.AddResource

addJavaScriptAtPosition

public void addJavaScriptAtPosition(javax.faces.context.FacesContext context,
                                    org.apache.myfaces.renderkit.html.util.ResourcePosition position,
                                    Class myfacesCustomComponent,
                                    String resourceName,
                                    boolean defer)
Specified by:
addJavaScriptAtPosition in interface org.apache.myfaces.renderkit.html.util.AddResource

addJavaScriptAtPosition

public void addJavaScriptAtPosition(javax.faces.context.FacesContext context,
                                    org.apache.myfaces.renderkit.html.util.ResourcePosition position,
                                    String uri)
Specified by:
addJavaScriptAtPosition in interface org.apache.myfaces.renderkit.html.util.AddResource

addJavaScriptAtPosition

public void addJavaScriptAtPosition(javax.faces.context.FacesContext context,
                                    org.apache.myfaces.renderkit.html.util.ResourcePosition position,
                                    String uri,
                                    boolean defer)
Specified by:
addJavaScriptAtPosition in interface org.apache.myfaces.renderkit.html.util.AddResource

addJavaScriptToBodyTag

public void addJavaScriptToBodyTag(javax.faces.context.FacesContext context,
                                   String javascriptEventName,
                                   String addedJavaScript)
Deprecated. 

Specified by:
addJavaScriptToBodyTag in interface org.apache.myfaces.renderkit.html.util.AddResource
Parameters:
context -
javascriptEventName -
addedJavaScript -

addJavaScriptAtPosition

public void addJavaScriptAtPosition(javax.faces.context.FacesContext context,
                                    org.apache.myfaces.renderkit.html.util.ResourcePosition position,
                                    org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler,
                                    boolean defer)
Specified by:
addJavaScriptAtPosition in interface org.apache.myfaces.renderkit.html.util.AddResource

addJavaScriptAtPositionPlain

public void addJavaScriptAtPositionPlain(javax.faces.context.FacesContext context,
                                         org.apache.myfaces.renderkit.html.util.ResourcePosition position,
                                         Class myfacesCustomComponent,
                                         String resourceName)
Specified by:
addJavaScriptAtPositionPlain in interface org.apache.myfaces.renderkit.html.util.AddResource

addStyleSheet

public void addStyleSheet(javax.faces.context.FacesContext context,
                          org.apache.myfaces.renderkit.html.util.ResourcePosition position,
                          Class myfacesCustomComponent,
                          String resourceName)
Specified by:
addStyleSheet in interface org.apache.myfaces.renderkit.html.util.AddResource

addStyleSheet

public void addStyleSheet(javax.faces.context.FacesContext context,
                          org.apache.myfaces.renderkit.html.util.ResourcePosition position,
                          String uri)
Specified by:
addStyleSheet in interface org.apache.myfaces.renderkit.html.util.AddResource

addStyleSheet

public void addStyleSheet(javax.faces.context.FacesContext context,
                          org.apache.myfaces.renderkit.html.util.ResourcePosition position,
                          org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler)
Specified by:
addStyleSheet in interface org.apache.myfaces.renderkit.html.util.AddResource

addInlineStyleAtPosition

public void addInlineStyleAtPosition(javax.faces.context.FacesContext context,
                                     org.apache.myfaces.renderkit.html.util.ResourcePosition position,
                                     String inlineStyle)
Specified by:
addInlineStyleAtPosition in interface org.apache.myfaces.renderkit.html.util.AddResource

addInlineScriptAtPosition

public void addInlineScriptAtPosition(javax.faces.context.FacesContext context,
                                      org.apache.myfaces.renderkit.html.util.ResourcePosition position,
                                      String inlineScript)
Specified by:
addInlineScriptAtPosition in interface org.apache.myfaces.renderkit.html.util.AddResource

getResourceUri

public String getResourceUri(javax.faces.context.FacesContext context,
                             Class myfacesCustomComponent,
                             String resource,
                             boolean withContextPath)
Specified by:
getResourceUri in interface org.apache.myfaces.renderkit.html.util.AddResource

getResourceUri

public String getResourceUri(javax.faces.context.FacesContext context,
                             Class myfacesCustomComponent,
                             String resource)
Specified by:
getResourceUri in interface org.apache.myfaces.renderkit.html.util.AddResource

getResourceUri

public String getResourceUri(javax.faces.context.FacesContext context,
                             org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler)
Get the Path used to retrieve an resource.

Specified by:
getResourceUri in interface org.apache.myfaces.renderkit.html.util.AddResource

getResourceUri

public String getResourceUri(javax.faces.context.FacesContext context,
                             org.apache.myfaces.renderkit.html.util.ResourceHandler resourceHandler,
                             boolean withContextPath)
Get the Path used to retrieve an resource.

Specified by:
getResourceUri in interface org.apache.myfaces.renderkit.html.util.AddResource

getResourceUri

public String getResourceUri(javax.faces.context.FacesContext context,
                             String uri)
Get the Path used to retrieve an resource.

Specified by:
getResourceUri in interface org.apache.myfaces.renderkit.html.util.AddResource

getResourceUri

public String getResourceUri(javax.faces.context.FacesContext context,
                             String uri,
                             boolean withContextPath)
Get the Path used to retrieve an resource.

Specified by:
getResourceUri in interface org.apache.myfaces.renderkit.html.util.AddResource

getResourceUri

protected String getResourceUri(javax.faces.context.FacesContext context,
                                Class resourceLoader,
                                boolean withContextPath)
Get the Path used to retrieve an resource.

Parameters:
context - current faces-context
resourceLoader - resourceLoader
withContextPath - use the context-path of the web-app when accessing the resources
Returns:
the URI of the resource

getCacheKey

protected long getCacheKey(javax.faces.context.FacesContext context)
Return a value used in the {cacheKey} part of a generated URL for a resource reference.

Caching in browsers normally works by having files served to them include last-modified and expiry-time http headers. Until the expiry time is reached, a browser will silently use its cached version. After the expiry time, it will send a "get if modified since {time}" message, where {time} is the last-modified header from the version it has cached.

Unfortunately this scheme only works well for resources represented as plain files on disk, where the webserver can easily and efficiently see the last-modified time of the resource file. When that query has to be processed by a servlet that doesn't scale well, even when it is possible to determine the resource's last-modified date from servlet code.

Fortunately, for the AddResource class a static resource is only ever accessed because a URL was embedded by this class in a dynamic page. This makes it possible to implement caching by instead marking every resource served with a very long expiry time, but forcing the URL that points to the resource to change whenever the old cached version becomes invalid; the browser effectively thinks it is fetching a different resource that it hasn't seen before. This is implemented by embedding a "cache key" in the generated URL.

Rather than using the actual modification date of a resource as the cache key, we simply use the webapp deployment time. This means that all data cached by browsers will become invalid after a webapp deploy (all the urls to the resources change). It also means that changes that occur to a resource without a webapp redeploy will not be seen by browsers.

Parameters:
context - the current faces-context
Returns:
the key for caching

isResourceUri

public boolean isResourceUri(javax.servlet.ServletContext servletContext,
                             javax.servlet.http.HttpServletRequest request)
Specified by:
isResourceUri in interface org.apache.myfaces.renderkit.html.util.AddResource

serveResource

public void serveResource(javax.servlet.ServletContext context,
                          javax.servlet.http.HttpServletRequest request,
                          javax.servlet.http.HttpServletResponse response)
                   throws IOException
Specified by:
serveResource in interface org.apache.myfaces.renderkit.html.util.AddResource
Throws:
IOException

parseResponse

public void parseResponse(javax.servlet.http.HttpServletRequest request,
                          String bufferedResponse,
                          javax.servlet.http.HttpServletResponse response)
                   throws IOException
Specified by:
parseResponse in interface org.apache.myfaces.renderkit.html.util.AddResource
Throws:
IOException

writeMyFacesJavascriptBeforeBodyEnd

public void writeMyFacesJavascriptBeforeBodyEnd(javax.servlet.http.HttpServletRequest request,
                                                javax.servlet.http.HttpServletResponse response)
                                         throws IOException
Specified by:
writeMyFacesJavascriptBeforeBodyEnd in interface org.apache.myfaces.renderkit.html.util.AddResource
Throws:
IOException

writeWithFullHeader

public void writeWithFullHeader(javax.servlet.http.HttpServletRequest request,
                                javax.servlet.http.HttpServletResponse response)
                         throws IOException
Specified by:
writeWithFullHeader in interface org.apache.myfaces.renderkit.html.util.AddResource
Throws:
IOException

writeResponse

public void writeResponse(javax.servlet.http.HttpServletRequest request,
                          javax.servlet.http.HttpServletResponse response)
                   throws IOException
Specified by:
writeResponse in interface org.apache.myfaces.renderkit.html.util.AddResource
Throws:
IOException

requiresBuffer

public boolean requiresBuffer()
Specified by:
requiresBuffer in interface org.apache.myfaces.renderkit.html.util.AddResource

responseStarted

public void responseStarted()
Specified by:
responseStarted in interface org.apache.myfaces.renderkit.html.util.AddResource

responseFinished

public void responseFinished()
Specified by:
responseFinished in interface org.apache.myfaces.renderkit.html.util.AddResource

hasHeaderBeginInfos

public boolean hasHeaderBeginInfos()
Specified by:
hasHeaderBeginInfos in interface org.apache.myfaces.renderkit.html.util.AddResource


Copyright © 2012 The Apache Software Foundation. All Rights Reserved.