Saturday, August 29, 2015

Create a simple server with current directory's content using Python SimpleHTTPServer

When using Linux in a network environment you may want to get some files in one computer to another. Python's SimpleHTTPServer is a great tool which can be used to serve the contents of the current directory from the command line.

You just need to go inside the relevant folder using command line and give the following command.
This will start a server in port 8000 in your machine which hosts the contents of the current folder.

python -m SimpleHTTPServer

Then you can download the files from another machine using the curl command as below.

curl http://:8000/file-name.extension > file-name.extension 

Here's another way of making the current directory an http server with ruby

ruby -run -e httpd -- --port=8000

[1] https://docs.python.org/2/library/simplehttpserver.html

Thursday, April 23, 2015

Download File or Java Serializable object with REST

In this post I'm going to tell how to export a file or Serialzable java object in the server using a REST api call.

From the client's perspective he is downloading a file.
If you want to enable downloading a file in the server you can do it in the following way.
    @GET
    @Path("/{modelName}")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response exportFile(@PathParam("modelName") String modelName) {

        try {
            MLModelNew model = mlModelHandler.getModel(tenantId, userName, modelName);
            if(model != null) {
                String filePath = model.getStorageDirectory();
                File file = new File(filePath);
                return Response.ok(file).build();
            } else {
                return Response.status(Response.Status.NOT_FOUND).build();
            }
        } catch (MLModelHandlerException e) {
            logger.error(String.format(
                    "Error while retrieving model [name] %s. Cause: %s",
                    modelName, e.getMessage()));
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
        }
    }
There's another useful scenario of downloading a java serializable object as it is. This can be done by sending it as a stream. Here is how you can send a java object as a stream.
This example send an object type of MLModel as a stream.
    @GET
    @Path("/{modelName}")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response exportModel(@PathParam("modelName") String modelName) {

        try {
            MLModelNew model = mlModelHandler.getModel(tenantId, userName, modelName);
            if(model != null) {
                final MLModel generateModel = mlModelHandler.retrieveModel(model.getId());
                StreamingOutput stream = new StreamingOutput() {
                    public void write(OutputStream outputStream) throws IOException {
                        ObjectOutputStream out = new ObjectOutputStream(outputStream);
                        out.writeObject(generateModel);
                    }
                };
                return Response.ok(stream).build();
            } else {
                return Response.status(Response.Status.NOT_FOUND).build();
            }
        } catch (Exception e) {
            logger.error(String.format(
                    "Error while retrieving model [name] %s. Cause: %s",
                    modelName, e.getMessage()));
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
        } 
    }
References :

[1] http://examples.javacodegeeks.com/enterprise-java/rest/jax-rs-download-file/
[2] http://stackoverflow.com/questions/10100936/file-downloading-in-restfull-web-services
[3] http://howtodoinjava.com/2013/05/10/resteasy-file-download-example/
[4] http://rest.elkstein.org/2008/02/using-rest-in-java.html
[5] http://java.dzone.com/articles/streaming-apis-json-vs-xml-and
[6] http://stackoverflow.com/questions/29640523/streaming-the-result-of-rest-api-from-twitter
[7] http://stackoverflow.com/questions/3496209/input-and-output-binary-streams-using-jersey
[8] http://stackoverflow.com/questions/23421967/jax-rs-send-serialized-objects
[9] https://developer.jboss.org/thread/174955
[10] http://www.ibm.com/developerworks/library/j-5things1/
[11] http://www.postseek.com/meta/8ee7d7c8a67ab5f555f0403d19d27dd9 -

Friday, April 17, 2015

Writing a Test Class with In-memory H2 Database

While I was implementing unit tests for one java component, I had a requirement to write @Before method inside which I needed to initialize a database. Further I had to create a DataSource using it and bind it to the InitialContext.

According to the requirement, the database had to be populated with some initial data.

