http - Forward a servlet request to another server -
java servlet api can forward requests path within same server (identical host:port). but, forwarding different host:port — proxy — story.
i've tried jersey client, adapting servletrequest — method, headers, mediatype , body — jersey clientrequest (with different base uri), making call, , adapting jersey clientresponse — method, headers, mediatype , body — servletresponse.
adapting manually seems wrong me.
isn't there pure servlet api solution? or http client capable of adapting requests , forth when changing host:port?
http-proxy-servlet 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 getpathinfo returns null if dispatcherservlet configured track url-pattern "/" //it should configured track url-pattern "/*". below 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>
Comments
Post a Comment