Search in sources :

Example 1 with SOAPMessageContext

use of javax.xml.ws.handler.soap.SOAPMessageContext 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;
}
Also used : SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) ServletRequest(javax.servlet.ServletRequest) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) URI(java.net.URI) SOAPException(javax.xml.soap.SOAPException) SOAPBody(javax.xml.soap.SOAPBody) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 2 with SOAPMessageContext

use of javax.xml.ws.handler.soap.SOAPMessageContext 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;
}
Also used : SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) QName(javax.xml.namespace.QName) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) IOException(java.io.IOException) SOAPBody(javax.xml.soap.SOAPBody) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 3 with SOAPMessageContext

use of javax.xml.ws.handler.soap.SOAPMessageContext in project narayana by jbosstm.

the class AbstractJTAOverWSATHandler method isWSATContext.

/**
 * Checks if message context contains WS-AT header element.
 *
 * @param context
 * @return true if WS-AT header element exists and false if not.
 */
private boolean isWSATContext(final MessageContext context) {
    boolean isWSATContext = false;
    if (context instanceof SOAPMessageContext) {
        final SOAPMessageContext soapMessageContext = (SOAPMessageContext) context;
        final SOAPHeaderElement soapHeaderElement = getHeaderElement(soapMessageContext, ArjunaConstants.WSARJ_NAMESPACE, ArjunaConstants.WSARJ_ELEMENT_INSTANCE_IDENTIFIER);
        isWSATContext = soapHeaderElement != null;
    }
    return isWSATContext;
}
Also used : SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext)

Example 4 with SOAPMessageContext

use of javax.xml.ws.handler.soap.SOAPMessageContext in project narayana by jbosstm.

the class JaxWSHeaderContextProcessor method handleInbound.

/**
 * Process the tx context header that is attached to the received message.
 *
 * @param msgContext
 * @return true
 */
protected boolean handleInbound(SOAPMessageContext msgContext) {
    final SOAPMessageContext soapMessageContext = (SOAPMessageContext) msgContext;
    final SOAPMessage soapMessage = soapMessageContext.getMessage();
    return handleInboundMessage(soapMessage);
}
Also used : SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 5 with SOAPMessageContext

use of javax.xml.ws.handler.soap.SOAPMessageContext in project scout.rt by eclipse.

the class WsConsumerCorrelationIdHandlerTest method testHandleInboundMessage.

@Test
public void testHandleInboundMessage() {
    // mock message context
    SOAPMessageContext ctx = mock(SOAPMessageContext.class);
    when(ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)).thenReturn(Boolean.FALSE);
    // handle outbound message
    WsConsumerCorrelationIdHandler handler = new WsConsumerCorrelationIdHandler();
    handler.handleMessage(ctx);
    // verify invocation
    verify(ctx, times(1)).get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    verifyNoMoreInteractions(ctx);
}
Also used : SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) Test(org.junit.Test)

Aggregations

SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)45 SOAPMessage (javax.xml.soap.SOAPMessage)33 WebServiceException (javax.xml.ws.WebServiceException)24 SOAPException (javax.xml.soap.SOAPException)22 SOAPElement (javax.xml.soap.SOAPElement)18 Test (org.junit.Test)13 SOAPBody (javax.xml.soap.SOAPBody)12 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)10 QName (javax.xml.namespace.QName)7 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)7 IOException (java.io.IOException)6 List (java.util.List)6 Set (java.util.Set)6 ContentType (javax.mail.internet.ContentType)6 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)6 SOAPHeaderElement (javax.xml.soap.SOAPHeaderElement)6 Handler (javax.xml.ws.handler.Handler)6 MessageContext (javax.xml.ws.handler.MessageContext)6 SOAPHandler (javax.xml.ws.handler.soap.SOAPHandler)6 ArrayList (java.util.ArrayList)5