Showing posts with label Axis. Show all posts
Showing posts with label Axis. Show all posts

Thursday, 28 April 2016

How to configure to use SSL certificate through Apache Axis ?


Axis Configuration for SSL

Set a Socket Secure Factory, at application startup

  AxisProperties.setProperty(
       "axis.socketSecureFactory",
       "com.genius.utils.CustomSSFactory");


Create Socket Secure Factory class

1. Import following APIs :
import java.security.KeyStore;
import java.security.cert.X509Certificate;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import org.apache.axis.components.net.JSSESocketFactory;
import org.apache.axis.components.net.SecureSocketFactory;


2.   Create class by extending JSSESocketFactory and implementing SecureSocketFactory
public class CustomSSLFactory extends JSSESocketFactory
        implements SecureSocketFactory

3.  Override a parameterized constructor, to set attributes :
public CustomSSLFactory(Hashtable attributes) {
        super(attributes);
}

4.  Override method initFactory to set sslFactory :
protected void initFactory() throws IOException {
        try {
               SSLContext context = getContext();
               sslFactory = context.getSocketFactory();
        } catch (Exception e) {
               e.printStackTrace();
        }
}
  

5.  Override method getContext to return the SSL context :
protected SSLContext getContext() throws Exception {
   . . . . .
}
              
 

Details of getContext method

Use 2 constants for keystore location and password :
private static String MY_KEYSTORE_PASSWORD = "passwd";
private static String RESOURCE_PATH_TO_KEYSTORE = "/usr/bin/certificate.P12";


Check if password is in correct format :
char[] keystorepass = MY_KEYSTORE_PASSWORD.toCharArray();

if (StringUtils.isBlank(new String(keystorepass))) {
    throw new Exception("Could not read password for configured keystore!");
}


Load the keystore :
KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(new FileInputStream(new File(RESOURCE_PATH_TO_KEYSTORE)),
                     MY_KEYSTORE_PASSWORD.toCharArray());


Check intialization of key manager factory :
KeyStore keyStore = ks;
KeyManagerFactory kmf = KeyManagerFactory.getInstance(
                    KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, keystorepass);


Check initialization of Trust manager factory :
TrustManagerFactory tmf =  TrustManagerFactory.getInstance(
                             TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);


Initialize the X509 Trust manager :
X509TrustManager tm = new X509TrustManager() {
  public X509Certificate[] getAcceptedIssuers() {
               return null;
  }

  public void checkClientTrusted(X509Certificate[] certs, String authType) {
  }

  public void checkServerTrusted(X509Certificate[] certs, String authType) {
  }
};


Initialize Key Manager Factory and Key managers
KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
                               KeyManagerFactory.getDefaultAlgorithm());
kmfactory.init(keyStore, keystorepass != null ? keystorepass : null);
KeyManager[] keymanagers = kmfactory.getKeyManagers();



Initialize SSL context with Key managers and Trust managers (X509)
sslContext.init(keymanagers, new TrustManager[]{tm}, null);


Return the SSL context

return sslContext;

Wednesday, 27 April 2016

Apache Axis2 vs. Apache CXF vs. Spring Web services


Apache Axis2 vs. Apache CXF vs. Spring Web services

  • CXF and SpringWS supports Java
  • Axis2 supports Java, C, C++
  • Axis2 and CXF supports Contract-First and Contract-Last approaches
  • SpringWS supports only Contract-First approach
  • Apache CXF supports data bindings : JAXB, XMLBeans, JiBX, Castor, Aegis
  • SpringWS supports data bindings : JAXB, XMLBeans, JiBX, Castor, Xstream
  • Axis2 supports more data bindings : JAXB, XML Beans, JaxME, JaxBRI , ADB (Axis Data Binding)
  • Integration with Spring : Axis 2 (compatible) , CXF (better integration) , SpringWS (Much better)
  • OSGi support : Axis 2 (Not better) , CXF (better support) , SpringWS (better support)


  • Axis2 and CXF supports WS-RM but SpringWS doesn't support WS-RM


Apache CXF advantages over Axis2 and Spring WS

  • Support for the Tuscany implementation
  • High extensibility
  • More configurable features via API
  • Ease of development
  • Better performance

Monday, 4 April 2016

How to create web service client using Axis 2 and eclipse ?


