Friday, June 17, 2011

http.async.client v0.3.0

http.async.client v0.3.0 intorduces few changes.


Explicit client usage

As it turned out, having implicit client management and default client started by itself, was not the smartest thing to do.

Since I have very little respect for code that is wrong, I've changed it.

Now client has to be managed explicitly, meaning you have to create and close it yourself.

All methods that issue requests now expect client as first argument.


This breaks API compatibility with anything prior to v0.3.0.

I repeat: you will have to create and close client yourself.

with-client is gone

with-client macro is gone now. It might have been useful with implicit client management, but with explicit situation it would be just a copy of with-open.

Please use with-open in connection with create-client.

Migration path from:


To:

Preemptive authentication

Thanks to xeqi for patch that provides means of controlling preemptive authentication both per request and global.

Clojure 1.2.1 support

Fixed cyclic dependency in http.async.client that was not reported in 1.2.0 but prevented you from been able to load it 1.2.1.

async-http-client v1.6.3

Underlying async-http-client library version has been upgraded to 1.6.3.

Thursday, March 31, 2011

Announcing status-codes

Announcing status-codes

status-codes is easy response status codes

It's easy status codes for compojure.
Not that status codes in compojure are particularly hard.
I just prefer in applications that I work with to use keywords for
status codes, rather than status codes as numbers.

(routes
(GET "/ok" _ :ok)
(GET "/accepted" _ :accepted)
(GET "/ok-map" _ {:status :ok})
(GET "/accepted-map" _ {:status :accepted}))

Not much really, but it reads better than 200, 202 and so on.
In order to have this sugar in your routes pick status-codes from
clojars, and require/use status-codes:

(ns your.ns
(:use status-codes))

Enjoy.

Thursday, October 7, 2010

Async HTTP Client 1.2.0

Async HTTP Client 1.2.0

Intro

About month ago we released async-http-client 1.1.0.
During this month we received lots of contributions and user feedback.
We took all of this great feedback into consideration,
and here it is: lots of bug fixes and new features in async-http-client are ready to be released.
You can get it from Maven Central near you.

Highlights

