mandag 18. november 2013

How I created testdata for a Clojure based statistic application

Currently I am working on a Clojure based statistic application showing the usage of a certain service we have in our netbank. 

The first step I wanted to have solved was to become independent from the database inside the bank. So I had to get me testdata. There were two options for me. 
The first one was to write a simple testdata generator. The disadvantage with that was that I would have spend time on building an application to be able to build the real target application. It would have cost me too much time even if I have plenty of that :) 

But I found my solution - the other option :) http://www.generatedata.com.
For my project the online testdata generator worked perfectly right in that sense that I could easily genate some testdata I can use for building my application and test it roughly.

søndag 27. oktober 2013

Starting Clojure - Web Application Intro with REST (Sven Malvik)

How to develop a Clojure Web Application with REST support. This demo uses Ring, Compojure and Hiccup for the Web Development part and Leiningen as the Project Manager. It is a short tutorial and meant to be a starting point for those who are new to Clojure.



All the code to this video are available on my GitHub account.
https://github.com/svenmalvik/SimpleClojureWebApp

Here comes a step by step tutorial for this video. It is the result of my notes for it.
http://blog.malvik.de/2013/10/how-to-write-standalone-clojure-based.html

How to write a Standalone Clojure based Web Application with an embedded Jetty Server

This blog entry is the result of my notes for the video "Starting Clojure - Web Application Intro with REST". It is meant to be a starting point of web application development with the Clojure language.

The Clojure syntax

Clojure is a dynamic programming language that targets the Java Virtual Machine. Clojure treats code as data and we write it in form of a list. Let's say we want to calculate (3 + 4). What most compilers do is to convert code expressions to an Abstract Syntax Tree (AST).  
The result of (3 + 4) would look like this.


Abstract Syntax Tree (AST)
In Clojure we write that AST directly in form of a list. (3 + 4) becomes (+ 3 4). So, if we would transform ((3 + 4) - (5 * 9)) into a Clojure list we would write it that way:
(- (+ 3 4) (* 5 9))


Web Application

