Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward a servlet request to another server

Java Servlet API can forward requests to another path within the same server (identical host:port). But, forwarding to a different host:port — like proxy do — is another story.

I've tried to do that with Jersey Client, adapting the ServletRequest — method, headers, mediatype and body — to a Jersey ClientRequest (with a different base uri), making the call, and adapting back the Jersey ClientResponse — method, headers, mediatype and body — to the ServletResponse.

Adapting those manually seems wrong to me.

Isn't there a pure Servlet API solution? Or an HTTP client capable of adapting requests back and forth when changing the host:port?

like image 477
yves amsellem Avatar asked Jun 02 '12 07:06

yves amsellem


People also ask

How do I forward a HTTP request to another server in Java?

Redirecting the Request To do this, we use the sendRedirect method belonging to the HttpServletResponse interface: response. sendRedirect("https://www.google.com"); This is useful when we want to send the user to a different domain or server outside of our web application.

How can we transfer data from one servlet to another?

The forward() method is used to transfer the client request to another resource (HTML file, servlet, jsp etc). When this method is called, the control is transferred to the next resource called.

What is the difference between sendRedirect () and forward ()?

The main important difference between the forward() and sendRedirect() method is that in case of forward(), redirect happens at server end and not visible to client, but in case of sendRedirect(), redirection happens at client end and it's visible to client.


1 Answers

HTTP-Proxy-Servlet does exactly what you need.

Quick configuration

pom.xml

<dependency>
    <groupId>org.mitre.dsmiley.httpproxy</groupId>
    <artifactId>smiley-http-proxy-servlet</artifactId>
    <version>1.7</version>
</dependency>

web.xml

<servlet>
    <servlet-name>solr</servlet-name>
    <servlet-class>org.mitre.dsmiley.httpproxy.ProxyServlet</servlet-class>
    <init-param>
        <param-name>targetUri</param-name>
        <param-value>http://solrserver:8983/solr</param-value>
    </init-param>
    <init-param>
        <param-name>log</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>solr</servlet-name>
    <url-pattern>/solr/*</url-pattern>
</servlet-mapping>

Spring Integration

see also: HTTP-Proxy-Servlet Issue #15

pom.xml

<dependency>
    <groupId>org.mitre.dsmiley.httpproxy</groupId>
    <artifactId>smiley-http-proxy-servlet</artifactId>
    <version>1.7</version>
</dependency>

ServletWrappingControllerExt.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.ServletWrappingController;

public class ServletWrappingControllerExt extends ServletWrappingController
{
    private String  pathToStrip;

    public void setPathToStrip(String pathToStrip)
    {
        this.pathToStrip = pathToStrip;
    }

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        final HttpServletRequest wrapper = new HttpServletRequestWrapper(request)
        {
            @Override
            public String getPathInfo()
            {
                //Please note that getPathInfo returns null if DispatcherServlet is configured to track url-pattern "/"
                //It should be configured to track url-pattern "/*". Below is a sample DispatcherServlet configuration
                /*
                    <servlet>
                        <servlet-name>spring</servlet-name>
                        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                        <load-on-startup>1</load-on-startup>
                    </servlet>
                    <servlet-mapping>
                        <servlet-name>spring</servlet-name>
                        <url-pattern>/*</url-pattern>
                    </servlet-mapping>
                 */
                String path = super.getPathInfo();                  
                if (path.startsWith(pathToStrip))
                {
                    final int length = pathToStrip.length();
                    path = path.substring(length);
                }
                return path;
            }
            
            @Override
            public String getServletPath()
            {
                return super.getServletPath();
            }
        };

        return super.handleRequestInternal(wrapper, response);
    }
}

Beans configuration

<bean id="myServletWrapper" class="ServletWrappingControllerExt">
    <property name="pathToStrip" value="/solr"/>
    <property name="servletClass" value="org.mitre.dsmiley.httpproxy.ProxyServlet" />
    <property name="servletName" value="solr" />
    <property name="initParameters">
        <props>
            <prop key="targetUri">http://solrserver:8983/solr</prop>
            <prop key="log">true</prop>
        </props>
    </property>
</bean>

<bean id="myServletUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">
    <map>
        <entry key="/solr/**" value-ref="myServletWrapper" />
    </map>
    </property>
    <property name="order" value="1" />
</bean>
like image 150
vahapt Avatar answered Oct 17 '22 12:10

vahapt