Created
March 29, 2012 03:41
-
-
Save seratch/2233069 to your computer and use it in GitHub Desktop.
Working with Jersey's Form objects at outside of Resource classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package example.threadlocal; | |
| import com.sun.jersey.api.representation.Form; | |
| public class ThreadLocalForm { | |
| private static final ThreadLocal<Form> THREAD_LOCAL = new ThreadLocal<Form>(); | |
| public static void set(Form value) { | |
| THREAD_LOCAL.set(value); | |
| } | |
| public static Form get() { | |
| return THREAD_LOCAL.get(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package example.containerfilter; | |
| import example.threadlocal.ThreadLocalForm; | |
| import com.sun.jersey.spi.container.ContainerRequest; | |
| import com.sun.jersey.spi.container.ContainerRequestFilter; | |
| public class ThreadLocalFormFilter implements ContainerRequestFilter { | |
| @Override | |
| public ContainerRequest filter(ContainerRequest request) { | |
| ThreadLocalForm.set(request.getFormParameters()); | |
| return request; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- Jersey with Spring --> | |
| <filter> | |
| <filter-name>Jersey with Spring</filter-name> | |
| <filter-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</filter-class> | |
| <init-param> | |
| <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> | |
| <param-value>example.containerfilter.ThreadLocalFormFilter</param-value> | |
| </init-param> | |
| </filter> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package example.interceptor; | |
| import com.sun.jersey.api.representation.Form; | |
| import example.threadlocal.ThreadLocalForm; | |
| import org.aopalliance.intercept.MethodInterceptor; | |
| import org.aopalliance.intercept.MethodInvocation; | |
| public class WorkingWithFormInterceptor implements MethodInterceptor { | |
| @Override | |
| public Object invoke(MethodInvocation invocation) throws Throwable { | |
| Form form = ThreadLocalForm.get(); | |
| // do something with the Form object | |
| // ... | |
| return invocation.proceed(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment