Search in sources :

Example 31 with MessageFactory

use of javax.xml.soap.MessageFactory in project camel by apache.

the class Client method invoke.

public String invoke() throws Exception {
    // Service Qname as defined in the WSDL.
    QName serviceName = new QName("http://apache.org/hello_world_soap_http", "SOAPService");
    // Port QName as defined in the WSDL.
    QName portName = new QName("http://apache.org/hello_world_soap_http", "SoapOverHttpRouter");
    // Create a dynamic Service instance
    Service service = Service.create(serviceName);
    // Add a port to the Service
    service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
    // Create a dispatch instance
    Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
    // Use Dispatch as BindingProvider
    BindingProvider bp = dispatch;
    MessageFactory factory = ((SOAPBinding) bp.getBinding()).getMessageFactory();
    // Create SOAPMessage Request
    SOAPMessage request = factory.createMessage();
    // Request Body
    SOAPBody body = request.getSOAPBody();
    // Compose the soap:Body payload
    QName payloadName = new QName("http://apache.org/hello_world_soap_http/types", "greetMe", "ns1");
    SOAPBodyElement payload = body.addBodyElement(payloadName);
    SOAPElement message = payload.addChildElement("requestType");
    message.addTextNode("Hello Camel!!");
    System.out.println("Send out the request: Hello Camel!!");
    // Invoke the endpoint synchronously
    // Invoke endpoint operation and read response
    SOAPMessage reply = dispatch.invoke(request);
    // process the reply
    body = reply.getSOAPBody();
    QName responseName = new QName("http://apache.org/hello_world_soap_http/types", "greetMeResponse");
    SOAPElement bodyElement = (SOAPElement) body.getChildElements(responseName).next();
    String responseMessageText = bodyElement.getTextContent();
    System.out.println("Get the response: " + responseMessageText);
    return responseMessageText;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) MessageFactory(javax.xml.soap.MessageFactory) QName(javax.xml.namespace.QName) SOAPElement(javax.xml.soap.SOAPElement) Service(javax.xml.ws.Service) SOAPBinding(javax.xml.ws.soap.SOAPBinding) BindingProvider(javax.xml.ws.BindingProvider) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPBodyElement(javax.xml.soap.SOAPBodyElement)

Example 32 with MessageFactory

use of javax.xml.soap.MessageFactory in project openhab1-addons by openhab.

the class Tr064Comm method constructTr064Msg.

/***
     * sets all required namespaces and prepares the SOAP message to send
     * creates skeleton + body data
     *
     * @param bodyData is attached to skeleton to form entire SOAP message
     * @return ready to send SOAP message
     */
private SOAPMessage constructTr064Msg(SOAPBodyElement bodyData) {
    SOAPMessage soapMsg = null;
    try {
        MessageFactory msgFac;
        msgFac = MessageFactory.newInstance();
        soapMsg = msgFac.createMessage();
        soapMsg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
        soapMsg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
        SOAPPart part = soapMsg.getSOAPPart();
        // valid for entire SOAP msg
        String namespace = "s";
        // create suitable fbox envelope
        SOAPEnvelope envelope = part.getEnvelope();
        envelope.setPrefix(namespace);
        // delete standard namespace which was already set
        envelope.removeNamespaceDeclaration("SOAP-ENV");
        envelope.addNamespaceDeclaration(namespace, "http://schemas.xmlsoap.org/soap/envelope/");
        Name nEncoding = envelope.createName("encodingStyle", namespace, "http://schemas.xmlsoap.org/soap/encoding/");
        envelope.addAttribute(nEncoding, "http://schemas.xmlsoap.org/soap/encoding/");
        // create empty header
        SOAPHeader header = envelope.getHeader();
        header.setPrefix(namespace);
        // create body with command based on parameter
        SOAPBody body = envelope.getBody();
        body.setPrefix(namespace);
        // bodyData already prepared. Needs only be added
        body.addChildElement(bodyData);
    } catch (Exception e) {
        logger.error("Error creating SOAP message for fbox request with data {}", bodyData);
        e.printStackTrace();
    }
    return soapMsg;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) MessageFactory(javax.xml.soap.MessageFactory) SOAPPart(javax.xml.soap.SOAPPart) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPHeader(javax.xml.soap.SOAPHeader) XPathExpressionException(javax.xml.xpath.XPathExpressionException) URISyntaxException(java.net.URISyntaxException) SOAPException(javax.xml.soap.SOAPException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) Name(javax.xml.soap.Name) QName(javax.xml.namespace.QName)

Example 33 with MessageFactory