To cut the long story short here are highlights of few new features.
  • JDK based provider

    This release support a new AsyncHttpProvider based on JDK's URLConnection.
    This provider can be used in environments that aren't supporting NIO.
    AsyncHttpClient client = new AsyncHttpClient(new JDKAsyncHttpProvider(config));
  • Authentication

    The library now support 3 authentication scheme: Basic, Digest and NTLM.
    RequestBuilder r = client.prepareGet(url)
    .setRealm((new Realm.RealmBuilder())
    .setScheme(AuthScheme)
    .setPrincipal(user)
    .setPassword(admin)
    .setUsePreemptiveAuth(true).build());
    AuthScheme can be BASIC, DIGEST or NTLM.
  • SSL Tunneling support

    This release also adds support for SSL Tunneling.
    AsyncHttpClientConfig.Builder b = new AsyncHttpClientConfig.Builder();
    b.setFollowRedirects(true);

    ProxyServer ps = new ProxyServer(ProxyServer.Protocol.HTTPS, "127.0.0.1", port1);
    b.setProxyServer(ps);

    AsyncHttpClientConfig config = b.build();
    AsyncHttpClient asyncHttpClient = new AsyncHttpClient(config);

    RequestBuilder rb = new RequestBuilder("GET").setUrl(url);
    Future<Response> responseFuture = asyncHttpClient.executeRequest(rb.build(), new AsyncCompletionHandlerBase() {

    public void onThrowable(Throwable t) {
    log.debug(t);
    }

    @Override
    public Response onCompleted(Response response) throws Exception {
    return response;
    }
    });
  • WebDav

    WebDav is now supported and can be used with two interfaces.
    • Low level
      AsyncHttpClient c = new AsyncHttpClient();

      Request mkcolRequest = new RequestBuilder("MKCOL").setUrl("http://127.0.0.1:8080/folder1").build();
      Response response = c.executeRequest(mkcolRequest).get();

      Request putRequest = new RequestBuilder("PUT").setUrl("http://127.0.0.1:8080/folder1/Test.txt").setBody("this is a test").build();
      response = c.executeRequest(putRequest).get();

      Request propFindRequest = new RequestBuilder("PROPFIND").setUrl("http://127.0.0.1:8080/folder1/Test.txt").build();
      response = c.executeRequest(propFindRequest).get();

      // Check the response.
      response.getStatusCode(); //Will return 207
      response.getResponseBody().contains("<status>HTTP/1.1 200 OK</status>"));
    • High level

      You can also use a special AsyncHandler which construct a response
      based on the webdav response's code.
      AsyncHttpClient c = new AsyncHttpClient();

      Request mkcolRequest = new RequestBuilder("MKCOL").setUrl("http://127.0.0.1:8080/folder1").build();
      Response response = c.executeRequest(mkcolRequest).get();
      assertEquals(response.getStatusCode(), 201);

      Request propFindRequest = new RequestBuilder("PROPFIND").setUrl("http://127.0.0.1:8080/folder1/").build();
      WebDavResponse webDavResponse = c.executeRequest(propFindRequest, new WebDavCompletionHandlerBase<WebDavResponse>() {

      @Override
      public WebDavResponse onCompleted(WebDavResponse response) throws Exception {
      return response;
      }
      }).get();

      webDavResponse.getStatusCode(); // will return 200.
  • Zero copy

    Sometimes you need to just download some content, with out
    copying/inspecting the bytes in memory, just download and store.
    File file = new File(url.toURI());

    File tmp = new File(System.getProperty("java.io.tmpdir") + File.separator + "zeroCopy.txt");
    final FileOutputStream stream = new FileOutputStream(tmp);
    // Upload a file using setBody(File);
    Future<Response> f = client.preparePost("http://127.0.0.1:" + port1 + "/").setBody(file).execute(new AsyncHandler<Response>() {
    public void onThrowable(Throwable t) {}

    public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
    bodyPart.writeTo(stream);
    // Copy the bytes directly to a File. In our case it is an FileInputStream
    return STATE.CONTINUE;
    }

    public STATE onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
    return STATE.CONTINUE;
    }

    public STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception {
    return STATE.CONTINUE;
    }

    public Response onCompleted() throws Exception {
    return null;
    }
    });

    Response resp = f.get();
    stream.close();
  • Automatic HTTPS redirects

    Another new feature is we do support redirect from http to HTTPS automatically:
    AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setFollowRedirects(true).build();
    AsyncHttpClient c = new AsyncHttpClient(cg);

    Response response = c.preparePost(url).execute().get();
    If the server redirect us to https, we will follow the new url
    without any issue.

Changelog

Friday, September 10, 2010

Asynchronous HTTP Client for Clojure and Twitter Streaming API

http.async.client & Twitter Streaming API

http.async.client is a Clojure library based on Asynchronous Http Client for Java which runs on top of Netty Project.

So much for introduction, lets see some sample code that uses Twitter streaming api.

Importing:

(ns twitter-sample
(:require [http.async.client :as c]
[org.danlarkin.json :as j]))

Settings and helper to print twits as they fly by:

(def u "username")
(def p "password")

(defn print-user-and-text [s]
(let [twit (j/decode-from-str s)
user (:screen_name (:user twit))
text (:text twit)]
(println user "=>" text)))

Lets read statuses/sample:

(doseq [twit-str (c/string
(c/stream-seq :get "http://stream.twitter.com/1/statuses/sample.json"
:auth {:user u :password p}))]
(print-user-and-text twit-str))

Or statuses/filter:

(doseq [twit-str (c/string
(c/stream-seq :post "http://stream.twitter.com/1/statuses/filter.json"
:body {"track" "basketball,football,baseball,footy,soccer"}
:auth {:user u :password p}))]
(print-user-and-text twit-str))

Those two samples show also how to consume HTTP Streams as clojure.core/seq, a new feature that was introduced in v0.2.0.

You can also handle sequences via callbacks.

Requesting HTTP resources that are not streams is even simpler:

(let [response (c/GET "http://github.com/neotyk/http.async.client/")]
(c/await response)
(c/string response))

