org.apache.ws.security.util
Class WSSecurityUtil

java.lang.Object
  extended byorg.apache.ws.security.util.WSSecurityUtil

public class WSSecurityUtil
extends java.lang.Object

WS-Security Utility methods.

Author:
Davanum Srinivas (dims@yahoo.com).

Constructor Summary
WSSecurityUtil()
           
 
Method Summary
static org.w3c.dom.Element appendChildElement(org.w3c.dom.Document doc, org.w3c.dom.Element parent, org.w3c.dom.Element child)
          append a child element

static org.w3c.dom.Text createBase64EncodedTextNode(org.w3c.dom.Document doc, byte[] data)
          create a base64 test node

static org.w3c.dom.Element createBinarySecurityToken(org.w3c.dom.Document doc, java.lang.String wsuIdVal, WSSConfig wssConfig)
          Create a BinarySecurityToken element

static org.w3c.dom.Element createNamespaceContext(WSSConfig wssConfig, org.w3c.dom.Document doc)
          Create a namespace context with namespaces of interest
static WSSecurityEngineResult fetchActionResult(java.util.Vector wsResultVector, int action)
          Fetch the result of a given action from a given result vector

static org.w3c.dom.Element findBodyElement(org.w3c.dom.Document doc, SOAPConstants sc)
          return the first soap "Body" element.
static org.w3c.dom.Node findElement(org.w3c.dom.Node startNode, java.lang.String name, java.lang.String namespace)
          Returns the first element that matches name and namespace.
static org.w3c.dom.Element findElementById(org.w3c.dom.Node startNode, java.lang.String value, java.lang.String namespace)
          Returns the first element that containes an Id with value uri and namespace.
static org.w3c.dom.Element findWsseSecurityHeaderBlock(WSSConfig wssConfig, org.w3c.dom.Document doc, org.w3c.dom.Element envelope, boolean doCreate)
          find the first ws-security header block

static org.w3c.dom.Element findWsseSecurityHeaderBlock(WSSConfig wssConfig, org.w3c.dom.Document doc, org.w3c.dom.Element envelope, java.lang.String actor, boolean doCreate)
          find a ws-security header block for a given actor

static java.lang.String getAttributeValueWSSE(org.w3c.dom.Element element, java.lang.String attrName, java.lang.String wsseNamespace)
          Gets the attribute value with specified localname and WSSE namespace.
static java.lang.String getAttributeValueWSU(org.w3c.dom.Element element, java.lang.String attrName, java.lang.String wsuNamespace)
          Gets the attribute value with specified localname and WSU namespace.
static javax.crypto.Cipher getCipherInstance(java.lang.String cipherAlgo)
           
static org.w3c.dom.Node getDirectChild(org.w3c.dom.Node fNode, java.lang.String localName, java.lang.String namespace)
          Gets a direct child with specified localname and namespace.
static org.w3c.dom.Node getDirectChildWSSE(org.w3c.dom.Node fNode, java.lang.String localName)
          Gets a direct child with specified localname and one of the WSSE namespaces.
static org.w3c.dom.Node getDirectChildWSU(org.w3c.dom.Node fNode, java.lang.String localName)
          Gets a direct child with specified localname and one of the WSU namespaces.
static org.w3c.dom.Element getElementByGenId(org.w3c.dom.Document doc, java.lang.String id)
          Search for an element given its generic id.
static org.w3c.dom.Element getElementByWsuId(WSSConfig wssConfig, org.w3c.dom.Document doc, java.lang.String id)
          Search for an element given its wsu:id.
static javax.xml.namespace.QName getFullQNameFromString(java.lang.String str, org.w3c.dom.Node e)
          Return a QName when passed a string like "foo:bar" by mapping the "foo" prefix to a namespace in the context of the given Node.
static java.lang.String getNamespace(java.lang.String prefix, org.w3c.dom.Node e)
           
static java.lang.String getPrefixNS(java.lang.String uri, org.w3c.dom.Node e)
           
static javax.xml.namespace.QName getQNameFromString(java.lang.String str, org.w3c.dom.Node e)
          Return a QName when passed a string like "foo:bar" by mapping the "foo" prefix to a namespace in the context of the given Node.
static org.w3c.dom.Element getSecurityHeader(WSSConfig wssConfig, org.w3c.dom.Document doc, java.lang.String actor, SOAPConstants sc)
          Returns the first WS-Security header element for a given actor.
static SOAPConstants getSOAPConstants(org.w3c.dom.Element startElement)
           
static java.lang.String getStringForQName(javax.xml.namespace.QName qname, org.w3c.dom.Element e)
          Return a string for a particular QName, mapping a new prefix if necessary.
static boolean isActorEqual(java.lang.String actor, java.lang.String hActor)
          Compares two actor strings and returns true if these are equal.
