Search in sources :

Example 86 with XMLStreamException

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

the class IdpEndpoint method processPostLogout.

@Override
@POST
@Path("/logout")
public Response processPostLogout(@FormParam(SAML_REQ) final String samlRequest, @FormParam(SAML_RESPONSE) final String samlResponse, @FormParam(RELAY_STATE) final String relayState, @Context final HttpServletRequest request) throws WSSecurityException, IdpException {
    LogoutState logoutState = getLogoutState(request);
    Cookie cookie = getCookie(request);
    try {
        if (samlRequest != null) {
            LogoutRequest logoutRequest = logoutMessage.extractSamlLogoutRequest(RestSecurity.inflateBase64(samlRequest));
            validatePost(request, logoutRequest);
            return handleLogoutRequest(cookie, logoutState, logoutRequest, SamlProtocol.Binding.HTTP_POST, relayState);
        } else if (samlResponse != null) {
            LogoutResponse logoutResponse = logoutMessage.extractSamlLogoutResponse(RestSecurity.inflateBase64(samlResponse));
            String requestId = logoutState != null ? logoutState.getCurrentRequestId() : null;
            validatePost(request, logoutResponse, requestId);
            return handleLogoutResponse(cookie, logoutState, logoutResponse, SamlProtocol.Binding.HTTP_POST);
        }
    } catch (IOException | XMLStreamException e) {
        throw new IdpException("Unable to inflate Saml Object", e);
    } catch (ValidationException e) {
        throw new IdpException("Unable to validate Saml Object", e);
    }
    throw new IdpException("Unable to process logout");
}
Also used : NewCookie(javax.ws.rs.core.NewCookie) Cookie(javax.servlet.http.Cookie) ValidationException(ddf.security.samlp.ValidationException) LogoutResponse(org.opensaml.saml.saml2.core.LogoutResponse) XMLStreamException(javax.xml.stream.XMLStreamException) LogoutRequest(org.opensaml.saml.saml2.core.LogoutRequest) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 87 with XMLStreamException

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

the class SoapRequestDecoder method decodeRelayState.

public String decodeRelayState(String samlRequest) {
    String relayState = null;
    try {
        SOAPPart soapMessage = SamlProtocol.parseSoapMessage(samlRequest);
        SOAPEnvelope envelope = soapMessage.getEnvelope();
        SOAPHeader header = envelope.getHeader();
        Iterator iterator = header.examineAllHeaderElements();
        while (iterator.hasNext()) {
            SOAPHeaderElement soapHeaderElement = (SOAPHeaderElement) iterator.next();
            if ("RelayState".equals(soapHeaderElement.getLocalName())) {
                relayState = soapHeaderElement.getValue();
                break;
            }
        }
    } catch (XMLStreamException e) {
        throw new IllegalArgumentException("Unable to convert parse SOAP request.");
    } catch (SOAPException e) {
        throw new IllegalArgumentException("Unable to get SOAP envelope.");
    }
    return relayState;
}
Also used : SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) XMLStreamException(javax.xml.stream.XMLStreamException) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) Iterator(java.util.Iterator) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 88 with XMLStreamException

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

the class CswResponseExceptionMapper method fromResponse.

@Override
public CswException fromResponse(Response response) {
    CswException cswException = null;
    if (response != null) {
        if (response.getEntity() instanceof InputStream) {
            String msg = null;
            try {
                InputStream is = (InputStream) response.getEntity();
                if (is.markSupported()) {
                    is.reset();
                }
                msg = IOUtils.toString(is);
            } catch (IOException e) {
                cswException = new CswException("Error received from remote Csw server" + (msg != null ? ": " + msg : ""));
                LOGGER.info("Unable to parse exception report: {}", e.getMessage());
                LOGGER.debug("Unable to parse exception report: {}", e);
            }
            if (msg != null) {
                try {
                    JAXBElementProvider<ExceptionReport> provider = new JAXBElementProvider<ExceptionReport>();
                    Unmarshaller um = provider.getJAXBContext(ExceptionReport.class, ExceptionReport.class).createUnmarshaller();
                    XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
                    xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
                    xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
                    xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
                    XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(msg));
                    ExceptionReport report = (ExceptionReport) um.unmarshal(xmlStreamReader);
                    cswException = convertToCswException(report);
                } catch (JAXBException | XMLStreamException e) {
                    cswException = new CswException("Error received from remote Csw server: " + msg, e);
                    LOGGER.info("Error parsing the exception report: {}", e.getMessage());
                    LOGGER.debug("Error parsing the exception report", e);
                }
            }
        } else {
            cswException = new CswException("Error reading response, entity type not understood: " + response.getEntity().getClass().getName());
        }
        cswException.setHttpStatus(response.getStatus());
    } else {
        cswException = new CswException("Error handling response, response is null");
    }
    return cswException;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) ExceptionReport(net.opengis.ows.v_1_0_0.ExceptionReport) JAXBException(javax.xml.bind.JAXBException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) IOException(java.io.IOException) JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 89 with XMLStreamException

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

