1
6 package com.google.soap.search;
7
8 import java.net.MalformedURLException;
9 import java.net.URL;
10import java.util.Vector;
11import org.apache.soap.Fault;
12import org.apache.soap.SOAPException;
13import org.apache.soap.encoding.SOAPMappingRegistry;
14import org.apache.soap.encoding.soapenc.BeanSerializer;
15import org.apache.soap.rpc.*;
16import org.apache.soap.transport.http.SOAPHTTPConnection;
17import org.apache.soap.util.xml.QName;
18import org.apache.soap.util.xml.XMLJavaMappingRegistry;
19
20
23public class GoogleSearch
24{
25
26 public void setKey(String s)
27 {
28 key = s;
29 }
30
31 public void setSoapServiceURL(String s)
32 {
33 soapServiceURL = s;
34 }
35
36 public void setQueryString(String s)
37 {
38 q = s;
39 }
40
41 public void setStartResult(int i)
42 {
43 start = new Integer(i);
44 }
45
46 public void setMaxResults(int i)
47 {
48 maxResults = new Integer(i);
49 }
50
51 public void setFilter(boolean flag)
52 {
53 filter = new Boolean(flag);
54 }
55
56 public void setRestrict(String s)
57 {
58 restrict = s;
59 }
60
61 public void setSafeSearch(boolean flag)
62 {
63 safeSearch = new Boolean(flag);
64 }
65
66 public void setLanguageRestricts(String s)
67 {
68 lr = new String(s);
69 }
70
71
74
75 public void setInputEncoding(String s)
76 {
77 ie = s;
78 }
79
80
83
84 public void setOutputEncoding(String s)
85 {
86 oe = s;
87 }
88
89 public void setProxyHost(String s)
90 {
91 proxyHost = s;
92 }
93
94 public void setProxyPort(int i)
95 {
96 proxyPort = i;
97 }
98
99 public void setProxyUserName(String s)
00 {
01 proxyUserName = s;
02 }
03
04 public void setProxyPassword(String s)
05 {
06 proxyPassword = s;
07 }
08
09 public GoogleSearchResult doSearch()
10 throws GoogleSearchFault
11 {
12 Response response = null;
13 try
14 {
15 response = callRemoteMethodUsingSOAP("doGoogleSearch", generateParamsVector());
16 }
17 catch(Exception exception)
18 {
19 throw new GoogleSearchFault(exception.toString());
20 }
21 if(!response.generatedFault())
22 {
23 Parameter parameter = response.getReturnValue();
24 return (GoogleSearchResult)parameter.getValue();
25 } else
26 {
27 Fault fault = response.getFault();
28 throw new GoogleSearchFault("Fault Code = " + fault.getFaultCode() + "\nFault String = " + fault.getFaultString());
29 }
30 }
31
32 public byte[] doGetCachedPage(String s)
33 throws GoogleSearchFault
34 {
35 Response response = null;
36 try
37 {
38 Vector vector = new Vector();
39 vector.addElement(new Parameter("key", java.lang.String.class, key, null));
40 vector.addElement(new Parameter("url", java.lang.String.class, s, null));
41 response = callRemoteMethodUsingSOAP("doGetCachedPage", vector);
42 }
43 catch(Exception exception)
44 {
45 throw new GoogleSearchFault(exception.toString());
46 }
47 if(!response.generatedFault())
48 {
49 Parameter parameter = response.getReturnValue();
50 return (byte[])parameter.getValue();
51 } else
52 {
53 Fault fault = response.getFault();
54 throw new GoogleSearchFault("Fault Code = " + fault.getFaultCode() + "\nFault String = " + fault.getFaultString());
55 }
56 }
57
58 public String doSpellingSuggestion(String s)
59 throws GoogleSearchFault
60 {
61 Response response = null;
62 try
63 {
64 Vector vector = new Vector();
65 vector.addElement(new Parameter("key", java.lang.String.class, key, null));
66 vector.addElement(new Parameter("phrase", java.lang.String.class, s, null));
67 response = callRemoteMethodUsingSOAP("doSpellingSuggestion", vector);
68 }
69 catch(Exception exception)
70 {
71 throw new GoogleSearchFault(exception.toString());
72 }
73 if(!response.generatedFault())
74 {
75 Parameter parameter = response.getReturnValue();
76 return (String)parameter.getValue();
77 } else
78 {
79 Fault fault = response.getFault();
80 throw new GoogleSearchFault("Fault Code = " + fault.getFaultCode() + "\nFault String = " + fault.getFaultString());
81 }
82 }
83
84 protected Vector generateParamsVector()
85 {
86 Vector vector = new Vector();
87 vector.addElement(new Parameter("key", java.lang.String.class, key, null));
88 vector.addElement(new Parameter("q", java.lang.String.class, q, null));
89 vector.addElement(new Parameter("start", java.lang.Integer.class, start, null));
90 vector.addElement(new Parameter("maxResults", java.lang.Integer.class, maxResults, null));
91 vector.addElement(new Parameter("filter", java.lang.Boolean.class, filter, null));
92 vector.addElement(new Parameter("restrict", java.lang.String.class, restrict, null));
93 vector.addElement(new Parameter("safeSearch", java.lang.Boolean.class, safeSearch, null));
94 vector.addElement(new Parameter("lr", java.lang.String.class, lr, null));
95 vector.addElement(new Parameter("ie", java.lang.String.class, ie, null));
96 vector.addElement(new Parameter("oe", java.lang.String.class, oe, null));
97 return vector;
98 }
99
00 private SOAPMappingRegistry constructTypeRegistryForGoogleSearch()
01 {
02 SOAPMappingRegistry soapmappingregistry = new SOAPMappingRegistry();
03 BeanSerializer beanserializer = new BeanSerializer();
04 soapmappingregistry.mapTypes("http://schemas.xmlsoap.org/soap/encoding/", new QName("urn:GoogleSearch", "GoogleSearchResult"), com.google.soap.search.GoogleSearchResult.class, beanserializer, beanserializer);
05 soapmappingregistry.mapTypes("http://schemas.xmlsoap.org/soap/encoding/", new QName("urn:GoogleSearch", "DirectoryCategory"), com.google.soap.search.GoogleSearchDirectoryCategory.class, beanserializer, beanserializer);
06 soapmappingregistry.mapTypes("http://schemas.xmlsoap.org/soap/encoding/", new QName("urn:GoogleSearch", "ResultElement"), com.google.soap.search.GoogleSearchResultElement.class, beanserializer, beanserializer);
07 return soapmappingregistry;
08 }
09
10 protected Response callRemoteMethodUsingSOAP(String s, Vector vector)
11 throws MalformedURLException, SOAPException
12 {
13 URL url = new URL(soapServiceURL);
14 Call call = constructCall(s, vector);
15 return call.invoke(url, "urn:GoogleSearchAction");
16 }
17
18 protected Call constructCall(String s, Vector vector)
19 {
20 Call call = new Call();
21 call.setSOAPMappingRegistry(constructTypeRegistryForGoogleSearch());
22 call.setTargetObjectURI("urn:GoogleSearch");
23 call.setMethodName(s);
24 call.setEncodingStyleURI("http://schemas.xmlsoap.org/soap/encoding/");
25 call.setParams(vector);
26 SOAPHTTPConnection soaphttpconnection = new SOAPHTTPConnection();
27 if(proxyHost != null)
28 {
29 soaphttpconnection.setProxyHost(proxyHost);
30 soaphttpconnection.setProxyPort(proxyPort);
31 if(proxyUserName != null)
32 soaphttpconnection.setProxyUserName(proxyUserName);
33 if(proxyPassword != null)
34 soaphttpconnection.setProxyPassword(proxyPassword);
35 } else
36 {
37 String s1 = System.getProperty("http.proxyHost");
38 if(s1 != null && !"".equals(s1))
39 {
40 soaphttpconnection.setProxyHost(s1);
41 int i = Integer.getInteger("http.proxyPort", 80).intValue();
42 soaphttpconnection.setProxyPort(i);
43 }
44 }
45 call.setSOAPTransport(soaphttpconnection);
46 return call;
47 }
48
49 public GoogleSearch()
50 {
51 key = null;
52 soapServiceURL = "";
53 q = null;
54 start = new Integer(0);
55 maxResults = new Integer(10);
56 filter = new Boolean(true);
57 restrict = "";
58 safeSearch = new Boolean(false);
59 lr = "";
60 ie = "UTF-8";
61 oe = "UTF-8";
62 proxyHost = null;
63 proxyPort = 80;
64 proxyUserName = null;
65 proxyPassword = null;
66 if(System.getProperty("google.soapEndpointURL") != null)
67 setSoapServiceURL(System.getProperty("google.soapEndpointURL"));
68 else
69 setSoapServiceURL("http://api.google.com/search/beta2");
70 }
71
72 protected static final String defaultEndpointURL = "http://api.google.com/search/beta2";
73 protected String key;
74 protected String soapServiceURL;
75 protected String q;
76 protected Integer start;
77 protected Integer maxResults;
78 protected Boolean filter;
79 protected String restrict;
80 protected Boolean safeSearch;
81 protected String lr;
82 protected String ie;
83 protected String oe;
84 protected String proxyHost;
85 protected int proxyPort;
86 protected String proxyUserName;
87 protected String proxyPassword;
88}
89