A. Configure Axis 2 in eclipse

Configure Axis 2 Runtime
1. Go to Windows - Preferences - Web services - Axis2 Preferences
2. Browse the location of Axis 2 API folder (for ex. axis2-1.4.1) in section Axis2 Runtime.
3. Check if the Axis2 runtime loaded successfully.
4. Click Apply and OK.

Configure web server
1. Go to Windows - Preferences - Server - Installed Runtimes
2. Click Add and choose server (for ex. Tomcat 5.5)
3. Browse the location of folder where server installed (for ex. apache-tomcat-5.5.25)
4. Configure installed JREs and select JRE to use.
5. Click Finish and OK.

Configure server and runtime for Web service
1. Go to Windows - Preferences - Web services - Server and Runtime
2. Select server (for ex. Tomcat 5x)
3. Select Web service Runtime (for ex. Apache Axis 2)
4. Click Apply and OK.



B. Generate stub code

1. Create a new project (Java or Web project)
2. Create a folder named "wsdl" in the web application and put your WSDL inside it.
3. Right click on WSDL file and choose Web services - Generate client
4. Check the configuration -
     Server (for ex. Tomcat 5x)
     Web service runtime (for ex. Apache Axis 2)
5. Click Next and at last, click Finish.
6. Check if the service Stub code is auto-generated.
    Note: Axis 2 generates following classes in stubs :
     a. Binding stub - For ex. Hello_BindingStub.java
     b. Port type - For ex. Hello_PortType.java
     c. Service Locator - For ex. Hello_ServiceLocator.java
     d. Service - For ex. Hello_Service.java


C. Write client code

Write code in a separate class to call the web service.
STEPS : 
   a. Create an object of Service locator.
   b. Create object of Binding stub by passing endpoint URL of service and service locator object.
   c. Call web service method and get the result.

Example :
Hello_ServiceLocator locator = new Hello_ServiceLocator();
Hello_BindingStub stub = new Hello_BindingStub( 
       new URL(
        "http//localhost:8081/HelloServer/services/Hello_Service"),
        locator);
String result = stub.sayHello("SHAAN");

How to create web service (server) using Axis 2 and eclipse ?



A. Configure Axis 2 in eclipse

Configure Axis 2 Runtime
1. Go to Windows - Preferences - Web services - Axis2 Preferences
2. Browse the location of Axis 2 API folder (for ex. axis2-1.4.1) in section Axis2 Runtime
3. Check if the Axis2 runtime loaded successfully.
4. Click Apply and OK.

Configure web server
1. Go to Windows - Preferences - Server - Installed Runtimes
2. Click Add and choose server (for ex. Tomcat 5.5)
3. Browse the location of folder where server installed (for ex. apache-tomcat-5.5.25)
4. Configure installed JREs and select JRE to use.
5. Click Finish and OK.

Configure server and runtime for Web service
1. Go to Windows - Preferences - Web services - Server and Runtime
2. Select server (for ex. Tomcat 5x)
3. Select Web service Runtime (for ex. Apache Axis 2)
4. Click Apply and OK.



B. Generate skeleton code and implement WS methods

1. Create a new web project
2. Create a folder named "wsdl" in the web application and put your WSDL inside it.
3. Right click on WSDL file and choose Web services - Generate java bean skeleton
4. Check the configuration -
     Server (for ex. Tomcat 5x)
     Web service runtime (for ex. Apache Axis 2)
5. Click Next - Next and at last, on start service page, click cancel.
6. Check if the skeleton Java code is auto-generated.
7. Check and open the generated skeleton java class. (for ex. Hello_ServiceSkeleton.java)
8. Check the required operation(s) under skeleton java class. (for ex. sayHello method)
9. Implement the operation(s) as per the business and return the proper response.
    Note: By default All the operations in skeleton class will have implementation to throw an exception.


C. Check the service deployment

1. Start the server and check your web application using its URL : 
     http://localhost:8080/HelloServer
2. Check the presence of the link services and click on it.
3. Check the presence of your web service and click to check the deployed WSDL.
     It will open the WSDL URL, 
     For ex. -  http://localhost:8080/HelloServer/services/Hello_Service?wsdl

Your web service will be accessible using the endpoint URL :  
http://localhost:8080/HelloServer/services/Hello_Service