Before we start we need leiningen. Leiningen is for automating Clojure projects (https://github.com/technomancy/leiningen).

- Create a project

Switch to your workspace and create a project using leiningen


> lein new mybank


I will just create an idea project as well.

- Add a web server

Inside your newly created project you will find a project.clj file where all the dependencies will be declared.


Here is how project.clj looks like after we created it.

(defproject mybank "0.1.0-SNAPSHOT"
 :description "FIXME: write description"
 :url "http://example.com/FIXME"
 :license {:name "Eclipse Public License"
           :url "http://www.eclipse.org/legal/epl-v10.html"}
 :dependencies [[org.clojure/clojure "1.5.1"]])


Check the version of Clojure if it is the one you want.

- Add now ring and compojure to your project file by adding these two libs to the key :dependencies
[ring/ring "1.2.0"]
[compojure "1.2.0-SNAPSHOT"]

Ring is a Clojure web applications library inspired by Python's WSGI and Ruby's Rack. By abstracting the details of HTTP into a simple, unified API, Ring allows web applications to be constructed of modular components that can be shared among a variety of applications, web servers, and web frameworks. (https://github.com/ring-clojure/ring)

Compojure is a small routing library for Ring that allows web applications to be composed of small, independent parts. (https://github.com/weavejester/compojure)

Leiningen offers a ring-plugin that automates common Ring task like starting a development web server
- Add ring-lein configurations to your project file.

:plugins [[lein-ring "0.8.7"]]
:ring {:handler mybank.core/my_routes
      :auto-reload? true
      :auto-refresh? false}

We just declared the my_routes as the entry point for our web server in the namespace core.
- Now we have to define my_routes in core.clj.

That’s how core.clj looks like before our changes.


- Define my_routes. Whenever we request for / we call the function foo.
(defroutes my_routes
 (GET "/" [] (foo "Jim"))
 (route/resources "/"))

The foo function does not work properly yet because it prints just to the console. 
- Let foo return a string by replacing println with str.

Here is a simpler version of foo.
(defn foo [x] (str "Hello, " x))
The namespace route is still unknown in core
We can change that by adding them the key :use in :ns and tell clojure that we only require compojure.route

(ns mybank.core
 (:use compojure.core)
 (:require [compojure.route :as route]))

- Resolve our new dependencies by executing
> lein deps

- Start the server.
> lein ring server 3000


- Replace the whole foo function call with (view/index-page) and
add [mybank.view :as view] in :require

- Delete the foo function

- Create a new file mybank.view.clj
(ns mybank.view)
(defn index-page [] "Bla")

- Restart the server
lein ring server 3000

- Include hiccup deps in the project
[hiccup "1.0.4"]

- Add this to :ns in view
 (:use hiccup.page hiccup.element)


(defn index-page []
 (html5
   [:html
    [:head]
    [:body "bla"]]))


- Restart the server lein ring server 3000


Want to add a REST service?

- Add [clojure.data.json :as json] in :require in core
- Add ring.middleware.params in :use in core
- Add [org.clojure/data.json "0.2.1"] in project
- Add this to my_routes
(GET "/rest" [] (json/json-str {:email “sven@malvik.de”}))

- To make it work we have to wrap my_routes
(def app (wrap-params my_routes))

- Let’s inform the project as well and change.
:ring {:handler mybank.core/my_routes to
:ring {:handler mybank.core/app

Ok, let the party begin.
- Restart the server.
> lein ring server 3000

Everything is ok? Stop the server again and let’s move on and deploy our very first clojure webapp.

- Stop the server for now.
Now, try lein run in the your console and you will get an error saying that you have to declare a main function. No :main namespace specified in project.clj.

- Let’s therefore add the main function in core.clj
(defn -main [& args] (run-jetty my_routes {:port 8080}))

- We have to tell the project where the main function is located by adding this to the project file :main mybank.core

- Add ring.adapter.jetty to :use in core as well.
(:use compojure.core ring.adapter.jetty)

- Try
> lein run

- Add (:gen-class :main true) to :ns in core.

lein clean & lein compile & lein uberjar
java -jar target/mybank-0.1.0-SNAPSHOT-standalone.jar


Vóila!

For the full code you can visit me on GitHub. The GitHub version also includes HTML templating with hiccup and AngularJS and how to connect to a database: https://github.com/svenmalvik/ClojureWebAppTemplate


torsdag 12. januar 2012

JBoss in 5 minutes (from Installing to Deployment)

In this 5 minute JBoss Quickstart Demo I will show how to install, setup, start, stop your JBoss AS7 in JBoss Developer Studio and deploy a simple dynamic web application.


onsdag 11. januar 2012

Migration my Playframework 1.0 project to version 2.0

I just worked on the migration from my little CMS4Play project which is based on the playframework  version 1.0 to the beta version 2.0. It was not as easy as exspected, unfortunately. Classes were moved, parameters became methods and of course the different template engine. All in all I can say that I would follow the advice Guillaume Bort gave:
"Remember: Play 1.2.3 is still the best way to write Web applications in Java while Play 2.0 is still under development, and will continue to be maintained and developed by the core team and contributors. Play 2.0 is an opportunity to move to for your next project, or the next major rewrite of your application."

fredag 6. januar 2012

Seam via JBoss ESB Quickstart


This Quickstart Demo shall demonstrate how to get access to a Seam Web Application through the JBoss Enterprise Service Bus.

torsdag 22. desember 2011

SOAP vs. REST

I just was wondering whether going for SOAP or REST. So I did a short research. Here my conclusions:

SOAP has standards and is designed to promote:

  • Interoperability (WS-I),
  • Security (WS-Security) and
  • Reliability (WS-Reliability).

When interacting with a SOAP service you know what to expect - it's always the same: WSDL, XML, SOAP envelopes/headers.

REST promotes no standards that describes the format of data being exchanged.
POST? GET? Query string? JSON? XML? TEXT?
Interface is never the same. Data formats are never the same.

SOAP helps preventing the flu. If you do get the flu, then you need REST.
Writing an iPhone application to interface with my bank I would need to go with SOAP.
Else, REST is just fine.

External Resources:
SOAP vs REST: The war between simplicity and standards
SOAP vs. REST