use of javax.xml.soap.MessageFactory in project nhin-d by DirectProject.

the class DirectSOAPHandlerTest method testGetMessageEncoding.

/**
     * Test the getMessageEncoding method.
     */
public void testGetMessageEncoding() {
    String output = null;
    SOAPMessage message = null;
    DirectSOAPHandler handler = new DirectSOAPHandler();
    try {
        MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
        message = mf.createMessage();
        message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "ISO-8859-1");
        assertNotNull("Test setup failed", message.getProperty(SOAPMessage.CHARACTER_SET_ENCODING));
        output = handler.getMessageEncoding(message);
        assertEquals("Message encoding does not match expected", "ISO-8859-1", output);
        message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, null);
        assertNull("Test setup failed", message.getProperty(SOAPMessage.CHARACTER_SET_ENCODING));
        output = handler.getMessageEncoding(message);
        assertEquals("Message encoding does not match expected", "utf-8", output);
    } catch (Exception e) {
        fail("Exception thrown during mock SOAPMessage creation/handling.");
        e.printStackTrace();
    }
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 34 with MessageFactory

use of javax.xml.soap.MessageFactory in project OpenAM by OpenRock.

the class FSSOAPReceiver method handleLogoutRequest.

/**
     * Initiates the processing of the logout request received from a remote
     * trusted provider.
     * @param elt containing the logout request in the XML message
     * @param logoutRequest logout notification
     * @param msgLogout logout message
     * @param request http request object
     * @param response http response object
     * @param hostedProviderDesc hosted provider meta descriptor
     * @param hostedConfig hosted provider's extended meta
     * @param providerAlias hosted provider's meta alias
     * @param realm The realm under which the entity resides.
     * @param hostedEntityId hosted provider's entity ID
     * @param hostedRole hosted provider's role
     * @return null if error in processing, or Map containing two
     * keys, MESSAGE for SOAPMessage object and USERID for userID string
     */
