OmniFaces CDNResourceHandler could not find resources when not included locally - jsf

I'm using OmniFaces CDNResourceHandler to point my resources to a CDN, instead of local files.
I added this line in my XHTML file: <h:outputStylesheet library="twitter-bootstrap" name="bootstrap.min.css" />
And my faces-config.xml have this line:
<context-param>
<param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
<param-value>
twitter-bootstrap:bootstrap.min.css=https://somehost/twitter-bootstrap/3.3.7/bootstrap.min.css
</param-value>
</context-param>
And I'm getting this error when access the page:
Unable to find resource twitter-bootstrap, bootstrap.min.css
Note: When I access the file at https://somehost/twitter-bootstrap/bootstrap.min.css I can download the file properly.
I'm using Mojarra under Wildfly configured to Development stage.
The resource handler is properly configured at faces-config.xml file.
<application>
<resource-handler>org.omnifaces.resourcehandler.CDNResourceHandler</resource-handler>
</application>
I did some tests, and I notice that the error doesn't occurs if I create an empty file bootstrap.min.css under WEBAPP_FOLDER/resources/twitter-bootstrap. If I delete the file, the errors occurs again.
Even I use CDN, do I need to keep resources locally?

The CDNResourceHandler is primarily intented to move auto-included JSF resources to a CDN, such as jsf.js file from <f:ajax>, or primefaces.js and jquery.js from PrimeFaces, or to automatically switch to a CDN when installed in production.
You don't need it in your case with a permanent CDN resource. Just use plain <link>.
<link rel="stylesheet" src="https://somehost/twitter-bootstrap/bootstrap.min.css" />
This is also explicitly mentioned in the CDNResourceHandler documentation.
For non-JSF resources, you can just keep using plain HTML <script> and <link> elements referring the external URL
Update: as you're not the first one who wondered about this, I've as per issue 122 bypassed this technical restriction for OmniFaces 2.6. In other words, you do not necessarily need a local resource anymore.

Related

Pointing to resources located on a remote CDN

I would like my server to point to resource files (i.e. css, js, images that are under /resources/default/VERSION_NUMBER/) to AWS S3 instead of delivering the files to the client side by itself.
Here are the relevant libraries and their version number taken from the pom file:
<javax.servlet-api.version>3.0.1</javax.servlet-api.version>
<weld-servlet.version>2.2.9.Final</weld-servlet.version>
<javax.el-api.version>3.0.0</javax.el-api.version>
<el-impl.version>2.2</el-impl.version>
<tomcat-jdbc.version>7.0.47</tomcat-jdbc.version>
<javax.faces.version>2.2.10</javax.faces.version>
<omnifaces.version>2.3</omnifaces.version>
<primefaces.version>6.1</primefaces.version>
I took a look at this post and modified my web.xml file with the following lines:
<context-param>
<param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
<param-value>
remote:*=https://[URL]
</param-value>
</context-param>
And updated the library tag in all my css, javascript and image files mentioned in my xhtml file accordingly.
example:
<h:outputStylesheet library="default" name="styles/header.css"/>
Is modified to
<h:outputStylesheet library="remote" name="styles/header.css"/>
Now I am noticing that while the css files are loaded properly from the remote server, the url tags such the following present in the css files (hosted on the remote server) are not being downloaded from the local or remote server
background-image: url("#{resource['remote:images/add-default.png']}") !important;
Note that everything under my resource folder is already hosted in my remote server. I imagine JSF is failing to properly create the url from #{resource['remote:images/add-default.png'}] because in firefox console I get the error "The resource at “” was blocked because content blocking is enabled." for these files.
Once I figure out how to fix this, I would also like to know how to make it so all the jsf resources required on the client side that I don't explicitly specify in my xhtml files can be also hosted in the remote server.
Thank you!

Web Flow + JSF integration default page

