use of javax.xml.soap.SOAPElement in project nhin-d by DirectProject.
the class DirectSOAPHandler 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 for successful handling, false otherwise.
*/
@Override
public boolean handleMessage(SOAPMessageContext context) {
LOGGER.info("Entering DirectSOAPHandler.handleMessage(SOAPMessageContext)");
// Inquire incoming or outgoing message.
boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
try {
if (outbound) {
LOGGER.info("Handling an outbound message");
boolean isACK = !context.containsKey(ENDPOINT_ADDRESS);
SafeThreadData threadData = SafeThreadData.GetThreadInstance(Thread.currentThread().getId());
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();
try {
if (threadData.getAction() != 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(threadData.getAction());
}
if (threadData.getRelatesTo() != null) {
QName qname = new QName("http://www.w3.org/2005/08/addressing", "RelatesTo");
SOAPHeaderElement relates = sh.addHeaderElement(qname);
relates.setValue(threadData.getRelatesTo());
}
if (threadData.getFrom() != 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(threadData.getFrom());
}
if (threadData.getMessageId() != null) {
QName qname = new QName("http://www.w3.org/2005/08/addressing", "MessageID");
SOAPHeaderElement message = sh.addHeaderElement(qname);
message.setValue(threadData.getMessageId());
}
if (threadData.getTo() != null) {
QName qname = new QName("http://www.w3.org/2005/08/addressing", "To");
SOAPHeaderElement sto = sh.addHeaderElement(qname);
sto.setValue(threadData.getTo());
}
SOAPHeaderElement directHeader = sh.addHeaderElement(new QName("urn:direct:addressing", "addressBlock"));
directHeader.setPrefix("direct");
directHeader.setRole("urn:direct:addressing:destination");
directHeader.setRelay(true);
if (StringUtils.isNotBlank(threadData.getDirectFrom())) {
SOAPElement directFromElement = directHeader.addChildElement(new QName("from"));
directFromElement.setPrefix("direct");
URI uri = new URI(threadData.getDirectFrom());
directFromElement.setValue((new URI("mailto", uri.getSchemeSpecificPart(), null)).toString());
}
if (StringUtils.isNotBlank(threadData.getDirectTo())) {
/**
* consider multiple recipients
*/
String[] directTos = threadData.getDirectTo().split(";");
for (String directToAddr : directTos) {
SOAPElement directToElement = directHeader.addChildElement(new QName("to"));
directToElement.setPrefix("direct");
URI uri = new URI(directToAddr);
directToElement.setValue((new URI("mailto", uri.getSchemeSpecificPart(), null)).toString());
}
}
SOAPElement directMetadataLevelElement = directHeader.addChildElement(new QName("metadata-level"));
directMetadataLevelElement.setPrefix("direct");
directMetadataLevelElement.setValue(MetadataLevelEnum.MINIMAL.getLevel());
} catch (Throwable tb) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Failed to write SOAP Header", tb);
} else {
LOGGER.error("Failed to write SOAP Header: " + tb.getMessage());
}
}
if (isACK) {
SafeThreadData.clean(Thread.currentThread().getId());
}
} else {
LOGGER.info("Handling an inbound message");
SOAPMessage msg = ((SOAPMessageContext) context).getMessage();
boolean isResponse = isResponse(msg);
if (!isResponse) {
// Issue 249 - before handling the inbound case, we should clear
// out the old thread data if we don't this the To: (SMTP recipients) will
// append from the previous thread data
SafeThreadData.clean(Thread.currentThread().getId());
}
SafeThreadData threadData = SafeThreadData.GetThreadInstance(Thread.currentThread().getId());
ServletRequest sr = (ServletRequest) context.get(MessageContext.SERVLET_REQUEST);
if (sr != null) {
threadData.setRemoteHost(sr.getRemoteHost());
threadData.setThisHost(sr.getServerName());
threadData.setPid(getPID());
}
SOAPPart sp = msg.getSOAPPart();
// edit Envelope
SOAPEnvelope env = sp.getEnvelope();
SOAPHeader sh = env.getHeader();
@SuppressWarnings("unchecked") Iterator<Node> it = sh.extractAllHeaderElements();
while (it.hasNext()) {
try {
Node header = it.next();
if (StringUtils.contains(header.toString(), "MessageID")) {
threadData.setMessageId(header.getTextContent());
} else if (StringUtils.contains(header.toString(), "Action")) {
threadData.setAction(header.getTextContent());
} else if (StringUtils.contains(header.toString(), "RelatesTo")) {
threadData.setRelatesTo(header.getTextContent());
} else if (StringUtils.contains(header.toString(), "ReplyTo")) {
NodeList reps = header.getChildNodes();
for (int i = 0; i < reps.getLength(); i++) {
Node address = reps.item(i);
if (StringUtils.contains(address.getNodeName(), "Address")) {
threadData.setEndpoint(address.getTextContent());
}
}
} else if (StringUtils.contains(header.toString(), "From")) {
NodeList reps = header.getChildNodes();
for (int i = 0; i < reps.getLength(); i++) {
Node address = reps.item(i);
if (StringUtils.contains(address.getNodeName(), "Address")) {
threadData.setFrom(address.getTextContent());
}
}
} else if (// must be after ReplyTo
StringUtils.contains(header.toString(), "To")) {
threadData.setTo(header.getTextContent());
} else if (StringUtils.contains(header.toString(), "addressBlock")) {
NodeList childNodes = header.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node node = childNodes.item(i);
if (StringUtils.contains(node.getNodeName(), "from")) {
threadData.setDirectFrom(node.getTextContent());
} else if (StringUtils.contains(node.getNodeName(), "to")) {
// XDR-MULTIPLE-RECIPIENT-ISSUE - this is the part where old thread data
// gets into the To: and will cause unwanted recipients
// (see above for the clear)
String recipient = node.getTextContent();
if (threadData.getDirectTo() == null) {
threadData.setDirectTo(recipient);
} else {
/**
* if multiple recipients, save addresses in one parameters separate by (;)
*/
threadData.setDirectTo(threadData.getDirectTo() + ";" + recipient);
}
} else if (StringUtils.contains(node.getNodeName(), "metadata-level")) {
threadData.setDirectMetadataLevel(node.getTextContent());
}
}
}
} catch (Throwable tb) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Failed to read input parameter.", tb);
} else {
LOGGER.error("Failed to read input parameter.");
}
}
}
threadData.save();
}
} catch (Exception e) {
LOGGER.warn("Error handling SOAP message.", e);
return false;
}
return true;
}
use of javax.xml.soap.SOAPElement in project jdk8u_jdk by JetBrains.
the class SaajEmptyNamespaceTest method testAddElementToNullNsNoDeclarations.
/*
* Test that adding element with explicitly null namespace URI shall put the
* element into global namespace. Namespace declarations are not added explicitly.
*/
@Test
public void testAddElementToNullNsNoDeclarations() throws Exception {
// Create empty SOAP message
SOAPMessage msg = createSoapMessage();
SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
// Add elements
SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);
SOAPElement childGlobalNS = parentExplicitNS.addChildElement("global-child", "", null);
SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");
// Check namespace URIs
Assert.assertNull(childGlobalNS.getNamespaceURI());
Assert.assertEquals(childDefaultNS.getNamespaceURI(), TEST_NS);
}
use of javax.xml.soap.SOAPElement in project jdk8u_jdk by JetBrains.
the class SaajEmptyNamespaceTest method testAddElementToGlobalNs.
/*
* Test that adding element with explicitly empty namespace URI shall put
* the element into global namespace.
*/
@Test
public void testAddElementToGlobalNs() throws Exception {
// Create empty SOAP message
SOAPMessage msg = createSoapMessage();
SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
// Add elements
SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);
parentExplicitNS.addNamespaceDeclaration("", TEST_NS);
SOAPElement childGlobalNS = parentExplicitNS.addChildElement("global-child", "", "");
childGlobalNS.addNamespaceDeclaration("", "");
SOAPElement grandChildGlobalNS = childGlobalNS.addChildElement("global-grand-child");
SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");
// Check namespace URIs
Assert.assertNull(childGlobalNS.getNamespaceURI());
Assert.assertNull(grandChildGlobalNS.getNamespaceURI());
Assert.assertEquals(childDefaultNS.getNamespaceURI(), TEST_NS);
}
use of javax.xml.soap.SOAPElement in project jdk8u_jdk by JetBrains.
the class SaajEmptyNamespaceTest method testAddElementToNullNsQName.
/*
* Test that adding element with explicitly empty namespace URI set via QName
* shall put the element into global namespace.
*/
@Test
public void testAddElementToNullNsQName() throws Exception {
// Create empty SOAP message
SOAPMessage msg = createSoapMessage();
SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
// Add elements
SOAPElement parentExplicitNS = body.addChildElement("content", "", TEST_NS);
parentExplicitNS.addNamespaceDeclaration("", TEST_NS);
SOAPElement childGlobalNS = parentExplicitNS.addChildElement(new QName(null, "global-child"));
childGlobalNS.addNamespaceDeclaration("", "");
SOAPElement grandChildGlobalNS = childGlobalNS.addChildElement("global-grand-child");
SOAPElement childDefaultNS = parentExplicitNS.addChildElement("default-child");
// Check namespace URIs
Assert.assertNull(childGlobalNS.getNamespaceURI());
Assert.assertNull(grandChildGlobalNS.getNamespaceURI());
Assert.assertEquals(childDefaultNS.getNamespaceURI(), TEST_NS);
}
use of javax.xml.soap.SOAPElement in project OpenAM by OpenRock.
the class SOAPCommunicator method createSOAPFault.
/**
* Forms a SOAP Fault and puts it in the SOAP Message Body.
*
* @param faultCode Fault code.
* @param faultString Fault string.
* @param detail Fault details.
* @return SOAP Fault in the SOAP Message Body or null if unable to generate the message.
*/
public SOAPMessage createSOAPFault(final String faultCode, final String faultString, final String detail) {
try {
SOAPMessage message = messageFactory.createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
SOAPFault fault = envelope.getBody().addFault();
fault.setFaultCode(envelope.createName(faultCode, null, SOAPConstants.URI_NS_SOAP_ENVELOPE));
fault.setFaultString(SAML2Utils.bundle.getString(faultString));
if (StringUtils.isNotEmpty(detail)) {
Detail faultDetail = fault.addDetail();
SOAPElement faultDetailEntry = (SOAPElement) faultDetail.addDetailEntry(envelope.createName("Problem"));
faultDetailEntry.addAttribute(envelope.createName("details"), SAML2Utils.bundle.getString(detail));
}
return message;
} catch (SOAPException e) {
debug.error("createSOAPFault:", e);
return null;
}
}
Aggregations