Creating RESTful Service Clients in NetBeans Modules
Starting in NetBeans IDE 6.9, native REST support is available in NetBeans Module projects. You can now directly generate RESTful client code in a NetBeans module. You can also insert Jersey RESTful client code in a Java or Java Web application.In this tutorial, you create a NetBeans platform application that consumes the Twitter What Are You Doing service, displaying a list of your Twitter friends' status messages. First you create the platform application. You select the libraries needed in the application. Then you create a NetBeans module. Finally, you add a RESTful client and some basic display elements to the module. The client uses OAuth authorization.
Note: To use the procedure described
in this tutorial with a Java or Java Web project, skip the section on
Creating a Platform Application and create a Java or Java Web
application instead of a NetBeans Module. Ignore any directions that are
specific to NetBeans modules. If you are creating a Java application,
you can use most of the design in Designing the Window in a JFrame + JPanel. If you are using a Java Web application, you have to design your own JSP or JSF.
Note: You can follow the procedure in
this tutorial with another RESTful web service than the Twitter service,
if you have the URL for that service's WADL. You only need to register
the service in the IDE. To register a service, open the Services window,
right-click the Web Services node, and select Add Web Service.
Contents- Creating the Platform Application
- Creating the Client Module
- Designing the Window
- More Exercises
- See Also
Software or Resource | Version Required |
---|---|
NetBeans IDE | Java EE download bundle |
Java Development Kit (JDK) | version 6 or version 7 |
Creating the Platform Application
You can add NetBeans IDE libraries to a NetBeans Platform Application. In this section, you create the platform application and add the necessary libraries.To create the module suite:
- Click New Project (Ctrl-Shift-N on Linux and Windows, ⌘-Shift-N on MacOS). The New Project wizard opens.
- Select the NetBeans Modules category. Then select the NetBeans Platform Application project. Click Next.
- Name the project RestfulClientPlatformApp. Choose a location for the project. Accept the other default settings and click Finish. The RestfulClientPlatformApp project appears in the Projects window.
- In the Projects window, right-click the RestfulClientPlatformApp project node and select Properties. The Properties dialog opens.
- In the Properties dialog, select the Libraries category. Note that only the Platform libraries are included.
- Expand the node for the Enterprise libraries. Tick the Included box for the RESTful Web Service Libraries.
- The Resolve button is highlighted in red, because the RESTful Web Service Libraries depend on other libraries that are not included in the suite. Click the Resolve button to include those libraries.
- Click OK. The platform application is ready for you to create the client module.
Creating the Client Module
In just a few steps, you create a module that serves as a client for the Twitter What Are You Doing service. Twitter services are registered "out of the box" in the NetBeans IDE web service manager. You can add additionalTo create the module and the client functionality:
- In the Projects window, right-click the Modules subnode of the RestfulClientPlatformApp and select Add New... The New Module Project wizard opens.
- Name the module TwitterClientModule. Accept the default settings in the other fields and click Next. The Basic Module Configuration panel opens.
- Give the code name base an arbitrary name, such as org.my.twitter.friends. Accept the default settings in the other fields and click Finish. TwitterClientModule now appears in the Projects window, under the Modules node of the platform application.
- Right-click the TwitterClientModule node and select Open Project. A
TwitterClientModule node now appears at the root level of the Projects
window.
- Select the new, root-level TwitterClientModule node. Launch the New File wizard (Ctrl-N/⌘-N, or New File icon, or context menu of the node).
- In the New File wizard, select the Web Services category and the RESTful Java Client file type. Click Next. The New RESTful Java Client panel opens.
- Name the class TwitterClient and give it an arbitrary package name, or select the code name base you previously created.
- Under Select the REST resource, select IDE Registered. Click
Browse and browse for Twitter > Twitter OAuth > [statuses] >
[friends_timeline.{format}]. Select this resource and click OK.
Note: You can register additional web services in the IDE. Go to the Services window, right-click the Web Services node, and choose Add Web Service. You can add a local file or a RESTful URL.
- OAuth is automatically selected as the authentication type. Accept all defaults and click Finish.
- A warning dialog opens. The dialog asks if you want to generate java artifacts from XML schema references in the WADL file. Click Yes.
- Another warning may appear asking you to add modules to the classpath. Click OK.
- If you need to add modules to the classpath, right-click the
TwitterClientModule node and open its Project Properties. Go to the
Libraries section, and add the modules with the Add Dependency button.
This button opens a list of module dependencies to browse.
- CONSUMER_KEY: Consumer Key string
- CONSUMER_SECRET: Consumer Sectret string
- initOAuth(): method for OAuh intitialization
- getFriendsTimeline(): method corresponding to HTTP method: getFriendsTimeline (from the REST resource)
- makeOAuthRequestUnique(): useful for multiple API calls in one session
- login: used to login to the Twitter Application (forces the authorization). This method calls two more generated methods, getOAuthAccessToken and getOAuthRequestToken.
Getting OAuth Keys from Twitter
In order for the NetBeans Platform application to access Twitter data, you need to get CUSTOMER and CUSTOMER_SECRET keys, along with a verification string, from Twitter. Twitter uses OAuth authorization, which requires these keys. However, OAuth is set up with the expectation that it will be called by a web application on a server. In order to get the authorization keys, you register a "dummy" web application.To get the OAuth keys from Twitter:
- Open a browser. Go to the Twitter > Applications page and click Register a new application . You need to be logged into a Twitter account. Make sure you are logged into the correct account, if you have multiple accounts.
- Type NB Platform Friends Application in the Application Name text field.
- Type a description into the Description field. This is required. You can type something like "NetBeans Platform application calling the friends_timeline operation."
- Type an arbitrary URL into the Application Website field.
- In the Application Type option, select the Client radio button.
- In the Default Access Type option, select the Read and Write radio button.
- Leave other options default and press Save. A browser page opens with the details of the application you registered. The important details are the Consumer key and Consumer secret.
- Copy the Consumer key from the browser. In the IDE, locate the line where CONSUMER_KEY is set. Paste the value of the consumer key between the quotation marks.
- Copy and paste the Consumer secret key from the browser into TwitterClient. Save your changes.
Designing the Window
To complete the project, add a window. Populate the window with UI components and add actions so that clicking a button, for example, will show a list of friends' statuses.To design the window:
- In the Projects window, right-click the module's node and select New > Window. The New Window wizard opens with the Basic Settings panel.
- In the Basic Settings panel, select the editor window position, select Open on Application Start, and click Next. The Name, Icon and Location panel opens.
- In the Class Name Prefix field, type twitterFriendsStatus. Select the org.my.twitter.friends package. Click Finish.
- The twitterFriendsStatusTopComponent file opens in the Design view. A palette of Swing UI components opens on the right.
- Drag the following GUI elements into the design area:
Component Display text Settings Button Get Friends' Statuses Change variable name to getStatusesButton.
Unselect "enabled" property
Button Log in Change variable name to loginButton Scroll pane -- Text area -- Drag into scroll pane
- Double-click the Login button. The IDE generates an action method for the button, and the editor switches to the Source view with the action method focused.
- Type or copy the following code into the body of the login button
action method. This code launches the method for logging the application
into Twitter, enables the getStatuses button, and disables the login
button. The application only needs to log in once. Note that
TwitterClient does not need to be instantiated to call its login method.
private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) { TwitterClient.login(); getStatusesButton.setEnabled(true); loginButton.setEnabled(false); }
- Double-click the getStatuses button. The IDE generates an action
method for the button, and the editor switches to the Source view with
the action method focused.
- Paste or type the following handling code into the body of the
getStatuses button action method. This code instantiates the
TwitterClient and initializes OAuth, using the tokens that the login method created. The code then calls getFriendsTimeline, gets a list of Statuses,
adds a line with the username and text for each status to a String, and passes the String to the text area.
private void getStatusesButtonActionPerformed(java.awt.event.ActionEvent evt) { TwitterClient client = new TwitterClient("xml"); client.initOAuth(); Statuses response = client.getFriendsTimeline(Statuses.class, null, null, null, "10"); response.getStatus().size(); String text = ""; for (StatusType st : response.getStatus()) { text += st.getUser().getName() + ": " + st.getText() + "\n"; } jTextArea1.setText(text); }
- The code has some warning icons for classes that aren't found.
Press Ctrl-Shift-I (⌘-Shift-I on MacOS). The Fix All Imports dialog
opens. Select the twitter.twitteroauth.twitterresponse classes. Click OK.
Click the link, and a browser page opens with Twitter asking if you want to allow your application to access Twitter data. Click Allow, and the page refreshes with a PIN. Copy the PIN and paste it into the authentication dialog's verifier string field. Click OK.
The Log In button is now disabled, and the Get Friends' Statuses button is enabled. Click Get Friends' Statuses , and a list of your Twitter friends' latest status messages appears.
More Exercises
Here are a few more ideas for you to explore:- Add another window to the module, using other methods in the friends_timeline API.
- Add another module to the project, using a different Twitter resource.
- Explore the Facebook Module Sample at New Project > Samples > NetBeans Modules > Facebook Module Sample
See Also
For more information about using NetBeans IDE to develop Java EE applications, see the following resources:- Creating a Graphical Client for Twitter
- DZone: OAuth Support in NetBeans IDE 6.9
- Getting Started with RESTful Web Services
- NetBeans Platform Quick Start
- Web Services Learning Trail
- NetBeans Platform Learning Trail
To send comments and suggestions, get support, and keep informed on topics connected with developing RCP applications on the NetBeans platform, join the dev@platform.netbeans.org mailing list.
Tidak ada komentar:
Posting Komentar