uk.ac.ic.doc.automed.p2p.util
Class Util

java.lang.Object
  extended by uk.ac.ic.doc.automed.p2p.util.Util

public class Util
extends java.lang.Object

Author:
Charis, Duc M Le

Constructor Summary
Util()
           
 
Method Summary
static byte[] assemblePackets(java.util.Vector fragments, int maxSize)
          Assemble UDP fragments together
static AbstractMessage getAdvertiseRequest(java.lang.String sender, java.lang.String receiver, java.lang.String context, java.util.Map content)
           
static AbstractMessage getAskRequest(java.lang.String sender, java.lang.String receiver, java.lang.String context, java.util.Map content)
           
static byte[] getBytesFromInputStream(java.io.InputStream is, int bufferSize)
          Get bytes array from an input stream
static AbstractMessage getEndFragmentMessage()
           
static java.net.InetAddress getIPAddress(java.lang.String hostName)
          A static utility method to perform reverse name lookup
static AbstractMessage getLoggingMessage(java.lang.String logStr, java.lang.String sender, java.lang.String receiver)
           
static AbstractMessage getMessage(java.lang.String performative, java.lang.String sender, java.lang.String receiver, java.lang.String context, java.util.Map content)
           
static byte[] getMessageBytes(AbstractMessage mesg)
          An overloading method of getMessageBytes(String performative, String context, Map content)
to return an array of bytes for an AbstractMessage object
static byte[] getMessageBytes(java.lang.String performative, java.lang.String context, java.util.Map content, java.lang.String sender, java.lang.String receiver)
          An utility method to get byte-representation of an XML message
This method is used in UDP communication
static AbstractMessage getMessageFromBytes(byte[] dataBytes)
          An utility method to construct an AbstractMessage from an array of
bytes This method is used in UDP communication
static AbstractMessage getMessageFromInput(java.io.InputStream inputStream)
           
static java.lang.Object getNotNullObject(java.util.Map content, java.lang.String key)
           
static java.lang.String getNotNullString(java.util.Map content, java.lang.String key)
          Retrieve a mandatory field from a message content
static java.lang.String getOptionalString(java.util.Map content, java.lang.String key)
          Retrieve an optional field from a message content
static AbstractMessage getPathwayRequest(java.lang.String fromSchema, java.lang.String toSchema, java.lang.String sender, java.lang.String receiver)
           
static AbstractMessage getPathwayToSourceAbstractRequest(java.lang.String schema, java.lang.String sender, java.lang.String receiver)
           
static AbstractMessage getPathwayToSourceRequest(java.lang.String schema, java.lang.String sender, java.lang.String receiver)
           
static AbstractMessage getPortRequest(java.lang.String peerName)
           
static AbstractMessage getQueryRequest(java.lang.String schema, java.lang.String query, java.lang.String sender, java.lang.String receiver)
           
static AbstractMessage getQueryResultMessage(AbstractMessage request, java.util.Map content)
           
static AbstractMessage getRecommendOneRequest(java.lang.String sender, java.lang.String receiver, java.lang.String context, java.util.Map content)
           
static AbstractMessage getRegisterRequest(java.lang.String name, int port)
           
static AbstractMessage getSchemaRequest(java.lang.String schema, java.lang.String sender, java.lang.String receiver)
           
static AbstractMessage getUnregisterRequest(java.lang.String name)
          A message for peer unregistering from the local registry
static AbstractMessage getUnsuccessfulMessage(AbstractMessage message, java.lang.String errorMsg, java.lang.String performative)
           
static java.lang.String getXMLDocumentString(org.w3c.dom.Document doc)
          Convert an XML document structure in to a printable string
static boolean isEndFragment(byte[] dataReceived)
          Check if a trunk of bytes form the end-of-fragmentation message.
static java.lang.String newID()
           
static void outputConfirm(java.lang.String reqId, java.util.Map content, java.io.OutputStream os)
          An overloading method of #outputConfirm() to output a confirmation message with optional content data
static void outputConfirm(java.lang.String reqId, java.util.Map content, ResponseHandler output)
           
static void outputConfirm(java.lang.String reqId, java.io.OutputStream os)
          Only works with TCP protocol
static void outputConfirm(java.lang.String reqId, ResponseHandler output)
          Serialise output through a handler object rather than through direct manipulation
of physical output streams
static void outputErrorMessage(AbstractMessage request, java.io.OutputStream os)
          Only works with TCP protocol
static void outputErrorMessage(AbstractMessage request, ResponseHandler output)
          Serialise output through a handler object rather than through direct manipulation
of physical output streams
static void outputErrorMessage(AbstractMessage request, java.lang.String message, java.io.OutputStream os)
          Only works with TCP protocol
static void outputErrorMessage(AbstractMessage request, java.lang.String message, ResponseHandler output)
          Serialise output through a handler object rather than through direct manipulation
of physical output streams
static void outputFailMessage(AbstractMessage request, java.io.OutputStream os)
          Only works with TCP protocol
static void outputFailMessage(AbstractMessage request, ResponseHandler output)
          Serialise output through a handler object rather than through direct manipulation
