com.maryanovsky.gwtutils.client.net
Class XmlResponseHandler<T>

java.lang.Object
  extended by com.maryanovsky.gwtutils.client.net.AbstractResponseHandler
      extended by com.maryanovsky.gwtutils.client.net.XmlResponseHandler<T>
All Implemented Interfaces:
RequestCallback
Direct Known Subclasses:
Capabilities.ResponseHandler

public class XmlResponseHandler<T>
extends AbstractResponseHandler

A base implementation of a RequestCallback which handles XML responses. An XmlResponseHandler can be either parameterized by a parser and a callback to parse and handle the response XML document or by subclassing and overriding the appropriate method. See the constructor(s) for more information.

Author:
Maryanovsky Alexander

Field Summary
 
Fields inherited from class com.maryanovsky.gwtutils.client.net.AbstractResponseHandler
requestUrl
 
Constructor Summary
XmlResponseHandler(String requestUrl, String successfulResponseTagName, String errorResponseTagName, XmlParser<T> parser, ResultCallback<T> callback)
          Creates a new XmlResponseHandler with the specified arguments.
 
Method Summary
protected  String getRequestUrl()
          Returns the request URL.
protected  void onAnyResponse()
          Invoked whenever any kind of response arrives, before any of the other methods.
protected  void onBadlyFormedXml(Response response, DOMException e)
          Invoked when the result could not be parsed as an XML document.
protected  void onBadResponse(Document document, ParseException e)
          Invoked when the received response cannot be parsed by parseResponse(Document).
protected  void onErrorXmlResponse(Document document)
          Invoked when an error response arrives.
protected  void onResult(T result)
          Invoked when a response successfully parsed by parseResponse(Document) arrives.
protected  void onSuccessfulResponse(Response response)
          Invoked when a successful response arrives.
protected  void onSuccessfulXmlResponse(Document document)
          Invoked when a successful XML response arrives.
protected  void onUnrecognizedResponse(Document document)
          Invoked when an unknown response arrives (a document whose main tag name is not recognized).
protected  T parseResponse(Document document)
          Parses the specified response and returns the resulting object.
protected  void showErrorMessage(String errorMessage)
          Displays the specified error message.
 
Methods inherited from class com.maryanovsky.gwtutils.client.net.AbstractResponseHandler
onError, onRequestError, onResponseReceived, onUnexpectedStatusCode
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlResponseHandler

public XmlResponseHandler(String requestUrl,
                          String successfulResponseTagName,
                          String errorResponseTagName,
                          XmlParser<T> parser,
                          ResultCallback<T> callback)
Creates a new XmlResponseHandler with the specified arguments. If parser is not null, the default implementation of parseResponse(Document) will use it to parse the received XML document; otherwise, it will return the document itself as the result. This means that T must be Document if parser is null and parseResponse(Document) is not overridden. If callback is not null, onAnyResponse() and onResult(Object) will invoke the appropriate method on it.

Parameters:
requestUrl - The URL of the request.
successfulResponseTagName - The name of the document's main tag in case of a successful response.
errorResponseTagName - The name of the document's main tag in case of an error response.
parser - The parser we use to parse the response XML document; may be null.
callback - The callback to notify when a response/result arrives; may be null.
Method Detail

getRequestUrl

protected String getRequestUrl()
Returns the request URL.


onSuccessfulResponse

protected void onSuccessfulResponse(Response response)
Invoked when a successful response arrives. Parses the response as an XML document and passes it on to either onSuccessfulXmlResponse(Document), onErrorXmlResponse(Document) or onUnrecognizedResponse(Document) depending on the root tag name of the response document. If the response could not be parsed as an XML document, invoked onBadlyFormedXml(Response, DOMException).

Specified by:
onSuccessfulResponse in class AbstractResponseHandler

onBadlyFormedXml

protected void onBadlyFormedXml(Response response,
                                DOMException e)
Invoked when the result could not be parsed as an XML document. The default implementation shows an error message.


parseResponse

protected T parseResponse(Document document)
                   throws ParseException
Parses the specified response and returns the resulting object. If the response is invalid, throws a ParseException. The default implementation returns uses the parser passed to the constructor to parse the response. If the parser is null, returns the document itself.

Throws:
ParseException

onSuccessfulXmlResponse

protected void onSuccessfulXmlResponse(Document document)
Invoked when a successful XML response arrives. The default implementation attempts to parse it and invoke onResult(Object).


onErrorXmlResponse

protected void onErrorXmlResponse(Document document)
Invoked when an error response arrives. The default implementation displays the text contents of the document's main tag as an error.


onUnrecognizedResponse

protected void onUnrecognizedResponse(Document document)
Invoked when an unknown response arrives (a document whose main tag name is not recognized). The default implementation shows an error message.


onBadResponse

protected void onBadResponse(Document document,
                             ParseException e)
Invoked when the received response cannot be parsed by parseResponse(Document). The default implementation shows an error message.

Parameters:
document - The response document.
e - The exception thrown when parsing the response.

onAnyResponse

protected void onAnyResponse()
Invoked whenever any kind of response arrives, before any of the other methods. The default implementation invokes ResultCallback.requestFinished() on the callback, if any.

Overrides:
onAnyResponse in class AbstractResponseHandler

onResult

protected void onResult(T result)
Invoked when a response successfully parsed by parseResponse(Document) arrives. The default implementation invokes ResultCallback.resultReceived(Object) on the callback, if any.

Parameters:
result - The object returned by parseResponse(Document).

showErrorMessage

protected void showErrorMessage(String errorMessage)
Displays the specified error message. The default implementation shows it in an alert window.