zaterdag 20 september 2014

Node.js reading json from rest service

Introduction:

The concept of a java script server is in my opinion revolutionary because of the fact that frontend technology also can be separated from the backend as a specialism. But what good is the one without the other. Creating a presentation  is no good without data. But what good is data if we cannot present it. So in this matter  I would show you that it is possible to combine the best of both worlds.


The solution:

In my previous post I described how to install a nodejs server and create an hello world application.
Because connecting to a rest service is a bit more complex we need to do some actions before we can start:

create an folder for example rest-api
cd rest-api

We need the superagent library:
npm install superagent@0.17.0 --save-dev

After this we can start building up the server:
// initializing the http package.
var http = require("http"),
//making the port available through a parameter 
port = process.argv[2] || 8888;
// initializing the superagent package
var superagent = require('superagent');
// creating the http server
http.createServer(function(request, response) {
//retrieving the rest url throught the superagent package
            superagent.get('http://localhost:8080/hello')
                .end(function(e, res){
//writing the res.text response from the rest server to the response which is the browser client response.
                    response.write(res.text, "binary");
                    response.end();
                })
//make the server listen to the port that is declared in my case 8090 or the standard port 8888
}).listen(parseInt(port, 10));
//just a friendly console message.
console.log("Static file server running at\n  => http://localhost:" + port + "/\nCTRL + C to shutdown");

Conclusion:

The world of javascript is not that different from the world of Java these days. There is this virtual machine, it is getting Object Oriented in a kind of way and you have to know your way in the library wilderness. This makes the gap between the two worlds smaller. There are still differences between the way of working between these kind of frontend technologies and Java as allways the devil is in the details.

Have fun!

vrijdag 5 september 2014

Hello world with node.js

Introduction:

In my previous post I wrote about the clean separation of concerns by using a javascript server like node.js. In the past I worked on a project where they used node.js but personally I never got involved. I wrote the backend part of the system where we used rest services that returned json as an answer for the node.js server (javascript can handle json). 

So curiousity killed the cat but made man kind evolve. So I tried to set this up on my linux machine. Googled around a bit and found the answers to my questions. 

Installation:

actually on linux it is pretty basic if you have git running:
My attempt under debian:


  • git clone --depth 1 git://github.com/joyent/node.git
  • cd node
  • git checkout v0.4.11
  • export JOBS=2
  • mkdir ~/local
  • ./configure --prefix=$HOME/local/node
  • make
  • make install
  • echo 'export PATH=$HOME/local/node/bin:$PATH' >> ~/.profile
  • echo 'export NODE_PATH=$HOME/local/node:$HOME/local/node/lib/node_modules' >> ~/.profile
  • source ~/.profile

My attempt under ubuntu:
  • sudo apt-get install curl
  • curl -sL https://deb.nodesource.com/setup | sudo bash -
  • sudo apt-get install nodejs
(The path under ubuntu will bie /usr/bin/node)


After you done litterly this (take care of the $PATH ELEMENTS. You need to replace them with the real path where you put the node server). 
after this you can create a test.js in any folder you like containing:
//require is the import in java
 var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
//creating the http server
var server = http.createServer(function (request, response) {
//bare writing to the response like in the old days with Java.
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8090);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8090/");
After you saved the file you have to tell nodejs that he has to start this application:
  1. ./node test.js in your console in the node directory
  2. Start a browser on the desired port 8090 in my case
The result should be hello world on a snowy white page.

have fun!

dinsdag 2 september 2014

separating the frontend from the backend

Introduction

Through the years I seen a couple of application architectures. Some of them had the multi tier design but where  still one war file. I never seen that as a bad design. That was the best available way. The fun part of a way like that was that the architect had to defend the borders between the layers. That is where the weak point in this architecture lies. It is easy for a developer, for what ever reason, to cross the borders and start mixing the responsibilities of the different tiers. This is the first step of downgrading an well setup application to a kind of chaos architecture.

As allways in time ideas improve. A couple of years ago we suddenly had SOA. The backend logic was pushed back into REST or SOAP services with a separate project that was the frontend. The downside of this architecture was, that the html code was still infected with tags from java frameworks. Fore example
JSF had is own XHTML and even the newer like Wicket have there own tags. The most Frontend guys dont like that. They know html and they dont want to know about all the java frameworks.

The other downside of weaving the java frameworks into html is: that if the compagny decides that in some cases it should not be Java but something like PHP or Ruby etc... they still endup with a java frontend framework.

Now we enter a time where the possibilities to separate the frontend from the backend in a sharper and smarter way. It is possible to run a frontend tier completely on a javascript based server like node.js. Here is a list of js solutions.

The architecture:


Behind teh node js server part you can build as many java rest services as needed. Now we have a complete separation from the frontend and the backend. On the node.js server it is also possible to declare small html objects like textboxes or buttons etc...

Conclusion:

There are allready compagnies working on a application architecture like this or have in place. It requires to see the frontend as separate specialism and for that you need people that know how to deal with a frontend architecture like this. As a backend developer you have to let te responsibility for the frontend go and concentrate on what you can do actually the best backend development.

Have fun!


maandag 11 augustus 2014

From String to calculation

Introduction:

After the marshalling drill I have been thinking about how can we do small changes in xml without marshalling. The only other option I could come up with is xpath queries combined with element value changes. For primitive type changes this is a good idea but what if someone needs some complex calculations with a value from the xml file. Even this can be done.

The solution:

What I could think of was some kind of calculation template in a String something


//The numbers to calculate.
        int number1 = 40;
        int number2 = 2;

//Creating the javascript engine
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("JavaScript");

//The calculation template can be more complex
        String calcuationTemplate = "%d * %d";
        calcuationTemplate = String.format(calcuationTemplate, number1,number2);
        try {
//The actual calculation
            System.out.println(engine.eval(calcuationTemplate));
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }

The conclusion:

After some googling and trying I found that yet again this is pretty simple. With this I can do as much math I like in a very simple way. The only thing I still have to take care of is a difference between the numbers that have been given and the numbers that are retrieved from the xml file.

have fun!

vrijdag 8 augustus 2014

replace diacritic characters

Introduction:

We had a request to remove all the diacritic  from a String. As usual Java has a nice solution for this.

Solution:


String str = "ÚÝâ";

String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD);
Pattern pattern = Pattern .compile("\\p{InCombiningDiacriticalMarks}+");