private Map handleLogoutRequest(Element elt, FSLogoutNotification logoutRequest, SOAPMessage msgLogout, HttpServletRequest request, HttpServletResponse response, ProviderDescriptorType hostedProviderDesc, BaseConfigType hostedConfig, String providerAlias, String realm, String hostedEntityId, String hostedRole) {
    try {
        String remoteEntityId = logoutRequest.getProviderId();
        ProviderDescriptorType remoteDesc = getRemoteProviderDescriptor(hostedRole, remoteEntityId, realm);
        if (remoteDesc == null) {
            return null;
        }
        boolean isIDP = false;
        if (hostedRole.equalsIgnoreCase(IFSConstants.SP)) {
            isIDP = true;
        }
        X509Certificate remoteCert = KeyUtil.getVerificationCert(remoteDesc, remoteEntityId, isIDP);
        if (!FSServiceUtils.isSigningOn() || verifyRequestSignature(elt, msgLogout, remoteCert)) {
            FSUtils.debug.message("Logout Signature successfully verified");
            if (providerAlias == null || providerAlias.length() < 1) {
                FSUtils.debug.message("Unable to retrieve alias, " + "Hosted Provider Cannot process logout request");
                return null;
            }
            if (FSUtils.debug.messageEnabled()) {
                FSUtils.debug.message("FSSOAPReceiver:handleLogoutRequest: " + "Completed forming request FSLogoutNotification");
            }
            IDFFMetaManager metaManager = FSUtils.getIDFFMetaManager();
            if (metaManager.isTrustedProvider(realm, hostedEntityId, remoteEntityId)) {
                String userID = FSLogoutUtil.getUserFromRequest(logoutRequest, realm, hostedEntityId, hostedRole, hostedConfig, providerAlias);
                if (FSUtils.debug.messageEnabled()) {
                    FSUtils.debug.message("FSSOAPReceiver:handleLogoutRequest" + " found user Id = " + userID);
                }
                // Call SP Adapter preSingleLogoutProcess for IDP/SOAP
                if (hostedRole != null && hostedRole.equalsIgnoreCase(IFSConstants.SP)) {
                    FederationSPAdapter spAdapter = FSServiceUtils.getSPAdapter(hostedEntityId, hostedConfig);
                    if (spAdapter != null) {
                        if (FSUtils.debug.messageEnabled()) {
                            FSUtils.debug.message("FSSOAPReceiver, " + "call preSingleLogoutProcess, IDP/SOAP");
                        }
                        try {
                            spAdapter.preSingleLogoutProcess(hostedEntityId, request, response, userID, logoutRequest, null, IFSConstants.LOGOUT_IDP_SOAP_PROFILE);
                        } catch (Exception e) {
                            // ignore adapter process error
                            FSUtils.debug.error("preSingleLogoutProcess." + "IDP/SOAP", e);
                        }
                    }
                }
                // TODO : change to use FSLogoutUtil.liveConnectionsExist
                if (!isUserExists(userID, providerAlias)) {
                    //to do the cleanup
                    if (FSUtils.debug.messageEnabled()) {
                        FSUtils.debug.message("FSSOAPReceiver:handleLogoutRequest: User " + "does not exist locally. Finding remotely");
                    }
                    List platformList = null;
                    try {
                        platformList = SystemConfigurationUtil.getServerList();
                    } catch (SystemConfigurationException se) {
                        if (FSUtils.debug.messageEnabled()) {
                            FSUtils.debug.message("FSSOAPReceiver:handleLogoutRequest: " + "Couldn't find remote server:", se);
                        }
                    }
                    if (platformList == null) {
                        if (FSUtils.debug.messageEnabled()) {
                            FSUtils.debug.message("FSSOAPReceiver:handleLogoutRequest" + "platformList is null");
                        }
                        return null;
                    }
                    Iterator iter = platformList.iterator();
                    while (iter.hasNext()) {
                        String remoteServerURL = (String) iter.next();
                        StringBuffer tmpremoteURL = new StringBuffer(remoteServerURL);
                        tmpremoteURL.append(SystemConfigurationUtil.getProperty("com.iplanet.am.services." + "deploymentDescriptor"));
                        if (FSUtils.debug.messageEnabled()) {
                            FSUtils.debug.message("FSSOAPReceiver:handleLogoutRequest" + "remoteServerURL = " + remoteServerURL + " and self serverUrl =" + FSServiceUtils.getBaseURL());
                        }
                        if ((FSServiceUtils.getBaseURL()).equalsIgnoreCase(remoteServerURL.toString())) {
                            continue;
                        }
                        FSAssertionManagerClient amc = new FSAssertionManagerClient(providerAlias, getFullServiceURL(remoteServerURL));
                        if (amc.isUserExists(userID)) {
                            if (FSUtils.debug.messageEnabled()) {
                                FSUtils.debug.message("FSSOAPReceiver:handleLogoutRequest" + "user found here =" + remoteServerURL);
                            }
                            StringBuffer remoteURL = new StringBuffer();
                            remoteURL.append(remoteServerURL.toString()).append(SystemConfigurationUtil.getProperty("com.iplanet.am.services." + "deploymentDescriptor")).append(IFSConstants.SOAP_END_POINT_VALUE).append("/").append(IFSConstants.META_ALIAS).append(providerAlias);
                            FSSOAPService instSOAP = FSSOAPService.getInstance();
                            SOAPMessage retSOAPMessage = null;
                            if (instSOAP != null) {
                                try {
                                    if (FSUtils.debug.messageEnabled()) {
                                        FSUtils.debug.message("Forward logout request to " + remoteURL.toString());
                                    }
                                    retSOAPMessage = instSOAP.sendMessage(msgLogout, remoteURL.toString());
                                    if (retSOAPMessage != null) {
                                        Map map = new HashMap();
                                        map.put(MESSAGE, retSOAPMessage);
                                        if (userID != null) {
                                            map.put(USERID, userID);
                                        }
                                        return map;
                                    } else {
                                        return null;
                                    }
                                } catch (SOAPException e) {
                                    FSUtils.debug.error("FSSOAPException in doSOAPProfile" + " Cannot send request", e);
                                    return null;
                                }
                            } else {
                                return null;
                            }
                        }
                    }
                }
                FSServiceManager instService = FSServiceManager.getInstance();
                if (instService != null) {
                    FSPreLogoutHandler logoutHandler = instService.getPreLogoutHandler();
                    if (logoutHandler != null) {
                        logoutHandler.setHostedDescriptor(hostedProviderDesc);
                        logoutHandler.setHostedDescriptorConfig(hostedConfig);
                        logoutHandler.setHostedEntityId(hostedEntityId);
                        logoutHandler.setHostedProviderRole(hostedRole);
                        logoutHandler.setMetaAlias(providerAlias);
                        logoutHandler.setRealm(realm);
                        logoutHandler.setRemoteDescriptor(remoteDesc);
                        logoutHandler.setRemoteEntityId(remoteEntityId);
                        logoutHandler.setLogoutRequest(logoutRequest);
                        FSLogoutStatus bProcessStatus = logoutHandler.processSingleLogoutRequest(logoutRequest);
                        if (bProcessStatus.getStatus().equalsIgnoreCase(IFSConstants.SAML_SUCCESS)) {
                            MessageFactory factory = MessageFactory.newInstance();
                            SOAPMessage successSOAP = factory.createMessage();
                            if (successSOAP != null) {
                                Map map = new HashMap();
                                map.put(MESSAGE, successSOAP);
                                if (userID != null) {
                                    map.put(USERID, userID);
                                }
                                return map;
                            } else {
                                return null;
                            }
                        } else if (bProcessStatus.getStatus().equalsIgnoreCase(IFSConstants.SAML_UNSUPPORTED)) {
                            SOAPMessage retSOAPMessage = soapService.formSOAPError("Server", "cannotProcessRequest", null);
                            if (retSOAPMessage != null) {
                                Map map = new HashMap();
                                map.put(MESSAGE, retSOAPMessage);
                                if (userID != null) {
                                    map.put(USERID, userID);
                                }
                                return map;
                            } else {
                                return null;
                            }
                        } else {
                            return null;
                        }
                    } else {
                        FSUtils.debug.error("Unable to get PreLogoutHandler");
                        FSUtils.debug.error("Cannot process request");
                        return null;
                    }
                } else {
                    FSUtils.debug.message("FSServiceManager instance is" + "null. Cannot process logout request");
                    return null;
                }
            }
            FSUtils.debug.message("Remote provider not in trusted list");
            return null;
        } else {
            FSUtils.debug.error("Logout Signature failed verification");
            return null;
        }
    } catch (Exception se) {
        FSUtils.debug.error("FSSOAPService::handleLogoutRequest failed", se);
        return null;
    }
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) HashMap(java.util.HashMap) ProviderDescriptorType(com.sun.identity.liberty.ws.meta.jaxb.ProviderDescriptorType) FSLogoutStatus(com.sun.identity.federation.services.logout.FSLogoutStatus) SystemConfigurationException(com.sun.identity.common.SystemConfigurationException) SOAPMessage(javax.xml.soap.SOAPMessage) X509Certificate(java.security.cert.X509Certificate) SOAPException(javax.xml.soap.SOAPException) SystemConfigurationException(com.sun.identity.common.SystemConfigurationException) IDFFMetaException(com.sun.identity.federation.meta.IDFFMetaException) FSMsgException(com.sun.identity.federation.message.common.FSMsgException) FSException(com.sun.identity.federation.common.FSException) IDFFMetaManager(com.sun.identity.federation.meta.IDFFMetaManager) SOAPException(javax.xml.soap.SOAPException) FSPreLogoutHandler(com.sun.identity.federation.services.logout.FSPreLogoutHandler) Iterator(java.util.Iterator) List(java.util.List) FederationSPAdapter(com.sun.identity.federation.plugins.FederationSPAdapter) Map(java.util.Map) HashMap(java.util.HashMap)

