Programming Tutorials

Creating a JavaBean to Connect with Google API

By: Bruce W. Perry in Java Beans Tutorials on 2008-11-24  

The first thing to do is get set up with a Google Web Services account.  Now create a JavaBean that will make keyword searches of Google and return the results.

Example java bean below, first imports the package contained in googleapi.jar: com.google.soap.search. Remember, you stored that JAR file in WEB-INF/lib. This means that the web application can find the Java classes in that package and the GoogleBean in the below sample program can use it.

A JavaBean that searches Google's web database
  

import com.google.soap.search.*;

public class GoogleBean {

    private GoogleSearch search;
    private GoogleSearchResult googleRes;
    private final static String GOOGLE_KEY = 
      "5W1BWPyzPSyI3rIa5Pt3DtXMatsniSGB";
  
    private String lineSep = "\n";
  
    //Settable bean properties
    private String query;
    private boolean filter;
    private int maxResults;
    private int startRes;
    private boolean safeSearch;
    private String restrict;
    private String langRestrict;

  public GoogleBean(){ //No-arguments constructor for the bean
      query = "";
      restrict = "";
      langRestrict = "";
  }
  
  public String structureResult(GoogleSearchResult res){
      //Each GoogleSearchResultElement
      GoogleSearchResultElement[] elements = res.getResultElements();
          String url ="";
          String results = "Estimated total results count: " +
              res.getEstimatedTotalResultsCount() + lineSep + lineSep;
          
          for (int i = 0; i < elements.length; i++){
            url = elements[i].getURL();
            results += ("Title: " + elements[i].getTitle() + lineSep +
                    "URL: <a href=\"" + url + "\">" + url + "</a>"+ lineSep +
                        "Summary: " + elements[i].getSummary() + lineSep +
                        "Snippet: " + elements[i].getSnippet() + lineSep + lineSep);
          }
          return results;
 }
  
  public String getSearchResults() throws GoogleSearchFault {

      search = new GoogleSearch();
          search.setKey(GOOGLE_KEY);
          search.setFilter(filter);
          if(restrict.length() > 0)
              search.setRestrict(restrict);
          search.setQueryString(query);
          googleRes = search.doSearch();
          return structureResult(googleRes);
  }
  
  public void setLineSep(String lineSep){
      this.lineSep=lineSep;
  }
   public String getLineSep(){
      return lineSep;
  }
  public void setQuery(String query){
      this.query = query;
  }
  
  public String getQuery(){
      return query;
  }
  
  public void setRestrict(String query){
      this.restrict = restrict;
  }
  
  public String getRestrict(){
      return restrict;
  }
  
  public void setLangRestrict(String langRestrict){
  
      this.langRestrict = langRestrict;
  }
  
  public String getLangRestrict(){
      return langRestrict;
  }
  
  public void setFilter(boolean filter){
      this.filter = filter;
  }
  
  public boolean getFilter(){
      return filter;
  }
  
  public void setSafeSearch(boolean safeSearch){
      this.safeSearch = safeSearch;
  }
  
  public boolean getSafeSearch(){
      return safeSearch;
  }
  
  public void setMaxResults(int maxResults){
      this.maxResults = maxResults;
  }
  
  public int getMaxResults(){
      return maxResults;
  }
  
  public void setStartRes(int startRes){
      this.startRes = startRes;
  }

  public int getStartRes(){
      return startRes;
   }
}//GoogleBean

The interesting action in the program above occurs in the methods getSearchResults() and structureResults().

In getSearchResults(), the code creates a GoogleSearch object, which is then customized with Google search options before the GoogleSearch doSearch()method is called. The GoogleSearch object uses setter methods to design a specific google.com search. For example, the setQueryString() method provides the user's search terms. The Java objects that are using the bean provides the search terms by calling the bean's setQuery() method.

You can set the various options for Google searches by calling the GoogleSearch setter methods. For example, calling setFilter(true) filters out all the results that derive from the same web host. And you can restrict the search to specific Google subsites by calling setRestrict("mac"). See http://www.google.com/apis/reference.html.

Every SOAP-related search of Google must call the GoogleSearch setKey() method with the proper license key, or the search is rejected.

The structureResults() method formats the search results. Google search results are encapsulated by a GoogleSearchResult object. This object contains an array of GoogleSearchResultElement objects, which represent each URL that the Google search has returned. The GoogleSearchResult getResultElements() method returns the GoogleSearchResultElement array.

The code then iterates through the array. Each returned element (the GoogleSearchResultElement object) has getter or accessor methods that provides information about the web-page search result:

  • getURL() returns the URL of the found item

  • getTitle() returns the title of the found HTML page

  • getSnippet() returns a snippet (a small, possibly ambiguous, piece of text from the web page)

  • getSummary() returns a text summary of the found web page

The bean uses these methods to display the URL, title, snippet, and summary of each web page the search returns.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java Beans )

Latest Articles (in Java Beans)