of physical output streams
static void outputFailMessage(AbstractMessage request, java.lang.String message, java.io.OutputStream os)
          Only works with TCP protocol
static void outputFailMessage(AbstractMessage request, java.lang.String message, ResponseHandler output)
          Serialise output through a handler object rather than through direct manipulation
of physical output streams
static void outputUnsuccessfulMessage(AbstractMessage request, java.lang.String message, java.lang.String performative, java.io.OutputStream os)
          Deprecated. use outputUnsuccessfulMessage(AbstractMessage request, String message, String performative, ResponseHandler output) instead
static void outputUnsuccessfulMessage(AbstractMessage request, java.lang.String message, java.lang.String performative, ResponseHandler output)
          Serialise output through a handler object rather than through direct manipulation
of physical output streams
static java.lang.Object[] parseFile(java.lang.String fileName)
           
static java.lang.String toUnicodeString(java.lang.String original)
          Get the Unicode representation of a String
static java.io.File writeInFile(AbstractMessage message)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Util

public Util()
Method Detail

outputErrorMessage

public static void outputErrorMessage(AbstractMessage request,
                                      java.io.OutputStream os)
                               throws java.io.IOException
Only works with TCP protocol

Throws:
java.io.IOException

outputErrorMessage

public static void outputErrorMessage(AbstractMessage request,
                                      ResponseHandler output)
                               throws CommunicationException
Serialise output through a handler object rather than through direct manipulation
of physical output streams

Throws:
CommunicationException

writeInFile

public static java.io.File writeInFile(AbstractMessage message)

outputErrorMessage

public static void outputErrorMessage(AbstractMessage request,
                                      java.lang.String message,
                                      java.io.OutputStream os)
                               throws java.io.IOException
Only works with TCP protocol

Throws:
java.io.IOException

outputErrorMessage

public static void outputErrorMessage(AbstractMessage request,
                                      java.lang.String message,
                                      ResponseHandler output)
                               throws CommunicationException
Serialise output through a handler object rather than through direct manipulation
of physical output streams

Throws:
CommunicationException

outputFailMessage

public static void outputFailMessage(AbstractMessage request,
                                     java.lang.String message,
                                     java.io.OutputStream os)
                              throws java.io.IOException
Only works with TCP protocol

Throws:
java.io.IOException

outputFailMessage

public static void outputFailMessage(AbstractMessage request,
                                     java.io.OutputStream os)
                              throws java.io.IOException
Only works with TCP protocol

Throws:
java.io.IOException

outputFailMessage

public static void outputFailMessage(AbstractMessage request,
                                     java.lang.String message,
                                     ResponseHandler output)
                              throws CommunicationException
Serialise output through a handler object rather than through direct manipulation
of physical output streams

Throws:
CommunicationException

outputFailMessage

public static void outputFailMessage(AbstractMessage request,
                                     ResponseHandler output)
                              throws CommunicationException
Serialise output through a handler object rather than through direct manipulation
of physical output streams

Throws:
CommunicationException

outputUnsuccessfulMessage

public static void outputUnsuccessfulMessage(AbstractMessage request,
                                             java.lang.String message,
                                             java.lang.String performative,
                                             java.io.OutputStream os)
                                      throws java.io.IOException
Deprecated. use outputUnsuccessfulMessage(AbstractMessage request, String message, String performative, ResponseHandler output) instead

Throws:
java.io.IOException

outputUnsuccessfulMessage

public static void outputUnsuccessfulMessage(AbstractMessage request,
                                             java.lang.String message,
                                             java.lang.String performative,
                                             ResponseHandler output)
                                      throws CommunicationException
Serialise output through a handler object rather than through direct manipulation
of physical output streams

Throws:
CommunicationException

outputConfirm

public static void outputConfirm(java.lang.String reqId,
                                 java.io.OutputStream os)
                          throws java.io.IOException
Only works with TCP protocol

Throws:
java.io.IOException

outputConfirm

public static void outputConfirm(java.lang.String reqId,
                                 ResponseHandler output)
                          throws CommunicationException
Serialise output through a handler object rather than through direct manipulation
of physical output streams

Throws:
CommunicationException

outputConfirm

public static void outputConfirm(java.lang.String reqId,
                                 java.util.Map content,
                                 java.io.OutputStream os)
                          throws java.io.IOException
An overloading method of #outputConfirm() to output a confirmation message with optional content data

Parameters:
reqId - request id to use for reply
content - a Map object for the optional content
output - the OutputStream object for outputing result
Throws:
java.io.IOException

outputConfirm

public static void outputConfirm(java.lang.String reqId,
                                 java.util.Map content,
                                 ResponseHandler output)
                          throws CommunicationException
Throws:
CommunicationException

getMessageFromInput

public static AbstractMessage getMessageFromInput(java.io.InputStream inputStream)

getRegisterRequest

public static AbstractMessage getRegisterRequest(java.lang.String name,
                                                 int port)

getUnregisterRequest

public static AbstractMessage getUnregisterRequest(java.lang.String name)
A message for peer unregistering from the local registry


getPortRequest