This post is about how I've used an in-memory H2 database inside the test classes.

    public static final String DRIVER_CLASS = "org.h2.Driver"; 
    public static final String CONNECTION_URL = "jdbc:h2:mem:WSO2ML_DB"; 
    public static final String USERNAME = "wso2carbon";
    public static final String PASSWORD = "wso2carbon";

    @Before
    public void createTestDB() throws SQLException, URISyntaxException, 
                                      NamingException, ClassNotFoundException, FileNotFoundException {

        // Create data source
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL(CONNECTION_URL);
        ds.setUser(USERNAME);
        ds.setPassword(PASSWORD);

        // Create initial context
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");

        // Bind data source -> 
        // DataSource can be accessed by : (DataSource) initContext.lookup("jdbc/WSO2ML_DB");
        InitialContext ic = new InitialContext();
        ic.createSubcontext("jdbc");
        ic.bind("jdbc/WSO2ML_DB", ds);

        Class.forName(DRIVER_CLASS);
        Connection connection = DriverManager.getConnection("jdbc:h2:mem:WSO2ML_DB", USERNAME, PASSWORD);

        // TO dump -> SCRIPT TO '<path-to-directory>/ml-db-dump.sql'
        // TO populate DB -> RUNSCRIPT

        // Populate DB with the given DB-dump
        URL resource = TestDB.class.getResource("/ml-db-dump.sql");
        String filePath = new File(resource.toURI()).getAbsolutePath();
        RunScript.execute(connection, new FileReader(filePath));
    }


References :
[1] http://stackoverflow.com/questions/3461310/how-can-i-bind-a-datasource-to-an-initialcontext-for-junit-testing
[2] http://www.journaldev.com/2509/jdbc-datasource-example-oracle-mysql-and-apache-dbcp-tutorial
[3] http://java.dzone.com/articles/presentation-and-use-h2
[4] http://www.h2database.com/javadoc/org/h2/jdbcx/JdbcDataSource.html
[5] http://stackoverflow.com/questions/3256694/how-in-h2db-get-sql-dump-like-in-mysql
[6] http://h2database.com/html/grammar.html#runscript
[7] http://stackoverflow.com/questions/10675768/executing-script-file-in-h2-database

Tuesday, March 17, 2015

How to see the H2 database content in a WSO2 Product

Every WSO2 product comes with an embedded H2 database as the default database configuration. There can be times we need to see the content of this database.

In this post, I'm going to explain how to see this database content through the H2 console.

First you have to configure the following in the <product-home>/repository/conf/carbon.xml file.

        
        8082
        
 
Once the configuration done as above, start the server(wso2 product you want).

Then open a browser and goto "http://localhost:8082/". You can see the login page as follows.



You need to give the JDBC URL, username and password.

You can find the relevant JDBC URL in the <product-home>/repository/conf/datasources/master-datasources.xml

For example, in ML, it is given as follows;

    WSO2ML_DB
    The datasource used for Machine Learner database
    
        jdbc/WSO2ML_DB
    
    
        
            jdbc:h2:repository/database/WSO2ML_DB;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=60000
            wso2carbon
            wso2carbon
            org.h2.Driver
            50
            60000
            true
            SELECT 1
            30000
        
    

Copy the url given in the element. Here it is jdbc:h2:repository/database/WSO2ML_DB.

Give the username and password given in the and elements in the above configuration. (The default username and password comes with every wso2 product is admin, admin)

You will see the H2 console like this.



Note :

When you need to do some change in the database of a wso2 product and setup the server with that change, you have to run the startup script as follows.

./wso2server.sh -Dsetup

Note that, in order to apply the changes first you have to delete the contents in the directory <product-home>/repository/database. Then start the server with -Dsetup option.

Tuesday, January 20, 2015

TCPMon to Monitor Messages

TCPMon is a tool which can be used to monitor the TCP based messages. Here I'm going to describe the steps to follow in order to monitor the messages using TCPMon.

I have two applications talking to each other.

One is WSO2 Identity Server, running on port 9443 in my local machine. The other is a web application deployed on Tomcat (port 8080).

The message exchange I want to monitor, happens in between the below endpoints. 

https://localhost:9443/samlsso
http://localhost:8080/travelocity.com/samlsso

To the Identity server the known endpoint of the other application is bind to port 8080. 

I need to monitor the messages send to port 8080, so what I need to do is just to change the Tomcat server's port. I changed it to 8081.

Then configured the TCPMon.



Listen port is configured as 8080. So all the messages send to 8080, comes to the TCPMon.

Then selected the option Listener. Then put the Tomcat server's hostname (127.0.0.1) as Target Hostname and the port (8081) as the Target port.