System.out.println(pattern.matcher(nfdNormalizedString).replaceAll(""));

result: UYa

The Normalizer class according to the javadoc:

 This class provides the method normalize which transforms Unicode
 text into an equivalent composed or decomposed form, allowing for easier
 sorting and searching of text.


Normalizer.Form.NFD
This is an Enum that tells the Normalizer class what kind of normalisation you want there are:

     /**
         * Canonical decomposition.
         */
        NFD,

        /**
         * Canonical decomposition, followed by canonical composition.
         */
        NFC,

        /**
         * Compatibility decomposition.
         */
        NFKD,

        /**
         * Compatibility decomposition, followed by canonical composition.
         */
        NFKC

The NFD variant decomposes the Ú in U and ' this makes it possible to do a regular expression like 
Pattern pattern = Pattern .compile("\\p{InCombiningDiacriticalMarks}+");

At last but not least the question we had was: what in heavens name is \\p{InCombiningDiacriticalMarks}
This answer we found at stackoverflow:

\p{InCombiningDiacriticalMarks} is a Unicode block property. In JDK7, you will be able to write it using the two-part notation \p{Block=CombiningDiacriticalMarks}, which may be clearer to the reader. It is documented here in UAX#44: “The Unicode Character Database”.

Conclusion:

The code we build today is just a tip of the iceberg. The functionality that the Normalizer can bring is much more. I hope in the future I can do some other stuff with this.

Have fun! 

dinsdag 5 augustus 2014

open closed Marshalling with Jaxb



Introduction

One of the things we had to tackle this week was some marshalling issue with a zillion small xml files. The feast of legacy. It was a very depressing thought to create separate marshalling objects for each of them. That Idea popped faster out of my head then in. After some thinking we came up with a component that could do all of the xml files

The solution

The idea I came up with is very simple it is base on Jaxb marshalling which is quit powerfull in the way that the code you need to write is very small. Also the speed of marshalling and unmarshalling is quit fast.

To get this to work you need the following dependencies:

<dependencies>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.14.4</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
   //This one you only need when you want to use xpath queries
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.0</version>
    </dependency>
</dependencies>


The one and only class for marshalling and unmarshalling:

package nl.lostlemon.Marshal;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

//The Type is to make it possible to instanciate this class with different marshalling beans.
public class Marshalling<T> {

    // Notice that T is the return typed bean. The xm is an inputStream. Jaxb can deal with that. It improves       performance 
    public T unMarshall(final InputStream xmlStream, final Class clazz) {
        T value = null;
        try {
//create an unmarshaller
            Unmarshaller unmarshal = JAXBContext.newInstance(clazz).createUnmarshaller();
//read the xml input stream into Jaxb
            XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(xmlStream);
//marshall the xml file into a JavaBean
            value = (T)unmarshal.unmarshal(reader, clazz).getValue();
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }

        return value;
    }
//Notice that the parameter is also The T of type.
    public ByteArrayOutputStream marshal(final T type) {
//Create the return type for this method.
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
//Create a marshaller
            Marshaller marshaller = JAXBContext.newInstance(type.getClass()).createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//Create the xml in the form of an outputstream to boost performance
            marshaller.marshal(type, outputStream);
        } catch (PropertyException e1) {
            e1.printStackTrace();
        } catch (JAXBException e1) {
            e1.printStackTrace();
        }
        return  outputStream;
    }
}

Running the following tests:

//This test uses the BookBean with an xml containing a single book.
    @Test
    public void testUnmarshallingXpath() {
        InputStream inputStream = this.getClass().getResourceAsStream("testSingle.xml");
        Marshalling<SingleBookBean> marshalling = new Marshalling<SingleBookBean>();
        SingleBookBean bookBean = marshalling.unMarshall(inputStream,SingleBookBean.class);
        Assert.assertTrue(bookBean.getTitle() != null);
    }