public static AbstractMessage getPortRequest(java.lang.String peerName)

getAdvertiseRequest

public static AbstractMessage getAdvertiseRequest(java.lang.String sender,
                                                  java.lang.String receiver,
                                                  java.lang.String context,
                                                  java.util.Map content)

getPathwayToSourceRequest

public static AbstractMessage getPathwayToSourceRequest(java.lang.String schema,
                                                        java.lang.String sender,
                                                        java.lang.String receiver)

getPathwayToSourceAbstractRequest

public static AbstractMessage getPathwayToSourceAbstractRequest(java.lang.String schema,
                                                                java.lang.String sender,
                                                                java.lang.String receiver)

getPathwayRequest

public static AbstractMessage getPathwayRequest(java.lang.String fromSchema,
                                                java.lang.String toSchema,
                                                java.lang.String sender,
                                                java.lang.String receiver)

getQueryRequest

public static AbstractMessage getQueryRequest(java.lang.String schema,
                                              java.lang.String query,
                                              java.lang.String sender,
                                              java.lang.String receiver)

getQueryResultMessage

public static AbstractMessage getQueryResultMessage(AbstractMessage request,
                                                    java.util.Map content)

getSchemaRequest

public static AbstractMessage getSchemaRequest(java.lang.String schema,
                                               java.lang.String sender,
                                               java.lang.String receiver)

getAskRequest

public static AbstractMessage getAskRequest(java.lang.String sender,
                                            java.lang.String receiver,
                                            java.lang.String context,
                                            java.util.Map content)

getRecommendOneRequest

public static AbstractMessage getRecommendOneRequest(java.lang.String sender,
                                                     java.lang.String receiver,
                                                     java.lang.String context,
                                                     java.util.Map content)

getMessage

public static AbstractMessage getMessage(java.lang.String performative,
                                         java.lang.String sender,
                                         java.lang.String receiver,
                                         java.lang.String context,
                                         java.util.Map content)

newID

public static java.lang.String newID()

getNotNullObject

public static java.lang.Object getNotNullObject(java.util.Map content,
                                                java.lang.String key)
                                         throws MalformedMessageException
Throws:
MalformedMessageException

getNotNullString

public static java.lang.String getNotNullString(java.util.Map content,
                                                java.lang.String key)
                                         throws MalformedMessageException
Retrieve a mandatory field from a message content

Throws:
MalformedMessageException

getOptionalString

public static java.lang.String getOptionalString(java.util.Map content,
                                                 java.lang.String key)
Retrieve an optional field from a message content


getUnsuccessfulMessage

public static AbstractMessage getUnsuccessfulMessage(AbstractMessage message,
                                                     java.lang.String errorMsg,
                                                     java.lang.String performative)

getLoggingMessage

public static AbstractMessage getLoggingMessage(java.lang.String logStr,
                                                java.lang.String sender,
                                                java.lang.String receiver)

parseFile

public static java.lang.Object[] parseFile(java.lang.String fileName)

getXMLDocumentString

public static java.lang.String getXMLDocumentString(org.w3c.dom.Document doc)
Convert an XML document structure in to a printable string

Parameters:
doc - an XML document

getMessageBytes

public static byte[] getMessageBytes(java.lang.String performative,
                                     java.lang.String context,
                                     java.util.Map content,
                                     java.lang.String sender,
                                     java.lang.String receiver)
An utility method to get byte-representation of an XML message
This method is used in UDP communication

Parameters:
performative - the message's performative
context - the message's context
content - a Map object of content fields

getMessageBytes

public static byte[] getMessageBytes(AbstractMessage mesg)
An overloading method of getMessageBytes(String performative, String context, Map content)
to return an array of bytes for an AbstractMessage object

Parameters:
mesg - AbstractMessage object to work with

getMessageFromBytes

public static AbstractMessage getMessageFromBytes(byte[] dataBytes)
                                           throws MalformedMessageException
An utility method to construct an AbstractMessage from an array of
bytes This method is used in UDP communication

Parameters:
performative - the message's performative
context - the message's context
content - a Map object of content fields
Throws:
MalformedMessageException

getBytesFromInputStream

public static byte[] getBytesFromInputStream(java.io.InputStream is,
                                             int bufferSize)
                                      throws java.io.IOException
Get bytes array from an input stream

Throws:
java.io.IOException

getEndFragmentMessage

public static AbstractMessage getEndFragmentMessage()
                                             throws MalformedMessageException
Throws:
MalformedMessageException

isEndFragment

public static boolean isEndFragment(byte[] dataReceived)
Check if a trunk of bytes form the end-of-fragmentation message. This is a special
message used for UDP communication.


assemblePackets

public static byte[] assemblePackets(java.util.Vector fragments,
                                     int maxSize)
Assemble UDP fragments together


toUnicodeString

public static java.lang.String toUnicodeString(java.lang.String original)
Get the Unicode representation of a String


getIPAddress

public static java.net.InetAddress getIPAddress(java.lang.String hostName)
                                         throws CommunicationException
A static utility method to perform reverse name lookup

Throws:
CommunicationException