static javax.crypto.SecretKey prepareSecretKey(java.lang.String symEncAlgo, byte[] rawKey)
           
static org.w3c.dom.Element prependChildElement(org.w3c.dom.Document doc, org.w3c.dom.Element parent, org.w3c.dom.Element child, boolean addWhitespace)
          prepend a child element

static org.w3c.dom.Node selectSingleNode(org.w3c.dom.Node contextNode, java.lang.String xpath, org.w3c.dom.Element nsContext)
          use xpath to find a node

static java.lang.String setNamespace(org.w3c.dom.Element element, java.lang.String namespace, java.lang.String prefix)
          set the namespace if it is not set already.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WSSecurityUtil

public WSSecurityUtil()
Method Detail

getSecurityHeader

public static org.w3c.dom.Element getSecurityHeader(WSSConfig wssConfig,
                                                    org.w3c.dom.Document doc,
                                                    java.lang.String actor,
                                                    SOAPConstants sc)
Returns the first WS-Security header element for a given actor. Only one WS-Security header is allowed for an actor.

Parameters:
doc -
actor -
Returns:
the wsse:Security element or null if not such element found

isActorEqual

public static boolean isActorEqual(java.lang.String actor,
                                   java.lang.String hActor)
Compares two actor strings and returns true if these are equal. Takes care of the null length strings and uses ignore case.

Parameters:
actor -
hActor -
Returns:

getDirectChild

public static org.w3c.dom.Node getDirectChild(org.w3c.dom.Node fNode,
                                              java.lang.String localName,
                                              java.lang.String namespace)
Gets a direct child with specified localname and namespace.

Parameters:
fNode - the node where to start the search
localName - local name of the child to get
namespace - the namespace of the child to get
Returns:
the node or null if not such node found

getDirectChildWSSE

public static org.w3c.dom.Node getDirectChildWSSE(org.w3c.dom.Node fNode,
                                                  java.lang.String localName)
Gets a direct child with specified localname and one of the WSSE namespaces.

Parameters:
fNode - the node where to start the search
localName - local name of the child to get
Returns:
the node or null if not such node found

getDirectChildWSU

public static org.w3c.dom.Node getDirectChildWSU(org.w3c.dom.Node fNode,
                                                 java.lang.String localName)
Gets a direct child with specified localname and one of the WSU namespaces.

Parameters:
fNode - the node where to start the search
localName - local name of the child to get
Returns:
the node or null if not such node found

getAttributeValueWSU

public static java.lang.String getAttributeValueWSU(org.w3c.dom.Element element,
                                                    java.lang.String attrName,
                                                    java.lang.String wsuNamespace)
Gets the attribute value with specified localname and WSU namespace.

Parameters:
element - the Element which contains the attribute
attrName - local name of the attribute
wsuNamespace - the WSU namespace of the attribute to get. Pass null to try all WSU namespaces

getAttributeValueWSSE

public static java.lang.String getAttributeValueWSSE(org.w3c.dom.Element element,
                                                     java.lang.String attrName,
                                                     java.lang.String wsseNamespace)
Gets the attribute value with specified localname and WSSE namespace.

Parameters:
element - the Element which contains the attribute
attrName - local name of the attribute
wsseNamespace - the WSSE namespace of the attribute to get. Pass null to try all WSSE namespaces

findBodyElement

public static org.w3c.dom.Element findBodyElement(org.w3c.dom.Document doc,
                                                  SOAPConstants sc)
return the first soap "Body" element.

Parameters:
doc -
Returns:
the body element or null if document does not contain a SOAP body

findElement

public static org.w3c.dom.Node findElement(org.w3c.dom.Node startNode,
                                           java.lang.String name,
                                           java.lang.String namespace)
Returns the first element that matches name and namespace.

This is a replacement for a XPath lookup //name with the given namespace. It's somewhat faster than XPath, and we do not deal with prefixes, just with the real namespace URI

Parameters:
startNode - Where to start the search
name - Local name of the element
namespace - Namespace URI of the element
Returns:
The found element or null

findElementById

public static org.w3c.dom.Element findElementById(org.w3c.dom.Node startNode,
                                                  java.lang.String value,
                                                  java.lang.String namespace)
Returns the first element that containes an Id with value uri and namespace.

This is a replacement for a XPath Id lookup with the given namespace. It's somewhat faster than XPath, and we do not deal with prefixes, just with the real namespace URI

Parameters:
startNode - Where to start the search
value - Value of the Id attribute
namespace - Namespace URI of the Id
Returns:
The found element or null

setNamespace

public static java.lang.String setNamespace(org.w3c.dom.Element element,
                                            java.lang.String namespace,
                                            java.lang.String prefix)
set the namespace if it is not set already.

Parameters:
element -
namespace -
prefix -
Returns:

getPrefixNS

public static java.lang.String getPrefixNS(java.lang.String uri,
                                           org.w3c.dom.Node e)

