corner imagecorner image
FeaturesPluginsDocs & SupportCommunityPartners

Consuming Google Web Services in Web Applications

The Google AdSense APIs, exposed as web services, are aimed at users who want to participate in Google's AdSense program. Google's AdSense program is a revenue sharing scheme where the user allows Google to place widgets on the user's site. These widgets can come in the form of ads, depending on the content of the user's site, or (as in the example used in this tutorial), the Google search widget. In all cases, Google shares its revenues with the user on the hits that these widgets attract.

Adding these widgets requires some programming effort, such as in this example, where you would typically be required to know about web services. However, this tutorial describes how you can use NetBeans IDE to add a Google search widget to your site with a very minimal level of knowledge and coding on your part.

Note: In this tutorial, we will be interacting with the Google developer sandbox. Its rules tend to change from time to time. If you encounter runtime errors or if the values specified in this tutorial do not work for you, you are advised to read the error text shown in the browser and to then check the code carefully for possible mistyping of entered values. Next, if everything is correct, then possibly the Google developer sandbox's values have changed. Refer to http://code.google.com/apis/adsense/ to adjust the values, if needed.

Contents

Content on this page applies to NetBeans IDE 6.1

Software Needed for the Tutorial

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE Web and Java EE, version 6.1
Java Development Kit (JDK) version 6 or
version 5
Java EE-compliant web or application server Tomcat web server 6.0 and/or GlassFish application server v2
Both included with the Web and Java EE distribution of NetBeans IDE

The current Google AdSense service ID

Obtain it in the form ca-pub-xxxxxx from Google support.

Creating a Web Project

First, you need to create a web project. In this case, we will use one of the samples that is included in the New Project wizard as our starting point.

  1. Choose File > New Project. Under Categories, select Samples and then RESTful Web Services. Under Projects, select Customer Database, as shown below:

    New Project wizard showing Customer Database sample project chosen

  2. Click Next and click Finish.

The sample project opens in the IDE, with its project outline in the Projects window.

Adding the Google Web Service

By dragging and dropping an item from the Component Palette, you can let the IDE generate all the code needed for communicating with the Google web service. Once you have done so, you can refer to the related Google APIs, as shown below, to customize the code. Finally, the IDE helps you test the application by providing a web client that invokes all the operations available on the web service.