the class AbstractStsRealm method createClaimsElement.

/**
     * Create the claims element with the claims provided in the STS client configuration in the
     * admin console.
     */
protected Element createClaimsElement() {
    Element claimsElement = null;
    List<String> claims = new ArrayList<>();
    claims.addAll(getClaims());
    if (contextPolicyManager != null) {
        Collection<ContextPolicy> contextPolicies = contextPolicyManager.getAllContextPolicies();
        Set<String> attributes = new LinkedHashSet<>();
        if (contextPolicies != null && contextPolicies.size() > 0) {
            for (ContextPolicy contextPolicy : contextPolicies) {
                attributes.addAll(contextPolicy.getAllowedAttributeNames());
            }
        }
        if (attributes.size() > 0) {
            claims.addAll(attributes);
        }
    }
    if (claims.size() != 0) {
        W3CDOMStreamWriter writer = null;
        try {
            writer = new W3CDOMStreamWriter();
            writer.writeStartElement("wst", "Claims", STSUtils.WST_NS_05_12);
            writer.writeNamespace("wst", STSUtils.WST_NS_05_12);
            writer.writeNamespace("ic", "http://schemas.xmlsoap.org/ws/2005/05/identity");
            writer.writeAttribute("Dialect", "http://schemas.xmlsoap.org/ws/2005/05/identity");
            for (String claim : claims) {
                LOGGER.trace("Claim: {}", claim);
                writer.writeStartElement("ic", "ClaimType", "http://schemas.xmlsoap.org/ws/2005/05/identity");
                writer.writeAttribute("Uri", claim);
                writer.writeAttribute("Optional", "true");
                writer.writeEndElement();
            }
            writer.writeEndElement();
            claimsElement = writer.getDocument().getDocumentElement();
        } catch (XMLStreamException e) {
            String msg = "Unable to create claims. Subjects will not have any attributes. Check STS Client configuration.";
            LOGGER.warn(msg, e);
            claimsElement = null;
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (XMLStreamException ignore) {
                //ignore
                }
            }
        }
        if (LOGGER.isDebugEnabled()) {
            if (claimsElement != null) {
                LOGGER.debug("Claims: {}", getFormattedXml(claimsElement));
            }
        }
    } else {
        LOGGER.debug("There are no claims to process.");
        claimsElement = null;
    }
    return claimsElement;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) XMLStreamException(javax.xml.stream.XMLStreamException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ContextPolicy(org.codice.ddf.security.policy.context.ContextPolicy)

Example 90 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project galley by Commonjava.

the class XMLInfrastructure method fallbackParseDocument.

private Document fallbackParseDocument(String xml, final Object docSource, final Exception e) throws GalleyMavenXMLException {
    logger.debug("Failed to parse: {}. DOM error: {}. Trying STaX parse with IS_REPLACING_ENTITY_REFERENCES == false...", e, docSource, e.getMessage());
    try {
        Source source;
        if (safeInputFactory != null) {
            xml = repairXmlDeclaration(xml);
            final XMLEventReader eventReader = safeInputFactory.createXMLEventReader(new StringReader(xml));
            source = new StAXSource(eventReader);
        } else {
            // Deal with &oslash; and other undeclared entities...
            xml = escapeNonXMLEntityRefs(xml);
            final XMLReader reader = XMLReaderFactory.createXMLReader();
            reader.setFeature("http://xml.org/sax/features/validation", false);
            source = new SAXSource(reader, new InputSource(new StringReader(xml)));
        }
        final DOMResult result = new DOMResult();
        final Transformer transformer = newTransformer();
        transformer.transform(source, result);
        return (Document) result.getNode();
    } catch (final TransformerException e1) {
        throw new GalleyMavenXMLException("Failed to parse: %s. Transformer error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
    } catch (final SAXException e1) {
        throw new GalleyMavenXMLException("Failed to parse: %s. SAX error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
    } catch (final XMLStreamException e1) {
        throw new GalleyMavenXMLException("Failed to parse: %s. STaX error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
    }
}
Also used : InputSource(org.xml.sax.InputSource) DOMResult(javax.xml.transform.dom.DOMResult) Transformer(javax.xml.transform.Transformer) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) SAXException(org.xml.sax.SAXException) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

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