Thursday, June 30, 2011

Response Header with MVC interceptors in Spring

Response Header's in Spring can be easily set using org.springframework.web.servlet.support.WebContentGenerator as this is a abstract class so we use a direct known sub class for the same which is org.springframework.web.servlet.mvc.WebContentInterceptor , this Interceptor checks and prepares request and response. Checks for supported methods and a required session, and applies the specified number of cache seconds.

Here is the Example, this should be under beans tag in your server-config.xml file

<mvc:annotation-driven />
<mvc:interceptors>
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="120"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="requireSession" value="false"/>
<property name="useCacheControlNoStore" value="true" />
<property name="cacheMappings">
<props>
<prop key="/**/*.html">2000</prop>
<prop key="/**/*.css">500000</prop>
<prop key="/**/*.js">2592000</prop>
</props>
</property>
</bean>
</mvc:interceptors>

In CacheMapping attribute we can specify the cache time for different file types, as this increases the preformance of application.

Gzip in Apache Tomcat - Faster Responses with Compression


Today's Browser have capability to support gzip content and uncompressed the content to plain text. The data comes from server to client in a compressed form with increases the performance many times as less data get transfered on network.

Just go to Tomcat/conf/Server.xml file and replace the default node

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" >

with follwing

<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true"
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,application/json">


Make sure you add the MIME Type which you want to add compression as follows
compressableMimeType="text/html,text/xml,application/json"

# Tomcat handles the compression for the supporting brorwers and do not compress response in case the browser is from monolithic age :)

- Varun Rathore

Tuesday, June 21, 2011

PJson in Spring using JacksonJson (Cross Domain Issues)

We can create pjson (json with padding) to achieve cross domain java-script call which is very important if data is coming from other domain.

Here is what i did to get pjson from object.
I created a class MappingJacksonJsonpView extended it by AbstractView, now here is the trick
I overrided a method renderMergedOutputModel as follows:

@Override
protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception
{
Object value = filterModel(model);
JsonGenerator generator = objectMapper.getJsonFactory().createJsonGenerator(response.getOutputStream(), encoding);
String callback = request.getParameter("jsoncallback");
prefixJson = false;
if (callback!=null)
{
prefixJson = true;
}
if (prefixJson)
{
generator.writeRaw(callback + "(");
}
objectMapper.writeValue(generator, value);
generator.flush();

if (prefixJson)
{
generator.writeRaw(");");
generator.flush();
}
}

and make sure you put entry in your servlet.xml file
<property name="defaultViews">
<list>
<bean class="com.views.utility.MappingJacksonJsonpView" />

About Me