// The BookBean the @Getter and @Setter is from Lombok.
// Take care of the 
@XmlRootElement(name = "book")
@XmlAccessorType(XmlAccessType.FIELD) These are the identifiers to tell JaxB what to do. The XmlRootElement refers to the xml rootelement in the xml file.

@Getter
@Setter
@XmlRootElement(name = "book")
@XmlAccessorType(XmlAccessType.FIELD)
public class BookBean {
  // These are the exact same names as the xml Elements
    private String author;
    private String title;
}

//The single xml file:
<book>
    <title>witches abroad</title>
    <author>Terry Pratchet</author>
</book>

//This test is using the books bean with an xml containing multiple books.
    @Test
    public void testUnmarshallingList() {
        long start = Calendar.getInstance().getTimeInMillis();
        InputStream inputStream = this.getClass().getResourceAsStream("test.xml");
        Marshalling<Books> marshalling = new Marshalling();
        Books books = marshalling.unMarshall(inputStream,Books.class);
        //Assert.assertTrue(books.getBookBeans().size() == 3);
        long stop = Calendar.getInstance().getTimeInMillis();
        long elapsedTime = (stop-start);
        System.out.println("number of elements " + books.getBookBeans().size() + " elapsed time " + elapsedTime + " ms");

    }

// The Books bean based on the same principle as the BookBean.
@XmlRootElement(name = "catalog")
@XmlAccessorType(XmlAccessType.FIELD)
public class Books {

    @Getter
    @Setter
    @XmlElement(name = "book", type = BookBean.class)
    private List<BookBean> bookBeans;
}

//the multiple books xml file:
<catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>
        <description>An in-depth look at creating applications
            with XML.</description>
    </book>
    <book id="bk102">
        <author>Ralls, Kim</author>
        <title>Midnight Rain</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-12-16</publish_date>
        <description>A former architect battles corporate zombies,
            an evil sorceress, and her own childhood to become queen
            of the world.</description>
    </book>
    <book id="bk103">
        <author>Corets, Eva</author>
        <title>Maeve Ascendant</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-11-17</publish_date>
        <description>After the collapse of a nanotechnology
            society in England, the young survivors lay the
            foundation for a new society.</description>

    </book>
</catalog>

//The marshalling test with the books bean:

    @Test
    public void testMarshallingList() {
        Books books = new Books();
        List<BookBean> bookBeans = new ArrayList<BookBean>();
        for(int i=0;i<100000;i++) {
            BookBean bookBean = new BookBean();
            bookBean.setAuthor("J.R.R Tolkien");
            bookBean.setTitle("The hobbit");
            bookBeans.add(bookBean);
        }
        books.setBookBeans(bookBeans);
        long start = Calendar.getInstance().getTimeInMillis();
        Marshalling<Books> marshalling = new Marshalling<Books>();
}

// The marshalling test with the BookBean.
   @Test
    public void testMarshalling() {
        BookBean bookBean = new BookBean();
        bookBean.setAuthor("Terry Pratchet");
        bookBean.setTitle("Witches abroad");
        Marshalling<BookBean> marshalling = new Marshalling<BookBean>();
        String xml = marshalling.marshal(bookBean).toString();
    }

Conclusion: 

Using Java typing in combination with a usefull library in the open closed principal, saves you a lot of code writing and maintainance. I had real pleassure to write in such a small piece of code so much power. 

Have fun!

dinsdag 24 juni 2014

Email Validation

Introduction

I was strugling with the apache commons email validator. This litle baby was not working according the specs they gave me. After a while I gave up on that and descided against my own rules to build it myself. What I mean with against my own rules is: Dont build if it is out there in a proper library. But in this case I had an exception. 

The best way to solve this issue was to use a regular expression. Not my favorite but mighty handy when you need them. 

Solution:

The scary part:

For those who have no experience with R.E. this might look like cursing in a comic :).

"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]+)$";

Lets analyze this:First lets have a look at the structure and later on we spill some information about the details.
The first thing you might notice in the structure are blocks like these [_A-Za-z0-9-\\+]+the A-Z, a-z or 0-9 this means that it contains an character in capitals (A-Z), or in non-capitals(a-z)or digits from 0-9. if the notation is like:[_A-Za-z0-9] It will only search for one character each.if you add a + it means iterate till you can find no more.if you add {2} or {2,5} instead of a + it means only 2 characters or from 2 to 5 characters.
In the case of the email validator you also need one .(dot) and one @ as you can see they are not followed by the annotations I described to iterate for more. 
Some of the details:
^ the carrot sign before the brackets means "Starts with" So in this case It can be a multiple underscores, characters or numbers.
The ^ inside the brackets means not like [^A-Z] everything but capitals.
The * is the same as +  occurs 1 or more times (iteration).
The $ checks if the end of line is following. 

Conclusion:

It still looks like cursing in a comic or something that gives you an headache. But it is the way forward if you want to check a string on patterns. This whole exercise is useless without execution. So you still need to use the Pattern and Matcher class (Examples all over the internet).
Or use the String.matches method. Will do two.

Have fun!