Sunday, May 22, 2016

Create a Class mediator and Use it in the sequences of the sample proxy

Step 1: Creating Class mediator
First create maven project called org.example.mediator using eclipse. The add the following dependencies in the pom.xml.

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

  <modelVersion>4.0.0</modelVersion> 

  <groupId>org.wso2.biruntha</groupId> 

  <artifactId>org.example.mediator</artifactId> 

  <version>0.0.1-SNAPSHOT</version> 

  <packaging>jar</packaging> 

  <name>org.example.mediator</name> 

  <url>http://maven.apache.org</url> 

  <properties> 

   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 

  </properties> 

  <repositories> 

     <repository> 

       <id>wso2-maven2-repository</id> 

       <url>http://dist.wso2.org/maven2</url> 

     </repository> 

     <repository> 

       <id>apache-Incubating-repo</id> 

       <name>Maven Incubating Repository</name> 

       <url>http://people.apache.org/repo/m2-incubating-repository</url> 

     </repository> 

     <repository> 

       <id>apache-maven2-repo</id> 

       <name>Apache Maven2 Repository</name> 

       <url>http://repo1.maven.org/maven2/</url> 

     </repository> 

   </repositories> 

   <build> 

     <plugins> 

       <plugin> 

         <groupId>org.apache.maven.plugins</groupId> 

         <artifactId>maven-compiler-plugin</artifactId> 

         <version>2.0</version> 

         <configuration> 

           <source>1.5</source> 

           <target>1.5</target> 

         </configuration> 

       </plugin> 

     </plugins> 

   </build> 

   <dependencies> 

        <dependency> 

         <groupId>junit</groupId> 

         <artifactId>junit</artifactId> 

         <version>3.8.1</version> 

         <scope>test</scope> 

        </dependency> 

     <dependency> 

       <groupId>org.apache.synapse</groupId> 

       <artifactId>synapse-core</artifactId> 

       <version>2.1.1-wso2v7</version> 

     </dependency> 

   </dependencies> 

 </project> 


Then to build the package use mvn clean install Step 2: Create class mediator create class mediator called MyMediator that extends AbstractMediator.

 package org.example.mediator; 

 import org.apache.synapse.MessageContext; 

 import org.apache.synapse.Mediator; 

 import org.apache.synapse.mediators.AbstractMediator; 

 import org.apache.axiom.om.OMElement; 

 import org.apache.axiom.om.OMAbstractFactory; 

 import org.apache.axiom.om.OMFactory; 

 import org.apache.axiom.soap.SOAPBody; 

 import org.apache.axiom.soap.SOAPFactory; 

 import org.apache.commons.logging.Log; 

 import org.apache.commons.logging.LogFactory; 

 import javax.xml.namespace.QName; 

 public class MyMediator extends AbstractMediator { 

   private static final Log log = LogFactory.getLog(MyMediator.class); 

   public MyMediator(){} 

   public boolean mediate(MessageContext mc) { 

     //get the SOAP body from message context 

    SOAPBody soapBody = mc.getEnvelope().getBody(); 

     //now you can modify the SOAP body as you wish 

     //... 

     return true; 

   } 

   public String getType() { 

     return null; 

   } 

   public void setTraceState(int traceState) { 

     this.traceState = traceState; 

   } 

   public int getTraceState() { 

     return 0; 

   } 

 } 


again run "mvn clean install" this will create org.example.mediator-0.0.1-SNAPSHOT.jar in the target folder.

copy that paste into wso2esb-4.9.0/repository/components/lin folder.

Then Run the ESB server.

cd wso2esb-4.9.0/samples/axis2Server/src/SimpleStockQuoteService
Run ant

cd wso2esb-4.9.0/samples/axis2Client ant stockquote -Dtrpurl=http://localhost:8280/services/StockQuoteProxy -Dmode=quote -Dsymbol=IBM

this will return stock price...

Step3 : Create proxy Service Follow [1] this to Create proxy service as below. And In the in sequence of the proxy add the class mediator we created already. After all of this the proxy will be like below.

 <?xml version="1.0" encoding="UTF-8"?> 

 <proxy xmlns="http://ws.apache.org/ns/synapse" 

     name="StockQuoteProxy" 

     transports="http,https,local" 

     statistics="enable" 

     trace="disable" 

     startOnLoad="true"> 

   <target> 

    <inSequence> 

      <class name="org.example.mediator.MyMediator"/> 

    </inSequence> 

    <outSequence> 

      <send/> 

    </outSequence> 

    <endpoint> 

      <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> 

    </endpoint> 

   </target> 

   <publishWSDL uri="file:./repository/samples/resources/proxy/sample_proxy_1.wsdl"/> 

   <description/> 

 </proxy> 



