Twitter status post with Apache Wink

I read from Davanum Srinivas’s tweet that the Apache Wink podling released 0.1. I didn’t know what Wink is offering, so I took a look. I read that Apache Wink 0.1 is a complete and TCK compliant implementation of the JAX-RS v1.0 specification. Also the documentation is quite large, I am impressed.

So, I gave it a try to post some status updates to Twitter. Here is a five to ten minute try/error hack by combining Apache Wink and Twitter:

package net.wessendorf.twitter.client.app;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import org.apache.wink.client.ClientConfig;
import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;
import org.apache.wink.common.internal.MultivaluedMapImpl;
import sun.misc.BASE64Encoder;

public class App
{
  public static void main( String[] args )
  {
    ClientConfig cc = new ClientConfig();
    cc.proxyHost("iam-behind-a-proxy.com");
    cc.proxyPort(80);

    String logon = "username:password";
    String encodedLogon = new BASE64Encoder().encode(logon.getBytes());

    RestClient client = new RestClient(cc);
    Resource twitter =
 client.resource("http://twitter.com/statuses/update.json");

    twitter.header("Authorization", "Basic " + encodedLogon);
    twitter.contentType(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    twitter.accept(MediaType.APPLICATION_JSON_TYPE);

    MultivaluedMap<String, String> msg =
 new MultivaluedMapImpl<String, String>();
    msg.add("status", "Hello Apache Wink!");

    twitter.post(String.class, msg);
  }
}

This is really simple form but, heck it is just a try/error hack. Please note that this “programm” requires the SUN JDK, as I was using the BASE64Encoder class. By default Apache Wink uses the HttpURLConnection class (from your JDK) –  but there is also a library for Apache Http Client 4.0.

The documentation is online.

Howdy!

Posted in Uncategorized
3 comments on “Twitter status post with Apache Wink
  1. Nick Gallardo says:

    Nice! Thanks for giving it a try. Glad to hear you like it.

  2. […] have a bit more comfort, one wants to use a (Java) REST API. Apache Wink is pretty nice and listening to the stream is […]

  3. […] previous blog described the pattern how to connect to the Twitter streaming, by using Apache Wink (a REST client […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: