How to Add new Namespace to SOAP Envelope


There are some points you may face instead of keeping the body content’s namespace in the SOAP envelope, in such scenarios you can use Enrich Mediator to get this done.

Add the following enrich mediators three times and add new namespace to envelope where ever you want. And then use the new namespace inside a payload factory mediator.

   <enrich>
      <source type="body" />
      <target type="property" property="ORG_BODY" />
   </enrich>
   <enrich>
      <source type="inline">
         <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:newns="http://abc.com">
            <soapenv:Body />
         </soapenv:Envelope>
      </source>
      <target type="envelope" />
   </enrich>
   <enrich>
      <source type="property" property="ORG_BODY" />
      <target type="body" />
   </enrich>
   <payloadfactory>
      <!--we have to define the new namespace here too but it doesn't appear in the new body since it's already defined in the envelope tag -->
      <newns:bodycontent xmlsns:newns="http://abc.com">.....</newns:bodycontent>
   </payloadfactory>

Adding CA Signed certificate to WSO2 UES.


Many users prefer to install a CA signed certificate into the product, in-order to make products more secure.

You will get root certificate, intermediate certificates and the domain certificate from the CA as follows

AddTrustExternalCARoot.crt
COMODORSADomainValidationSecureServerCA.crt
COMODORSAAddTrustCA.crt
demo.abc.com.crt

The what you have to do is import these certificates in following order to the keystore file which the -genkeypair and -certreq were done (wso2carbon.jks).

keytool -importcert -keystore wso2carbon.jks -file AddTrustExternalCARoot.crt -alias somealias1 -trustcacerts
keytool -importcert -keystore wso2carbon.jks -file COMODORSAAddTrustCA.crt -alias somealias2
keytool -importcert -keystore wso2carbon.jks -file COMODORSADomainValidationSecureServerCA.crt -alias somealias3
keytool -importcert -keystore wso2carbon.jks -file demo_site_domain.crt -alias wso2carbon

If the response of the last command should be

Certificate reply was installed in keystore

not the

Certificate was added to keystore

Then copy the wso2carbon.jks to UES_HOME/repository/resources/security and it will replace the existing wso2carbon.jks file.

Then change the
Password and KeyPassword found in following classes with the one you used.

 repository/conf/identity.xml
repository/conf/carbon.xml

Then restart the server, if it doesn’t complain any password issue then it means, you have successfully configured the new keystore with CA signed certificate.

Usage of a Respond Mediator in WSO2 ESB


In a situation like you want to send a message back to the client depending on some conditions, we can use Respond Mediator. This mediator stops the processing on the current message and sends the message back to the client as a response. So just before this mediator we can use mediator like Payload Factory Mediator to construct the response.

Following is a simple use case for this,

Continue reading

How to move to out sequence from in sequence in WSO2 ESB


I faced a situation, where I need to send a custom response after the request is being sent to the message queue. I couldn’t use FORCE_SC_ACCEPT property as I need to send a response. In such a case we can use loopback mediator which switch from in sequence to out sequence and in side the out sequence payload factory mediator can be used to construct the custom response.

Simple skeleton as follows,

Continue reading

Linux Commands


I am updating this post when I find a useful linux command. And keep this post, just for my reference and for anyone who may find it helpful.

    1. Clear | Truncate the data in a file without opening.
      truncate -s 0 <file name>
    2. Find files which has given text in given location
      grep -nrwl  -e <"text to find">

      n: line number
      r : recursive
      w: match the whole word, if search for a part of the word omit w
      l : show the file only

    3. Create folder tree
      mkdir -p level_1/{level_2_a,level_2_b}
    4. Find and delete file | folder
       find . -name ".project" -exec rm -r "{}" \;
    5. List all the files which are edited in the given date range
       find repository/conf/ -type f -name "*.xml" -newermt 2015-05-11 ! -newermt 2015-06-07
    6. Print the last n number of lines of a file.
      tail -n file_name
    7. Find and replace the text of all the files recursively in given location.
      grep --include={*.java,*.xml} -rnl './' -e "find-text" | xargs -i@ sed -i 's/find-text/replace-text/g' @

JSON Support for WSO2 ESB Class Mediator


