2009-07-09

Cookie handling with Apache XML-RPC

The example on the Apache website that explains how to pass cookies when calling an XML-RPC service is unfortunately... a bit b0rked. It doesn't compile, for starters. It's also quite a bit more complicated (and handwavy) than it needs to be. I don't know if this is because the API has changed over time. I tend to think it has suffered the copy-paste equivalent of chinese whispers from an origin in some mailing list.

Anyway, I needed to send a single sign on (SSO) cookie to an XML-RPC service recently. I'm using Apache XML-RPC 3.1 (I notice in the Javadoc that this code might be slightly different if you're using a later version). Here's roughly what I did:

First, the standard, boring stuff:

XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("https://myservice.com/api"));
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);

Now, the juicy part. It's really, really easy. Look, ma, so much less code than the example on the ws.apache.org website, and it's even syntactically correct!:

XmlRpcTransportFactory factory = new XmlRpcSunHttpTransportFactory(client) {
  public XmlRpcTransport getTransport() {
    return new XmlRpcSunHttpTransport(client) {
      @Override protected void initHttpHeaders(XmlRpcRequest request) throws XmlRpcClientException {
        super.initHttpHeaders(request);
        setRequestHeader("Cookie", myLovelyCookieData);
      }
    }
  }
};
client.setTransportFactory(factory);
Update: Fixed a bug in the code. Oh, the irony. That'll teach me to be so arrogant :P

AddThis Social Bookmark Button

2009-06-04

Find a file inside a zip

Brutally simple shell script I often use to find a class file that I know exists somewhere in a directory tree full of jar files (but generally useful for finding files somewhere in a directory tree of zips):

#!/bin/bash                                                                             
                                                                                        
for zip in $*                                                                           
do                                                                                      
  echo $zip                                                                             
  for file in $(unzip -Z -1 $zip)                                                       
  do                                                                                    
    echo "$zip:$file"                                                                   
  done                                                                                  
done
I put this in a file called zipdump, then do things like this:
find -name "*.jar" | xargs zipdump | grep SomeClass
And get:
./some/random/path/foo.jar:com/google/whatever/SomeClass.class

AddThis Social Bookmark Button

2009-05-13

Listing Chinese Words by Frequency of Use

A commenter on my previous blog entry, Yong Huang, pointed me to some wonderful research he did using search engine results to compile a list of Chinese characters in frequency order. I managed to miss his comment when he posted it back in March (d'oh. I really should have paid more attention to my blogger settings), but it's an interesting read and technique:

http://yong321.freeshell.org/misc/ChineseCharFrequency.html

AddThis Social Bookmark Button

2009-01-11

Getting serious about Mandarin

I'm finally making a big effort to learn mandarin properly. In addition to taking formal lessons at UC Berkley starting in February, and learning about usage from Cindy, I'm using flash cards to increase my knowledge of Chinese characters and their mandarin pronunciation (including the tones, which I've had a hard job remembering up till now while picking up mandarin informally through random conversation).

I've been using some tricks to memorize characters, and this technique seems to work well for me.

Here's the first set of characters. The first ten are numbers, and mostly pretty easy.

一 二 三

one (yi1), two (er4), three (san1). These are quite possibly the simplest Chinese characters ever. :)

four (si4). This one is kind of easy for me to remember, since I visited sichuan last year, and this character was everywhere :) It's also a box with four sides.

five (wu3). I convinced myself that this character has five distinct lines in it, which made it easy to remember (although it actually has only four strokes - the middle horizontal line and the smaller vertical line are combined together as a single stroke).

six (liu4). I found this one a bit tough. I sort of convinced myself that it looks like a lion (the sound liu4 is a bit like the English "leo", as in "Leo the Lion").

seven (qi1). This was tricky too. Until I turned my flash card upside down and realized it looks like an upside down 7 with a stroke through it. Nice.

eight (ba1). No tricks here, I just remember this character because it's pretty simple.

nine (jiu3). With the right amount of squinting, this character looks like a roman J and lower case i without the dots and with a weird line connecting the J to the top of the i. So if you can remember that jiu3 == nine, it's pretty easy.

ten (shi2). If you have Chinese friends, you might notice that sometimes when they say "ten", they make a hand gesture where they cross both index fingers over each other. If you ever wondered why, this character is the answer :)

And here's a random sampling of two more non numeric characters:

ask (wen4). This combines the character for door (门 men2) with the character for mouth (口 kou3). Can think of it as opening the door of your mouth to ask a question. The sound (wen2) is also similar to the sound for door (men2).

good (hao3). This combines the characters for female (女 nü3) and offspring / son / child (子 zi3). I guess that's good and harmonic.

Anyway, lots more where those came from. Kind of fun to learn.... :)

AddThis Social Bookmark Button