examples.news
Class NewsFormatter

java.lang.Object
  extended by examples.news.NewsFormatter
All Implemented Interfaces:
ResponseFormatter

public class NewsFormatter
extends Object
implements ResponseFormatter

This is a simple example of how you can implement your own ResponseFormatter to suit your own needs. This class implements the ResponseFormatter interface and requires an extra URL parameter called 'wires'. It has a predefined set of possible values for wires and a default should the user specify an unrecognised wire.
The possible values for wires are:

Each type of wire has its own response. The response is comprised of a user name and a message about the current news.

Author:
David Shorten

Field Summary
static String DEFAULT_WIRE_TYPE
           Specifies the default value of 'wires' if the one supplied was invalid.
static String[] POSSIBLE_WIRE_TYPES
           Specifies the different types of 'wires' that are valid.
static String[] POSSIBLE_WIRE_VALUES
          Specifies the relevant response to each valid value for 'wires'.
 
Constructor Summary
NewsFormatter()
           
 
Method Summary
 String formatErrorResponse(javax.servlet.http.HttpServletRequest request, String errorMessage)
           This method records in the log than an error response is being returned and what the error was.
 String formatResponse(javax.servlet.http.HttpServletRequest request, String token)
           Implementation of the interface method.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_WIRE_TYPE

public static final String DEFAULT_WIRE_TYPE

Specifies the default value of 'wires' if the one supplied was invalid.

See Also:
Constant Field Values

POSSIBLE_WIRE_TYPES

public static final String[] POSSIBLE_WIRE_TYPES

Specifies the different types of 'wires' that are valid.


POSSIBLE_WIRE_VALUES

public static final String[] POSSIBLE_WIRE_VALUES

Specifies the relevant response to each valid value for 'wires'.

Constructor Detail

NewsFormatter

public NewsFormatter()
Method Detail

formatResponse

public String formatResponse(javax.servlet.http.HttpServletRequest request,
                             String token)

Implementation of the interface method. The method initialises a Map to hold all the different types of wire and each corresponding message. This allows quick retrieval of the message as the parameter is used as the key. if the key ("wires") returns null then the default message is returned, otherwise the corresponding message is returned.

Specified by:
formatResponse in interface ResponseFormatter
Parameters:
request - the HttpServletRequest object that contains the URL and parameters etc.
token - the generated token for this request.
Returns:
a string containing a message and the token or an error message.

formatErrorResponse

public String formatErrorResponse(javax.servlet.http.HttpServletRequest request,
                                  String errorMessage)

This method records in the log than an error response is being returned and what the error was. It then returns the errorMessage.

Specified by:
formatErrorResponse in interface ResponseFormatter
Parameters:
request - A request object that was used to call the calling servlet.
errorMessage - A description of what has gone wrong.
Returns:
the error message that has been passed in.


Please send bug reports and comments to Caplin support