Saturday, January 07, 2012

Software of the day: Fence


Fence is a software created by Stardock that helps organize icons/files in your Windows's desktop. Basically what a user can do is to create a "fence" (kind of group) and associate icons of your choice into it. The fences are blocks that does not enlarge/shrink according to the number of icons/files you put there, but instead the size are determined by the user themselves. Thus, no matter how many icons/files you have, it will still look like it's very well organized ;)

For someone like me who loves to put everything in desktop (who doesn't?), Fence is a must-have-tool. Quite often I put something that I would need immediately, but most probably wouldn't be for long in the desktop. Fence help he organize it so that in the future it is very easy to find find and remove them. Another nice thing that I like: Fence does not separate the icons in different fences to actual different directories, so it does not mess up the files location.

Fence is free, and if you want more features, they have a paid version for it as well.

screen shot was taken from Fence website

Monday, May 02, 2011

Using ClientLogin to do Authentication for App Engine Application

General information about ClientLogin: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html

Discussion on Google Groups (including the original solution posted by geoffd123):
http://groups.google.com/group/google-appengine-java/browse_thread/thread/c96d4fff73117e1d

My solution uses Apache Http library instead of HttpUnit.
public static String loginToGoogle(String userid, String password,
   String appUrl) throws Exception {
  HttpClient client = new DefaultHttpClient();
  HttpPost post = new HttpPost(
    "https://www.google.com/accounts/ClientLogin");
  
  MultipartEntity reqEntity = new MultipartEntity();
  reqEntity.addPart("accountType", new StringBody("HOSTED_OR_GOOGLE",
    "text/plain", Charset.forName("UTF-8")));
  reqEntity.addPart("Email", new StringBody(userid));
  reqEntity.addPart("Passwd", new StringBody(password));
  reqEntity.addPart("service", new StringBody("ah"));
  reqEntity.addPart("source", new StringBody(
    "YourCompany-YourApp-YourVersion"));
  post.setEntity(reqEntity);
  HttpResponse response = client.execute(post);
  if (response.getStatusLine().getStatusCode() == 200) {
   InputStream input = response.getEntity().getContent();
   String result = IOUtils.toString(input);
   String authToken = getAuthToken(result);
   post = new HttpPost(appUrl + "/_ah/login?auth=" + authToken);
   response = client.execute(post);
   Header[] cookies = response.getHeaders("SET-COOKIE");
   for (Header cookie : cookies) {
    if (cookie.getValue().startsWith("ACSID=")) {
     return cookie.getValue();
    }
   }
   throw new Exception("ACSID cookie cannot be found");
  } else
   throw new Exception("Error obtaining ACSID");
 }
A simple example on how to use it:

String authCookie = logonHelper.loginToGoogle("email@gmail.com",
   "password","http://yourapp.appspot.com");  
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://yourapp.appspot.com/service");
get.setHeader("Cookie", authCookie);
HttpResponse response = client.execute(get);
response.getEntity().writeTo(System.out);

Thursday, February 03, 2011

GWT and Delayed Execution for Google Maps Geocoding

Google Maps provide us with an asynchronous way of doing a geocoding, which is pretty much how GWT is dealing with all type of client-server communication. However, sometimes we need a way to synchronize them. By synchronizing I mean waiting for the required asynchronous call to return a value before continue with other execution. Using something like Timer to wait for a reply doesn't work since it will only block the whole process on the browser since JavaScript interpreter is single-threaded. Fortunately, there is a way to deal with it within GWT by using DeferredCommand.

I'm going to take an example of using Geocoding from Google Maps. What I would like to do is geocode two address into latitude, longitude coordinate and use them as parameters for another method.

Geocoder geocode = new Geocoder();
final Callback firstCallback = new Callbak();
geocode.getLatLng("Jl. Tb Ismail Bandung Indonesia", firstCallback);
final Callback secondCallback = new Callbak();
geocode.getLatLng("Jl. Ganesha Bandung Indonesia", secondCallback);
final Command command = new Command() {
   @Override
   public void execute(){
      if ((firstCallback.location != null) 
            && (secondCallback.location != null)) {
         //call the method
         anotherMethod(firstCallback.location, secondCallback.location);
      } else {
         DeferredCommand.addCommand(this); //delay execution
      }
   }
}
command.execute();

With "Callback" as a class to temporarily store value from geocode.

class Callback implements LatLngCallback {
   public LatLng location;

   @Override
   public void onFailure() {
      //put something here
   }

   @Override
   public void onSuccess(LatLng point) {
      location = point;
   }
}

This works for me well, however, I do realize that DeferredCommand is now deprecated. Any other solution?