org.faceless.util
Class HttpProxyServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by org.faceless.util.HttpProxyServlet
All Implemented Interfaces:
Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
Direct Known Subclasses:
PDFProxyServlet

public abstract class HttpProxyServlet
extends javax.servlet.http.HttpServlet

A generic HTTP proxy servlet, currently handling the GET method only. Requests are received by this servlet and passed on to a remote URL. The response from that URL may then be processed and passed back to the original client. The responses may optionally be cached internally by this servlet.

Overriders must implement the following methods:

The following headers are added to the proxied request, which may be checked for.

Also, the following headers may be set in the response to control the servlets behaviour

Finally the org.faceless.util.proxytimeout initialization parameter may be set to control the timeout in milliseconds for a proxy request. The default is 30000, or 30 seconds.

See Also:
Serialized Form

Constructor Summary
HttpProxyServlet()
           
 
Method Summary
 void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
 void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Currently the POST method is unimplemented, and returns a 405 error to the browser.
abstract  HttpRequestWriter getRequest(HttpRequestReader reader, javax.servlet.http.HttpServletResponse response)
           Create a request to be passed on by the proxy.
abstract  HttpResponseWriter getResponse(HttpResponseReader response)
           Creates a response for the browser, based on the response received from the proxied request.
 void init()
           
 
Methods inherited from class javax.servlet.http.HttpServlet
service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpProxyServlet

public HttpProxyServlet()
Method Detail

init

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

doPost

public void doPost(javax.servlet.http.HttpServletRequest request,
                   javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException,
                   IOException
Currently the POST method is unimplemented, and returns a 405 error to the browser.

Overrides:
doPost in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
IOException

doGet

public void doGet(javax.servlet.http.HttpServletRequest request,
                  javax.servlet.http.HttpServletResponse response)
           throws javax.servlet.ServletException,
                  IOException
Overrides:
doGet in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
IOException

getResponse

public abstract HttpResponseWriter getResponse(HttpResponseReader response)
                                        throws javax.servlet.ServletException,
                                               IOException

Creates a response for the browser, based on the response received from the proxied request.

Overriders need to implement this method to pass back a response to the client. At it's simplest, this method could do something like

    public HttpResponseWriter getResponse(response)
    {
      return new HttpResponseWriter(response);
    }
 
which would pass the response from the proxied request directly back to the browser.

Parameters:
response - the response from the request specified by getRequest()
Returns:
the response that this proxy should pass on
Throws:
javax.servlet.ServletException
IOException

getRequest

public abstract HttpRequestWriter getRequest(HttpRequestReader reader,
                                             javax.servlet.http.HttpServletResponse response)
                                      throws javax.servlet.ServletException,
                                             IOException

Create a request to be passed on by the proxy.

Overriders need to implement this method to determine what the proxy should request. At the very least this method should probably contain the following stub, or something close to it:

   public HttpRequestWriter getRequest(reader, response)
   {
     HttpRequestWriter writer = null;
     if (isInvalidRequest(reader)) {
        response.sendError(404, "Invalid request to this proxy");
     } else {
        writer = new HttpRequestWriter(reader);
        writer.setURL("/path/to/proxy/request.html");
     }
     return writer;
   }
 

If an error occurs, the method should handle the error message to the response and return null as you see here. If the setURL method isn't called an endless loop will result, so don't forget it.

Parameters:
reader - the incoming request to this proxy
response - the outgoing response. Should only be written to if this method is returning null due to an error.
Returns:
the request that this proxy should pass on.
Throws:
javax.servlet.ServletException
IOException


Copyright © 2001-2004 Big Faceless Organization