Saturday, July 24, 2010

Extending Android XML-RPC to support regular Java serialization

For my current Android project I wanted to have some communication with a server component. After spending some time on finding a decent solution I arrived at XML-RPC.

A client would use this code to ask the server the sum of two numbers:

int sum = (Integer) client.call("Calculator.add", 2, 4);

The server side would have a class “Calculator” with a method “add”, like so:

public int add(int i1, int i2) {
    return i1 + i2;
}

Nice and clean!

For the client (Android) I use Android XML-RPC and for the server side I use use Apache WS XML-RPC. It is not possible to use Apache WS XML-RPC on Android out of the box, as there are some issues relating to the core Java classes that are supported on Android. I am sure you could get it running with some effort, but then you still will add hundreds of kilobytes to your app because of the jar files that need to be included. Android XML RPC is around 50 kilobytes, which is much nicer from an end-user perspective.

By default there is a limited set of “data types” that can be used and I want to use my own “data types” (like “User”, “Project”, etc.). Apache XML RPC has a special mode “enabledForExtensions”, if you check that option it will give you access to the use of longs, shorts, bytes, floats, DOM nodes, instances of java.io.Serializable, or JAXB objects. This is only handy in a use case that has Apache XML RPC on both client and server. So not in my case!

Luckily, it is easy to extend Android XML RPC to allow other data types. You just have to add some code to the XMLRPCSerializer class. As I only wanted support for deserializing objects I added an extra if clause to the deserialize method:

} else if (typeNodeName.equals("ex:serializable")) {
    obj = null;
    String value = parser.nextText();
    BufferedReader reader = new BufferedReader(new StringReader(value));
    String line;
    StringBuffer sb = new StringBuffer();
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(Base64Coder.decode(sb.toString()));
    ObjectInputStream ois = new ObjectInputStream(bais);
    obj = ois.readObject();
    parser.nextTag();
    // parser.require(XmlPullParser.END_TAG, null, ..

    return obj;

} else {

Jarvana

When using Maven you often want to locate dependencies: Jarvana can help out!

image

http://www.jarvana.com

Thursday, July 22, 2010

Adding servlet support to the Maven webapp archetype

Today I wanted to create a servlet and used the m2eclipse plugin for Eclipse to create a web application based on the available Maven webapp archetype.

When using the archetype you will miss some essential jars and cannot create a Java 5 servlet. So I did the following:

1. adjust the pom: add dependency

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>

2. adjust the web.xml: adjust the header

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

3. open the properties of the project and under Java Compiler change the compliance level to 1.5

Friday, July 16, 2010

How to implement user authentication in your web application

When you are building web sites then you will often need to implement a user authentication mechanism. Be it a Java, PHP, Grails, or whatever technology based site – the issues are the same.

Researching for authentication I found this article: http://www.openwall.com/articles/PHP-Users-Passwords

It is written for a PHP programmer audience, but it touches all the  subjects and makes a really interesting read – highly recommended!

Wednesday, July 14, 2010

Pregnancy Ticker has been downloaded over 100.000 times!

image

Well, I never saw this coming. Originally made for following the progress on our baby Tristan, who now is 1.5 years old. After releasing it in the market many nice comments were added and requests for features were mailed. So I added some extra stuff and within 2 years it passed the 100.000 downloads barrier. It is very difficult to imagine 100.000 persons using Pregnancy Ticker – it means that the progress of thousands of pregnancies has been followed with it – unbelievable.

I am planning to add more features to celebrate this moment and am actually working on one right now. It is a bit of a technical challenge, but more about that later.

Thanks for all the nice comments and best wishes!