org.clazzes.util.http
Class ReverseProxyServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by org.clazzes.util.http.ReverseProxyServlet
All Implemented Interfaces:
Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class ReverseProxyServlet
extends javax.servlet.GenericServlet

A servlet, which acts as a reverse proxy.

The following example shows how to use the servlet inside a web.xml configuration:

     <servlet>
       <servlet-name>proxy-gwt-jaas-login-service</servlet-name>
       <servlet-class>org.clazzes.util.http.ReverseProxyServlet</servlet-class>
       <init-param>
         <param-name>targetUrl</param-name>
         <param-value>http://127.0.0.1:8081/gwt-jaas-login-service</param-value>
       </init-param>
     </servlet>
     <servlet>
       <servlet-name>proxy-gwt-jaas-login-service-star</servlet-name>
       <servlet-class>org.clazzes.util.http.ReverseProxyServlet</servlet-class>
       <init-param>
         <param-name>targetUrl</param-name>
         <param-value>http://127.0.0.1:8081/gwt-jaas-login-service/</param-value>
       </init-param>
     </servlet>

     <servlet-mapping>
       <servlet-name>proxy-gwt-jaas-login-service</servlet-name>
       <url-pattern>/gwt-jaas-login-service</url-pattern>
     </servlet-mapping>
     <servlet-mapping>
       <servlet-name>proxy-gwt-jaas-login-service-star</servlet-name>
       <url-pattern>/gwt-jaas-login-service/*</url-pattern>
     </servlet-mapping>
 

Inside OGSi-containers the servlet might be configured using property tags for the setters setConnectTimeout(int), setReadTimeout(int), setTargetUrl(URL), setRewriteLocations(boolean) and setStreamHandlerFactory(URLStreamHandlerFactory). The only mandatory property is the target URL.

See Also:
Serialized Form

Constructor Summary
ReverseProxyServlet()
           
 
Method Summary
 void destroy()
           
 int getConnectTimeout()
           
 int getReadTimeout()
           
 String getServletInfo()
           
 URLStreamHandlerFactory getStreamHandlerFactory()
           
 URL getTargetUrl()
           
 void init()
           
 boolean isRewriteLocations()
           
protected  HttpURLConnection pullConnection(Long id)
           
protected  Long pushConnection(HttpURLConnection conn)
           
 void service(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res)
           
 void setConnectTimeout(int connectTimeout)
           
 void setReadTimeout(int readTimeout)
           
 void setRewriteLocations(boolean rewriteLocations)
           
 void setStreamHandlerFactory(URLStreamHandlerFactory streamHandlerFactory)
           
 void setTargetUrl(URL targetUrl)
           
 
Methods inherited from class javax.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReverseProxyServlet

public ReverseProxyServlet()
Method Detail

pushConnection

protected Long pushConnection(HttpURLConnection conn)

pullConnection

protected HttpURLConnection pullConnection(Long id)

init

public void init()
          throws javax.servlet.ServletException
Overrides:
init in class javax.servlet.GenericServlet
Throws:
javax.servlet.ServletException

service

public void service(javax.servlet.ServletRequest req,
                    javax.servlet.ServletResponse res)
             throws javax.servlet.ServletException,
                    IOException
Specified by:
service in interface javax.servlet.Servlet
Specified by:
service in class javax.servlet.GenericServlet
Throws:
javax.servlet.ServletException
IOException

getServletInfo

public String getServletInfo()
Specified by:
getServletInfo in interface javax.servlet.Servlet
Overrides:
getServletInfo in class javax.servlet.GenericServlet

destroy

public void destroy()
Specified by:
destroy in interface javax.servlet.Servlet
Overrides:
destroy in class javax.servlet.GenericServlet

getTargetUrl

public URL getTargetUrl()
Returns:
The target URL under which all requests to the base URL of this servlet are mapped.

setTargetUrl

public void setTargetUrl(URL targetUrl)
Parameters:
targetUrl - The target URL under which all requests to the base URL of this servlet are mapped to set.

getReadTimeout

public int getReadTimeout()
Returns:
The read timeout for proxy connections in milliseconds.

setReadTimeout

public void setReadTimeout(int readTimeout)
Parameters:
readTimeout - The read timeout for proxy connections in milliseconds to set. The default value is 60000.

getConnectTimeout

public int getConnectTimeout()
Returns:
The read timeout for proxy connections in milliseconds.

setConnectTimeout

public void setConnectTimeout(int connectTimeout)
Parameters:
connectTimeout - The connect timeout for proxy connections in milliseconds to set. The default value is 5000.

getStreamHandlerFactory

public URLStreamHandlerFactory getStreamHandlerFactory()
Returns:
A custom stream handler factory or null, if the built-in factory should be used.

setStreamHandlerFactory

public void setStreamHandlerFactory(URLStreamHandlerFactory streamHandlerFactory)
Parameters:
streamHandlerFactory - A custom stream handler factory to set or null, if the built-in factory should be used.

isRewriteLocations

public boolean isRewriteLocations()
Returns:
Whether to rewrite Location headers returned from upstream, which are a sub-URL of getTargetUrl().

setRewriteLocations

public void setRewriteLocations(boolean rewriteLocations)
Parameters:
rewriteLocations - Set, Whether Location-rewriting is active.


Copyright © 2013. All Rights Reserved.