* @see {@link ParserNamespaceSupport#parse(XMLEventReader)}
*/
public Object parse(XMLEventReader xmlEventReader) throws ParsingException {
StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
RequestSecurityTokenResponse responseToken = new RequestSecurityTokenResponse();
QName contextQName = new QName("", WSTrustConstants.RST_CONTEXT);
Attribute contextAttribute = startElement.getAttributeByName(contextQName);
String contextValue = StaxParserUtil.getAttributeValue(contextAttribute);
responseToken.setContext(contextValue);
while (xmlEventReader.hasNext()) {
XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
if (xmlEvent == null)
break;
if (xmlEvent instanceof EndElement) {
xmlEvent = StaxParserUtil.getNextEvent(xmlEventReader);
EndElement endElement = (EndElement) xmlEvent;
String endElementTag = StaxParserUtil.getEndElementName(endElement);
if (endElementTag.equals(WSTrustConstants.RSTR))
break;
else
throw logger.parserUnknownEndElement(endElementTag);
}
try {
StartElement subEvent = StaxParserUtil.peekNextStartElement(xmlEventReader);
if (subEvent == null)
break;
String tag = StaxParserUtil.getStartElementName(subEvent);
if (tag.equals(WSTrustConstants.REQUEST_TYPE)) {
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
if (!StaxParserUtil.hasTextAhead(xmlEventReader))
throw logger.parserExpectedTextValue("request type");
String value = StaxParserUtil.getElementText(xmlEventReader);
responseToken.setRequestType(new URI(value));
} else if (tag.equals(WSTrustConstants.LIFETIME)) {
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
StaxParserUtil.validate(subEvent, WSTrustConstants.LIFETIME);
LifetimeType lifeTime = new LifetimeType();
// Get the Created
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
String subTag = StaxParserUtil.getStartElementName(subEvent);
if (subTag.equals(WSTrustConstants.CREATED)) {
AttributedDateTime created = new AttributedDateTime();
created.setValue(StaxParserUtil.getElementText(xmlEventReader));
lifeTime.setCreated(created);
}
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
subTag = StaxParserUtil.getStartElementName(subEvent);
if (subTag.equals(WSTrustConstants.EXPIRES)) {
AttributedDateTime expires = new AttributedDateTime();
expires.setValue(StaxParserUtil.getElementText(xmlEventReader));
lifeTime.setExpires(expires);
} else
throw logger.parserUnknownTag(subTag, subEvent.getLocation());
responseToken.setLifetime(new Lifetime(lifeTime));
EndElement lifeTimeElement = StaxParserUtil.getNextEndElement(xmlEventReader);
StaxParserUtil.validate(lifeTimeElement, WSTrustConstants.LIFETIME);
} else if (tag.equals(WSTrustConstants.TOKEN_TYPE)) {
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
if (!StaxParserUtil.hasTextAhead(xmlEventReader))
throw logger.parserExpectedTextValue("token type");
String value = StaxParserUtil.getElementText(xmlEventReader);
responseToken.setTokenType(new URI(value));
} else if (tag.equals(WSTrustConstants.ON_BEHALF_OF)) {
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
WSTrustOnBehalfOfParser wstOnBehalfOfParser = new WSTrustOnBehalfOfParser();
OnBehalfOfType onBehalfOf = (OnBehalfOfType) wstOnBehalfOfParser.parse(xmlEventReader);
responseToken.setOnBehalfOf(onBehalfOf);
EndElement onBehalfOfEndElement = StaxParserUtil.getNextEndElement(xmlEventReader);
StaxParserUtil.validate(onBehalfOfEndElement, WSTrustConstants.ON_BEHALF_OF);
} else if (tag.equals(WSTrustConstants.KEY_TYPE)) {
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
if (!StaxParserUtil.hasTextAhead(xmlEventReader))
throw logger.parserExpectedTextValue("key type");
String keyType = StaxParserUtil.getElementText(xmlEventReader);
try {
URI keyTypeURI = new URI(keyType);
responseToken.setKeyType(keyTypeURI);
} catch (URISyntaxException e) {
throw new ParsingException(e);
}
} else if (tag.equals(WSTrustConstants.KEY_SIZE)) {
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
if (!StaxParserUtil.hasTextAhead(xmlEventReader))
throw logger.parserExpectedTextValue("key size");
String keySize = StaxParserUtil.getElementText(xmlEventReader);
try {
responseToken.setKeySize(Long.parseLong(keySize));
} catch (NumberFormatException e) {
throw logger.parserException(e);
}
} else if (tag.equals(WSTrustConstants.ENTROPY)) {
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
EntropyType entropy = new EntropyType();
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
if (StaxParserUtil.matches(subEvent, WSTrustConstants.BINARY_SECRET)) {
BinarySecretType binarySecret = new BinarySecretType();
Attribute typeAttribute = subEvent.getAttributeByName(new QName("", "Type"));
if (typeAttribute != null) {
binarySecret.setType(StaxParserUtil.getAttributeValue(typeAttribute));
}
if (!StaxParserUtil.hasTextAhead(xmlEventReader))
throw logger.parserExpectedTextValue("binary secret value");
binarySecret.setValue(StaxParserUtil.getElementText(xmlEventReader).getBytes());
entropy.addAny(binarySecret);
}
responseToken.setEntropy(entropy);
EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
StaxParserUtil.validate(endElement, WSTrustConstants.ENTROPY);
} else if (tag.equals(WSTrustConstants.USE_KEY)) {
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
UseKeyType useKeyType = new UseKeyType();
StaxParserUtil.validate(subEvent, WSTrustConstants.USE_KEY);
// We peek at the next start element as the stax source has to be in the START_ELEMENT mode
subEvent = StaxParserUtil.peekNextStartElement(xmlEventReader);
if (StaxParserUtil.matches(subEvent, X509CERTIFICATE)) {
Element domElement = StaxParserUtil.getDOMElement(xmlEventReader);
// Element domElement = getX509CertificateAsDomElement( subEvent, xmlEventReader );
useKeyType.add(domElement);
responseToken.setUseKey(useKeyType);
} else if (StaxParserUtil.matches(subEvent, KEYVALUE)) {
Element domElement = StaxParserUtil.getDOMElement(xmlEventReader);
useKeyType.add(domElement);
responseToken.setUseKey(useKeyType);
} else
throw logger.parserUnknownStartElement(StaxParserUtil.getStartElementName(subEvent), subEvent.getLocation());
} else if (tag.equals(WSTrustConstants.REQUESTED_TOKEN_CANCELLED)) {
StaxParserUtil.getNextEndElement(xmlEventReader);
responseToken.setRequestedTokenCancelled(new RequestedTokenCancelledType());
} else if (tag.equals(WSTrustConstants.REQUESTED_PROOF_TOKEN)) {
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
RequestedProofTokenType requestedProofToken = new RequestedProofTokenType();
subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
if (StaxParserUtil.matches(subEvent, WSTrustConstants.BINARY_SECRET)) {
BinarySecretType binarySecret = new BinarySecretType();
Attribute typeAttribute = subEvent.getAttributeByName(new QName("", "Type"));
binarySecret.setType(StaxParserUtil.getAttributeValue(typeAttribute));
if (!StaxParserUtil.hasTextAhead(xmlEventReader))
throw logger.parserExpectedTextValue("binary secret value");
binarySecret.setValue(StaxParserUtil.getElementText(xmlEventReader).getBytes());
requestedProofToken.add(binarySecret);
} else if (StaxParserUtil.matches(subEvent, WSTrustConstants.COMPUTED_KEY)) {
ComputedKeyType computedKey = new ComputedKeyType();
if (!StaxParserUtil.hasTextAhead(xmlEventReader))
throw logger.parserExpectedTextValue("computed key algorithm");
computedKey.setAlgorithm(StaxParserUtil.getElementText(xmlEventReader));
requestedProofToken.add(computedKey);
}
responseToken.setRequestedProofToken(requestedProofToken);
EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
StaxParserUtil.validate(endElement, WSTrustConstants.REQUESTED_PROOF_TOKEN);
} else if (tag.equals(WSTrustConstants.REQUESTED_TOKEN)) {
responseToken.setRequestedSecurityToken(parseRequestedSecurityTokenType(xmlEventReader));
} else if (tag.equals(WSTrustConstants.REQUESTED_ATTACHED_REFERENCE)) {
responseToken.setRequestedAttachedReference(parseRequestedReference(xmlEventReader, WSTrustConstants.REQUESTED_ATTACHED_REFERENCE));
} else if (tag.equals(WSTrustConstants.REQUESTED_UNATTACHED_REFERENCE)) {
responseToken.setRequestedUnattachedReference(parseRequestedReference(xmlEventReader, WSTrustConstants.REQUESTED_UNATTACHED_REFERENCE));
} else if (tag.equals(WSTrustConstants.STATUS)) {
responseToken.setStatus(this.parseStatusType(xmlEventReader));
}
else if (tag.equals(WSTrustConstants.RENEWING)) {
responseToken.setRenewing(WSTrustUtil.parseRenewingType(xmlEventReader));
} else {
QName qname = subEvent.getName();
logger.trace("Looking for parser for element: " + qname);
ParserNamespaceSupport parser = ParserController.get(qname);
if (parser == null)
throw logger.parserUnknownTag(qname.getLocalPart(), subEvent.getLocation());
Object parsedObject = parser.parse(xmlEventReader);
if (parsedObject instanceof AppliesTo) {
responseToken.setAppliesTo((AppliesTo) parsedObject);
}
}
} catch (URISyntaxException e) {
throw logger.parserException(e);
}