ix.util.http
Class HttpObjectServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by ix.util.http.HttpObjectServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
Direct Known Subclasses:
HttpStringServlet, IServeCommServer.NextMessageServlet, IServeCommServer.RegistrationServlet, IServeCommServer.SendServlet

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

Handles HTTP requests that can be regarded as sending an object and receiving one in reply. The usual I-X XML encoding is used; however, it's possible to change that in a subclass.

An HttpObjectServelt is like an HttpObjectClient but on the server side: where a client sends an object as XML, and receives an object as XML in reply, the servlet receives an object as XML, and sends one back.

At present, only POST requests are supported.

See Also:
HttpServer, XMLTranslator, Serialized Form

Field Summary
protected  java.lang.String responseCharsetName
           
protected  java.lang.String responseContentType
           
protected  HttpUtilities util
           
 
Constructor Summary
HttpObjectServlet()
           
 
Method Summary
protected  java.lang.Object decodeReceived(java.lang.String contents)
          Turns request contents into an object.
protected  void describeRequest(javax.servlet.http.HttpServletRequest req)
          Utility method that produces debugging output that describes the request.
protected  void doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
          Handle an HTTP POST request.
protected  byte[] encodeForSend(java.lang.Object contents)
          Turns an object into response contents.
protected abstract  java.lang.Object handleRequest(javax.servlet.http.HttpServletRequest req, java.lang.Object received)
          The main part of handling the request.
static void main(java.lang.String[] argv)
          Main program for testing.
protected  java.lang.String readRequest(javax.servlet.http.HttpServletRequest req)
          Utility method that reads the request's contents and returns it as a string.
protected  void sendResponse(byte[] bytes, javax.servlet.http.HttpServletResponse resp)
          Utility method that writes the response contents.
 void setResponseCharsetName(java.lang.String name)
          Set the character set used when sending a response.
 void setResponseContentType(java.lang.String type)
          Set the Content-Type used when sending a response.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

util

protected HttpUtilities util

responseContentType

protected java.lang.String responseContentType

responseCharsetName

protected java.lang.String responseCharsetName
Constructor Detail

HttpObjectServlet

public HttpObjectServlet()
Method Detail

setResponseContentType

public void setResponseContentType(java.lang.String type)
Set the Content-Type used when sending a response.


setResponseCharsetName

public void setResponseCharsetName(java.lang.String name)
Set the character set used when sending a response.


doPost

protected void doPost(javax.servlet.http.HttpServletRequest req,
                      javax.servlet.http.HttpServletResponse resp)
               throws javax.servlet.ServletException,
                      java.io.IOException
Handle an HTTP POST request.

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

describeRequest

protected void describeRequest(javax.servlet.http.HttpServletRequest req)
Utility method that produces debugging output that describes the request.


readRequest

protected java.lang.String readRequest(javax.servlet.http.HttpServletRequest req)
                                throws HttpRequestException,
                                       java.io.IOException
Utility method that reads the request's contents and returns it as a string.

Throws:
HttpRequestException
java.io.IOException

decodeReceived

protected java.lang.Object decodeReceived(java.lang.String contents)
Turns request contents into an object. For example, the content might be an XML representation of an object.

See Also:
encodeForSend(Object contents)

handleRequest

protected abstract java.lang.Object handleRequest(javax.servlet.http.HttpServletRequest req,
                                                  java.lang.Object received)
                                           throws HttpRequestException
The main part of handling the request. The contents have already been read and turned into an object by calling decodeReceived(String). The HttpServletRequest is passed primarily to allow this method to look at the request headers.

Returns:
the object to encode and send as a response.
Throws:
HttpRequestException - to indicate that an error response should be sent. The status value and message will be taken from the exception.

encodeForSend

protected byte[] encodeForSend(java.lang.Object contents)
                        throws java.io.UnsupportedEncodingException
Turns an object into response contents.

Throws:
java.io.UnsupportedEncodingException
See Also:
decodeReceived(String contents)

sendResponse

protected void sendResponse(byte[] bytes,
                            javax.servlet.http.HttpServletResponse resp)
                     throws java.io.IOException
Utility method that writes the response contents.

Throws:
java.io.IOException

main

public static void main(java.lang.String[] argv)
Main program for testing. It creates an HttpServer and adds an HttpObjectServlet (subclass) for path "/take"; the servlet acts as a SafeLispInterpreter for the objects it's sent. Each object that's received is treated as an expression and evaluated; the result is sent back as the reply. The http-server-port parameter can be used to say what port the server should use.