List of usage examples for javax.xml.soap SOAPElement addTextNode
public SOAPElement addTextNode(String text) throws SOAPException;
From source file:com.qubit.solution.fenixedu.bennu.webservices.services.client.WebServiceClientHandler.java
public boolean handleMessage(SOAPMessageContext smc) { boolean isOutbound = (Boolean) smc.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY); if (isOutbound) { try {//from w w w .j ava 2 s .c om final byte[] sessionKey = generateAESKey(); final String encriptedPassword = cypher(sessionKey, password); final String encriptedTimestamp = cypher(sessionKey, getTimestamp()); final String nonce = cypherSessionKey(getPublicKey(), sessionKey); SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope(); SOAPFactory soapFactory = SOAPFactory.newInstance(); // WSSecurity <Security> header SOAPElement wsSecHeaderElm = soapFactory.createElement("Security", AUTH_PREFIX, AUTH_NS); SOAPElement userNameTokenElm = soapFactory.createElement("UsernameToken", AUTH_PREFIX, AUTH_NS); // Username SOAPElement userNameElm = soapFactory.createElement("Username", AUTH_PREFIX, AUTH_NS); userNameElm.addTextNode(username); // Password SOAPElement passwdElm = soapFactory.createElement("Password", AUTH_PREFIX, AUTH_NS); passwdElm.addTextNode(encriptedPassword); // Nonce SOAPElement nonceElm = soapFactory.createElement("Nonce", AUTH_PREFIX, AUTH_NS); nonceElm.addTextNode(nonce); // Created SOAPElement createdElm = soapFactory.createElement("Created", AUTH_PREFIX, AUTH_NS); createdElm.addTextNode(encriptedTimestamp); userNameTokenElm.addChildElement(userNameElm); userNameTokenElm.addChildElement(passwdElm); userNameTokenElm.addChildElement(nonceElm); userNameTokenElm.addChildElement(createdElm); // add child elements to the root element wsSecHeaderElm.addChildElement(userNameTokenElm); SOAPHeader sh = envelope.getHeader(); if (sh == null) { // create SOAPHeader instance for SOAP envelope sh = envelope.addHeader(); } // add SOAP element for header to SOAP header object sh.addChildElement(wsSecHeaderElm); } catch (Exception e) { throw new RuntimeException("Problems in the securityHandler", e); } } return true; }
From source file:whitelabel.cloud.wsclient.WebServiceAuthenticator.java
public void authenticateInClear(final SOAPMessage request, final String username, final String password) throws WsAuthenticationException { if (request == null) { LOG.error(" SoapMessage request not defined"); throw new WsAuthenticationException("SOAP_REQUEST_NOT_DEFINED"); }/*www . j a va2 s.c o m*/ if (username == null || password == null || username.trim().length() == 0 || password.trim().length() == 0) { LOG.error("Username: " + username + " password: " + password + " - invalid parameters"); throw new WsAuthenticationException("INVALID_PARAMETERS"); } String nonceValue = generateNonceBase64(16); String createdValue = dfe.print(new Date()); String userValue = username; String pwdValue = password; String pwdType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"; QName securityQName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "wsse"); QName usernameTokenQName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "UsernameToken", "wsse"); QName usernameQName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Username", "wsse"); QName PasswordQName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Password", "wsse"); QName PasswordTypeQName = new QName("Type"); QName nonceQName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Nonce", "wsse"); QName createdQName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Created", "wsu"); try { SOAPElement securitySoap = request.getSOAPHeader().addChildElement(securityQName); SOAPElement usernameTokenSoap = securitySoap.addChildElement(usernameTokenQName); SOAPElement usernameSoap = usernameTokenSoap.addChildElement(usernameQName); usernameSoap.addTextNode(userValue); SOAPElement passwordSoap = usernameTokenSoap.addChildElement(PasswordQName); passwordSoap.addTextNode(pwdValue); passwordSoap.addAttribute(PasswordTypeQName, pwdType); SOAPElement nonceSoap = usernameTokenSoap.addChildElement(nonceQName); nonceSoap.addTextNode(nonceValue); SOAPElement creadedSoap = usernameTokenSoap.addChildElement(createdQName); creadedSoap.addTextNode(createdValue); } catch (SOAPException se) { LOG.error(se); throw new WsAuthenticationException("SOAPHEADER_CREATION", se); } }
From source file:whitelabel.cloud.wsclient.WebServiceAuthenticator.java
public void authenticateWithDigest(final SOAPMessage request, final String username, final String password) throws WsAuthenticationException { if (request == null) { LOG.error(" SoapMessage request not defined"); throw new WsAuthenticationException("SOAP_REQUEST_NOT_DEFINED"); }/*ww w. j a va 2 s. c o m*/ if (username == null || password == null || username.trim().length() == 0 || password.trim().length() == 0) { LOG.error("Username: " + username + " password: " + password + " - invalid parameters"); throw new WsAuthenticationException("INVALID_PARAMETERS"); } String nonceValue = generateNonceBase64(16); String createdValue = dfe.print(new Date()); String userValue = username; String pwdValue = crypthPassword(nonceValue, createdValue, password); String pwdType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"; QName securityQName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "wsse"); QName usernameTokenQName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "UsernameToken", "wsse"); QName usernameQName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Username", "wsse"); QName PasswordQName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Password", "wsse"); QName PasswordTypeQName = new QName("Type"); QName nonceQName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Nonce", "wsse"); QName createdQName = new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Created", "wsu"); SOAPElement securitySoap; try { securitySoap = request.getSOAPHeader().addChildElement(securityQName); SOAPElement usernameTokenSoap = securitySoap.addChildElement(usernameTokenQName); SOAPElement usernameSoap = usernameTokenSoap.addChildElement(usernameQName); usernameSoap.addTextNode(userValue); SOAPElement passwordSoap = usernameTokenSoap.addChildElement(PasswordQName); passwordSoap.addTextNode(pwdValue); passwordSoap.addAttribute(PasswordTypeQName, pwdType); SOAPElement nonceSoap = usernameTokenSoap.addChildElement(nonceQName); nonceSoap.addTextNode(nonceValue); SOAPElement creadedSoap = usernameTokenSoap.addChildElement(createdQName); creadedSoap.addTextNode(createdValue); } catch (SOAPException se) { LOG.error(se); throw new WsAuthenticationException("SOAPHEADER_CREATION", se); } }
From source file:be.agiv.security.handler.WSAddressingHandler.java
private void handleOutboundMessage(SOAPMessageContext context) throws SOAPException { LOG.debug("adding WS-Addressing headers"); SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope(); SOAPHeader header = envelope.getHeader(); if (null == header) { header = envelope.addHeader();//from www. java 2s .c om } String wsuPrefix = null; String wsAddrPrefix = null; Iterator namespacePrefixesIter = envelope.getNamespacePrefixes(); while (namespacePrefixesIter.hasNext()) { String namespacePrefix = (String) namespacePrefixesIter.next(); String namespace = envelope.getNamespaceURI(namespacePrefix); if (WSConstants.WS_ADDR_NAMESPACE.equals(namespace)) { wsAddrPrefix = namespacePrefix; } else if (WSConstants.WS_SECURITY_UTILITY_NAMESPACE.equals(namespace)) { wsuPrefix = namespacePrefix; } } if (null == wsAddrPrefix) { wsAddrPrefix = getUniquePrefix("a", envelope); envelope.addNamespaceDeclaration(wsAddrPrefix, WSConstants.WS_ADDR_NAMESPACE); } if (null == wsuPrefix) { /* * Using "wsu" is very important for the IP-STS X509 credential. * Apparently the STS refuses when the namespace prefix of the * wsu:Id on the WS-Addressing To element is different from the * wsu:Id prefix on the WS-Security timestamp. */ wsuPrefix = "wsu"; envelope.addNamespaceDeclaration(wsuPrefix, WSConstants.WS_SECURITY_UTILITY_NAMESPACE); } SOAPFactory factory = SOAPFactory.newInstance(); SOAPHeaderElement actionHeaderElement = header .addHeaderElement(new QName(WSConstants.WS_ADDR_NAMESPACE, "Action", wsAddrPrefix)); actionHeaderElement.setMustUnderstand(true); actionHeaderElement.addTextNode(this.action); SOAPHeaderElement messageIdElement = header .addHeaderElement(new QName(WSConstants.WS_ADDR_NAMESPACE, "MessageID", wsAddrPrefix)); String messageId = "urn:uuid:" + UUID.randomUUID().toString(); context.put(MESSAGE_ID_CONTEXT_ATTRIBUTE, messageId); messageIdElement.addTextNode(messageId); SOAPHeaderElement replyToElement = header .addHeaderElement(new QName(WSConstants.WS_ADDR_NAMESPACE, "ReplyTo", wsAddrPrefix)); SOAPElement addressElement = factory.createElement("Address", wsAddrPrefix, WSConstants.WS_ADDR_NAMESPACE); addressElement.addTextNode("http://www.w3.org/2005/08/addressing/anonymous"); replyToElement.addChildElement(addressElement); SOAPHeaderElement toElement = header .addHeaderElement(new QName(WSConstants.WS_ADDR_NAMESPACE, "To", wsAddrPrefix)); toElement.setMustUnderstand(true); toElement.addTextNode(this.to); String toIdentifier = "to-id-" + UUID.randomUUID().toString(); toElement.addAttribute(new QName(WSConstants.WS_SECURITY_UTILITY_NAMESPACE, "Id", wsuPrefix), toIdentifier); try { toElement.setIdAttributeNS(WSConstants.WS_SECURITY_UTILITY_NAMESPACE, "Id", true); } catch (UnsupportedOperationException e) { // Axis2 has missing implementation of setIdAttributeNS LOG.error("error setting Id attribute: " + e.getMessage()); } context.put(TO_ID_CONTEXT_ATTRIBUTE, toIdentifier); }
From source file:cl.nic.dte.net.ConexionSii.java
@SuppressWarnings("unchecked") private RESPUESTADocument getEstadoDTE(String rutConsultante, Documento dte, String token, String urlSolicitud) throws UnsupportedOperationException, SOAPException, MalformedURLException, XmlException { String rutEmisor = dte.getEncabezado().getEmisor().getRUTEmisor(); String rutReceptor = dte.getEncabezado().getReceptor().getRUTRecep(); Integer tipoDTE = dte.getEncabezado().getIdDoc().getTipoDTE().intValue(); long folioDTE = dte.getEncabezado().getIdDoc().getFolio(); String fechaEmision = Utilities.fechaEstadoDte .format(dte.getEncabezado().getIdDoc().getFchEmis().getTime()); long montoTotal = dte.getEncabezado().getTotales().getMntTotal(); SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance(); SOAPConnection con = scFactory.createConnection(); MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); SOAPPart soapPart = message.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPHeader header = envelope.getHeader(); SOAPBody body = envelope.getBody(); header.detachNode();/* w w w.jav a2 s . c o m*/ Name bodyName = envelope.createName("getEstDte", "m", urlSolicitud); SOAPBodyElement gltp = body.addBodyElement(bodyName); Name toKname = envelope.createName("RutConsultante"); SOAPElement toKsymbol = gltp.addChildElement(toKname); toKsymbol.addTextNode(rutConsultante.substring(0, rutConsultante.length() - 2)); toKname = envelope.createName("DvConsultante"); toKsymbol = gltp.addChildElement(toKname); toKsymbol.addTextNode(rutConsultante.substring(rutConsultante.length() - 1, rutConsultante.length())); toKname = envelope.createName("RutCompania"); toKsymbol = gltp.addChildElement(toKname); toKsymbol.addTextNode(rutEmisor.substring(0, rutEmisor.length() - 2)); toKname = envelope.createName("DvCompania"); toKsymbol = gltp.addChildElement(toKname); toKsymbol.addTextNode(rutEmisor.substring(rutEmisor.length() - 1, rutEmisor.length())); toKname = envelope.createName("RutReceptor"); toKsymbol = gltp.addChildElement(toKname); toKsymbol.addTextNode(rutReceptor.substring(0, rutReceptor.length() - 2)); toKname = envelope.createName("DvReceptor"); toKsymbol = gltp.addChildElement(toKname); toKsymbol.addTextNode(rutReceptor.substring(rutReceptor.length() - 1, rutReceptor.length())); toKname = envelope.createName("TipoDte"); toKsymbol = gltp.addChildElement(toKname); toKsymbol.addTextNode(Integer.toString(tipoDTE)); toKname = envelope.createName("FolioDte"); toKsymbol = gltp.addChildElement(toKname); toKsymbol.addTextNode(Long.toString(folioDTE)); toKname = envelope.createName("FechaEmisionDte"); toKsymbol = gltp.addChildElement(toKname); toKsymbol.addTextNode(fechaEmision); toKname = envelope.createName("MontoDte"); toKsymbol = gltp.addChildElement(toKname); toKsymbol.addTextNode(Long.toString(montoTotal)); toKname = envelope.createName("Token"); toKsymbol = gltp.addChildElement(toKname); toKsymbol.addTextNode(token); message.getMimeHeaders().addHeader("SOAPAction", ""); URL endpoint = new URL(urlSolicitud); SOAPMessage responseSII = con.call(message, endpoint); SOAPPart sp = responseSII.getSOAPPart(); SOAPBody b = sp.getEnvelope().getBody(); for (Iterator<SOAPBodyElement> res = b.getChildElements( sp.getEnvelope().createName("getEstDteResponse", "ns1", urlSolicitud)); res.hasNext();) { for (Iterator<SOAPBodyElement> ret = res.next().getChildElements( sp.getEnvelope().createName("getEstDteReturn", "ns1", urlSolicitud)); ret.hasNext();) { HashMap<String, String> namespaces = new HashMap<String, String>(); namespaces.put("", "http://www.sii.cl/XMLSchema"); XmlOptions opts = new XmlOptions(); opts.setLoadSubstituteNamespaces(namespaces); return RESPUESTADocument.Factory.parse(ret.next().getValue(), opts); } } return null; }
From source file:cl.nic.dte.net.ConexionSii.java
@SuppressWarnings("unchecked") public String getToken(PrivateKey pKey, X509Certificate cert) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyException, MarshalException, XMLSignatureException, SAXException, IOException, ParserConfigurationException, XmlException, UnsupportedOperationException, SOAPException, ConexionSiiException { String urlSolicitud = Utilities.netLabels.getString("URL_SOLICITUD_TOKEN"); String semilla = getSemilla(); GetTokenDocument req = GetTokenDocument.Factory.newInstance(); req.addNewGetToken().addNewItem().setSemilla(semilla); HashMap<String, String> namespaces = new HashMap<String, String>(); namespaces.put("", "http://www.sii.cl/SiiDte"); XmlOptions opts = new XmlOptions(); opts = new XmlOptions(); opts.setSaveImplicitNamespaces(namespaces); opts.setLoadSubstituteNamespaces(namespaces); opts.setSavePrettyPrint();/* www. j av a 2s. com*/ opts.setSavePrettyPrintIndent(0); req = GetTokenDocument.Factory.parse(req.newInputStream(opts), opts); // firmo req.sign(pKey, cert); SOAPConnectionFactory scFactory = SOAPConnectionFactory.newInstance(); SOAPConnection con = scFactory.createConnection(); MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); SOAPPart soapPart = message.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPHeader header = envelope.getHeader(); SOAPBody body = envelope.getBody(); header.detachNode(); Name bodyName = envelope.createName("getToken", "m", urlSolicitud); SOAPBodyElement gltp = body.addBodyElement(bodyName); Name toKname = envelope.createName("pszXml"); SOAPElement toKsymbol = gltp.addChildElement(toKname); opts = new XmlOptions(); opts.setCharacterEncoding("ISO-8859-1"); opts.setSaveImplicitNamespaces(namespaces); toKsymbol.addTextNode(req.xmlText(opts)); message.getMimeHeaders().addHeader("SOAPAction", ""); URL endpoint = new URL(urlSolicitud); message.writeTo(System.out); SOAPMessage responseSII = con.call(message, endpoint); SOAPPart sp = responseSII.getSOAPPart(); SOAPBody b = sp.getEnvelope().getBody(); cl.sii.xmlSchema.RESPUESTADocument resp = null; for (Iterator<SOAPBodyElement> res = b.getChildElements( sp.getEnvelope().createName("getTokenResponse", "ns1", urlSolicitud)); res.hasNext();) { for (Iterator<SOAPBodyElement> ret = res.next().getChildElements( sp.getEnvelope().createName("getTokenReturn", "ns1", urlSolicitud)); ret.hasNext();) { namespaces = new HashMap<String, String>(); namespaces.put("", "http://www.sii.cl/XMLSchema"); opts.setLoadSubstituteNamespaces(namespaces); resp = RESPUESTADocument.Factory.parse(ret.next().getValue(), opts); } } if (resp != null && resp.getRESPUESTA().getRESPHDR().getESTADO() == 0) { return resp.getRESPUESTA().getRESPBODY().getTOKEN(); } else { throw new ConexionSiiException( "No obtuvo Semilla: Codigo: " + resp.getRESPUESTA().getRESPHDR().getESTADO() + "; Glosa: " + resp.getRESPUESTA().getRESPHDR().getGLOSA()); } }
From source file:net.sf.jasperreports.olap.xmla.JRXmlaQueryExecuter.java
protected void addParameterList(SOAPEnvelope envelope, SOAPElement eParent, String typeName, String listName, Map<String, String> params) throws SOAPException { Name nPara = envelope.createName(typeName, "", XMLA_URI); SOAPElement eType = eParent.addChildElement(nPara); nPara = envelope.createName(listName, "", XMLA_URI); SOAPElement eList = eType.addChildElement(nPara); if (params == null) { return;//w w w . jav a 2 s .co m } for (Iterator<Map.Entry<String, String>> entryIt = params.entrySet().iterator(); entryIt.hasNext();) { Map.Entry<String, String> entry = entryIt.next(); String tag = entry.getKey(); String value = entry.getValue(); nPara = envelope.createName(tag, "", XMLA_URI); SOAPElement eTag = eList.addChildElement(nPara); eTag.addTextNode(value); } }
From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java
@Validated @Test/*from www. j ava 2s . c om*/ public void testGetValueSingleTextChild() throws Exception { SOAPElement element = saajUtil.createSOAPElement(null, "test", null); element.addTextNode("test"); assertEquals("test", element.getValue()); }
From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java
@Validated @Test(expected = IllegalStateException.class) public void testSetValueTwoTextChildren() throws Exception { SOAPElement element = saajUtil.createSOAPElement(null, "test", null); element.addTextNode("foo"); element.addTextNode("bar"); element.setValue("test"); }
From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java
@Validated @Test/*from ww w.j a va 2 s . c o m*/ public void testAddTextNode2() throws Exception { SOAPElement element = saajUtil.createSOAPElement(null, "test", null); element.addTextNode("ABC"); element.addTextNode("DEF"); assertEquals(2, element.getChildNodes().getLength()); assertEquals("ABCDEF", element.getTextContent()); }