To add the Google Web Service:

  1. Open the CustomerResource class in the customerdb.service package.
  2. Add the following method to the class:
    @Path("adSenseForSearch")
    @GET
    @ProduceMime("text/html")
    
    public String getAdSenseForSearchCode() {
    // dnd here and return the generated html code.
    
        return null;
    
    }
  3. Open the Services tab in the IDE. Expand the Web Services node. You see a list of web services. Locate the Google node and expand it. From the list of Google services, locate the AdSenseForSearch Service and expand it, revealing the GoogleAdSenseForSearch operation. Accept all certificates that you are asked to accept.

    Google services in Web Services Manager

  4. Drag the generateSearchBoxCode operation and drop it into the getAdSenseForSearch method body.


    When you drop the item, a certificate appears (unless you already trust sandbox.google.com). Accept the certificate. The following customizer appears (the image shows the customizer after it is filled out):

    generateSearchBoxCode customization dialog

  5. Fill in the customizer with the following information:
    • country. US
    • searchType. GoogleSiteSearch
    • selectedDomain. wiki.netbeans.org
  6. Click OK. A progress bar appears while the IDE adds the web service to the project. The complete getAdSenseForSearch method body follows:
    @Path("adSenseForSearch")
        @GET
        @ProduceMime("text/html")
        public String getAdSenseForSearchCode() {
    // dnd here and return the generated html code.
            try {
            java.lang.String synServiceId = "";
            java.lang.String country = "US";
            java.lang.String searchType = "GoogleSiteSearch";
            com.google.api.adsense.v2.SiteProperties siteProperties = null;
            com.google.api.adsense.v2.SearchOptions searchOptions = null;
            java.util.List domains = null;
            java.lang.String selectedDomain = "wiki.netbeans.org";
            com.google.api.adsense.v2.SearchBoxStyle searchBoxStyle = null;
            java.lang.String channelName = "";
            com.google.api.adsense.v2.AdSenseForSearchServiceService service = new com.google.api.adsense.v2.AdSenseForSearchServiceService();
            com.google.api.adsense.v2.AdSenseForSearchService port = service.getAdSenseForSearchService();
            // TODO process result here
            java.lang.String result = port.generateSearchBoxCode(synServiceId, country, searchType, siteProperties, searchOptions, domains, selectedDomain, searchBoxStyle, channelName);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    
        return null;
    
    }
  7. Customize the getAdSenseForSearch method body code slightly, for the following local variables:
    synServiceId = "partner-pub-6309917007979926";
    // the digits in synServiceId must be the same as the digits in the AdSense Service ID
    

    Copy the numeric part of the Google AdSense service ID you obtained at the beginning (see Software Needed for This Tutorial). Use this number as the numeric part of synServiceId.

Adding Code to the Application

This section walks you through implementing code from the Google API and shows you where to go for further information. All code is implemented in the class AdSenseForSearchResource.

  1. Add the following setHeaderParameters method anywhere in CustomerResource. Use the value for partner-pub-xxxxx that you obtained before beginning the tutorial (see Software Needed for This Tutorial). Fix imports after pasting the code.
    private void setHeaderParameters(AdSenseForSearchService port) {
           WSBindingProvider bp = (WSBindingProvider) port;
           bp.setOutboundHeaders(
               com.sun.xml.ws.api.message.Headers.create(new QName("http://www.google.com/api/adsense/v2", "client_id"), "partner-pub-6309917007979926"),
               com.sun.xml.ws.api.message.Headers.create(new QName("http://www.google.com/api/adsense/v2", "developer_password"), "Q6R3l6a483"),
               com.sun.xml.ws.api.message.Headers.create(new QName("http://www.google.com/api/adsense/v2", "developer_email"), "adsensedeveloper1@google.com")
           );
    }
          
  2. Add the following to the getAdSenseForSearch method body, below // TODO process result here:
    domains = java.util.Arrays.asList(new String[]{selectedDomain});
    // Set the search options
    searchOptions = new SearchOptions();
    
    // Set the site properties
    siteProperties = new com.google.api.adsense.v2.SiteProperties();
    siteProperties.setLocale("en");
    siteProperties.setEncoding("");
    
    // Set the style
    searchBoxStyle = new com.google.api.adsense.v2.SearchBoxStyle();
    searchBoxStyle.setLogoType("GoogleLogo");
    searchBoxStyle.setBackgroundColor("#CCCCCC");
    searchBoxStyle.setTextColor("black");
    searchBoxStyle.setTextBoxLength(40);
          searchBoxStyle.setSearchStyleName("Blue Sky");
  3. Also add this line to the getAdSenseForSearchCode method body:
    setHeaderParameters(port);
  4. Read AdSenseForSearch#generateSearchBox for details on further customization that you can add to the search options.

Testing the Application

To test the application, do the following:

  1. In the Projects window, right click the project and choose Test RESTful Web Services. The server starts and the browser opens, displaying the RESTful web client, which you can use for testing the interaction with the RESTful web service.
  2. Now, Select the '/customers' link and then click Test. Next, select a customer link (such as '/customers/1/') and then click Test again. Finally, select the '/customers/1/adSenseForSearch/' link and make sure to select MIME type 'text/html', as shown below, and click Test again.

    In the Raw View tab, you should now see the Google Search box.

    REST test client showing Google Search box

  3. Enter some words in the search box and then click Search. The browser opens, with your search results, proving that you application is functioning correctly.
  4. Click the link above the Test Input line. The link looks like http://localhost:8080/CustomerDB/resources/customers/1/adSenseForSearch/. The link takes you out of the test mode and into a new browser window for the web application itself.


See Also

For more information about using NetBeans IDE 6.0 to develop Java EE applications, see the following resources:

To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, join the mailing list.

Companion
Projects:
MySQL Database Server   GlassFish Community: an Open Source Application Server   Open Solaris  Open JDK: an Open SourceJDK   Mobile & Embedded Community     Sponsored by 
Sponsored by Sun Microsystems