Tag Archives: myjavarecipes

How to Read Post Request data twice in Spring

Hi Readers,

i got requirement to read to HTTP Post Request data twice in spring MVC interceptor but the problem is when am trying read it,am getting this exception in spring controller “java.lang.IllegalStateException: getReader() has already been called for this request

We are getting this exception because request.getInputStream() called twice, once in MVC interceptor and once spring controller.To fix this we need write wrapper on HTTP request but it will not solve our problem because after wrapping request there is no way to hand over same wrapper request to spring controller,to do that we need to write traditional filter.

follow the below steps

Step 1)Write simple wrapper on HTTP request so that it can be used in filter we are going write in Step 2.

package com.myjavarecipes.web;
public class MultiReadHttpServletRequest extends HttpServletRequestWrapper {
 private String _body;
public MultiReadHttpServletRequest(HttpServletRequest request) throws IOException {
 _body = "";
 BufferedReader bufferedReader = request.getReader();
 String line;
 while ((line = bufferedReader.readLine()) != null){
 _body += line;

public ServletInputStream getInputStream() throws       IOException {
 final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_body.getBytes());
 return new ServletInputStream() {
 public int read() throws IOException {
 return byteArrayInputStream.read();

public BufferedReader getReader() throws IOException {
 return new BufferedReader(new InputStreamReader(this.getInputStream()));

2)Write Traditional J2EE filer,in Filter doFilter method we wrap HTTP request with custom wrapper MultiReadHttpServletRequest and pass it back to filter chain,so that now onward when ever request.getInputStream() get called it will call the overrided method in MultiReadHttpServletRequest class and it returns same ServletInputStream which is already read by wrapper.

 package com.myjavarecipes.web;
 import java.io.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 import java.util.*;

 public class RequestWrapperFilter implements Filter {
 public void init(FilterConfig config)
      throws ServletException{
 // nothing goes here
public void doFilter(ServletRequest request,ServletResponse           response, FilterChain chain)
     throws java.io.IOException, ServletException {
MultiReadHttpServletRequest requestWrapper=new      MultiReadHttpServletRequest(request);
 // Pass request back down the filter chain

 public void destroy( ){
 /* Called before the Filter instance is removed
 from service by the web container*/

3)Configure filter in web.xml so that container will call our filter for each HTTP request.


4)Now onward HTTP request object can be read more than once it means we can read it in Spring MVC interceptor and as well as in Spring controller.

Things to do after landing in U.S.A first time

These are  things you needed to after landing in U.S.A

1)Buy prepaid sim so that you can make phones call,to get prepaid sim you really don’t need any thing like id proof or any thing like in India.

check out this carrier http://www.lycamobile.us/en/29-international-cellphone-plan

2)Install Google Maps for navigation, set the address and go.
Here no one helps in navigation because this country so big to remember any thing.

3)Install Uber application in mobile to book cab,if you lost just book cab from mobile using Uber app.I personally felt very comfortable with Uber app.

4)Figure out public transport in the place you are going to live just google it.Cabs are expensive public transport is cheap but you should know the routes.

5)Apply for SSN this is the fist thing to do if you are here on H1B/L1 Visa.

documents needed to apply SSN https://www.socialsecurity.gov/ssnumber/ss5doc.htm

6)Apply for driving license,you can drive here with India license for couple months but better to apply Learners permit as soon you landed here so that slowly you can get valid driving license.

7)Find nearest Indian grocery store just google it.

8)More importantly Google it for anything before you start doing.

finally Grab beer:)

What are things need to carry to USA fist time check below post:


Spring Profiling/Audit using MVC interceptors

Hi Readers,

recently i got requirement from customer that they want to audit Spring web application services. In simple terms they want to know which of services used more and less. Requirement is simple,just by writing one Spring MVC web interceptor we can scan all the Http requests and write audit logic but here is the actual problem that they want us to audit only few of the services not all other services,it means i have to figure out the methods which need be audited in interceptor.

To achieve i did following  steps

  1. Define custom annotation.
  2. Use this annotation at spring MVC controller method.
  3. Write own interceptor,access called method and class level annotations in it and if expected annotation present then write audit logic

STEP 1:  Define @ApplicationAudit custom annotation.

package com.myjavarecipes.common.controller

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD) //can use in method only.
public @interface AppicationAudit{
	String value();

STEP 2:Add this custom annotation in Spring controller method addAccount().

package com.myjavarecipes.common.controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

public class HelloWorldController{

@RequestMapping(value="/addAccount",method = RequestMethod.POST)
public ModelAndView addAccount(){
  ModelAndView model = new ModelAndView("AddAccount");
   return model;

public ModelAndView getAccount(){

 ModelAndView model=new ModelAndView("getAccount");

 //implementation goes here

 return model;

 STEP 3: Add Spring MVC interceptor entry in spring context xml.
  <bean class="com.myjavarecipes.interceptor.AuditInterceptor" />
STEP 4:  write own AuditInterceptor spring web mvc interceptor,in it’s methods preHandle,postHandle,afterCompletion identify called method controller class and method has expected annotation if it present write audit logic.
package com.myjavarecipes.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
import com.myjavarecipes.common.controller.ApplicationAudit; 

public class AuditInterceptor implements HandlerInterceptor { 

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 
HandlerMethod hm=(HandlerMethod)handler; 
Method method=hm.getMethod(); if(method.getDeclaringClass().isAnnotationPresent(Controller.class)){
return true; 

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerMethod hm=(HandlerMethod)handler;
 Method method=hm.getMethod();

{ System.out.println(method.getAnnotation(ApplicationAudit.class).value()); request.setAttribute("ENDTIME",System.currentTimemillis());  }

public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 
System.out.println("After completion handle"); HandlerMethod hm=(HandlerMethod)handler; 
Method method=hm.getMethod(); if(method.isAnnotationPresent(ApplicationAudit.class))
{ System.out.println(method.getAnnotation(ApplicationAudit.class).value()); 
System.out.println("Total Took:"+((Long)request.getAttribute("ENDTIME")-(Long)request.getAttribute("STARTTIME")));  

How to get H1B visa

  1. Find employer who can apply for H1B Visa for every march.
  2. Employer will have attorney  to prepare H1B visa petitions for their employees.
  3. Attorney file visa petition at USCIS in every march month.
  4. USCIS will perform computerized lottery in April to select limited cap visa.
  5. If your petition selected in lottery then USCIS will do verification on your via petitions.
  6. If your petition got approved then your employer will receive H1B approval letter.
  7. Your employer share all necessary documents with H1B approval letter.
  8. Attend Visa interview at nearest USA embassy.
  9. If everything goes well visa officer will keep your passport to stamp it.
  10. You will receive your passport with Visa in 5 working days.

Watch this video for better understanding https://www.youtube.com/watch?v=DkYmWFD3gd0