Run again ant stockquote -Dtrpurl=http://localhost:8280/services/StockQuoteProxy -Dmode=quote -Dsymbol=IBM

this will return stock price...

Friday, May 20, 2016

WSO2 Jaggery Application

I create simple Jaggery Application [1] which has the facility to show customer details and add customer details. For that i follow this [2].


[1] - https://github.com/Biruntha/Simple-Jaggery-Application
[2] - http://blog.lasindu.com/2014/05/how-to-write-wso2-jaggery-application.html


Creating WSO2 Carbon Component Example (Could not find src/org folder)

When i create the first carbon component, I follow this link [1] that performs two specific tasks. It will allows users to view a menu of food items offered by the O2 restaurant and they can place an order according to their choice. Once the order is placed, a confirmation message will be appeared asking users to proceed with the payment.

When i tried the maven build on org.wso2.carbon.orderprocess.mgt.stub/pom.xml , I got target/generated-code folder but i haven't got the src/org inside the generated-code folder because the format of wsdl file is wrong. Then i Right click on [2] --> view page source --> copy that code and paste in wsdl file.

 Then i have successfully created carbon components.




How to Build WSO2 Carbon

To build WSO2 carbon from source I follow this post [1]. But when i tried to build using this mvn clean install -Dmaven.test.skip=true, I got error like below.

"
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR] 
[ERROR]   The project org.apache.tomcat.wso2:jdbc-pool:${jdbc.pool.version}.wso2v1 (/home/biruntha/Desktop/carbon/orbit/trunk/jdbc-pool/pom.xml) has 1 error
[ERROR]     Non-resolvable parent POM for org.apache.tomcat.wso2:jdbc-pool:${jdbc.pool.version}.wso2v1: Failure to find org.wso2.carbon:carbon-dependents:pom:4.0.0 in 
https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced and 'parent.relativePath' points at wrong local POM @ line 22, column 13 -> [Help 2]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] 
http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] 
http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException  "

Then Using [2] , i solve this by changing the repository link or by editing jdbc-pool/pom.xml file.




[1] - https://dzone.com/articles/how-build-wso2-carbon-420
[2] - http://mail.wso2.org/mailarchive/dev/2014-July/033978.html 




Build WSO2 ESB from source and remote debugging the source code


We have to debug an application which is not running from it's source code in a IDE. So to do we need this remote debug concept. When it runs it will broadcast debug information via a socket deployed. The IDE running locally will capture those broadcast debug information through that socket and it will do the debug through the source code. From Below image we can understand clearly.
To build, debug WSO2 ESB product i followed this link [1]. But In the middle, After deploying the
SimpleStockQuoteService sample in the axis2server i tried to execute sh axis2server.sh to run the axis2 server. But i got error 

" ls: cannot access '/home/biruntha/Downloads/axis2-1.7.2/../../repository/components/plugins/*.jar': No such file or directory
ls: cannot access '/home/biruntha/Downloads/axis2-1.7.2/../../repository/components/plugins/*.jar': No such file or directory
Using JAVA_HOME: /usr/lib/jvm/jdk1.8.0_91
Using AXIS2 Repository : /home/biruntha/Downloads/axis2-1.7.2/repository
Using AXIS2 Configuration : /home/biruntha/Downloads/axis2-1.7.2/repository/conf/axis2.xml
Error: Could not find or load main class samples.util.SampleAxis2Server "

To solve i follow [2] this by removing the AXIS2_HOME variable,then server started as expected







Sunday, May 15, 2016

Functional Components

Mediators

Basic component of ESB. Other components are build on top of mediators. When a message come to a mediator, it transform that message according to the configuration defined of that mediator and output another the message according to the configuration defined. In mediators some kind of processing will happen.


Sequences (sequence of mediators)

we need several mediators to achieve the requirement so we can have a chain of mediators define as a sequence.

End points

A logical entity to which we are sending the messages from ESB it is actual back end. It can be Http url, JMS end point. There the quality of service parameters can be defined (eg : time out value of end point, retry count).


Proxy services