Here, what happens is TCPMon receives the messages come into port 8080 and forward to the port 8081 of the given host.

Now the messages can be seen through TCPMon.

Thursday, January 15, 2015

DTO and DAO

You may have seen in some Java codes there are Java class names end with ...DTO and ...DAO. Why do we need this practice?

First of all let's look at what they stand for?

DTO : Data Transfer Object
DAO : Data Access Object

DTO objects are used to transfer data between classes and modules of an application. DTO consists of only private fields, getters, setters and constructors. It is not recommended to add business logic methods to such classes. But they may contain some util methods. 

DAO classes encapsulate the logic for retrieving, saving and updating data in some data source or storage (a database, a file-system, etc.).
They abstract and encapsulate all access to the data source. The DAO manages the connection with the data source to obtain and store data.

Here is a good explanation about the DAO pattern.

Friday, January 9, 2015

How to Implement a Class Mediator for WSO2 ESB

A Mediator is simply a processing unit in the ESB. A mediator can do any modification to the messages.

In WSO2 ESB there are so many built-in mediators available. Also we can write custom mediators and add more to the available capabilities of the ESB.

Among those built in mediators Class Mediator is capable of creating an instance of a custom-specified class and set it as a mediator. There are two approaches to create custom mediators.
  • Extend the org.apache.synapse.mediators.AbstractMediator class
  • Implement the org.apache.synapse.api.Mediator interface 

In this post I'm going to show you how to implement a simple Class Mediator and using it in a proxy service.
  1. Create a maven project with the name OrderServiceMediator.
  2. You need to add the following dependency to your pom.xml
  3.         
                org.apache.synapse
                synapse-core
                2.1.2-wso2v4
            
    
  4. Also you will have to configure the required maven repository as follows. If this is not configured once you try mvn clean install it will give the following error.
    [ERROR] Failed to execute goal on project OrderServiceMediator: Could not resolve dependencies for project
  5.         
                wso2-nexus
                WSO2 internal Repository
                http://maven.wso2.org/nexus/content/groups/wso2-public/
                
                    true
                    daily
                    ignore
                
            
    
  6. Here is my pom.xml after configuring everything.
  7. 
        4.0.0
    
        org.wso2.training.manorama
        OrderServiceMediator
        1.0
    
        
        
        
            
                wso2-nexus
                WSO2 internal Repository
                http://maven.wso2.org/nexus/content/groups/wso2-public/
                
                    true
                    daily
                    ignore
                
            
        
    
        
            
                org.apache.synapse
                synapse-core
                2.1.2-wso2v4
            
    
        
        
    
    
