/** * @param chnlId * @param xmlStr * @param chnlStr * @return */ public String processXMLForChnls(String chnlId, String xmlStr, final String chnlStr) { if ("106".equals(chnlId) || "102".equals(chnlId)) { try { xmlStr = URLEncoder.encode(xmlStr.trim(), "UTF-8"); } catch (UnsupportedEncodingException e) { } xmlStr = "xml=".concat(xmlStr); } else if ("103".equals(chnlId) && !"Route1_CONT".equals(chnlStr)) { xmlStr = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Header>" + "<SOAP-SEC:Signature xmlns:SOAP-SEC=\"http://schemas.xmlsoap.org/soap/security/2000-12\">" + "</SOAP-SEC:Signature>" + "</soap:Header>".concat(xmlStr).concat("</soap:Envelope>"); try { // Create a DOM XMLSignatureFactory that will be used to // generate the enveloped signature. XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); Reference ref = fac.newReference( "#Body", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList( fac.newTransform( "http://www.w3.org/2001/10/xml-exc-c14n#", (TransformParameterSpec) null)), null, null); // Create the SignedInfo. SignedInfo si = fac.newSignedInfo( fac.newCanonicalizationMethod( CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref)); // Load the KeyStore and get the signing key and // certificate. KeyStore ks = KeyStore.getInstance("JKS"); String certFileNm = ""; // commonConfigHelper.getConfigMap().get( // "ROUTE1_CERTIFICATE_JSK"); String pwd = ""; // commonConfigHelper.getConfigMap().get( // "ROUTE1_CERTIFICATE_PASSWORD"); String aliasNm = ""; // commonConfigHelper.getConfigMap().get( // "ROUTE1_CERTIFICATE_ALIAS"); ks.load(new FileInputStream(certFileNm), pwd.toCharArray()); KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) ks.getEntry(aliasNm, new KeyStore.PasswordProtection(pwd.toCharArray())); X509Certificate cert = (X509Certificate) keyEntry.getCertificate(); // Create the KeyInfo containing the X509Data. KeyInfoFactory kif = fac.getKeyInfoFactory(); List x509Content = new ArrayList(); // x509Content.add(cert.getSubjectX500Principal().getName()); x509Content.add(cert.getIssuerX500Principal().getName()); X509Data xd = kif.newX509Data(x509Content); KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd)); // Instantiate the document to be signed. // Document doc = xmlUtil.getDocumentFromString(xmlStr); Document doc = null; // Create a DOMSignContext and specify the RSA PrivateKey // and // location of the resulting XMLSignature's parent element. DOMSignContext dsc = new DOMSignContext(keyEntry.getPrivateKey(), doc.getDocumentElement()); // Create the XMLSignature, but don't sign it yet. XMLSignature signature = fac.newXMLSignature(si, ki); // Marshal, generate, and sign the enveloped signature. signature.sign(dsc); // Output the resulting document. xmlStr = xmlUtil.convertXMLDocToString(doc); } catch (Exception e) { } } return xmlStr; }