From 78c6ac29f9f3266c82760a658013794a25d9f3a6 Mon Sep 17 00:00:00 2001 From: lavrovdv Date: Wed, 14 Mar 2012 09:25:15 +0400 Subject: [PATCH] Parsing the attributes using xml dom parser. --- .../org/jasig/cas/client/util/XmlUtils.java | 29 ++++++++++ .../Cas20ServiceTicketValidator.java | 54 ++++++------------- 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/util/XmlUtils.java b/cas-client-core/src/main/java/org/jasig/cas/client/util/XmlUtils.java index 064ccd0..4c1524c 100644 --- a/cas-client-core/src/main/java/org/jasig/cas/client/util/XmlUtils.java +++ b/cas-client-core/src/main/java/org/jasig/cas/client/util/XmlUtils.java @@ -21,6 +21,8 @@ package org.jasig.cas.client.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -28,6 +30,10 @@ import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.List; @@ -171,4 +177,27 @@ public final class XmlUtils { return builder.toString(); } + + /** + * Retrieve the child nodes for a specific element + * + * @param xmlAsString the xml response + * @param tagName the element to look for + * @return the node list. + * @throws ParserConfigurationException + * @throws IOException + * @throws SAXException + */ + public static NodeList getNodeListForElements(final String xmlAsString, final String tagName) + throws ParserConfigurationException, + IOException, + SAXException { + + DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + InputSource inStream = new InputSource(); + inStream.setCharacterStream(new StringReader(xmlAsString)); + Document document = documentBuilder.parse(inStream); + + return document.getElementsByTagName(tagName).item(0).getChildNodes(); + } } diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/validation/Cas20ServiceTicketValidator.java b/cas-client-core/src/main/java/org/jasig/cas/client/validation/Cas20ServiceTicketValidator.java index c007282..e994348 100644 --- a/cas-client-core/src/main/java/org/jasig/cas/client/validation/Cas20ServiceTicketValidator.java +++ b/cas-client-core/src/main/java/org/jasig/cas/client/validation/Cas20ServiceTicketValidator.java @@ -26,14 +26,10 @@ import org.jasig.cas.client.proxy.ProxyGrantingTicketStorage; import org.jasig.cas.client.proxy.ProxyRetriever; import org.jasig.cas.client.util.CommonUtils; import org.jasig.cas.client.util.XmlUtils; +import org.w3c.dom.NodeList; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.StringReader; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; /** @@ -122,45 +118,25 @@ public class Cas20ServiceTicketValidator extends AbstractCasProtocolUrlBasedTick * @return the map of attributes. */ protected Map extractCustomAttributes(final String xml) { - final int pos1 = xml.indexOf(""); - final int pos2 = xml.indexOf(""); - - if (pos1 == -1) { + + if (!xml.contains("")) { return Collections.emptyMap(); } - - final String attributesText = xml.substring(pos1+16, pos2); - - final Map attributes = new HashMap(); - final BufferedReader br = new BufferedReader(new StringReader(attributesText)); - - String line; - final List attributeNames = new ArrayList(); - try { - while ((line = br.readLine()) != null) { - final String trimmedLine = line.trim(); - if (trimmedLine.length() > 0) { - final int leftPos = trimmedLine.indexOf(":"); - final int rightPos = trimmedLine.indexOf(">"); - attributeNames.add(trimmedLine.substring(leftPos+1, rightPos)); - } - } - br.close(); - } catch (final IOException e) { - //ignore - } - for (final String name : attributeNames) { - final List values = XmlUtils.getTextForElements(xml, name); + final Map attributes = new HashMap(); - if (values.size() == 1) { - attributes.put(name, values.get(0)); - } else { - attributes.put(name, values); + try { + NodeList nodeList = XmlUtils.getNodeListForElements(xml,"cas:attributes"); + for (int i = 0; i < nodeList.getLength(); i++) { + String attributeName = nodeList.item(i).getNodeName().substring(4); // remove the "cas:" prefix from node name + Object attributeValue = nodeList.item(i).getTextContent(); + attributes.put(attributeName, attributeValue); } - } - - return attributes; + return attributes; + + } catch (Exception e) { + return Collections.emptyMap(); + } } /**