getNamespace

public static java.lang.String getNamespace(java.lang.String prefix,
                                            org.w3c.dom.Node e)

getQNameFromString

public static javax.xml.namespace.QName getQNameFromString(java.lang.String str,
                                                           org.w3c.dom.Node e)
Return a QName when passed a string like "foo:bar" by mapping the "foo" prefix to a namespace in the context of the given Node.

Returns:
a QName generated from the given string representation

getFullQNameFromString

public static javax.xml.namespace.QName getFullQNameFromString(java.lang.String str,
                                                               org.w3c.dom.Node e)
Return a QName when passed a string like "foo:bar" by mapping the "foo" prefix to a namespace in the context of the given Node. If default namespace is found it is returned as part of the QName.

Returns:
a QName generated from the given string representation

getStringForQName

public static java.lang.String getStringForQName(javax.xml.namespace.QName qname,
                                                 org.w3c.dom.Element e)
Return a string for a particular QName, mapping a new prefix if necessary.


getElementByWsuId

public static org.w3c.dom.Element getElementByWsuId(WSSConfig wssConfig,
                                                    org.w3c.dom.Document doc,
                                                    java.lang.String id)
Search for an element given its wsu:id.

Parameters:
wssConfig -
doc -
id -
Returns:

getElementByGenId

public static org.w3c.dom.Element getElementByGenId(org.w3c.dom.Document doc,
                                                    java.lang.String id)
Search for an element given its generic id.

Parameters:
doc -
id -
Returns:

createBinarySecurityToken

public static org.w3c.dom.Element createBinarySecurityToken(org.w3c.dom.Document doc,
                                                            java.lang.String wsuIdVal,
                                                            WSSConfig wssConfig)
Create a BinarySecurityToken element

Parameters:
doc -
wsuIdVal -
wssConfig -
Returns:

appendChildElement

public static org.w3c.dom.Element appendChildElement(org.w3c.dom.Document doc,
                                                     org.w3c.dom.Element parent,
                                                     org.w3c.dom.Element child)
append a child element

Parameters:
doc -
parent -
child -
Returns:

prependChildElement

public static org.w3c.dom.Element prependChildElement(org.w3c.dom.Document doc,
                                                      org.w3c.dom.Element parent,
                                                      org.w3c.dom.Element child,
                                                      boolean addWhitespace)
prepend a child element

Parameters:
doc -
parent -
child -
addWhitespace -
Returns:

findWsseSecurityHeaderBlock

public static org.w3c.dom.Element findWsseSecurityHeaderBlock(WSSConfig wssConfig,
                                                              org.w3c.dom.Document doc,
                                                              org.w3c.dom.Element envelope,
                                                              boolean doCreate)
find the first ws-security header block

Parameters:
doc -
envelope -
doCreate -
Returns:

findWsseSecurityHeaderBlock

public static org.w3c.dom.Element findWsseSecurityHeaderBlock(WSSConfig wssConfig,
                                                              org.w3c.dom.Document doc,
                                                              org.w3c.dom.Element envelope,
                                                              java.lang.String actor,
                                                              boolean doCreate)
find a ws-security header block for a given actor

Parameters:
doc -
envelope -
actor -
doCreate -
Returns:

createBase64EncodedTextNode

public static org.w3c.dom.Text createBase64EncodedTextNode(org.w3c.dom.Document doc,
                                                           byte[] data)
create a base64 test node

Parameters:
doc -
data -
Returns:

selectSingleNode

public static org.w3c.dom.Node selectSingleNode(org.w3c.dom.Node contextNode,
                                                java.lang.String xpath,
                                                org.w3c.dom.Element nsContext)
                                         throws java.lang.Exception
use xpath to find a node

Parameters:
contextNode -
xpath -
nsContext -
Returns:
Throws:
java.lang.Exception

createNamespaceContext

public static org.w3c.dom.Element createNamespaceContext(WSSConfig wssConfig,
                                                         org.w3c.dom.Document doc)
Create a namespace context with namespaces of interest

Parameters:
doc -
Returns:

prepareSecretKey

public static javax.crypto.SecretKey prepareSecretKey(java.lang.String symEncAlgo,
                                                      byte[] rawKey)

getSOAPConstants

public static SOAPConstants getSOAPConstants(org.w3c.dom.Element startElement)

getCipherInstance

public static javax.crypto.Cipher getCipherInstance(java.lang.String cipherAlgo)
                                             throws WSSecurityException
Throws:
WSSecurityException

fetchActionResult

public static WSSecurityEngineResult fetchActionResult(java.util.Vector wsResultVector,
                                                       int action)
Fetch the result of a given action from a given result vector

Parameters:
wsResultVector - The result vector to fetch an action from
action - The action to fetch
Returns:
The result fetched from the result vector, null if the result could not be found