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.

Issues with “in” operator in JavaScript – using hasOwnProperty() function

Imagine you are using an JavaScript array on the client-side that stores some client-side components (or vanilla HTML elements) by their IDs – for what ever reason. Now imagine you have a function that checks if a given ID is in that particular array. Based on the result, some (different) logic is triggered. Pretty common scenario – nothing fancy.

Code could look like this:

var componentIds = new Array();
componentIds["table"] = "my table component";
componentIds["tree"] = "my tree component";

containsComponent = function(compId)
{
  if((compId in componentIds))
  {
    return true;
  }
  else
  {
    return false;
  }
}

//some callings callings...
alert("tree: in array ? -> " + containsComponent("tree"));
alert("myTabe: in array ? -> " + containsComponent("myTree"));
alert("table: in array ? -> " + containsComponent("table"));

Now I noticed some problem when your component ID is a “reserved” word. Like “map” or “push”. Does that ring your ear ? Yes, correct – that’s the name of a JavaScript function of the Array class:

// huh ???
alert("map: in array ? -> " + containsComponent("map"));
 

So that means on an empty array a check for “map” (or “slice”) would return TRUE. That can cause some interesting troubles and side effects, well BUGs :-)

The problem with “in” operator is that it does not only check if the damn array does have a key with the “name” in question – it also checks for functions on the prototype. I talked to my colleague Blake Sullivan about avoiding to query for the function. He simply suggested to just use the “hasOwnProperty()” function instead of checking if the returned value is type of “function”. I personally never used that function before (and never seen it in code before) – but it helps to avoid odd and nasty issue ;-)