Home
 

1.1 Document History

 
Version Date Author Comments
0.1 04/15/2002 Patrick Chanezon Created the google API proxy patch
0.2 05/31/2002 Patrick Chanezon added GoogleAPIDemo.class to the patch so that it can be used without compiling any file
0.3 09/03/2002 Patrick Chanezon end of life for this document: Google fixed the APIs in beta 2 to allow the use of proxy in the java API, so this document is not relevant anymore

Obsolete Document: bug fixed in beta 2

This document relates to the fix of a problem with the java client of the Google APIs in beta 1: it did not let the programmer specify a proxy.
This problem is fixed in the beta 2 of the APIs, so this patch and documentation are not relevant anymore.
See http://www.google.com/apis/release_notes.html
If you need to set a proxy with the java client of the Google API, download the current version and you will be able to set it: java -cp "D:\\doc\\dev\\googleapibeta2\\googleapi.jar" -Dhttp.proxyHost=your.host.here -Dhttp.proxyPort=your_port_here com.google.soap.search.GoogleAPIDemo your_key_here %1 %2

setProxyHost

public void setProxyHost(java.lang.String host)
Set a host to use as an HTTP proxy. If unset, GoogleSearch will also check the Java system properties "http.proxyHost" and "http.proxyPort" and use those values. If those are also unset, HTTP requests go direct with no proxy.

setProxyPort

public void setProxyPort(int port)
Set a port to use as an HTTP proxy. Only used if proxyHost is also set. If unset, port 80 is assumed to be the default.

setProxyUserName

public void setProxyUserName(java.lang.String name)
Set the username required for the HTTP proxy. Only used if proxyHost is also set.


setProxyPassword

public void setProxyPassword(java.lang.String password)
Set the username required for the HTTP proxy. Only used if proxyHost is also set.

I would like to congratulate Google for their handling of this very minor problem in their APIs.

Nelson Minar from Google contacted me just 4 days after I published the patch to thank me for it, and then they fixed it in the next release.
This kind of professionalism applied a most minor problem (an easy workaround existed: use the WSDL instead of the java API if you are behind a firewall) demonstrates a commitment to developers from Google that is not so common in our industry, and feels very refreshing.
This company continues to surprise me :-)
Go Google !

Patch For Google APIs to handle proxy settings

1. Why ?

I played with the Google APIs when they went out last friday, but my company operates behind a firewall and requires us to go through a proxy in order to access the web.
I made the sample GoogleAPIDemo work at home during the week end, but I wanted to experiment with some more at work.
I could use the WSDL and my favorite web service toolkit, but why bother when Google already did the work for me ?

The problem is that com.google.soap.search.GoogleSearch performs the SOAP call using a SOAPHTTPConnection but does not let you call the following methods on this object:

setProxyHost(java.lang.String host)
          Set HTTP proxy host.
void setProxyPassword(java.lang.String password)
          Set the password for HTTP proxy basic authentication.
void setProxyPort(int port)
          Set HTTP proxy port.
void setProxyUserName(java.lang.String userName)
          Set the username for HTTP proxy basic authentication.

2. What ?

What I did is essentially provide a new version of com.google.soap.search.GoogleSearch which performs the same tasks as the original, but adds the 4 methods above.
When the SOAPHTTPConnection is created, if these methods have been called on GoogleSearch, they will be called on  SOAPHTTPConnection as well before the SOAP call is made.

3. How ?

I modified GoogleAPIDemo to take advantage of it.
The new usage is:
Usage:
java -cp "patgoogle.jar;googleapi.jar" com.google.soap.search.GoogleAPIDemo <client-key> (search <query> |cached <url> | spell <phrase>) [proxy host] [proxy port] [proxy username] [proxy password]
You can download it here: GoogleAPIDemo.java .

