Skip to content

Instantly share code, notes, and snippets.

@jc-lab
Last active July 10, 2019 07:44
Show Gist options
  • Save jc-lab/773d809053184529b52a4089aab72a9e to your computer and use it in GitHub Desktop.
Save jc-lab/773d809053184529b52a4089aab72a9e to your computer and use it in GitHub Desktop.
EjbcaConnector.java
import com.sun.xml.internal.ws.client.WSServiceDelegate;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.ejbca.core.protocol.ws.client.gen.EjbcaWS;
import org.ejbca.core.protocol.ws.client.gen.EjbcaWSService;
import javax.net.ssl.*;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.WebServiceFeature;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;
public class EjbcaConnector {
SSLContext sslContext = null;
public void init() throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException, KeyManagementException {
KeyStore trustKeyStore = KeyStore.getInstance( KeyStore.getDefaultType() );
try(FileInputStream fis = new FileInputStream( "trust.jks" )) {
trustKeyStore.load(fis, "keystore".toCharArray());
}
KeyStore clientKeyStore;
try(FileInputStream fis = new FileInputStream( "end_entity.jks" )){
clientKeyStore = KeyStore.getInstance("JKS");
clientKeyStore.load(fis, "1234".toCharArray());
}
SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
sslContextBuilder.loadTrustMaterial(trustKeyStore, new TrustSelfSignedStrategy());
sslContextBuilder.loadTrustMaterial(clientKeyStore, new TrustSelfSignedStrategy());
this.sslContext = sslContextBuilder.build();
}
public void connect() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
this.sslContext,
new String[] { "TLSv1.2" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setSSLSocketFactory(sslsf)
.build();
HttpResponse httpRequest = httpClient.execute(new HttpGet("https://your-domain:8442/ejbca/ejbcaws/ejbcaws?wsdl"));
try {
StreamSource wsdlSource = new StreamSource(httpRequest.getEntity().getContent());
WSServiceDelegate delegate = new WSServiceDelegate(wsdlSource, new QName("http://ws.protocol.core.ejbca.org/", "EjbcaWSService"), EjbcaWSService.class, new WebServiceFeature[0]);
EjbcaWS ejbcaWS = delegate.getPort(new QName("http://ws.protocol.core.ejbca.org/", "EjbcaWSPort"), EjbcaWS.class);
((BindingProvider)ejbcaWS).getRequestContext().put(JAXWSProperties.SSL_SOCKET_FACTORY, this.sslContext.getSocketFactory());
// Now, You can use invoke ejbcaWS.
System.out.println("version : " + ejbcaWS.getEjbcaVersion());
} finally {
httpRequest.getEntity().getContent().close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment