Resteasy spring security - duplicate requests - rest

I'm having hard time to get RESTeasy working with my existing application. I can hit my end points (AdminService). My problem is everytime I make a GET request (over browser) I hit twice on my service so it leads to duplicate results. I tried with POST using a java client still the problem is there. Following you can find my technology list.
Spring security 3.x
Spring 2.5
RestEasy 2.3.5.Final
==Part of my web.xml==
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>au.com.esriau.dekho.application.ApplicationLoader</listener-class>
</listener>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>rest</param-value>
</context-param>
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.admin.AdminService
</param-value>
</context-param>
I'd appreciate your solution or idea to trouble shoot this.
cheers

Don't bother reading this. This was due to my custom implementation of AbstractAuthenticationProcessingFilter in spring-security. I was basically making unnecessary call to the super class with super.doFilter(request, response, chain). This was done right after I execute my custom filters. Getting rid of super.doFilter resolved the issue for me.

Related

Getting com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes on JBoss EAP 7

My Environment: JBOSS EAP 7, Jersey Rest Service 1.15
Getting com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
web.xml
<display-name>Rest App 2</display-name>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.test.app.service</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<!-- <load-on-startup>1</load-on-startup> -->
</servlet>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
I tried all the different options, but no success. Any help greatly appreciated.

JPetShop and Azure Application Insight

I would like to to test the Application Insight tool of Microsoft Azure.
For this reason, I took a Spring application, jpetshop (https://github.com/mybatis/jpetstore-6), and I am trying to set up the Application Insight on top of it following this guide: https://learn.microsoft.com/en-us/azure/application-insights/app-insights-java-get-started
I added the Maven dependency and repository and, thanks to Eclipse, I created the ApplicationInsights.xml file with my InstrumentationKey. The only thing that I am not sure about is how to add is the HTTP filter on this application.
I modified the web.xml in this way:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>JPetStore</display-name>
<description>Online Pet Store Sample Application</description>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>StripesResources</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<display-name>Stripes Filter</display-name>
<filter-name>StripesFilter</filter-name>
<filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
<init-param>
<param-name>ActionResolver.Packages</param-name>
<param-value>org.mybatis.jpetstore.web</param-value>
</init-param>
<init-param>
<param-name>Extension.Packages</param-name>
<param-value>net.sourceforge.stripes.integration.spring</param-value>
</init-param>
<init-param>
<param-name>ApplicationInsightsRequestNameInterceptor</param-name>
<param-value>com.microsoft.applicationinsights.web.struts.RequestNameInterceptor</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>StripesFilter</filter-name>
<servlet-name>StripesDispatcher</servlet-name>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>StripesDispatcher</servlet-name>
<servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>StripesDispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
However, if I change the filter-mapper field as shown in the guide, the application doesn't work anymore.
Since the jpetshop application uses the Spring framework I also though to modify the *-servlet.xml file. However, this file is not present in this project.
Do you know how can I make the Azure Application Insight work on this application?
Thank you
I think you missed the section where you need to define ApplicationInsights's filter:
<filter>
<filter-name>ApplicationInsightsWebFilter</filter-name>
<filter-class>
com.microsoft.applicationinsights.web.internal.WebRequestTrackingFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>ApplicationInsightsWebFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

REST using several resteasy providers in different war files

In the deployed EAR file there are a couple of WARs which configure rest providers.
file1.war:
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>
path1.exception.Mapper1
</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/rest/path1</url-pattern>
</servlet-mapping>
file2.war:
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>
path2.exception.Mapper2
</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
Now when the /rest/path2 url is used, the path1.exception.Mapper1 is invoked, although only the path2.exception.Mapper2 should be used.
How can I archive it?
I tried to use in web.xml of file1.war:
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest/path1/* </param-value>
</context-param>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/rest/path1</url-pattern>
</servlet-mapping>
with no success (when try to access the /rest/path2 url, path1.exception.Mapper1 catched it and worked).
Any ideas?

RESTEasy - Resource Class Register Failed when #Path is Marked in the Interface

I am using RESTEasy 2.0.1.GA on Jboss 4.3.0.GA.
The interface that my resource class implemented looks like this:
#Path("/")
public interface CwindRestfulService {
#GET
#Path("accounts/{email}")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Account getAccountInfo(#PathParam("email") String email) throws Exception;
}
When I set resteasy.scan=true in web.xml, it works fine. But it should be false with spring context. So now my web.xml is:
<web-app>
<display-name>Archetype RestEasy Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.cwind.rest.CwindRestfulServiceImpl</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
And I got this error:
2014-02-20 18:10:23,303 ERROR [org.apache.catalina.core.ContainerBase] Exception sending context initialized event to listener instance of class org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
java.lang.RuntimeException: Class is not a root resource. It, or one of its interfaces must be annotated with #Path: com.cwind.rest.CwindRestfulServiceImpl implements: com.cwind.rest.CwindRestfulService
It suggested that I missed #Path while actually I didn't. But when I put the annotation to the resource class it does work. I googled and found a solution in http://pilhuhn.blogspot.com/2011/06/seemingly-common-resteasy-error-and.html. It needs to add a #ApplicationPath annotation. Now my interface is in a third party lib so I can't modify it. Does anybody have some suggestions? Thanks in advance.
It appears that you have not registered a Resteasy specific SpringContextLoaderListener. This is required for Spring to register scanned resource classes with Resteasy.
Below is an example web.xml configuration (this uses annotation configuration, but will also work with xml configuration):
<!-- Spring Configuration -->
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.myapp.SpringConfig</param-value>
</context-param>
<!-- RESTEasy Configuration -->
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/</param-value>
</context-param>
<!-- RESTEasy <-> Spring Connector (Needed so that RESTEasy has access to Spring managed beans!) -->
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>
<!-- RESTEasy HTTP Request Processor Servlet -->
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Then you simply annotate your resource classes for Spring either with #Component or #Named as shown below:
#Named
#Path("/")
public class MyResource implements CwindRestfulService
{
//Stuff Goes Here...
}

Deployment Error for module in GlassFish Server 3.1.2

I found the same question asked previously here but I didn't found the suitable answer. I am designing my web in JSF2.0 with PrimeFaces 3.5 as a JSF component suite. Below is the error coming while automatic deploy is in process-
Below is the error which is coming while restarting the GlassFish Server 3.1.2 (As automatic deploy is not working so i thought of restarting it)-
Below is the error displayed by GlassFish Server 3.1.2 in Eclipse console-
SEVERE: service exception
java.lang.NullPointerExceptionnull at java.lang.String.replace(String.java:2219)null at com.sun.enterprise.v3.common.PropsFileActionReporter.setMessage(PropsFileActionReporter.java:67)null at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:219)null at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179)null at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)null at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354)null at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)null at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)null at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)null at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)null at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)null at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)null at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)null at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)null at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)null at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)null at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)null at com.sun.grizzly.ContextTask.run(ContextTask.java:71)null at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)null at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)null at java.lang.Thread.run(Thread.java:679)null
My web.xml looks like below-
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="portal" version="3.0">
<display-name>Portal</display-name>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>cupertino</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<!-- Primefaces file upload -->
<!-- thresholdSize specifies the maximum file size in bytes to keep uploaded
files in memory. If it exceeds this limit, it’ll be temporarily written to
disk. -->
<!-- uploadDirectory is the folder where to keep temporary files that exceed
thresholdSize. -->
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<context-param>
<param-name>uploadDirectory</param-name>
<param-value>/opt/upload/</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
I am using Eclipse Indigo on Ubuntu 10.04.4 LTS.