The code to add the proxy prefs is:

    // Create a Google Search object, set our authorization key
    GoogleSearch s = new GoogleSearch();
    s.setKey(clientKey);

    //P@ added proxy support
    if (withProxy) {
        s.setProxyHost(proxyHost);
        s.setProxyPort(proxyPort);
        if (proxyUserName != null) {
            s.setProxyUserName(proxyUserName);
        }
        if (proxyPassword != null) {
            s.setProxyPassword(proxyPassword);
        }
    }

In order to be able to call these methods, download patgoogle.jar
It contains the modified com.google.soap.search.GoogleSearch.

After 2 request (1, 2) in the google.public.web-apis group, I decided to include com.google.soap.search.GoogleAPIDemo.class in the patch as well.
I did this because the original GoogleAPIDemo.class is present in googleapi.jar, so people needed to build the new version and put it in their classpath in order to test the API. Many people do not want to build this class, so I included it in the patch.
With this new version of the jar, you do not need to compile anything, just run the command line above and you're done.

Put it in front of googleapi.jar in your classpath, and presto, you're done !

4. Conclusion

I love Google: this company reminds me my old days at Netscape, and I liked the comparison Dave winer made in DaveNet : Google's SOAP API .
This little jar is just a personal hack that I share for your convenience: there is no guarantee attached to it at all: run it at your own risks.
You can use the WSDL interface directly if you prefer, and do not need this to get over the proxy problem.
I hope that Google will fix their java API someday, to include this functionality in one way or another.

In the meantime, Happy Googling !

P@
 

PS: Why Setting the environment variables proxySet, proxyHost and proxyPort won't work

A few people have argued that setting the environment variables proxySet, proxyHost and proxyPort, as desribed in http://www.davidreilly.com/java/java_network_programming/#2.4 should solve the problem.

jre -DproxySet=true -DproxyHost=myhost -DproxyPort=myport MyApp

I tried it and it did not work.
I provided an analysis of why it doesn't work as an answer in the Google web-api group.

I reproduce it hereunder.

victor@bit-man.com.ar (Victor Rodriguez  Bit-Man) wrote in message news:<463f91ab.0204130850.130219d4@posting.google.com>...
> Hi there !!!
>
> > The call to the Google Web APIs failed:
> > com.google.soap.search.GoogleSearchFault: [SOAPException:
> > faultCode=SOAP-ENV:Client; msg=Error opening socket: Operation timed
> > out: connect; targetException=java.lang.IllegalArgumentException:
> > Error opening socket: Operation timed out: connect]
>
> You're having not connection to the server !!1
>
> > I suspect the problem comes from I'm working behind a proxy
>
> You're right, that's the problem, and you can find the answer in
> http://www.davidreilly.com/java/java_network_programming/#2.4
>
> Hope this helps

Ozark, Victor is right in the fact that your problem comes from being behind a proxy.
But the solution he advocates is wrong: it does not work.
The solution using System properties to set proxies as described by David Reilly applies to URLConnection.
It is also very well decribed in this java tip:
http://www.javaworld.com/javaworld/javatips/jw-javatip42.html

But here the Google API, more accurately the class GoogleSearch, uses org.apache.soap.transport.http.SOAPHTTPConnection to perform the SOAP
call.
SOAPHTTPConnection inherits from object, so it has nothing to do with URLConnection.
Moreover, if you look at SOAPHTTPConnection's implementation, the network call is delegated to org.apache.soap.util.net.HTTPUtils.post()
method, which uses a java.net.Socket.

So setting the system properties won't work, and now you know why.

Manjul Sahay is right: if you do not use the Google Java APIs but access the Web Service directly using Apache SOAP, you can specify a proxy.
SOAPHTTPConnection has 4 methods that allows it to set proxy settings.
GoogleSearch uses SOAPHTTPConnection but does not let you take advantage of these methods.
I had the same problem you had, being behind a proxy at work, so I created a patch for GoogleSearch which allows you to specify proxy settings.

You can find it at http://www.chanezon.com/pat/google_proxy_patch.html until Google adds it to the API.

I hope this helps.

P@