There is extensive documentation available on the github pages of the project, you can also find Changelog and API.

Big thanks go to Amsterdam Clojurians, this library received so much love and help from this group.
Kudos to Jeanfrancois Arcand for starting async-http-client recently reaching the 1.1.0 release.

I would love to hear from you what you think of this library.

Monday, June 7, 2010

clojure.core/lazy-seq makes sense

clojure.core/lazy-seq makes sense

Few days ago on #clojure bartj asked about an infamous wiki entry.
I remember that while ago, when I tried to understand laziness in Clojure I stumbled upon this entry as well,
and it caused me only confusion, as it is totally outdated.
Let's try to shed some light on it.

Not lazy seq

When you construct a seq in non lazy way you should expect that all elements are realized at creation time.
(let [a (cons 1 (cons 2 nil))] (first a))
=> 1
Nothing special here. Let's introduce side effects to see what happens.
(let [a (cons
(do (println "one") 1)
(cons
(do (println "two") 2)
nil))]
(first a))
: one
: two

=> 1
As you expected all elements has been realized while constructing a, though we only needed the first element.
And that's exactly where laziness kicks in.

lazy-seq

First lets construct lazy seq with no side effects.
(let [a (lazy-seq (cons 1
(lazy-seq (cons 2 nil))))]
(first a))
=> 1
So what is new? You have to type more and effect is the same. Why?
Adding side effects will help understanding it.
(let [a (lazy-seq (cons
(do (println "one") 1)
(lazy-seq (cons
(do (println "two") 2) nil))))]
(first a))
: one
=> 1
Now you should be able to see the benefit. Result is the same, except the whole sequence hasn't been realized,
except for the first element, as it was the only element that was needed.

Laziness is a very powerful tool:
  • you can construct infinite sequences,
  • have side effects create elements on demand, if seq construction is heavy, you might want to only issue creation of elements you'll use,
  • it has same interface as non-lazy seq, code using it does not have to care about it.

To sum it up

(let [a (cons
(do (println "one") 1)
(cons
(do (println "two") 2) nil))])
: one
: two

=> nil
We didn't need even single element but the whole seq was realized.
(let [a (lazy-seq (cons
(do (println "one") 1)
(lazy-seq (cons
(do (println "two") 2) nil))))])
=> nil
If not even a single element is needed, than not even a single element will be realized.
So there you have it clojure.core/lazy-seq explained.

Monday, January 11, 2010

clojure.core/str makes sense now

clojure.core/str makes sense

While reading excellent Programming Clojure by Stuart Halloway, instead just reading I started looking into how things are done by Clojure itself.
Yesterday it took me some time before I understood how things are done by clojure.core/str. This post is documentation of my findings.
Following modification of said function was very helpful in understanding how it works:
:tag metadata key is a symbol naming a class or a Class object that indicates the Java type of the object in the var, or its return value if the object is a fn..
Next are three parameters body pairs.
([] "")
No arguments (arity 0) will result in "" (empty string)

