0

In my application I have the following filter in web.xml

<filter>
    <filter-name>webServiceFilter</filter-name> 
    <filter-class>org.bakhtiari.filter.WebServiceFilter</filter-class> 
</filter>
<filter-mapping>

    <filter-name>webServiceFilter</filter-name>
    <url-pattern>/webservice/RestEasy/*</url-pattern>
</filter-mapping>

And two dispatcher Servlets are declared

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/webservice/RestEasy/*</url-pattern>
</servlet-mapping>

Now suppose a single HTTP request comes to url "/webservice/RestEasy/redirect/callUserService".

Considering my business, if the third part of URL is equal to "redirect" I have to call the URL "/rest/callUserService".

With all this in mind, I want to send a request to "mvc-dispatcher" with new URL and retrieve HTTPServletResponse from that.

Note that I cannot send a request to new URL using java.net or something like that, because my application has security filter on "/*" URLs and putting authentication parameters in each request cause lots of overload on server.

Tiny
  • 27,221
  • 105
  • 339
  • 599
Khoda
  • 955
  • 2
  • 14
  • 42
  • you will be using apache http server as well ? if yes, you can use redirect (If I understood your question at all) – akshayb Sep 15 '14 at 09:48
  • yes, tomcat. No thats not possible. I want to get the response from servlet, do some changes on it and then send it back as result. – Khoda Sep 15 '14 at 09:52
  • http://httpd.apache.org/ apache http server is here. btw, you have option of using jsp redirect as well. I believe, redirect is what you are looking for. – akshayb Sep 15 '14 at 09:54
  • Thanks, BTW redirection is not an appropriate solution for my case. – Khoda Sep 15 '14 at 09:56
  • okey, is it something like : get the url, find "redirect" using the regular expression (or whatever), if it is at third place, send a request to a new url /rest/call.... via ajax and get the content ? – akshayb Sep 15 '14 at 10:01
  • you came all the way except the last part :D, we got new URL on server side, now we want to inform mvc-dispatcher that we want the result of the new URL. All these steps should be done via java codes. – Khoda Sep 15 '14 at 10:07
  • But yes, you got it. – Khoda Sep 15 '14 at 10:09
  • If code must be in Java and not via ajax (or anyother frontend trick possible), you can download that url in "if" condition as above and you get the html of that url as String. You can further get required data by parsing that HTML using html parser like JSoup. – akshayb Sep 15 '14 at 10:13
  • Like I said we shouldn't try to get result with new request and above security level because of authentication issue. I think the best way is sending a request to detected dispatcher, but I dont know how. Do you have any other idea? thanks. – Khoda Sep 15 '14 at 10:17
  • possible duplicate of [How can I send http request to another servlet](http://stackoverflow.com/questions/18148932/how-can-i-send-http-request-to-another-servlet) – Joe Sep 15 '14 at 10:18
  • other than http request, I have no idea. – akshayb Sep 15 '14 at 10:21
  • brilliant Joe, it is exactly that. let me test it more and then I'll mark it as the answer. – Khoda Sep 15 '14 at 10:33

1 Answers1

0

The solution is

public class WebServiceFilter implements Filter {

   @Override
   public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException,
         IOException {
      if(/* CONDITION */){

         RequestDispatcher rd = req.getRequestDispatcher("/rest/callUserService");
         rd.forward(req, res);
      }
   }
}
Khoda
  • 955
  • 2
  • 14
  • 42