use of javax.xml.soap.SOAPEnvelope 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;
}
use of javax.xml.soap.SOAPEnvelope in project nhin-d by DirectProject.
the class RepositorySOAPHandler method handleMessage.
/**
* This method handles the incoming and outgoing SOAP-Message. It's an
* excellent point to manipulate the SOAP.
*
* @param SOAPMessageContext
* The SOAPMessageContext object.
*
* @return true if successful handling, false otherwise.
*/
@Override
public boolean handleMessage(SOAPMessageContext context) {
//Inquire incoming or outgoing message.
boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
try {
if (outbound) {
getHeaderData();
SOAPMessage msg = ((SOAPMessageContext) context).getMessage();
// dumpSOAPMessage(msg);
SOAPPart sp = msg.getSOAPPart();
// edit Envelope
SOAPEnvelope env = sp.getEnvelope();
SOAPHeader sh = env.addHeader();
@SuppressWarnings("unused") SOAPBody sb = env.getBody();
if (action != null) {
QName qname = new QName("http://www.w3.org/2005/08/addressing", "Action");
SOAPHeaderElement saction = sh.addHeaderElement(qname);
boolean must = true;
saction.setMustUnderstand(must);
saction.setValue(action);
}
if (relatesTo != null) {
QName qname = new QName("http://www.w3.org/2005/08/addressing", "RelatesTo");
SOAPHeaderElement relates = sh.addHeaderElement(qname);
relates.setValue(relatesTo);
}
if (from != null) {
QName qname = new QName("http://www.w3.org/2005/08/addressing", "From");
QName child = new QName("http://www.w3.org/2005/08/addressing", "Address");
SOAPHeaderElement efrom = sh.addHeaderElement(qname);
SOAPElement address = efrom.addChildElement(child);
address.setValue(from);
}
if (messageId != null) {
QName qname = new QName("http://www.w3.org/2005/08/addressing", "MessageID");
SOAPHeaderElement message = sh.addHeaderElement(qname);
message.setValue(messageId);
}
if (to != null) {
QName qname = new QName("http://www.w3.org/2005/08/addressing", "To");
SOAPHeaderElement sto = sh.addHeaderElement(qname);
sto.setValue(to);
}
} else {
//should not be any inbound
}
} catch (Exception e) {
LOGGER.error("Error handling SOAP message", e);
return false;
}
return true;
}
use of javax.xml.soap.SOAPEnvelope in project cxf by apache.
the class SAAJInInterceptor method handleMessage.
@SuppressWarnings("unchecked")
public void handleMessage(SoapMessage message) throws Fault {
if (isGET(message)) {
return;
}
Boolean bodySet = (Boolean) message.get(BODY_FILLED_IN);
if (Boolean.TRUE.equals(bodySet)) {
return;
}
message.put(BODY_FILLED_IN, Boolean.TRUE);
try {
SOAPMessage soapMessage = message.getContent(SOAPMessage.class);
if (soapMessage == null) {
MessageFactory factory = preInterceptor.getFactory(message);
soapMessage = factory.createMessage();
message.setContent(SOAPMessage.class, soapMessage);
}
XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
if (xmlReader == null) {
return;
}
final SOAPPart part = soapMessage.getSOAPPart();
Document node = (Document) message.getContent(Node.class);
if (node != part && node != null) {
StaxUtils.copy(node, new SAAJStreamWriter(part));
} else {
SOAPEnvelope env = soapMessage.getSOAPPart().getEnvelope();
if (node == null) {
adjustPrefixes(env, (String) message.get(ReadHeadersInterceptor.ENVELOPE_PREFIX), (String) message.get(ReadHeadersInterceptor.BODY_PREFIX));
}
List<XMLEvent> events = (List<XMLEvent>) message.get(ReadHeadersInterceptor.ENVELOPE_EVENTS);
applyEvents(events, env);
SOAPBody body = soapMessage.getSOAPBody();
events = (List<XMLEvent>) message.get(ReadHeadersInterceptor.BODY_EVENTS);
applyEvents(events, body);
}
message.setContent(Node.class, soapMessage.getSOAPPart());
Collection<Attachment> atts = message.getAttachments();
if (atts != null) {
for (Attachment a : atts) {
if (a.getDataHandler().getDataSource() instanceof AttachmentDataSource) {
try {
((AttachmentDataSource) a.getDataHandler().getDataSource()).cache(message);
} catch (IOException e) {
throw new Fault(e);
}
}
AttachmentPart ap = soapMessage.createAttachmentPart(a.getDataHandler());
Iterator<String> i = a.getHeaderNames();
while (i != null && i.hasNext()) {
String h = i.next();
String val = a.getHeader(h);
ap.addMimeHeader(h, val);
}
if (StringUtils.isEmpty(ap.getContentId())) {
ap.setContentId(a.getId());
}
soapMessage.addAttachmentPart(ap);
}
}
// replace header element if necessary
if (message.hasHeaders()) {
replaceHeaders(soapMessage, message);
}
if (soapMessage.getSOAPPart().getEnvelope().getHeader() == null) {
soapMessage.getSOAPPart().getEnvelope().addHeader();
}
// If we have an xmlReader that already is counting the attributes and such
// then we don't want to rely on the system level defaults in StaxUtils.copy
// CXF-6173
boolean secureReader = StaxUtils.isSecureReader(xmlReader, message);
StaxUtils.copy(xmlReader, new SAAJStreamWriter(soapMessage.getSOAPPart(), soapMessage.getSOAPPart().getEnvelope().getBody()), true, !secureReader);
DOMSource bodySource = new DOMSource(soapMessage.getSOAPPart().getEnvelope().getBody());
xmlReader = StaxUtils.createXMLStreamReader(bodySource);
xmlReader.nextTag();
// move past body tag
xmlReader.nextTag();
message.setContent(XMLStreamReader.class, xmlReader);
} catch (SOAPException soape) {
throw new SoapFault(new org.apache.cxf.common.i18n.Message("SOAPHANDLERINTERCEPTOR_EXCEPTION", BUNDLE), soape, message.getVersion().getSender());
} catch (XMLStreamException e) {
throw new SoapFault(new org.apache.cxf.common.i18n.Message("SOAPHANDLERINTERCEPTOR_EXCEPTION", BUNDLE), e, message.getVersion().getSender());
}
}
use of javax.xml.soap.SOAPEnvelope in project cxf by apache.
the class StaxSerializer method deserialize.
private Node deserialize(Node ctx, XMLStreamReader reader, boolean wrapped) throws XMLEncryptionException {
Document contextDocument = null;
if (Node.DOCUMENT_NODE == ctx.getNodeType()) {
contextDocument = (Document) ctx;
} else {
contextDocument = ctx.getOwnerDocument();
}
XMLStreamWriter writer = null;
try {
if (ctx instanceof SOAPElement) {
SOAPElement el = (SOAPElement) ctx;
while (el != null && !(el instanceof SOAPEnvelope)) {
el = el.getParentElement();
}
// cannot load into fragment due to a ClassCastException within SAAJ addChildElement
// which only checks for Document as parent, not DocumentFragment
Element element = ctx.getOwnerDocument().createElementNS("dummy", "dummy");
writer = new SAAJStreamWriter((SOAPEnvelope) el, element);
return appendNewChild(reader, wrapped, contextDocument, writer, element);
}
if (DOMUtils.isJava9SAAJ()) {
// cannot load into fragment due to a ClassCastException within SAAJ addChildElement
// which only checks for Document as parent, not DocumentFragment
Element element = ctx.getOwnerDocument().createElementNS("dummy", "dummy");
writer = new OverlayW3CDOMStreamWriter(ctx.getOwnerDocument(), element);
return appendNewChild(reader, wrapped, contextDocument, writer, element);
}
// Import to a dummy fragment
DocumentFragment dummyFragment = contextDocument.createDocumentFragment();
writer = StaxUtils.createXMLStreamWriter(new DOMResult(dummyFragment));
StaxUtils.copy(reader, writer);
if (wrapped) {
DocumentFragment result = contextDocument.createDocumentFragment();
Node child = dummyFragment.getFirstChild().getFirstChild();
if (child != null && child.getNextSibling() == null) {
return child;
}
while (child != null) {
Node nextChild = child.getNextSibling();
result.appendChild(child);
child = nextChild;
}
dummyFragment = result;
}
return dummyFragment;
} catch (XMLStreamException ex) {
throw new XMLEncryptionException(ex);
}
}
use of javax.xml.soap.SOAPEnvelope in project cxf by apache.
the class CryptoCoverageChecker method handleMessage.
/**
* Checks that the WSS4J results refer to the required signed/encrypted
* elements as defined by the XPath expressions in {@link #xPaths}.
*
* @param message
* the SOAP message containing the signature
*
* @throws SoapFault
* if there is an error evaluating an XPath or an element is not
* covered by the required cryptographic operation
*/
public void handleMessage(SoapMessage message) throws Fault {
if (this.xPaths == null || this.xPaths.isEmpty()) {
// return
}
if (message.getContent(SOAPMessage.class) == null) {
throw new SoapFault("Error obtaining SOAP document", Fault.FAULT_CODE_CLIENT);
}
Element documentElement = null;
try {
SOAPMessage saajDoc = message.getContent(SOAPMessage.class);
SOAPEnvelope envelope = saajDoc.getSOAPPart().getEnvelope();
if (!checkFaults && envelope.getBody().hasFault()) {
return;
}
documentElement = envelope;
documentElement = (Element) DOMUtils.getDomElement(documentElement);
} catch (SOAPException e) {
throw new SoapFault("Error obtaining SOAP document", Fault.FAULT_CODE_CLIENT);
}
final Collection<WSDataRef> signed = new HashSet<>();
final Collection<WSDataRef> encrypted = new HashSet<>();
List<WSHandlerResult> results = CastUtils.cast((List<?>) message.get(WSHandlerConstants.RECV_RESULTS));
// Get all encrypted and signed references
if (results != null) {
for (WSHandlerResult wshr : results) {
List<WSSecurityEngineResult> signedResults = wshr.getActionResults().get(WSConstants.SIGN);
if (signedResults != null) {
for (WSSecurityEngineResult signedResult : signedResults) {
List<WSDataRef> sl = CastUtils.cast((List<?>) signedResult.get(WSSecurityEngineResult.TAG_DATA_REF_URIS));
if (sl != null) {
if (sl.size() == 1 && sl.get(0).getName().equals(new QName(WSS4JConstants.SIG_NS, WSS4JConstants.SIG_LN))) {
// endorsing the signature so don't include
continue;
}
signed.addAll(sl);
}
}
}
List<WSSecurityEngineResult> encryptedResults = wshr.getActionResults().get(WSConstants.ENCR);
if (encryptedResults != null) {
for (WSSecurityEngineResult encryptedResult : encryptedResults) {
List<WSDataRef> el = CastUtils.cast((List<?>) encryptedResult.get(WSSecurityEngineResult.TAG_DATA_REF_URIS));
if (el != null) {
encrypted.addAll(el);
}
}
}
}
}
CryptoCoverageUtil.reconcileEncryptedSignedRefs(signed, encrypted);
// XPathFactory and XPath are not thread-safe so we must recreate them
// each request.
final XPathFactory factory = XPathFactory.newInstance();
final XPath xpath = factory.newXPath();
if (this.prefixMap != null) {
xpath.setNamespaceContext(new MapNamespaceContext(this.prefixMap));
}
for (XPathExpression xPathExpression : this.xPaths) {
Collection<WSDataRef> refsToCheck = null;
switch(xPathExpression.getType()) {
case SIGNED:
refsToCheck = signed;
break;
case ENCRYPTED:
refsToCheck = encrypted;
break;
default:
throw new IllegalStateException("Unexpected crypto type: " + xPathExpression.getType());
}
try {
CryptoCoverageUtil.checkCoverage(documentElement, refsToCheck, xpath, Arrays.asList(xPathExpression.getXPath()), xPathExpression.getType(), xPathExpression.getScope());
} catch (WSSecurityException e) {
throw new SoapFault("No " + xPathExpression.getType() + " element found matching XPath " + xPathExpression.getXPath(), Fault.FAULT_CODE_CLIENT);
}
}
}
Aggregations