A virtual service hosted in ESB. A actual entity that client can see. When a client send a message to ESB it will come to one of proxy services (In sequence, out sequence, fault sequence). In sequence is a sequence which receive message from client. After receives, it will execute the mediators defined in that in sequence. After processing the message, it will send to the end point defined in that in sequence. In that, send mediator is used to send message to back end. Then message is routed to the particular service. That server will send a response back to the ESB. Then the response will be retrieve from the out sequence. There response can be send to client or to another service.
If any error occur in back end call then the error will come to the error sequence. Depending on our requirements the fault sequence can be used log the error, ignore it or send a SOAP fault back to the client indicating that something went wrong.


REST API

In here also we have In sequence, out sequence, fault sequence.
Consider a web page with SOAP service, client send request in REST, we can use ESB to transform. Inside ESB REST API will be defined. When client send the message to REST API, the ESB will send SOP message to back end. The response will come as SOAP message then it transform into REST and send it to the client.



ESB Connectors

Connector is used to connect the cloud based APIs(facebook,linkedin,JIRA) with ESB.  

High level Architecture of ESB

When client send a message to wso2 ESB with any transport like HTTP, Mail, JMS, FIX then ESB will decide what is the content of the message (like text/xml, application/xml, JSON) and select a message builder according to the content type. So message builder take a specific content type message and build a SOAP/XML message which can be use inside ESB. If there are quality of service mechanism defined then it will go through that mechanism and send it to the mediation engine.

Mediation engine transformation, proxy service will happen. After transforming the message, need to send from ESB to back end service. And also the format of the outgoing message will be defined inside ESB.


Key features

Routing

If different clients are sending messages to ESB, ESB will decide to which server the message should be routed. Routing can be based on headers, content of message.




Filtering

According to the content, can filter and then decide whether sending to back end or discarding the messages.


Transformation

Client has their own format of sending messages but back end services has their own defined format. So ESB get the message from client and change to the format the back end can understand give it then get the response from back end then send it to client.
Transformation is using XSLT.


Protocol Switching

Client will communicate with the ESB with SOAP or HTTP protocol but the back end service is deployed with Fix protocol. So ESB is used to transform from HTTP protocol to Fix protocol.


Load balancing

When multiple messages coming into the ESB, some of the messages routed to different servers.


QOS (quality of services)

when service is deployed into wso2 ESB , client will communicate with ESB with a security token.But back end has no authentication mechanism. So the non secure back end service can be secured through wso2 ESB secure mechanism.  


Why we need ESB?

Think about banking system. In early days, banking system is a single application which handles all the authentication, transformation, data storage and any other methods it needs. After increase usage of information system, try to differentiate different features into different modules which serve to create a single system. For example, In Bank Fixed deposit, transaction, database application. So the entire system is divided into separate modules. And each modules is connected with each other. When connecting these different system to each other, it is hard to maintain if the users increases. In case users increase, we need lot of servers to store the data. Then all these servers need to connect to each other system as well. Complexity will increase. And also when Fixed deposit system connect to transaction system they need to know the protocol they are using. Like that each system need to maintain every protocols to connect each other...

Consider a sample system it has different modules which has been written different languages and technologies. It has .net , J2EE, SOAP, REST different kind of modules. When try to connect these system, hard to maintain, when one system has issue then it will affect the all other systems, scalability problem, troubleshooting problem.

So… ESB act as central application which connect all the other systems. Then .net , J2EE, SOAP, REST are connected to ESB so that they can talk to each other.

In ESB, consider JMS client application, HTTP system, JSON, SAP are connected through the ESB. When HTTP calling JMS service, It only needs to know HTTP protocol not need to know JMS protocol then HTTP connect to the ESB and ESB connect JMS system through JMS protocol.
So here to connect with different system only need to know the protocol they are using.

Saturday, May 14, 2016

Creating Web Service Using Axis2(Servlet.init() fir servlet AxisServlet threw exception)

When i tried to create Web Service using Axis2, i successfully get the Apache welcome page. But when i click the Services i got error like below,


I solve this by downloading xmlschema-core-2.2.1  and copy the xmlschema-core-2.2.1.jar file inside the lib folder and paste it to the project WebContent/WEB-INF/lib. Then right click --> Build path --> Configure Build Path

Then the Servlet.init is gone.

Friday, May 13, 2016

Axis 2

Axis 2 is a web service/SOAP/WSDL engine provided by Apache. It is a java based implementation.