([#^Object x]
(if (nil? x) "" (. x (toString))))
One argument call (arity 1) will call java.lang.Object.toString method.
#^Object is a Type Hint.
if should not need much explanation.

([x & ys]
((fn [#^StringBuilder sb more]
(if more
(recur (. sb (append (str (first more)))) (next more))
(str sb)))
(new StringBuilder #^String (str x)) ys)))
This is where it starts to be interesting.
First unnamed function is defined that accepts StringBuilder sb and more params.
If more is true, that is not nil or false than call recursively self with new parameters
  • sb with appended result of calling str with first element of more,
  • more with elements after first.
If more is false, that in our context will happen only when more is nil, call sb's toString.
Next just defined function is getting called with new StringBuilder constructed from x as sb and ys as more.

It took me some time to realize that ys are turning into more.

So there you have it clojure.core/str explained.

Saturday, December 26, 2009

Clojure Start Again

Clojure start again

It's my other attempt to learn Clojure.
The landscape has changed a lot since last time I took a look.

Toolbox

Some tools that I used to play comfortably

Building and dependency tracking

Most popular is unfortunately Ant in building Clojure. I'm not looking forward to editing build.xml files again.
But there are two alternatives that keep track of dependencies, and do packaging.

Leiningen - build tool

Leiningen is a build tool for Clojure designed to not set your hair on fire.
That certainly sounds nice. Installation is straightforward as well, following Zefs instructions does the trick.
It boils down to this:
cd ~/bin
wget http://github.com/technomancy/leiningen/raw/stable/bin/lein
chmod +x lein
lein self-install
Leiningen is wrapping up Maven2 in nice Clojure syntax and gives you by default access not only to Maven Central Repository but also clojars.org.

clojure-maven-plugin - build tool

Another option is clojure-maven-plugin Maven2 plug-in to help you out with Clojure.
Adding following snippet should get you started:
<plugins>
<plugin>
<groupId>com.theoryinpractise</groupId>
<artifactId>clojure-maven-plugin</artifactId>
<version>1.3</version>
</plugin>
</plugins>

Clojuresque Third option

Responses I received after publishing this post showed third option that I've missed.
It is used by ClojureQL and certainly has supporters. It lacks a bit of documentation but ClojureQL - Where are we (going)? describes process quite well.

IDE

There is quite some options here, following one is most popular amongst Clojure developers.

Emacs + SLIME

Well normally I use VIM, but after watching Dynamic Interactive Webdevelopment I took for a spin Emacs, well Aquaemacs.
Following two posts were very helpful in setting up nice environment:
After some problems that got solved by setting up swank-clojure-classpath my new IDE was up.
And it welcomes me with message: Connected. Your hacking starts... NOW!

Lets get learning Clojure.

Wednesday, October 21, 2009

Test your build with Maven 3

Maven 3 is coming

This post is to show you how to get Maven 3.
Maven 3 is supposed to be drop in replacement of Maven 2.
So you should be able to build your Maven based build with Maven 3 with no problem.
So lets take Maven 3 for a spin.

Getting Maven 3

You can get Maven 3 binaries or build it by hand.

Getting binaries

You can grab binaries from Sonatypes Hudson grid.
Depending on your platform:
Linux
lastSuccessfulBuild from ubuntu
Windows
lastSuccessfulBuild from windows
And extract it to your new M2_HOME.

Building from source

Check it out, and build:
$ svn co http://svn.apache.org/repos/asf/maven/maven-3/trunk maven-3
$ cd maven-3
$ export M2_HOME=/your/new/m3/home/
$ ant
So you have your own build of Maven 3 at /your/new/m3/home/. Congratulations!

Add to path

Independently how you got your new Maven 3, you need to add M2_HOME/bin to your PATH environment variable.

Start using it

And that is it, you have Maven 3.
As verification step execute, end expect something similar:
$ mvn -version
Apache Maven 3.0-SNAPSHOT (r828135; 2009-10-21 20:46:19+0200)
...
You probably will see quite some warnings while building your projects that previously where unnoticed.
Following those instructions will improve your build.
Enjoy your new build tool!

Wednesday, July 8, 2009

MOP version 1.6

It's been some time since I performed MOP release.

Quite a few things has changed in MOP:
With new release 1.6 that can be found in Maven Central Repository new configuration option has been introduced.

Let me introduce you to showVersion option :-)
showVersion

Thursday, April 2, 2009

Securing Grizzly Web Server

Running GrizzlyWebServer is very easy and lightweight. Serving HTTPS? The same.

It's just one more step to perform, provide Grizzly with SSL configuration.

But first thing first. Lets create GrizzlyWebServer telling it to serve secure content.
Just invoke this constructor:

/**
* Create a WebServer that listen for secure tls/https requests
* @param port The port opened
* @param webResourcesPath the path to the web resource (ex: /var/www)
* @param secure true if https needs to be used.
*/
public GrizzlyWebServer(int port, String webResourcesPath, boolean secure)
Say like this:
gws = new GrizzlyWebServer(PORT, ".", true);
If you would try start() now you get exception.
What is missing is SSL configuration.

Grizzly comes with SSLConfig helper class, that allows you to work with SSL configuration easily.
SSLConfig cfg = new SSLConfig();
This constructor fetches configuration from default properties locations, which are:
  • trust store - javax.net.ssl.trustStore
  • key store - javax.net.ssl.keyStore
  • trust store password - javax.net.ssl.trustStorePassword
  • key store password - javax.net.ssl.keyStorePassword
  • trust store type - javax.net.ssl.trustStoreType
  • key store type - javax.net.ssl.keyStoreType
If you need to override any configuration value, like key store location, just do:

URL resource = getClass().getClassLoader().getResource("test-keystore.jks");
if (resource != null) {
cfg.setKeyStoreFile(new File(resource.toURI()).getAbsolutePath());
} else {
// Couldn't find keystore, exit appropriately.
}
Once you have configuration ready give it to Grizzly:
gws.setSSLConfig(cfg);
And you are done, now do normal setup like providing GrizzlyAdapters and start().

Default configuration values are:
  • key store file - keystore.jks
  • key store type - JKS
  • key store algorithm - SunX509
  • key store pass - changeit
  • trust store file - truststore.jks
  • trust store type - JKS
  • trust store algorithm - SunX509
  • trust store pass - changeit
In case you want to authenticate your clients with certificates signed by your trusted CA you have to include CA public certificate in trust store.
Trust store configuration is as simple as key store configuration, methods just start with trust not key.

And that's how easy encryption with Grizzly is :-)

Saturday, March 14, 2009

Grizzly OSGi Http Service Guide

This post is an introduction to Grizzly OSGi Http Service.

Code for this post is available as Gitorial: http://github.com/neotyk/httpservice-gitorial/commits/master.

You'll find here:
  • how to use Grizzly OSGi Http Service
  • how to configure Grizzly OSGi Http Service
  • how to register servlet and resource
  • how to perform authentication on registered servlet or resource
  • what is currently implemented from spec and what is missing

Introduction

Grizzly

The Grizzly framework has been designed to help developers to take advantage of the Java™ NIO API. Grizzly goals is to help developers to build scalable and robust servers using NIO and we are also offering embeddable components supporting HTTP, Bayeux Protocol, Servlet (Partially) and Comet.

OSGi

OSGi is the powerful dynamic framework that underlies the Eclipse IDE and platform, but its use is not restricted to Eclipse. In fact it is used everywhere from mobile phone and vehicle entertainment systems to enterprise application servers. It is, essentially, the module system for Java.
It's lightweight and let's you escape JAR hell.

OSGi Http Service

The Http Service allows other bundles in the OSGi environment to dynamically register resources and servlets into the URI namespace of Http Service. A bundle may later unregister its resources or servlets.

Usage

Grizzly OSGi Http Service is available in two versions one with it's all dependencies and one by itself.
For version with dependencies you'll need to depend on:

For version without dependencies you'll need:

And examine its pom.xml to see dependencies that you'll need to satisfy.
Workspace with specified dependency on -bundle version is here.
Remember that even if you are using -bundle version you'll need to specify dependencies on org.osgi.core and org.osgi.compendium.
Next you'll need to configure bundle packaging as shown here.
Now you can configure your project to use maven-pax-plugin as specified here so you can do mvn pax:provision for easy startup of OSGi with your bundle and its dependencies provisioned.

Configuration

If you've tried mvn pax:provision from previous step you probably have seen exception that Grizzly OSGi Http Service couldn't bind to port 80.
Binding to port <1024 on most platforms requires special permissions.
Grizzly OSGi Http Service tried to bind to port 80 because it has not been configured to do otherwise, this is specified by spec as default.
To configure port that OSGi Http Service binds to you'll need to configure org.osgi.service.http.port property.
This configuration change is shown here.

Registration

In OSGi Http Service to expose something via http you have to register it.
But before you do it lets configure Declarative Services and Dependencies, this will allow much easier integration with OSGi Http Service.
Checkout this commit to see how to enable it.
Now that we have made our life easier lets get to registering Servlet and Resources.

Registering Servlet

To register a servlet you need to call HttpService.registerServlet method. Last two parameters (initparams and context) can be null for now.
alias is name in the URI namespace at which the servlet is registered,
servlet is servlet to register.
To see changes been done already, click.

Registering Resources

Registering resources is way to expose static content in OSGi Http Service.
To register resources you need to call HttpService.registerResources method. Last parameter context is optional and can be null for now.
alias is name in the URI namespace at which the resources are registered,
name is the base name of the resources that will be registered.
For working example go here.

Authentication

So far registering dynamic and static content has been described, that is nice but what if you would like to secure it?
Sure thing, you just need to provide last parameter (context) to resource/servlet registration.
If you don't provide context default context is used and this one is allowing all access.
Your authentication have to be implemented in HttpContext.handleSecurity.
This method is called by OSGi Http Service before request is handled. If your implementation returns true the request should be serviced, false if the request should not be serviced and Http Service will send the response back to the client.
And you probably saw it coming, here is a code for it.

Status

Not all specification has been implemented.
Things not implemented yet are:
  • SSL support,
  • Security as described in 102.8 Security
Those things will get implemented if users will be interested in it.
Rest was implemented as specification requested.


Enjoy working with Grizzly OSGi Http Service and make sure to report issues as well as success stories to Grizzly Users List.

Thursday, February 26, 2009

Clojure?

Just finished watching Clojure for Java Programmers.
Second part is much more interesting.



If you like Clojure you might want to take a look at Clojure on Grizzly.

Tuesday, February 3, 2009

Monday, January 12, 2009

Maven Archetype Automation 1

In my need for automation I found another place for improvements in Maven2.

Here is a situation: you have sample project showing how to use something.

And you want to be nice to your users so you would like to offer them this sample project as archetype via archetype-catalog.xml.

Here is a bit longer version of above:


ArchetypeNG seems like possible solution, have to research this one.

Wednesday, December 24, 2008

Grizzly OSGi Archetype

This is take 2 of Grizzly on OSGi platform (take 1).

Previous problems running this configuration has been solved by Grizzly Devs, awesome feedback.

So if you want a Quick Start for Grizzly on OSGi jump to your console and execute following:
mvn archetype:generate -DarchetypeCatalog=http://kungfoo-m2.googlecode.com/svn/trunk/archetype-catalog.xml
This should allow you to choose grizzly-osgi-startup project and provide all necessary project details.
Next thing you do is to cd to newly created project and execute:
mvn clean install pax:provision
Now you should have Felix (default for Pax, you can choose other platform providers) running your bundle that uses Grizzly and is listening on localhost:8282.

Have fun Grizzling in OSGi.

Friday, December 19, 2008

Grizzly Dependency Overview

You probably could see it comming :)

Grizzly OSGi

As a side effect of working on Grizzly Proxy simple project showing how to use Grizzly in OSGi environment popped up.

Links:
Hope to have archetype available soon, will keep you posted.

Grizzly proxy

After Devoxx I finnaly started working on Grizzly Proxy Server.
Project page and sources tree are available.

For now proxy is not functional.
Initialy I used commons-httpclient to make connections to target server.
This turned out to be not really nice since httpclient is blocking.

My current focus is to change it to use HttpCore and especially I'm interested in NIO Extensions Module.
This will allow much better scaling, but still will relly on two difrent APIs, and I'm dont think I like having it done like that.

Once Grizzly team releases thair HTTP Client I'm going to switch to it.
So don't expect g-http-proxy to be usable right now, but keep an eye on it, and fill free to contribute.

g-http-proxy is OSGi bundle :)

Thanks for listening and stay tuned.

Monday, July 7, 2008

Super Helpful Integration Testing ThingY

Maven plug-in integration testing.



After walking around integration tests for Overview I finally set down and made it happen.

Most helpful integration testing solution I found is called SHITTY http://mojo.codehaus.org/shitty-maven-plugin/.

It is very easy to use.

As sane developer would expect, you'll have to put your integration tests somewhere in src (src/it).

Integration test is:
  • yes you are right, pom.xml,
  • than goals.txt - defining goals to execute,
  • optional setup and validation Groovy scripts.
  • some additional stuff that wasn't needed for me.
You can find sample usage here.

Big thanks to Jason Dillon for creating Super Helpful Integration Testing ThingY.