0

I have developed my rest service in JAX-RS Jersey. I have deployed in the Tomcat 7.0. Now I am exposing my web service URL to third party client. I want to put validation mechanism that includes getting host name i.e. the client host name that is using my service. I would like to match with our database entered host name. How to get the host name of the client?

Here is my web.xml:

<servlet>
    <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/intellixservices/*</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>secureRESTFilter</filter-name>
        <filter-class>com.astroved.intellix.security.SecurityFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>secureRESTFilter</filter-name>
        <url-pattern>/intellixservices/*</url-pattern>
    </filter-mapping>

Now I am creating a SecurityFilter class implementing Filter. inside doFilter() method -

@Override
public void doFilter(ServletRequest req, ServletResponse res,
        FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpReq = (HttpServletRequest) req;
    HttpServletResponse httpRes = (HttpServletResponse)res;
    String url = "http://localhost:8888/IntellixWebApi/intellixservices/dnareport";
    System.out.println("In security filter");
    req.getRequestDispatcher(url).forward(req, res);    

    chain.doFilter(httpReq, httpRes);
}

But it is not forwarding to the next URL. In Resource class, it is returning xml/json type.

kryger
  • 12,906
  • 8
  • 44
  • 65
Kumar
  • 955
  • 5
  • 20
  • 50

1 Answers1

0

Have you considered using a filter ? not sure if this will be a good design, but you can get the values in the doFilter method using request.getRemoteHost() request.getRemoteAddr() and do your validation edit: forgot about client behind proxy.. this Getting IP address of client link might help

Community
  • 1
  • 1
Balaji Krishnan
  • 1,007
  • 3
  • 12
  • 29
  • Yes, I have tried to do the same with filter. Pls see the update...I am unable to redirect to the resource url. Some where in web.xml and getDispatcher(url) probelms exists. Please help. – Kumar Sep 30 '13 at 09:08
  • what are you trying to achieve by using the requestDispatcher inside the filter ? – Balaji Krishnan Sep 30 '13 at 12:20
  • ah.... if your application name is IntellixWebApi (based on web.xml url pattern), then you have to change the configuration. ie.. even for the new URL that you are forwarding, you filter will intercept the request. The new URL to which you are trying to dispatch also falls into the url pattern mapped for the filter. – Balaji Krishnan Sep 30 '13 at 12:24