Think about a bank doing day to day transactions on their hundreds of branches and each one of them are connected with each other. In this scenario, you can understand that the computing resources are “Distributed” among several locations (Branches). To achieve this kind of computing need, we use a Distributed Computing System in which the computers and resources are distributed in several physical and logical locations.

These systems need to be developed as separate systems but they need to be interconnected when needed and all of these systems should be accessible from the web interface which is present at the computers of bank officers. We can develop these systems as Services and then we can inter-connect these services in to a common architecture (platform). This kind of system is called as a Service Oriented Architecture (SOA).


Representation of what is happening inside an axis2 engine :



Handlers

Handler is the message interceptor. Which intercepts the messaging flow and does whatever task it is assigned to do. In fact, the interceptor is the smallest execution unit in a messaging system.

Handlers, in Axis, are stateless, meaning they do not keep their past execution states in memory.
Handler can read SOAP messages, remove elements from the message (mostly headers), add new elements (headers), or modify elements as well as add, delete,
or modify content from the Message Context.




Phases


The concept of a phase is introduced by Axis2 and it was mainly to support the dynamic ordering of handlers to provide better extensible, and better flexibility of the handler chain. A phase can be defined in various ways:
•  It can be considered as a logical collection of handlers
•  It can be considered as a specific time interval in the message execution
•  It can be considered as a bucket into which one can put his/her handler
•  One can consider a phase as a handler too


Pipe (Flow)

A flow is the message pipe where the message enters from one end of the flow and leaves from the other end of the flow. A flow or execution chain can be considered as a collection of phases.
At a very high level, what the engine really does is call the invoke method of each phase in a given flow, and then the phase will sequentially invoke all the handlers in it.

Web Service


A Web Service is a code that is exposed over the network and can be invoked using HTTP.
Using web service your functionality can be exposed to rest of the world. Web service uses SOAP to transport data. It is a open protocol. With web service the functionality can be consumed from different platforms. For example you can create a web service in your windows computer and that can be consumed from your android mobile.


Using web service you can create a loosely coupled client server environment. Which means, when the server code is changed it is not always necessary to change the client code.

Axis2, Tomcat and Eclipse setup

Download Axis 2 WAR Distribution :

Find latest WAR Distribution and download it here 

Install WAR to Tomcat Server :

Extract WAR Distribution ZIP contents and Copy axis2.war to the webapps directory of your Tomcat installation.

Start Eclipse :

Navigate to Server view and start Tomcat,

In a browser navigate to http://localhost:8080/axis2,
Click Validate link – validation report shouldn’t contain any errors.

If any error comes,
Here is how I did it :

  1. Right click on my tomcat server in "Servers" view, select "Properties…"
  2. In the "General" panel, click on the "Switch Location" button
  3. The "Location: [workspace metadata]" bit should have been replaced by something else.
  4. Open (or close and reopen) the Overview screen for the server.

XML parser

XML parser is a software library (or package) that reads and parses XML. It is designed to read XML and create a way for programs. Different types of XML parses are available for various languages.

XML parser validates the document and check that the document is well formatted.


Let's understand the working of XML parser by the figure given below:

Main Types of XML parsers in Java

DOM (Document Object Model)

  • A DOM document is an object which contains all the information of an XML document. It is composed like a tree structure. The DOM Parser implements a DOM API.
  • A DOM Parser creates an internal structure in memory which is a DOM document object and the client applications get information of the original XML document by invoking methods on this document object.
  • DOM Parser has a tree based structure.
  • It supports both read and write operations and the API is very simple to use.
  • It is preferred when random access to widely separated parts of a document is required.
  • It is memory inefficient. (consumes more memory, whole XML document needs to loaded into memory).
  • It is comparatively slower than other parsers.


SAX (Simple API for XML)

  • A SAX Parser implements SAX API. This API is an event based API and less intuitive. it reads each unit of XML, it creates an event which can be used by calling program. It is used for high performance applications or areas where XML size might exceed the available memory. SAX reads the XML documents as a stream of XML tags(starting elements, ending elements, text sections etc). Programmer decides what to do with every event. SAX parser does not create any object at all, it simply delivers events.
  • It does not create any internal structure.
  • It is simple and memory efficient.
  • It is very fast and works for huge documents.
  • Here data is broken and client never have all the information.




StAX: Streaming API for XML

StAX is a pull parser. his parser pulls the required data from XML. It can give a significance performance improve. It maintains a cursor at the current position.

