Java code generation using WSDL2Java over https


(Ryan Larson) #1

So I have done this twice with gradle, and both times were a pain in the butt. I could not find a good tutorial or a good fix for the SSL problems. It is certainly not gradles fault, but I thought if I could save somebody some time, it would be well worth it.

This is done as a JavaExec task because of the fact that I fake all the SSL stuff out, and I could not find a way to do this inside the axis ant task.

dependencies {
    compile group: 'org.apache.axis2', name:'axis2-codegen', version: '1.6.2'
    compile group: 'org.apache.axis2', name: 'axis2', version: '1.6.2'
}
  task wsdl2Java(dependsOn: 'classes', type: JavaExec) {
    main = 'org.example.WSDL2Java'
    classpath = sourceSets.main.runtimeClasspath
    def outputDir = project.projectDir.toString() + '\src\main\java'
    def wsdlUrl = 'https://example.org?wsdl'
    args '-uri', wsdlUrl, '-o', outputDir, '-p', 'org.example', '-S', '

First, we have to deal with the SSL certificates. The https uri that the wsdl is located at has a certificate, as well as some of the schema’s, etc. I could have installed the certificate in my keystore, but this is all operating over a local network and I wanted any of my team members to be able to check this out, build it, and go. So we fake the TrustManager and the HostNameVerifier like so:

public class SSLTruster {
    public static void trustAll(){
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
          try {
            SSLContext context = SSLContext.getInstance("SSL");
              context.init(null,
                    new TrustManager[]{new X509TrustManager() {
                        public void checkClientTrusted(X509Certificate[] chain, String authType) {}
                          public void checkServerTrusted(X509Certificate[] chain, String authType) {}
                          public X509Certificate[] getAcceptedIssuers() {
                            return new X509Certificate[]{};
                        }}},
                    new SecureRandom());
              HttpsURLConnection.setDefaultSSLSocketFactory(context.
                    getSocketFactory());
        } catch (GeneralSecurityException gse) {
            throw new IllegalStateException(gse.getMessage());
        }
    }
}

Now, we need to run the axis2 (important that you use axis2), WSDL2Java utility to generate code, with the SSL authentication faked out so that we dont get any handshake errors. I just wrote a simple wrapper for it that gradle can call:

public class WSDL2Java {
    public static void main(String... args) throws Exception {
        SSLTruster.trustAll();
        org.apache.axis2.wsdl.WSDL2Java.main(args);
    }
}

}


(Luke Daley) #2

Thanks for sharing your work Ryan. Hopefully if someone else has this issue they’ll stumble upon this.

Thanks again.