In WSO2 documentation you can find how the following mediators can be used with json payloads.

  • Log Mediator
  • Property Mediator
  • PayloadFactory Mediator
  • Switch Mediator
  • Filter Mediator

But there’s no any clue about how the class mediator can be used with json payloads. Following is the way you can do manipulation to json payload using class mediator.

In this post I just add a new tag to existing json payload

package com.wso2.test;

import org.apache.synapse.MessageContext;
import org.apache.synapse.commons.json.JsonUtil;
import org.json.JSONObject;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

/**
* Class mediator for JSON transformation.
*
* @author isuru
*
*/
public class JSONClassMediator extends AbstractMediator {

/**
* Holds the name.
*/
private String nameParam;

/**
* Mediate overridden method to set the token property.
*/
@Override
public boolean mediate(MessageContext context) {

try {

// Getting the json payload to string
String jsonPayloadToString = JsonUtil
.jsonPayloadToString(((Axis2MessageContext) context)
.getAxis2MessageContext());
// Make a json object
JSONObject jsonBody = new JSONObject(jsonPayloadToString);

// Adding the name:nameParam.
jsonBody.put("name", getNameParam());

String transformedJson = jsonBody.toString();

// Setting the new json payload.
JsonUtil.newJsonPayload(
((Axis2MessageContext) context).getAxis2MessageContext(),
transformedJson, true, true);

System.out.println("Transformed JSON body:\n" + transformedJson);

} catch (Exception e) {
System.err.println("Error occurred: " + e);
return false;
}

return true;
}

/**
* @return the nameParam
*/
public final String getNameParam() {
return nameParam;
}

/**
* @param nameParam
* the nameParam to set
*/
public final void setNameParam(String nameParam) {
this.nameParam = nameParam;
}
}

Continue reading

Generating Java Classes from IFC4 XSD Schema


To generate Java classes representing IFC Objects using IFC4 schema, I tried many ways and I got through this by using XJC XML Binding. But since JAXB is getting confused compiling the complex schema we have to specify confusing bindings in a separate file and provide it as a supporting file to solve the confusions.

if you run the following command it will get confused about the Path property.
(The -d is where the generated classes should be placed and -p is the package name of the classes being generated.)

$ xjc -d src/ -p com.test ifcXML4.xsd
parsing a schema…
[ERROR] Property “Path” is already defined. Use to resolve this conflict.
line 8381 of file:/media/isuru/Projects/stuff/ifcXML4.xsd
[ERROR] The following location is relevant to the above error
line 12910 of file:/media/isuru/Projects/stuff/ifcXML4.xsd
Failed to parse a schema.

To solve this add the following bindings.xjb file to the same folder where the schema is. Continue reading

Strategy Pattern


The definition of the strategy pattern is:

The Strategy Pattern defines a family of algorithms, 
encapsulates each one, and makes them interchangeable. 
Strategy lets the algorithm vary independently from clients that use it.

What are algorithms here?

"Identify the aspects of the application 
that vary and separate them from what stays the same."

Those aspects are the algorithms which can be encapsulated and make interchangeable, and it lets those varying aspects to vary independently from the client which uses those.

Continue reading

Design Principles


1. Identify the aspects of your application that vary and separate them from what stays the same.

Take what varies and encapsulate it so it won’t affect the rest of your code

The result is : Fewer unintended consequences from code changes and more flexibility in your systmes

2. Program to an interface (super type), not an implementation.

 

3. Favor composition over inheritance.

‘Has a’ can be better than ‘Is a’

 

4. Strive for loosely coupled designs between objects that interact.

Loosely coupled designs allow us to build flexible OO systems that can handle change because they minimize the inter-dependency between objects.

 

5. Classes should be open for extension, but closed for modification.

 

6. Depend upon abstractions. Do not depend upon concrete classes.

This is the Dependency Inversion Principle. High level components should not depend on low level components, instead they should both depend on abstractions. * Factory Method adheres to the dependency inversion principle.

Why it is called Dependency Inversion?
Normally higher level components depends on lower level components. After adhering to this principle, lower level components depends on higher level abstraction. So the top to bottom dependency chart has inverted itself and both higher level and lower level components start depending on abstraction.