05 March 2014

JAXWS 2.x wsimport - rpc encoding problem

If you have chance to make wsdl modify; do it immediately. If not the solution is not so difficult: "USE APACHE AXIS" with eclipse web service client generation wizard as below.









17 April 2013

Observer-Observable Idea in SOA

A traditional SOA platform is composed of individual web services. All the the flow of interaction scenarios of web services are designed from client to web services. Let me draw the architectural concepts basically.

Basic UI - WS interaction scenario: blue part on UI indicates client stub codes to call WS



All right, but; if this is an accessible application, there is something odd we have missed. What if we have to notify our clients, to update a critical information? Some naive argument goes like this: "We recommend you, to invoke our "getX" method in a short period of time". This offer needs to be more clear and certain.

First of all, "recommend" is not indicating how crucial activity is, even if it is not an essential part of product. Also, "short period of time" is so ambigious term that can probably cause failures of users on run-time.

Adding the observer web service that has single one-way method to listen changes on domain


There is a need for applying Observer-Observable pattern between UI and web-services. The distinction between UI and web services makes use of observer-observable pattern simple impossible. Thus, a new candidate solution to be adopted in a SOA manner. I draw the below figure to indicate how to notify UI. The notification goes from webservice to UI with invoking a simple method of another web service on client(UI).

By doing this, responsibility of action is taken from UI developers to web service developers (also responsible of business logic). Additionally, the questions of "WHY to show" and "WHEN to show" is gone away for UI developers.

I inspired from model in Ivar Jacobson's book [Object-oriented software engineering: a use case driven approach]. The architecture that is about boundry interfaces between UI and Controllers.



Now everybody can be more happy but things are not so easy. Client must register to be notified and web service must manage the registrants' status. Also, the notification messages format and the actions to be taken for individual message must be defined, well.

Other questions come to my mind: How many service should be observable, how many services can be observed? I haven' t google' d for this so far, if i found i will be posted here, literally.

Thx for reading.

27 February 2013

Applying Basic HTTP Authentication on Axis2 Web Services in Tomcat


Basic authentication is sort of authentication mechanism for restricting access of resources on tomcat. This is a simple way to limiting accesses but it uses easy to sniff plain http messages with base64 encoded username and password pair inside http header. 
In order to apply basic http authentication on axis2 web services, please follow the steps, below:
  1. Add this config xml to tomcat-users.xml under conf directory.
  2. 
     
     
     
    

  3. Go to \webapps\axis2\WEB-INF, add the following lines to web.xml. tag defines the url where to bind security constraints. Also, you can specifically define this security config for a specific service on axis2.
  4. 
    
       
          secured services
          /axis2/services
       
    
       
          wsclient
       
    
    
    
       BASIC
       wsclient
    
    
  5. Start tomcat and test default web service(Version) on Axis2. There is little button “aut” written on its label, in soapUI test window. I fill the username and password as I set in tomcat-users-xml and select authorization type “preemptive”. Send the request and everything will work fine. If credentials are not supplied, ClientProtocolException will occur and logged in “soapui logs.”
  6. ERROR:Exception in request: org.apache.http.client.ClientProtocolException 
    ERROR:An error occured [org.apache.http.client.ClientProtocolException], see error log for details 
    
Thats all, you have to do for applying http basic auth... Thanks for reading...

30 January 2013

Union Multiple Collections into a Single Collection

As developing daily tasks at work, I realize the need of a generic method that merges given an array of collections into a single collection as stated in this StackOverflow Thread.

There are reasonable references to well grounded frameworks (e.g. guava)  and sample implementations in that thread. I also check it out the popular "apache commons collections" that already offers a method that union two collections and returns a single non-modifiable collection; however I think it is not enough. There should be still some improvements to raise the bar to the next level.

 As a result i define the method as follows:

public static <T> Collection<T> union(Collection<T>... collections) {  } 


Implementation is as follows, I hope it helps you...


import java.util.Iterator;
import java.util.Collection;

/**
 * This class offers an alternative implementation for union an array of
 * (java.util.Collection) collections instead of only two collections.
 * http://commons.apache.org/collections/apidocs/org/apache/commons/collections/
 * CollectionUtils.html#union%28java.util.Collection,%20java.util.Collection%29
 * 
 * @author oguzhan.acargil
 * @see http://0guzhan.blogspot.com
 * */
public class CollectionUtil {

