Saturday August 06, 2005
Mein Königreich für ein Pferd!
Once I'd worked out how to retrieve Shakespeare's wise words from a web service (see Friends, Romans, Countrymen... for details), I thought the next logical step would be to send a returned Shakespeare quotation onwards... to a web service that would translate it for me. After all, that would demonstrate not only a scenario that makes use of multiple web services within one application, but is also a pretty worthwhile endeavour in and of itself. So -- again within seconds, thanks to the IDE's cool web service client wizard -- I had added a translation WSDL (http://www.webservicex.net/TranslateService.asmx?WSDL) to the application that already referenced the Shakepeare quotation WSDL (http://www.xmlme.com/WSShakespeare.asmx?WSDL). And, a few lines of code later, I had set things up so that the returned quotation would be sent off to the translation service before being printed out to an XML file:
//Get the search string and language from the JSP
String quotation = request.getParameter("quotation");
String language = request.getParameter("language");
//Get the quotation from the Shakespeare web service
String ret_quotation = getShakespeareSoap().getSpeech(quotation);
//Get the translation from the translation web service
String ret_translation = getTranslateServiceSoap().translate(Language.fromValue(language),quotation);
//Create an XML file and output the returned quotation
OutputStream fout= new FileOutputStream("shakespeare/quotation.xml");
OutputStream bout= new BufferedOutputStream(fout);
OutputStreamWriter out = new OutputStreamWriter(bout, "UTF-8");
out.write("<?xml version=\"1.0\" ");
out.write("encoding=\"UTF-8\"?>\r\n");
out.write(ret_translation);
out.flush();
out.close();
I would've preferred to splash my translated quotation all over my screen, but there was one problem: I use an XSL stylesheet to style the quotation, since I receive the quotation as an XML stream from the web service. So, when I receive the quotation back from the translation service (or, if I were to, as you'll find out in the paragraphs below), not only the quotation but also the tags are translated, which causes a big problem with the XSL stylesheet, since the tags are used to differentiate between the parts of the XML stream in order to structure it for presentation. For example, if you look at Friends, Romans, Countrymen..., you'll see that when the <PLAY> tag is found, a template is applied in the stylesheet, and another template is applied when the <SPEAKER> tag is found. This means that either the tags should be stripped before being sent for translation (and then somehow re-attached upon the quotation's return, so that it can be processed by the stylesheet) or that I need to add new templates to the styleheet for the translated tags (and then do that for each supported language).
Anyway, that was only one of the problems. The biggest problem was -- and is -- that the translation web service is unreliable. Sometimes it works within seconds, other times it spits out this helpful message:
The underlying connection was closed: Unable to connect to the remote server.
If you google the above error message, you'll find a lot of sadness and despair all over the world. An international clamor. A global wailing and gnashing of teeth. If ever there was a cryptic error message, this is it. Most people tell you that the problem is with your firewall. Well, that could be the case -- but if so, why does the web service sometimes succeed admirably and other times leave you waiting long dreary minutes only to dump the above text in your carefully created XML file? It's all very mysterious, but points to the larger problem of web services in general -- your application is only as strong as the weakest of the web services you're using. For even worse wailing and teeth gnashing, google for 'Babelfish WSDL' (I've saved you some time and done it for you, click here for a lot of frustration). You'll find some sorry souls who built their wonderful applications around this web service, only to have Altavista decide to shut it down...
So, anyway, currently I haven't been able to get a successfully translated Shakespeare quotation, apart from the odd -- unexpected and occasional -- burst of translated returns. It's possibly because the quotations I send are very long. I mean, the Shakespeare web service returns a lengthy speech based on a search string. And this lengthy speech is what is sent off to the translation service. Maybe that's why things are going haywire. But that still doesn't fully explain it, because even when I send off small bits of text to be translated, I sometimes do and sometimes don't get a successful return from the translation web service. So, I'm a bit confused. One of the successfully returned small strings (i.e., not the full quotation from the Shakespeare web service, but just something I sent off for translation in a separate process) is in the subject line of this blog entry -- I'm actually very proud of it, because it shows that at least my code is correct. And, on some level, I now understand King Richard III even better than I thought I did (I always found him strangely sympathetic): "A translation service! A translation service! My kingdom for a translation service!"
Aug 06 2005, 07:17:40 AM PDT Permalink


