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.
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.
Choose File > New Project. Under Categories, select Samples
and then RESTful Web Services.
Under Projects, select Customer Database, as shown
below:
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:
Open the CustomerResource class in the customerdb.service
package.
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;
}
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.
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):
Fill in the customizer with the following information:
country.US
searchType.GoogleSiteSearch
selectedDomain.wiki.netbeans.org
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;
}
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.
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.
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");
Also add this line to the getAdSenseForSearchCode method body:
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.
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.
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.
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.
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.