 public static  Collection union(Collection... collections) {
  if (hasElements(collections)) {
   int totalSize = totalSizeOf(collections);
   final UnmodifiableCollection union = new UnmodifiableCollection(
     totalSize);
   for (Collection collection : collections) {
    if (hasElements(collection)) {
     union.appendAll(collection);
    }
   }
   return union;
  }
  return null;
 }

 private static  int totalSizeOf(Collection... collections) {
  int totalSize = 0;
  if (hasElements(collections)) {
   for (Collection collection : collections) {
    if (hasElements(collection)) {
     totalSize += collection.size();
    }
   }
  }
  return totalSize;
 }

 private static  boolean hasElements(T[] array) {
  if (array != null && array.length > 0) {
   return true;
  } else {
   return false;
  }
 }

 private static  boolean hasElements(Collection collection) {
  if (collection != null && collection.size() > 0) {
   return true;
  } else {
   return false;
  }
 }

 private static class UnmodifiableCollection implements Collection {

  private int cursor;
  private int size;
  private Object[] elements;

  public UnmodifiableCollection(int totalSize) {
   this.cursor = 0;
   this.size = totalSize;
   this.elements = new Object[totalSize];
  }

  private void appendAll(Collection collection) {
   for (T element : collection) {
    elements[this.cursor++] = element;
   }
  }

  @Override
  public int size() {
   return this.size;
  }

  @Override
  public boolean isEmpty() {
   return this.size > 0;
  }

  @Override
  public boolean contains(Object o) {
   if (o != null && !isEmpty()) {
    for (Object element : this.elements) {
     if (o.equals(element)) {
      return true;
     }
    }
   }
   return false;
  }

  @Override
  public Iterator iterator() {
   @SuppressWarnings({ "unchecked", "rawtypes" })
   Iterator iterator = new Iterator() {

    private int current = -1;

    @Override
    public boolean hasNext() {
     return (this.current + 1) < size;
    }

    @Override
    public Object next() {
     return elements[++this.current];
    }

    @Override
    public void remove() {
     throw new RuntimeException(
       "Iterator.remove() is not supported; "
         + "since this is a non-modifable collection implementation");
    }
   };
   return iterator;
  }

  @Override
  public Object[] toArray() {
   return elements;
  }

  @SuppressWarnings({ "unchecked", "hiding" })
  @Override
  public  T[] toArray(T[] a) {
   if (a != null && !isEmpty()) {
    if (a.length == size()) {
     for (int index = 0; index < a.length; index++) {
      a[index] = (T) elements[index];
     }
    }
   }
   return a;
  }

  @Override
  public boolean add(T e) {
   throw new RuntimeException("Collection.add() is not supported; "
     + "since this is a non-modifable collection implementation");
  }

  @Override
  public boolean remove(Object o) {
   throw new RuntimeException(
     "Collection.remove(Object o) is not supported; "
       + "since this is a non-modifable collection implementation");
  }

  @Override
  public boolean containsAll(Collection c) {
   throw new RuntimeException(
     "Collection.containsAll(Collection c) is not supported; "
       + "since this is a non-modifable collection implementation");
  }

  @Override
  public boolean addAll(Collection c) {
   throw new RuntimeException(
     "Collection.addAll(Collection c) is not supported; "
       + "since this is a non-modifable collection implementation");
  }

  @Override
  public boolean removeAll(Collection c) {
   throw new RuntimeException(
     "Collection.removeAll((Collection c) is not supported; "
       + "since this is a non-modifable collection implementation");
  }

  @Override
  public boolean retainAll(Collection c) {
   throw new RuntimeException(
     "Collection.retainAll(Collection c) is not supported; "
       + "since this is a non-modifable collection implementation");
  }

  @Override
  public void clear() {
   throw new RuntimeException("Collection.clear() is not supported; "
     + "since this is a non-modifable collection implementation");
  }
 }
}

26 November 2012

POJOs with Packages Example Problem - Apache Axis2 Web Services 2nd Ed.

POJO style web service development is a variation of  bottom-up web service development allowed by Axis2. Compiling a single java file then put .class file under "\webapps\axis2\WEB-INF\pojo" to use it as a web service is simple and straightforward. However, it is so weak because of its limitation of usages. When more complex requirements occurs it is time to gather all required classes under a .jar file and mark exposed web service implementation classes with appropriate annotations then deploy it.

I followed the directions from Apache Axis2 Web Services 2nd Ed(page 106). But my project's jar file failed to deploy without warnings and run without errors. Because, Apache axis2 1.6.x and below distributions do not support JSR 181 annotated POJO as it' s declared below issue.

https://issues.apache.org/jira/browse/AXIS2-4127