Buscar este blog

lunes, 31 de octubre de 2016

Spring MVC prevent caching static resources

Static resources like scripts or stylesheets are very good candidates for be cached by the browser. These resources don't  change during the life of the web application version, neither the adress where they are published, so there is no need to be actively fetch them in each request.

The problem is, what happen when one of these resources do change, for example, after the deployment of a new version of the web application. In this case, the browser keeps the previous resource version and the final user can't see the changes. In order to force the browser to refresh these resources, the user has to do something like Ctrl + F5, but this is quite cumbersome for the User Call Center.

That´s what happened to one of our web applications, so I started to thinking about some, more o less dirty, solution.

My first thought was to rename the resource file after each release. For example, if I have a /resources/css/styles.css file, I would rename it to styles-1.4.0.css file. In this way, when the resource was called from a HTML, the browser would interpret that this was a new one and made a new request.
This solution can be achieved by using Maven resource plugin, with filtering. But you have to parse all your JSP files where the resource is referenced, and also you need to rename these files to something like styles-${project.version}.css. I didn´t like.

Second try, google.
The first google solution was to use a cache control directive inside the mvc:resources configuration. You configure your resources to be valid only for a short period of time, so the browser will understand that they need to be reloaded after some time.
<mvc:resources mapping="/resources/**" location="/resources/">
    <mvc:cache-control max-age="3600" cache-public="true"/>
</mvc:resources>
This is valid, but you still depends on the browser goodwill. Besides, if your static resources don't change during the version lifespan, the browser should never reload then.

The second google solution was using Spring 4. As you can see in this good post, Spring 4 can create resources's fingeprints, so the file name is attached with some sort of hash automatically created.
This is exactly what I needed, but I was working with Spring 3. My web application had also dependencies with spring security and other spring components, so migrating all this stuff could take me some time.

Finally, I decided to make a mix with the Spring 4 aproximation and use the web application version as a query parameter. In this way, from the HTML the request will be something like http://domain/app/resources/css/styles.css?v=1.4.0

I created a custom tag (resources.tag) that is responsible for composing the full URL based on the resource path and on the application version.
<%@ tag pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<%-- Parámetros --%>
<%@ attribute name="resource" required="true" description="Path del resource"%>


<spring:eval expression="@applicationProperties.getProperty('version')" var="version"/>
<spring:url value="${resource}">
 <spring:param name="v" value="${version}"/>
</spring:url>

You need to have a properties file inside the application context which holds the version key.
<context:property-placeholder properties-ref="applicationProperties" order="1" />
<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
 <property name="locations">
  <list>
   <value>classpath:conf.properties</value>
  </list>
 </property>
</bean>

And here is the conf.properties.
version = ${project.version}

(... other keys ...)

As the version property value is also a placeholder, it is Maven who sets the proper value during the build phase. You need to set your configuration in the pom.xml file.
<build>
 <resources>   
  <resource>
   <directory>src/main/resources</directory>
   <includes>
    <include>conf.properties</include>    
   </includes>
   <filtering>true</filtering>
  </resource>
 </resources>

 <plugins>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>2.3.2</version>
   <configuration>
    <source>${java-version}</source>
    <target>${java-version}</target>
   </configuration>
  </plugin> 

  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-resources-plugin</artifactId>
   <version>2.5</version>
   <configuration>
    <encoding>UTF-8</encoding>
   </configuration>
  </plugin>

  (... Other plugins ...)
 </plugins>
</build>

Finally, inside your JSP, when you need to reference a static resource, you only need to use your custom resources.tag:
<link rel="stylesheet" type="text/css" href='<custom:resources resource="/resources/css/styles.css"/>'/>

domingo, 30 de octubre de 2016

JBoss - Spring URL UTF-8 encoding

If you need to pass special characters in the request parameters, the browser will encode them by using the specified encoding. This is the case, for example, when using the JQuery serialize method to compose a URL with the elements inside a form.

In my case, the problem arose when I was trying to load a jqGrid base table. The user fills some fields inside a form and when she presses the search button, a jqGrid is created and a request is sent to a Spring MVC Controller.
$("#myGrid").jqGrid({
      url: '/myapp/mypage/search?+ $('#myForm').serialize(),
      loadonce:true,
      mtype: 'GET',
      datatype: "json",
      (...)
});

When some field inside "myForm" has special characters, for example 'áéíóú', the URL generated will be as follows:
http://localhost:8080/myapp/mypage/search?descripcion=%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA&_search=false&nd=1477817915534&rows=20&page=1&sidx=&sord=asc
These values are the result of converting the input text to UTF-8. Inside the controller you would see ANSI characters but the application is expecting UTF-8.

The solution is to configure URI encoding in JBoss EAP 6.2 connector using the following standalone.xml configuration:
<system-properties>
     <property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/>
     <property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="true"/>
</system-properties>

In domain mode you can configure these properties at server level or server group level.
There are more useful JBoss properties I collected in a previous post: http://trabajosdesisifo.blogspot.com.es/2015/12/jboss-useful-system-properties.html

Anyway, if you are working with UTF-8, don't forget the the Spring CharacterEncodingFilter.

domingo, 23 de octubre de 2016

Jenkins - SonarQube - Exclude sources from code analysis

If you need to exclude some folders from SONAR code analysis, for example, style sheets or javascript files, there are two options:
  • Set exclusions in the POM.XML file.
  • Set exclusions in the SONAR Runner plugin, in Jenkins.
In both cases, the key properties are (check http://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus):
Use exclusion to analyze everything but the specified files:
  • sonar.exclusions - to exclude source code files
  • sonar.test.exclusions - to exclude unit test files
Use inclusion to analyzes only the specified files:
  • sonar.inclusions 
  • sonar.test.inclusions


For example, in POM.XML:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 (...)
 
 <properties>
  (...)  
        <sonar.exclusions>**/*.css,**/*.js</sonar.exclusions>      
 </properties>
 
</project>


In Jenkins, you can configure the SONAR plugin inside each job. In the Post-build Action you can set this configuration in the "Aditional properties" fields with "-D" prefix.


When the job runs, in the output console you will see something like this:
[INFO] [15:20:44.174] Source paths: src/main/webapp, pom.xml, src/main/java
[INFO] [15:20:44.175] Test paths: src/test/java
[INFO] [15:20:44.175] Binary dirs: target/classes
[INFO] [15:20:44.176] Source encoding: UTF-8, default locale: en_US
[INFO] [15:20:44.176] Index files
[INFO] [15:20:44.194] Excluded sources: 
[INFO] [15:20:44.194]   **/*.css
[INFO] [15:20:44.194]   **/*.js
[INFO] [15:20:47.235] 366 files indexed