I use Web Flow and JSF, so they works well actually. But I am trying to find out alternative way for set default page different from redirecting on index.html.
The main problem is web analytics scripts don't work properly. I can't track user source fore home page.
The application run on Tomcat 8
Web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
index.html
<html>
<head>
<meta http-equiv="Refresh" content="0; URL=web/home-page">
</head>
</html>
UPDATE :
I replace index.html with index.jsp and I set response status as 301. At least it works for google analytics, so I'll check it out for other analytics tools.
But this solution still did not satisfy me.
Web.xml
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
index.jsp
<%
response.setStatus(301);
String path=(String)request.getAttribute("javax.servlet.forward.request_uri");
if(path==null){
path="web/home-page";
}
response.setHeader( "Location", path);
response.setHeader( "Connection", "close" );
%>
I would use spring security project instead of welcome files because JSF (or any view framework) is not aware how to resolve a view in welcome-file that is why your logic is not executing.
A possible solution is if you are using the spring security project. Add the following to your security config
<security:http auto-config="true" use-expressions="true">
<!--- config omitted for brevity -->
<security:session-management invalid-session-url="/index.jsp"/>
</security:http>
Again this is just a sample. You can also use other means to define rules. It is quite modular
<security:intercept-url pattern="/**" ....
Moreover, you will need to define a classless controller with the following defintion: (assuming you also are using Spring MVC with Webflow)
<mvc:view-controller path="/index.jsp" />
I think it's less cryptic to define intercept,forward,routing, etc... rules all in Spring Security config that way it is clear that any such rules are stored in one place.
Note: you might be able to keep your current setup and use that spring <mvc:view-controller path="/index.jsp" /> definition only to trigger the JSF to execute.

Set HTTP headers properly to force caching on JS, CSS and PNG files

How can I tell to GlassFish server, to store all JS, CSS and PNG files into browser cache in order to reduce HTTP GET requests?
I am using JSF and PrimeFaces.
Just make use of JSF builtin resource handler. I.e. use <h:outputStylesheet name>, <h:outputScript name> and <h:graphicImage name> with files in /resources folder instead of "plain vanilla" <link rel="stylesheet">, <script> and <img>.
<h:outputStylesheet name="css/style.css" />
<h:outputScript name="js/script.js" />
<h:graphicImage name="images/logo.png" />
This way you don't need to worry about resource caching at all. JSF builtin resource handler has already set the necessary response headers. The expiration time defaults already to 1 week.
In Mojarra you can control the expiration time by the following context parameter (the value is in millis):
<context-param>
<param-name>com.sun.faces.defaultResourceMaxAge</param-name>
<param-value>3628800000</param-value> <!-- 6 weeks. -->
</context-param>
And in MyFaces:
<context-param>
<param-name>org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES</param-name>
<param-value>3628800000</param-value> <!-- 6 weeks. -->
</context-param>
See also:
How to reference CSS / JS / image resource in Facelets template?
What is the JSF resource library for and how should it be used?

how to enable browser caching in jsf

I have created a web application using JSF 2.0. I got feedback from my friend saying I should do "Browser Caching" as I have many images.
However I don't know how to do same in JSF. Any idea/ hint would be appreciated.
Concept on what to be done would also work.
Just use <h:graphicImage name="..."> instead of <img src="...">. This way the default JSF resource handler will instruct the browser to cache them for 1 week by default, which is configureable with an implementation dependent context parameter, which is the following in case of Mojarra:
<context-param>
<param-name>com.sun.faces.defaultResourceMaxAge</param-name>
<param-value>3628800000</param-value> <!-- 6 weeks -->
</context-param>
Note, the same applies when using <h:outputScript> and <h:outputStylesheet> instead of <script> and <link rel="stylesheet">.

JSF 1.2 migration to Facelets

My all application is written with jsf 1.2
I want to use from this moment on facelets where xhtml files are .
So I have some main questions:
1.I want to
face one:upgrade to jsf 1.2 with servlets
face two:from there to upgrade to 2.0
would this two changes break my application ?
2.how to do this ? can some one explain to me ? I have been trying all day long to do it i had :
changed a file to xhtml amd changed his taglibs to xmlns such as :
<%# taglib prefix="a4j" uri="http://richfaces.org/a4j" %> will be converted to : xmlns:a4j="http://richfaces.org/a4j"
adding
<view-handler>
com.sun.facelets.FaceletViewHandler
</view-handler>
inside application in the faces config
changing file name from xxx.jsp to xxx.xhtml
adding
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
to web xml
inserting
f:view tag above all
the problem i am facing now
when *.jsf page is called the server says no page like this exists in the system...
when *.xhtml is called the page itself is blank and the server asks me to download it meaning a pop up with open and save options popes.

Resources