Search in sources :

Example 81 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project spring-framework by spring-projects.

the class AbstractStaxHandler method startDocument.

@Override
public final void startDocument() throws SAXException {
    removeAllNamespaceMappings();
    newNamespaceMapping();
    try {
        startDocumentInternal();
    } catch (XMLStreamException ex) {
        throw new SAXException("Could not handle startDocument: " + ex.getMessage(), ex);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException)

Example 82 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project spring-framework by spring-projects.

the class AbstractXMLStreamReader method getElementText.

@Override
public String getElementText() throws XMLStreamException {
    if (getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new XMLStreamException("parser must be on START_ELEMENT to read next text", getLocation());
    }
    int eventType = next();
    StringBuilder builder = new StringBuilder();
    while (eventType != XMLStreamConstants.END_ELEMENT) {
        if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
            builder.append(getText());
        } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT) {
        // skipping
        } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
            throw new XMLStreamException("unexpected end of document when reading element text content", getLocation());
        } else if (eventType == XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("element text content may not contain START_ELEMENT", getLocation());
        } else {
            throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
        }
        eventType = next();
    }
    return builder.toString();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException)

Example 83 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project ddf by codice.

the class AssertionConsumerService method processSoapResponse.

@POST
@Consumes({ "text/xml", "application/soap+xml" })
public Response processSoapResponse(InputStream body, @Context HttpServletRequest request) {
    try {
        SOAPPart soapMessage = SamlProtocol.parseSoapMessage(IOUtils.toString(body));
        String relayState = getRelayState(soapMessage);
        org.opensaml.saml.saml2.core.Response samlpResponse = getSamlpResponse(soapMessage);
        boolean validateResponse = validateResponse(samlpResponse);
        if (validateResponse) {
            return processSamlResponse(samlpResponse, relayState);
        }
    } catch (XMLStreamException e) {
        LOGGER.debug("Unable to parse SOAP message from response.", e);
    } catch (IOException e) {
        LOGGER.debug("Unable to get SAMLP response.", e);
    } catch (SOAPException e) {
        LOGGER.debug("Unable to get relay state from response.", e);
    }
    return Response.serverError().entity("Invalid AuthN response.").build();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) IOException(java.io.IOException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 84 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project ddf by codice.

the class DynamicSchemaResolver method addFields.

/**
     * Adds the fields of the Metacard into the {@link SolrInputDocument}
     */
public void addFields(Metacard metacard, SolrInputDocument solrInputDocument) throws MetacardCreationException {
    MetacardType schema = metacard.getMetacardType();
    for (AttributeDescriptor ad : schema.getAttributeDescriptors()) {
        if (metacard.getAttribute(ad.getName()) != null) {
            List<Serializable> attributeValues = metacard.getAttribute(ad.getName()).getValues();
            if (attributeValues != null && attributeValues.size() > 0 && attributeValues.get(0) != null) {
                AttributeFormat format = ad.getType().getAttributeFormat();
                String formatIndexName = ad.getName() + getFieldSuffix(format);
                if (AttributeFormat.XML.equals(format)) {
                    List<String> parsedTexts = parseTextFrom(attributeValues);
                    // text => metadata_txt_ws
                    String whitespaceTokenizedIndexName = ad.getName() + getFieldSuffix(AttributeFormat.STRING) + SchemaFields.WHITESPACE_TEXT_SUFFIX;
                    solrInputDocument.addField(whitespaceTokenizedIndexName, parsedTexts);
                    // text => metadata_txt_ws_has_case
                    String whiteSpaceTokenizedHasCaseIndexName = ad.getName() + getFieldSuffix(AttributeFormat.STRING) + SchemaFields.WHITESPACE_TEXT_SUFFIX + SchemaFields.HAS_CASE;
                    solrInputDocument.addField(whiteSpaceTokenizedHasCaseIndexName, parsedTexts);
                    // text => metadata_txt_tokenized
                    String specialStringIndexName = ad.getName() + getFieldSuffix(AttributeFormat.STRING) + getSpecialIndexSuffix(AttributeFormat.STRING);
                    solrInputDocument.addField(specialStringIndexName, parsedTexts);
                    // text case sensitive
                    solrInputDocument.addField(specialStringIndexName + SchemaFields.HAS_CASE, parsedTexts);
                } else if (AttributeFormat.OBJECT.equals(format)) {
                    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
                    List<Serializable> byteArrays = new ArrayList<>();
                    try (ObjectOutputStream out = new ObjectOutputStream(byteArrayOS)) {
                        for (Serializable serializable : attributeValues) {
                            out.writeObject(serializable);
                            byteArrays.add(byteArrayOS.toByteArray());
                            out.reset();
                        }
                    } catch (IOException e) {
                        throw new MetacardCreationException(COULD_NOT_SERIALIZE_OBJECT_MESSAGE, e);
                    }
                    attributeValues = byteArrays;
                }
                // Prevent adding a field already on document
                if (solrInputDocument.getFieldValue(formatIndexName) == null) {
                    solrInputDocument.addField(formatIndexName, attributeValues);
                    if (AttributeFormat.GEOMETRY.equals(format)) {
                        solrInputDocument.addField(formatIndexName + SchemaFields.SORT_KEY_SUFFIX, createCenterPoint(attributeValues));
                    } else if (!(AttributeFormat.BINARY.equals(format) || AttributeFormat.OBJECT.equals(format))) {
                        solrInputDocument.addField(formatIndexName + SchemaFields.SORT_KEY_SUFFIX, attributeValues.get(0));
                    }
                } else {
                    LOGGER.trace("Skipping adding field already found on document ({})", formatIndexName);
                }
            }
        }
    }
    if (!ConfigurationStore.getInstance().isDisableTextPath()) {
        if (StringUtils.isNotBlank(metacard.getMetadata())) {
            try {
                byte[] luxXml = createTinyBinary(metacard.getMetadata());
                solrInputDocument.addField(LUX_XML_FIELD_NAME, luxXml);
            } catch (XMLStreamException | SaxonApiException e) {
                LOGGER.debug("Unable to parse metadata field.  XPath support unavailable for metacard {}", metacard.getId());
            }
        }
    }
    /*
         * Lastly the metacardType must be added to the solr document. These are internal fields
         */
    String schemaName = String.format("%s#%s", schema.getName(), schema.hashCode());
    solrInputDocument.addField(SchemaFields.METACARD_TYPE_FIELD_NAME, schemaName);
    byte[] metacardTypeBytes = metacardTypeNameToSerialCache.getIfPresent(schemaName);
    if (metacardTypeBytes == null) {
        MetacardType coreMetacardType = new MetacardTypeImpl(schema.getName(), convertAttributeDescriptors(schema.getAttributeDescriptors()));
        metacardTypesCache.put(schemaName, coreMetacardType);
        metacardTypeBytes = serialize(coreMetacardType);
        metacardTypeNameToSerialCache.put(schemaName, metacardTypeBytes);
        addToFieldsCache(coreMetacardType.getAttributeDescriptors());
    }
    solrInputDocument.addField(SchemaFields.METACARD_TYPE_OBJECT_FIELD_NAME, metacardTypeBytes);
}
Also used : Serializable(java.io.Serializable) MetacardCreationException(ddf.catalog.data.MetacardCreationException) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) MetacardType(ddf.catalog.data.MetacardType) AttributeFormat(ddf.catalog.data.AttributeType.AttributeFormat) XMLStreamException(javax.xml.stream.XMLStreamException) List(java.util.List) ArrayList(java.util.ArrayList)

