package com.microsoft.azure.storage;
import com.microsoft.azure.storage.core.SR;
import com.microsoft.azure.storage.core.Utility;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;
import java.util.UUID;
public class UserDelegationKeyHandler extends DefaultHandler {
private final Stack<String> elementStack = new Stack<>();
private final UserDelegationKey key = new UserDelegationKey();
private StringBuilder sb = new StringBuilder();
public static UserDelegationKey readUserDelegationKeyFromStream(final InputStream stream)
throws SAXException, IOException, ParserConfigurationException {
SAXParser saxParser = Utility.getSAXParser();
UserDelegationKeyHandler handler = new UserDelegationKeyHandler();
saxParser.parse(stream, handler);
return handler.key;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
this.elementStack.push(localName);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
String currentNode = this.elementStack.pop();
if (!localName.equals(currentNode)) {
throw new SAXException(SR.INVALID_RESPONSE_RECEIVED);
}
String value = this.sb.toString();
sb = new StringBuilder();
if (value.isEmpty()) {
value = null;
}
switch (localName) {
case Constants.SIGNED_OID_ELEMENT:
try {
key.setSignedOid(UUID.fromString(value));
}
catch (Exception e) {
}
break;
case Constants.SIGNED_TID_ELEMENT:
try {
key.setSignedTid(UUID.fromString(value));
}
catch (Exception e) {
}
break;
case Constants.SIGNED_START_ELEMENT:
try {
key.setSignedStart(Utility.parseDate(value));
}
catch (Exception e) {
}
break;
case Constants.SIGNED_EXPIRY_ELEMENT:
try {
key.setSignedExpiry(Utility.parseDate(value));
}
catch (Exception e) {
}
break;
case Constants.SIGNED_VERSION_ELEMENT:
key.setSignedVersion(value);
break;
case Constants.SIGNED_SERVICE_ELEMENT:
key.setSignedService(value);
break;
case Constants.VALUE_ELEMENT:
key.setValue(value);
break;
default:
break;
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
this.sb.append(ch, start, length);
}
}