Next step is to write the Class Mediator Java code. 
  1. Create a new package called mediator (or any other package name you prefer).  If you skip this step once you try to deploy the mediator in ESB Carbon will not make a bundle from it. So you will get the following error.
    wso2 esb org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
  2. Add a new Java class CustomMediator which extends the AbstractMediator.
  3. Now you will have override the public boolean mediate(MessageContext messageContext) method.
  4. Implement the logic inside this method to do whatever modifications you need to do to the messages .
  5. Here in my simple class mediator I am going to give a discount for the orders submit to the OrderProcessingService which is deployed in the Axis2 server. (This is the same OrderProcessingService described in my previous post).
  6. package mediator;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.synapse.MessageContext;
    import org.apache.synapse.mediators.AbstractMediator;
    
    import javax.xml.namespace.QName;
    
    /**
     * Created with IntelliJ IDEA.
     * User: manorama
     * Date: 1/05/15
     * Time: 10:25 AM
     * To change this template use File | Settings | File Templates.
     */
    public class CustomMediator extends AbstractMediator {
    
        private static final Log log = LogFactory.getLog(CustomMediator.class);
    
        private String discountFactor = "10";
        private String bonusFor = "10";
        private int bonusCount = 0;
    
        @Override
        public boolean mediate(MessageContext messageContext) {
    
            // xs:schema targetNamespace given in the WSDL is "http://ws.apache.org/axis2"
    
            String price = messageContext.getEnvelope().getBody().getFirstElement().
                    getFirstChildWithName(new QName("http://ws.apache.org/axis2","amount")).getText();
    
            int discount = Integer.parseInt(discountFactor);
            int bonusNo = Integer.parseInt(bonusFor);
            double currentPrice = Double.parseDouble(price);
    
            // Discounting factor is deducted from current price form every response
            Double lastPrice = new Double(currentPrice - currentPrice * discount / 100);
    
            // Special discount of 5% offers for the first responses as set in the bonusFor property
            if (bonusCount <= bonusNo) {
                lastPrice = new Double(lastPrice.doubleValue() - lastPrice.doubleValue() * 0.05);
                bonusCount++;
            }
    
            String discountedPrice = lastPrice.toString();
    
            messageContext.getEnvelope().getBody().getFirstElement().
                    getFirstChildWithName(new QName("http://ws.apache.org/axis2","amount")).setText(discountedPrice);
    
            log.info("Order price discounted");
            log.info("Original price: " + price);
            log.info("Discounted price: " + discountedPrice);
    
            return true;
        }
    
        public String getType() {
            return null;
        }
    
        public void setTraceState(int traceState) {
            traceState = 0;
        }
    
        public int getTraceState() {
            return 0;
        }
    
        public void setDiscountFactor(String discount) {
            discountFactor = discount;
        }
    
        public String getDiscountFactor() {
            return discountFactor;
        }
    
        public void setBonusFor(String bonus) {
            bonusFor = bonus;
        }
    
        public String getBonusFor() {
            return bonusFor;
        }
    }
    
    
  7. Note that the setter methods are implemented, to set the required property values through the mediator.
  8. Next we will create the proxy service to the back-end service deployed in the Axis2 server. You can follow the steps mentioned in the WSO2 ESB user guide to create the proxy service. Here is my proxy service configuration.
    
        
            
                
                    
                    
                
            
            
                
                
            
            
                
    scenario1
  9. Now try to send a message to this proxy service and see the discounted price is assigned for to the amount. You can see the resulted value after going through the mediator using this url.

    http://localhost:8080/axis2/services/OrderProcessingService/getAmount?orderID=25

References :
[1] https://docs.wso2.com/display/ESB481/Sample+380:+Writing+your+own+Custom+Mediation+in+Java
[2] https://docs.wso2.com/display/ESB481/Class+Mediator
[3] http://stackoverflow.com/questions/24260091/wso2-esb-jar-file-with-callback-handler-not-loading 

Thursday, January 8, 2015

Explaining Logging with java.util.Logging System

Logging is a kind of a recording mechanism of the activities occurred during the execution of a program. Further it can store exception and warnings as well as information which are mentioned as to log.

java.util.logging system facilitates logging functions. Logger is the java object used to log a message. Logger creates LogRecord object to store the relevant information to be logged and this object is passed to the handlers to be used for relevant formatting and printing.

Handler objects are assigned to the Logger. These are responsible for publishing the log messages to the external systems. The associated Filter objects do the relevant filtering.


When creating Loggers it is recommended to provide a name based on the package and the class name in which the logger is created.

import java.io.IOException;
import java.util.logging.Logger;

public class LoggerExample {

 private static final Logger LOGGER = Logger.getLogger(LoggerExample.class.getName());

 // Code goes here..........
}

Handler is responsible for publishing the log message at the target destination. It can be a file or the Standard output.

StreamHandler : Publishes all the log messages to an OutputStream
FileHandler : Records all the log messages to a specific file
SocketHandler : Publishes all the log messages to a network stream connection.
MemoryHandler : Keeps the log messages in a memory buffer

We can configure the handlers, formatters, filters and levels through the methods provided for the Logger class.

Also we can configure those using a properties file inside which the relevant configuration information are defined.

Here is a simple configuration file.

handlers=java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
confLogger.level=ALL

Using the above configuration file here is how to configure the logger.

import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class LoggerConfigurationExample {

     private static final LogManager logManager = LogManager.getLogManager();
     private static final Logger LOGGER = Logger.getLogger(LoggerConfigurationExample.class.getName());

     static{
           try {
                      logManager.readConfiguration(new FileInputStream("./logger.properties"));
           } catch (IOException exception) {
                      LOGGER.log(Level.SEVERE, "Error in loading configuration file",exception);
           }
     } 

     public static void main(String[] args) {
           LOGGER.fine("Test logging configuration");
     }
}

Happy coding.....:)