List of usage examples for javax.xml.stream XMLInputFactory createXMLEventReader
public abstract XMLEventReader createXMLEventReader(java.io.InputStream stream) throws XMLStreamException;
From source file:org.sakaiproject.nakamura.auth.cas.CasAuthenticationHandler.java
private String retrieveCredentials(String responseBody) { String username = null;/* w ww. j a va 2s. c om*/ String pgtIou = null; String failureCode = null; String failureMessage = null; try { XMLInputFactory xmlInputFactory = new WstxInputFactory(); xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true); xmlInputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false); xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true); XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(new StringReader(responseBody)); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); // process the event if we're starting an element if (event.isStartElement()) { StartElement startEl = event.asStartElement(); QName startElName = startEl.getName(); String startElLocalName = startElName.getLocalPart(); LOGGER.debug(responseBody); /* * Example of failure XML <cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'> <cas:authenticationFailure code='INVALID_REQUEST'> 'service' and 'ticket' parameters are both required </cas:authenticationFailure> </cas:serviceResponse> */ if ("authenticationFailure".equalsIgnoreCase(startElLocalName)) { // get code of the failure Attribute code = startEl.getAttributeByName(QName.valueOf("code")); failureCode = code.getValue(); // get the message of the failure event = eventReader.nextEvent(); assert event.isCharacters(); Characters chars = event.asCharacters(); failureMessage = chars.getData(); break; } /* * Example of success XML <cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'> <cas:authenticationSuccess> <cas:user>NetID</cas:user> </cas:authenticationSuccess> </cas:serviceResponse> */ if ("authenticationSuccess".equalsIgnoreCase(startElLocalName)) { // skip to the user tag start while (eventReader.hasNext()) { event = eventReader.nextTag(); if (event.isEndElement()) { if (eventReader.hasNext()) { event = eventReader.nextTag(); } else { break; } } assert event.isStartElement(); startEl = event.asStartElement(); startElName = startEl.getName(); startElLocalName = startElName.getLocalPart(); if (proxy && "proxyGrantingTicket".equals(startElLocalName)) { event = eventReader.nextEvent(); assert event.isCharacters(); Characters chars = event.asCharacters(); pgtIou = chars.getData(); LOGGER.debug("XML parser found pgt: {}", pgtIou); } else if ("user".equals(startElLocalName)) { // move on to the body of the user tag event = eventReader.nextEvent(); assert event.isCharacters(); Characters chars = event.asCharacters(); username = chars.getData(); LOGGER.debug("XML parser found user: {}", username); } else { LOGGER.error("Found unexpected element [{}] while inside 'authenticationSuccess'", startElName); break; } if (username != null && (!proxy || pgtIou != null)) { break; } } } } } } catch (XMLStreamException e) { LOGGER.error(e.getMessage(), e); } if (failureCode != null || failureMessage != null) { LOGGER.error("Error response from server code={} message={}", failureCode, failureMessage); } String pgt = pgts.get(pgtIou); if (pgt != null) { savePgt(username, pgt, pgtIou); } else { LOGGER.debug("Caching '{}' as the IOU for '{}'", pgtIou, username); pgtIOUs.put(pgtIou, username); } return username; }
From source file:org.sakaiproject.nakamura.auth.cas.CasAuthenticationHandler.java
private String getProxyTicketFromXml(String responseBody) { String ticket = null;/*from w w w . ja v a 2 s .com*/ try { XMLInputFactory xmlInputFactory = new WstxInputFactory(); xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true); xmlInputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false); xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true); XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(new StringReader(responseBody)); LOGGER.debug(responseBody); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); // process the event if we're starting an element if (event.isStartElement()) { StartElement startEl = event.asStartElement(); QName startElName = startEl.getName(); String startElLocalName = startElName.getLocalPart(); // Example XML // <cas:serviceResponse> // <cas:proxySuccess> // <cas:proxyTicket>PT-957-ZuucXqTZ1YcJw81T3dxf</cas:proxyTicket> // </cas:proxySuccess> // </cas:serviceResponse> if ("proxySuccess".equalsIgnoreCase(startElLocalName)) { event = eventReader.nextTag(); assert event.isStartElement(); startEl = event.asStartElement(); startElName = startEl.getName(); startElLocalName = startElName.getLocalPart(); if ("proxyTicket".equalsIgnoreCase(startElLocalName)) { event = eventReader.nextEvent(); assert event.isCharacters(); Characters chars = event.asCharacters(); ticket = chars.getData(); } else { LOGGER.error("Found unexpected element [{}] while inside 'proxySuccess'", startElName); break; } } } } } catch (XMLStreamException e) { LOGGER.error(e.getMessage(), e); } return ticket; }
From source file:ru.codeinside.gws.crypto.cryptopro.CryptoProvider.java
private String saxFilter(Node node) { try {//www .j ava 2 s. com final Transformer transformer = TransformerFactory.newInstance().newTransformer(); final StringWriter w1 = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(w1)); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLEventReader eventReader = xif .createXMLEventReader(new StreamSource(new StringReader(w1.toString()))); XMLEventReader filteredReader = xif.createFilteredReader(eventReader, new EventFilter() { @Override public boolean accept(XMLEvent event) { int type = event.getEventType(); if (type == XMLStreamConstants.START_DOCUMENT || type == XMLStreamConstants.END_DOCUMENT) { return false; } if (event.isStartElement()) { StartElement startElement = (StartElement) event; QName name = startElement.getName(); if ("".equals(name.getNamespaceURI()) && "root".equals(name.getLocalPart())) { return false; } } if (event.isEndElement()) { EndElement endElement = (EndElement) event; QName name = endElement.getName(); if ("".equals(name.getNamespaceURI()) && "root".equals(name.getLocalPart())) { return false; } } return true; } }); StringWriter sw = new StringWriter(); XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLEventWriter writer = xof.createXMLEventWriter(sw); while (filteredReader.hasNext()) { writer.add(filteredReader.nextEvent()); } return sw.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:tds.itemrenderer.processing.ITSDocumentHelper.java
/** * Gets the root element of XML//from w w w .ja v a 2 s. c om * * @param uri * @return xml root element * @throws FileNotFoundException * @throws UnsupportedEncodingException * @throws XMLStreamException */ public static String getRootElementName(URI uri) { XMLInputFactory inputFactory = TdsXmlInputFactory.newInstance(); try { InputStream input = null; String uriPath = getUriOriginalString(uri); if (isFtp(uri)) { input = new ByteArrayInputStream(FileFtpHandler.getBytes(uri)); } else { input = new FileInputStream(uriPath); } XMLEventReader inputEventReader = inputFactory.createXMLEventReader(input); while (inputEventReader.hasNext()) { XMLEvent event = inputEventReader.nextEvent(); if (event.isStartElement()) { return event.asStartElement().getName().getLocalPart(); } } } catch (FileNotFoundException | XMLStreamException e) { throw new ITSDocumentProcessingException(e); } return null; }