Difference between SAX parser and StAX parser:
  1. The SAX parser pushes the data whereas StAX parser pulls the data from XML
  2. StAX parser maintains a cursor at the current position to exract the content available at the cursor but SAX parser issues event when some certain data is encountered

HTTP

HTTP is the protocol that allows for sending documents back and forth on the web. A protocol is a set of rules that determines which messages can be exchanged, and which messages are appropriate replies to others.

HTTP Proxies
An HTTP proxy is a program that acts as an intermediary between a client and a server. It receives requests from clients, and forwards those requests to the intended servers. When a client uses a proxy, it typically sends all requests to that proxy, instead of to the servers in the URLs. Requests to a proxy differ from normal requests in one way: in the first line, they use the complete URL of the resource being requested, instead of just the path. For example,

GET http://www.somehost.com/path/file.html HTTP/1.0
 That way, the proxy knows which server to forward the request to.

HTTP Verbs

HTTP verbs tell the server what to do with the data identified by the URL. The request can optionally contain additional information in its body, which might be required to perform the operation for instance, data you want to store with the resource.

GET / HTTP/1.1

means the GET method is being used, while


DELETE /clients/anne HTTP/1.1
means the DELETE method is being used.


Other HTTP verbs are PUT,DELETE,OPTIONS,HEAD.


HTTP Status codes

The status code is meant to be computer-readable;
  • The status code is a three-digit integer, and the first digit identifies the general category of response:

  • 1xx indicates an informational message only
  • 2xx indicates success of some kind
  • 3xx redirects the client to another URL
  • 4xx indicates an error on the client's part
  • 5xx indicates an error on the server's part

Basic REST principels

REST is a simple way to organize interactions between independent systems. REST is a set of principles that define how Web standards, such as HTTP and URIs, are supposed to be used. The five key principles are:

  • Give every “thing” an ID
  • Link things together
  • Use standard methods
  • Resources with multiple representations
  • Communicate statelessly
Give every “thing” an ID

Everything that should be identifiable should obviously get an ID on the Web, there is a unified concept for IDs: The URI. URIs make up a global namespace, and using URIs to identify your key resources means they get a unique, global ID. The main benefit of a consistent naming scheme for things is that you don’t have to come up with your own scheme you can rely on one that has already been defined, works pretty well on global scale and is understood by practically anybody.  

Link things together

<order self='http://example.com/customers/1234' > 
   <amount>23</amount> 
   <product ref='http://example.com/products/4554' /> 
   <customer ref='http://example.com/customers/1234' /> 
</order>

we can easily imagine how an application that has retrieved it can “follow” the links to retrieve more information. This would be the case if there were a simple “id” attribute adhering to some application-specific naming scheme, too — but only within the application’s context. The beauty of the link approach using URIs is that the links can point to resources that are provided by a different application.
links are useful way to make an application dynamic.

Use standard methods

Set of standard methods includes GET,POST,PUT, DELETE, HEAD and OPTIONS.
GET supports very efficient and sophisticated caching, so in many cases, you don’t even have to send a request to the server. You can also be sure that a GET is idempotent — if you issue a GET request and don’t get a result, you might not know whether your request never reached its destination or the response got lost on its way back to you. The idempotence guarantee means you can simply issue the request again. Idempotence is also guaranteed for PUT (which basically means “update this resource with this data, or create it at this URI if it’s not there already”) and for DELETE (which you can simply try again and again until you get a result — deleting something that’s not there is not a problem). POST, which usually means “create a new resource”, can also be used to invoke arbitrary processing and thus is neither safe nor idempotent.

Resources with multiple representations

Provide multiple representations of resources for different needs. For example, Using HTTP content negotiation, a client can ask for a representation in a particular format:

GET /customers/1234 HTTP/1.1
Host: example.com 
Accept: application/vnd.mycompany.customer+xml  

The result might be some company-specific XML format that represents
customer information. If the client sends a different request, e.g.
one like this:

GET /customers/1234 HTTP/1.1
Host: example.com 
Accept: text/x-vcard 

The result could be the customer address in VCard format.

Communicate statelessly


In REST, stateless means that there is no client session data stored on the server. The server only records and manages the state of the resources it exposes. If there needs to be session-specific data, it should be held and maintained by the client and transferred to the server with each request as needed. A service layer that does not have to maintain client sessions is a lot easier to scale, as it has to do a lot fewer expensive replications in a clustered environment.