use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.
the class SamlEnvelopedInHandler method filter.
@Override
public void filter(ContainerRequestContext context) {
Message message = JAXRSUtils.getCurrentMessage();
String method = (String) message.get(Message.HTTP_REQUEST_METHOD);
if (HttpMethod.GET.equals(method)) {
return;
}
Document doc = null;
InputStream is = message.getContent(InputStream.class);
if (is != null) {
try {
doc = StaxUtils.read(new InputStreamReader(is, StandardCharsets.UTF_8));
} catch (Exception ex) {
throwFault("Invalid XML payload", ex);
}
} else {
XMLStreamReader reader = message.getContent(XMLStreamReader.class);
if (reader instanceof W3CDOMStreamReader) {
doc = ((W3CDOMStreamReader) reader).getDocument();
}
}
if (doc == null) {
throwFault("No payload is available", null);
}
Element samlElement = getNode(doc.getDocumentElement(), SAML2_NS, SAML_ASSERTION);
if (samlElement == null) {
samlElement = getNode(doc.getDocumentElement(), SAML1_NS, SAML_ASSERTION);
}
if (samlElement == null) {
throwFault("SAML Assertion is not available", null);
}
validateToken(message, samlElement);
doc.getDocumentElement().removeChild(samlElement);
if (bodyIsRoot) {
message.setContent(XMLStreamReader.class, new W3CDOMStreamReader(doc));
message.setContent(InputStream.class, null);
} else {
Element actualBody = getActualBody(doc.getDocumentElement());
if (actualBody != null) {
Document newDoc = DOMUtils.createDocument();
newDoc.adoptNode(actualBody);
message.setContent(XMLStreamReader.class, new W3CDOMStreamReader(actualBody));
message.setContent(InputStream.class, null);
}
}
}
use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.
the class AbstractXmlEncInHandler method decryptContent.
public void decryptContent(Message message) {
Message outMs = message.getExchange().getOutMessage();
Message inMsg = outMs == null ? message : outMs.getExchange().getInMessage();
Document doc = getDocument(inMsg);
if (doc == null) {
return;
}
Element root = doc.getDocumentElement();
byte[] symmetricKeyBytes = getSymmetricKeyBytes(message, root);
String symKeyAlgo = getEncodingMethodAlgorithm(root);
if (encProps != null && encProps.getEncryptionSymmetricKeyAlgo() != null && !encProps.getEncryptionSymmetricKeyAlgo().equals(symKeyAlgo)) {
throwFault("Encryption Symmetric Key Algorithm is not supported", null);
}
byte[] decryptedPayload = null;
try {
decryptedPayload = decryptPayload(root, symmetricKeyBytes, symKeyAlgo);
} catch (Exception ex) {
throwFault("Payload can not be decrypted", ex);
}
Document payloadDoc = null;
try {
payloadDoc = StaxUtils.read(new InputStreamReader(new ByteArrayInputStream(decryptedPayload), StandardCharsets.UTF_8));
} catch (Exception ex) {
throwFault("Payload document can not be created", ex);
}
message.setContent(XMLStreamReader.class, new W3CDOMStreamReader(payloadDoc));
message.setContent(InputStream.class, null);
}
use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.
the class AbstractXmlSecInHandler method getDocument.
protected Document getDocument(Message message) {
if (isServerGet(message)) {
return null;
}
Integer responseCode = (Integer) message.get(Message.RESPONSE_CODE);
if (responseCode != null && responseCode != 200) {
return null;
}
Document doc = null;
InputStream is = message.getContent(InputStream.class);
if (is != null) {
try {
doc = StaxUtils.read(new InputStreamReader(is, StandardCharsets.UTF_8));
} catch (Exception ex) {
throwFault("Invalid XML payload", ex);
}
} else {
XMLStreamReader reader = message.getContent(XMLStreamReader.class);
if (reader instanceof W3CDOMStreamReader) {
doc = ((W3CDOMStreamReader) reader).getDocument();
}
}
if (doc == null && !allowEmptyBody) {
throwFault("No payload is available", null);
}
return doc;
}
Aggregations