Example 35 with MessageFactory

use of javax.xml.soap.MessageFactory in project jdk8u_jdk by JetBrains.

the class MailTest method addSoapAttachement.

void addSoapAttachement() {
    try {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        AttachmentPart a = message.createAttachmentPart();
        a.setContentType("binary/octet-stream");
        message.addAttachmentPart(a);
    } catch (SOAPException e) {
        e.printStackTrace();
    }
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) SOAPException(javax.xml.soap.SOAPException) AttachmentPart(javax.xml.soap.AttachmentPart) SOAPMessage(javax.xml.soap.SOAPMessage)

Aggregations

MessageFactory (javax.xml.soap.MessageFactory)70 SOAPMessage (javax.xml.soap.SOAPMessage)65 URL (java.net.URL)24 InputStream (java.io.InputStream)22 QName (javax.xml.namespace.QName)22 SOAPException (javax.xml.soap.SOAPException)22 Test (org.junit.Test)21 ByteArrayInputStream (java.io.ByteArrayInputStream)19 SOAPBody (javax.xml.soap.SOAPBody)18 SOAPPart (javax.xml.soap.SOAPPart)17 StreamSource (javax.xml.transform.stream.StreamSource)14 SOAPConnection (javax.xml.soap.SOAPConnection)13 IOException (java.io.IOException)11 SOAPElement (javax.xml.soap.SOAPElement)11 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)10 JBossWSTest (org.jboss.wsf.test.JBossWSTest)10 NodeList (org.w3c.dom.NodeList)9 AttachmentPart (javax.xml.soap.AttachmentPart)8 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)8 Element (org.w3c.dom.Element)8