I'm working on a web application (Java) server side and I want to use servlet filter to filter client requests.
I created a class that implements Filter, like this:
public class ServletFilter implements Filter{
private ServletContext context;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
...
}
@Override
public void destroy(){
...
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain){
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
if(req.getContentType() != null && req.getContentType().toLowerCase().contains("multipart/form-data")){
RequestDispatcher rd = request.getRequestDispatcher("servlet/called");
rd.forward(request, response);
}
chain.doFilter(request,response);
}
}
In this case, there is only one filter, so when request arrived, it is intercepted from filter and if the Content-Type of the request is multipart/form-data, call a specific servlet via RequestDispatcher. It works.
Then, since there are no other filters, the command chain.doFilter(request,response) call myMethod (the method which is called from client request).
myMethod like as:
@POST
@Path("example1")
@Consumes("application/x-www-form-urlencoded")
public void myMethod(@FormParam("element") String entity){
...
}
The problem is that this method is never called from filter. But, if client make a request for otherMethod, like this:
@POST
@Path("example2")
@Consumes("multipart/form-data")
public void otherMethod(@FormDataParam("element") String entity){
...
}
the request is forwarded and the method is called.
myMethod and otherMethod belong to the same class, that has annotation: @Path(value="ws").
This is my web.xml:
<filter>
<filter-name>FilterServlet</filter-name>
<filter-class>packages.servlets.FilterServlet</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterServlet</filter-name>
<url-pattern>/ws/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
Body data are sent as FormDataParam in client request.
I think the problem is represented by different media type (multipart or form-urlencoded) consumed by webservices. So, can I convert Content-Type of the request from multipart to form-urlencoded? How can I solve it? What am I doing wrong?
Thanks!