Example 85 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project ddf by codice.

the class IdpEndpoint method determineAuthMethod.

private AuthObj determineAuthMethod(String bodyStr, AuthnRequest authnRequest) {
    XMLStreamReader xmlStreamReader = null;
    try {
        xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(bodyStr));
    } catch (XMLStreamException e) {
        LOGGER.debug("Unable to parse SOAP message from client.", e);
    }
    SoapMessage soapMessage = new SoapMessage(Soap11.getInstance());
    SAAJInInterceptor.SAAJPreInInterceptor preInInterceptor = new SAAJInInterceptor.SAAJPreInInterceptor();
    soapMessage.setContent(XMLStreamReader.class, xmlStreamReader);
    preInInterceptor.handleMessage(soapMessage);
    SAAJInInterceptor inInterceptor = new SAAJInInterceptor();
    inInterceptor.handleMessage(soapMessage);
    SOAPPart soapMessageContent = (SOAPPart) soapMessage.getContent(Node.class);
    AuthObj authObj = new AuthObj();
    try {
        Iterator soapHeaderElements = soapMessageContent.getEnvelope().getHeader().examineAllHeaderElements();
        while (soapHeaderElements.hasNext()) {
            SOAPHeaderElement soapHeaderElement = (SOAPHeaderElement) soapHeaderElements.next();
            if (soapHeaderElement.getLocalName().equals("Security")) {
                Iterator childElements = soapHeaderElement.getChildElements();
                while (childElements.hasNext()) {
                    Object nextElement = childElements.next();
                    if (nextElement instanceof SOAPElement) {
                        SOAPElement element = (SOAPElement) nextElement;
                        if (element.getLocalName().equals("UsernameToken")) {
                            Iterator usernameTokenElements = element.getChildElements();
                            Object next;
                            while (usernameTokenElements.hasNext()) {
                                if ((next = usernameTokenElements.next()) instanceof Element) {
                                    Element nextEl = (Element) next;
                                    if (nextEl.getLocalName().equals("Username")) {
                                        authObj.username = nextEl.getTextContent();
                                    } else if (nextEl.getLocalName().equals("Password")) {
                                        authObj.password = nextEl.getTextContent();
                                    }
                                }
                            }
                            if (authObj.username != null && authObj.password != null) {
                                authObj.method = USER_PASS;
                                break;
                            }
                        } else if (element.getLocalName().equals("Assertion") && element.getNamespaceURI().equals("urn:oasis:names:tc:SAML:2.0:assertion")) {
                            authObj.assertion = new SecurityToken(element.getAttribute("ID"), element, null, null);
                            authObj.method = SAML;
                            break;
                        }
                    }
                }
            }
        }
    } catch (SOAPException e) {
        LOGGER.debug("Unable to parse SOAP message.", e);
    }
    RequestedAuthnContext requestedAuthnContext = authnRequest.getRequestedAuthnContext();
    boolean requestingPki = false;
    boolean requestingUp = false;
    if (requestedAuthnContext != null) {
        List<AuthnContextClassRef> authnContextClassRefs = requestedAuthnContext.getAuthnContextClassRefs();
        for (AuthnContextClassRef authnContextClassRef : authnContextClassRefs) {
            String authnContextClassRefStr = authnContextClassRef.getAuthnContextClassRef();
            if (SAML2Constants.AUTH_CONTEXT_CLASS_REF_X509.equals(authnContextClassRefStr) || SAML2Constants.AUTH_CONTEXT_CLASS_REF_SMARTCARD_PKI.equals(authnContextClassRefStr) || SAML2Constants.AUTH_CONTEXT_CLASS_REF_SOFTWARE_PKI.equals(authnContextClassRefStr) || SAML2Constants.AUTH_CONTEXT_CLASS_REF_SPKI.equals(authnContextClassRefStr) || SAML2Constants.AUTH_CONTEXT_CLASS_REF_TLS_CLIENT.equals(authnContextClassRefStr)) {
                requestingPki = true;
            } else if (SAML2Constants.AUTH_CONTEXT_CLASS_REF_PASSWORD.equals(authnContextClassRefStr) || SAML2Constants.AUTH_CONTEXT_CLASS_REF_PASSWORD_PROTECTED_TRANSPORT.equals(authnContextClassRefStr)) {
                requestingUp = true;
            }
        }
    } else {
        //The requested auth context isn't required so we don't know what they want... just set both to true
        requestingPki = true;
        requestingUp = true;
    }
    if (requestingUp && authObj.method != null && authObj.method.equals(USER_PASS)) {
        LOGGER.trace("Found UsernameToken and correct AuthnContextClassRef");
        return authObj;
    } else if (requestingPki && authObj.method == null) {
        LOGGER.trace("Found no token, but client requested PKI AuthnContextClassRef");
        authObj.method = PKI;
        return authObj;
    } else if (authObj.method == null) {
        LOGGER.debug("No authentication tokens found for the current request and the client did not request PKI authentication");
    }
    return authObj;
}
Also used : SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) XMLStreamReader(javax.xml.stream.XMLStreamReader) Node(org.w3c.dom.Node) SOAPElement(javax.xml.soap.SOAPElement) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) Element(org.w3c.dom.Element) AuthnContextClassRef(org.opensaml.saml.saml2.core.AuthnContextClassRef) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SAAJInInterceptor(org.apache.cxf.binding.soap.saaj.SAAJInInterceptor) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) RequestedAuthnContext(org.opensaml.saml.saml2.core.RequestedAuthnContext) XMLStreamException(javax.xml.stream.XMLStreamException) SOAPException(javax.xml.soap.SOAPException) StringReader(java.io.StringReader) SOAPPart(javax.xml.soap.SOAPPart) Iterator(java.util.Iterator) SOAPElement(javax.xml.soap.SOAPElement) SignableSAMLObject(org.opensaml.saml.common.SignableSAMLObject) SignableXMLObject(org.opensaml.xmlsec.signature.SignableXMLObject) XMLObject(org.opensaml.core.xml.XMLObject)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)442 XMLStreamReader (javax.xml.stream.XMLStreamReader)137 IOException (java.io.IOException)126 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)75 XMLInputFactory (javax.xml.stream.XMLInputFactory)69 InputStream (java.io.InputStream)66 Document (org.w3c.dom.Document)36 JAXBException (javax.xml.bind.JAXBException)35 Fault (org.apache.cxf.interceptor.Fault)34 Element (org.w3c.dom.Element)32 StringReader (java.io.StringReader)30 XMLEvent (javax.xml.stream.events.XMLEvent)28 DOMSource (javax.xml.transform.dom.DOMSource)27 ByteArrayInputStream (java.io.ByteArrayInputStream)25 ArrayList (java.util.ArrayList)23 QName (javax.xml.namespace.QName)23 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)23 Node (org.w3c.dom.Node)22 StringWriter (java.io.StringWriter)21 XMLEventReader (javax.xml.stream.XMLEventReader)20