JAX-WS code samples for the Yahoo! Mail Web Service

Even though I’ve moved off of Yahoo! Mail, I still cuddle and care for the Yahoo! Mail Web Service. Back when I was still on the web service team, I built code samples for Java and .NET. In the process of writing those code samples, I was able to kick around a fair number of SOAP toolkits. In the end, Java ended up having only Axis2 samples because it was the only toolkit I could get working with our WSDL.

Recently, Yahoo!’s own Sam Pullara figured out how to get things working with JAX-WS. This is significant because JAX-WS seems to be held in higher esteem with many Java developers. At least it seems that way since I have a handful of hate mail from JAX-WS users. I took Sam’s code and instructions and used it to port my Axis2 code sample to JAX-WS, the results of which have now been posted on the Yahoo! Developer Network.

Now that I’ve used both Axis2 and JAX-WS, it’s pretty clear why JAX-WS seems to be more popular. The bindings it generates are considerably cleaner than those generated by Axis2. For those unfamiliar with Axis2 and JAX-WS, each of them generates Java code from the WSDL that allows you to treat the web service as though it were a local object. Axis2 likes to generate classes with numbers in the names and is, in general, incredibly verbose. That being said, it’s mappings of WSDL to object are quite literal. If you understand the WSDL, then the code bindings it creates will be pretty clear to you (as long as you can figure out what number to append to the classnames). This verbosity leads to a lot more code when you want to use it. You end up instantiating a lot of objects setting up the request and you traverse a lot of objects dealing with the response.

JAX-WS, in contrast, generates bindings that look a bit more normal. Whereas Axis2 generates service methods that take a single, object, parameter…JAX-WS is able to generate service methods that take a list of parameters because it understands that the Yahoo! Mail Web Service uses document-literal wrapped SOAP methods. However, just because JAX-WS thinks it knows what it’s doing, in many cases that only gets it into trouble. Despite the fact that the methods are crafted uniformly in the WSDL, JAX-WS generates entirely different semantics for similar methods. One method may take a list of input parameters and return an array of strings. Another method may take a list of input parameters and return an object. Another method may take a single object parameter and return an object or an array of strings. Finally, my personal favorite, a method may take a list of in/out parameters and have a void return. Even though some methods look more like what you’d expect to see in a Java API, others are so off the wall that they could only be the result of AI gone wrong or the most misguided engineer ever devising your interfaces.

Either way, Java works pretty well with the Yahoo! Mail Web Service. The only thing I haven’t gotten working is the BatchExecute method, although I haven’t tested it with JAX-WS.

Update: I’ll be damned…BatchExecute DOES work with JAX-WS. That’s reason enough to use it over Axis2. Code sample below:

Update 2: After having my balls busted by someone, I’ve gone through and replaced my “quaint” collection iteration code (the “for” loops) with more up to date Java semantics.

    // BatchExecute, execute multiple methods at once.
    List<BatchCall> calls = new ArrayList<BatchCall>();

    // Set up the batched call to ListFolders.
    BatchCall listFoldersBatchCall = new BatchCall();
    listFoldersBatchCall.setId("listFolders");
    listFoldersBatchCall.setListFolders(new ListFolders());
    listFoldersBatchCall.getListFolders().setResetMessengerUnseen(false);
    calls.add(listFoldersBatchCall);

    // Set up the batched call to ListMessages.
    BatchCall listMessagesBatchCall = new BatchCall();
    listMessagesBatchCall.setId("listMessages");
    listMessagesBatchCall.setListMessages(new ListMessages());
    listMessagesBatchCall.getListMessages().setFid("Inbox");
    listMessagesBatchCall.getListMessages().setStartInfo(BigInteger.ZERO);
    listMessagesBatchCall.getListMessages().setNumInfo(new BigInteger("10"));
    calls.add(listMessagesBatchCall);

    // Call BatchExecute.
    List<BatchResponse> batchResponse = stub.batchExecute(calls);

    // ListFolders is the first response in the batch.
    ListFoldersResponse listFoldersBatchResponse = batchResponse.get(0).getListFoldersResponse();
    System.out.println("Batched ListFolders response:");
    for (FolderData folderData : listFoldersBatchResponse.getFolder()) {
        System.out.println("    " + folderData.getFolderInfo().getName() + " (" + folderData.getUnread() + ")");
    }
    System.out.println();

    // ListMessages is the second response in the batch.
    ListMessagesResponse listMessagesBatchResponse = batchResponse.get(1).getListMessagesResponse();
    System.out.println("Batched ListMessages response:");
    for (MessageInfo messageInfo : listMessagesBatchResponse.getMessageInfo()) {
        System.out.println("    " + messageInfo.getSubject());
    }

One Response to “JAX-WS code samples for the Yahoo! Mail Web Service”

  1. mattlink Says:

    well I would like to know if I could help, I read in your blog some interesting articles, but especially’m more interested in what yahoo mail web services, I would like to know if I could help them to initiate in the mail from yahoo, I mean, How to gain access to these services?, But I tried but not exit the authentication, I am programming in Java 1.4 and axis 1.4, I would like to know if I could teach some classes for example, how do the Soap and the Client you need, thanks in advance for your understanding …

Leave a Reply