protected ByteArrayOutputStream sendMessage(URL urlWSDL, QName qService, QName qPort, String soapMessage) throws Exception {
MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage message;
MimeHeaders header = new MimeHeaders();
header.addHeader("Content-Type", "application/soap+xml");
message = factory.createMessage(header, new ByteArrayInputStream(soapMessage.toString().getBytes()));
Service service = Service.create(urlWSDL, qService);
Dispatch<SOAPMessage> dispatch = service.createDispatch(qPort, SOAPMessage.class,Service.Mode.MESSAGE);
setSecurity(dispatch);
SOAPMessage res = dispatch.invoke(message);
ByteArrayOutputStream out = new ByteArrayOutputStream();
res.writeTo(out);
return out;
}
private void setSecurity(Dispatch<SOAPMessage> dispatch) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, InvalidKeyException, InvalidKeySpecException, UnrecoverableKeyException {
Client client = ((org.apache.cxf.jaxws.DispatchImpl) dispatch).getClient();
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
setProxy(security, http, httpClientPolicy);
http.setClient(httpClientPolicy);
TLSClientParameters tlsParams = new TLSClientParameters();
TrustManager[] trustMgr = addTrustManagers(security);
KeyManager[] keyMgr = addCertificado(security);
tlsParams.setKeyManagers(keyMgr);
tlsParams.setTrustManagers(trustMgr);
http.setTlsClientParameters(tlsParams);
}
private void setProxy(SegurancaDES security, HTTPConduit http,
HTTPClientPolicy httpClientPolicy) {
httpClientPolicy.setProxyServer("192.168.1.1");
httpClientPolicy.setProxyServerPort("3128");
http.getProxyAuthorization().setUserName("proxyUser");
http.getProxyAuthorization().setPassword("proxyPassword);
}
private KeyManager[] addCertificado(SegurancaDES security)
throws KeyStoreException, IOException, NoSuchAlgorithmException,
CertificateException, FileNotFoundException,
UnrecoverableKeyException {
KeyStore ks1;
ks1 = KeyStore.getInstance("PKCS12");
ks1.load(new FileInputStream(new File("file path")),"password".toCharArray());
KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyFactory.init(ks1, "password".toCharArray());
KeyManager[] keyMgr = keyFactory.getKeyManagers();
return keyMgr;
}