IT Certificaions

Google Cloud Platform

Thursday, May 25, 2017

Create java client from WSDL for Google App engine

This post will explain how to consume SOAP based web services on Google App Engine platform.

  1. Keep your wsdl files in resources/wsdl folder
  2. Make these files entry in appengine-web.xml like
  3.   <static-files>
           <include path="/**.wsdl"/>
         <include path="/**.WSDL"/>
     </static-files>
  4.  Now update POM file for wsdl to java plugin
 <plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>3.0.1</version>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>             
                <sourceRoot>${basedir}/src/main/java</sourceRoot>
                <wsdlOptions>
                    <wsdlOption>
                          <wsdl>src/main/resources/wsdl/ReadData.wsdl</wsdl>  
                          <wsdlLocation>https://${appengine.app.id}.appspot.com/wsdl/ReadData.wsdl</wsdlLocation>
                          <extraargs>
                                 <extraarg>-p</extraarg>
                                 <extraarg>com.example.ws.readdata</extraarg>
                          </extraargs>                                      
                    </wsdlOption>
                    <wsdlOption>
                        <wsdl>src/main/resources/wsdl/SaveData.wsdl</wsdl>
                        <wsdlLocation>https://${appengine.app.id}.appspot.com/wsdl/SaveData.wsdl</wsdlLocation>
                        <extraargs>
                                <extraarg>-p</extraarg>
                                <extraarg>com.example.ws.savedata</extraarg>
                         </extraargs>
                    </wsdlOption>
                   
                </wsdlOptions>
            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
    </executions>
</plugin>







Now you can use maven commands mvn clean install and stubs will be generated in com.example.ws.savedata and com.example.ws.readdata packages.
 

Convert pfx file into jks using java

This is a post which shows how to convert pfx file into jks.

Prerequisites : Java 6+
Step 1: Use cmd with java in your classpath

keytool -importkeystore -srckeystore testKey.pfx -srcstoretype pkcs12 -srcalias <myAliasName>
 -destkeystore jksFile -deststoretype jks -deststorepass password -destalias <alias>

Java Client with certificate authentication to access SOAP Webservice

This is an example to explain  how to authenticate java stub client with certification and send request to SOAP WSDL.

public static void applyCertificateAuthentication(MyWsdlDataRequest wsdlClientRequest)
            throws Exception {


       String keyStoreLocation="resources/keys/testKey.jks";

       String password="key@12345";
        ClassLoader classLoader = this.class.getClassLoader();
        InputStream keyInputStream = classLoader.getResourceAsStream(keyStoreLocation);

        SSLContext sc = SSLContext.getInstance("SSLv3");
        KeyManagerFactory factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(keyInputStream, password.toCharArray());
        factory.init(keyStore, password.toCharArray());
        sc.init(factory.getKeyManagers(), null, null);
             


        MyWsdlService serviceImpl = new MyWsdlService ();
       MyWsdlDataStub servicePort = serviceImpl.getPort(MyWsdlDataStub .class);               BindingProvider bp = (BindingProvider) servicePort;

((BindingProvider) servicePort).getRequestContext().put("com.sun.xml.internal.ws.transport.https.client.SSLSocketFactory",
                            sc.getSocketFactory());

 
 MyWsdlDataResponse response = servicePort.siSaveSocDataOut(wsdlClientRequest);

    }