You define the client filter in a Java Application project in NetBeans using the Jersey client API to define the logic. For more information about the Jersey API, see the Jersey 1.0.3.1 Javadocs. The following are the primary classes of interest:
com.sun.jersey.api.client.ClientHandlerException
com.sun.jersey.api.client.ClientRequest
com.sun.jersey.api.client.ClientResponse
com.sun.jersey.api.client.filter.ClientFilter
Right-click in the Projects window of the NetBeans IDE, and then select New Project.
The New Project Wizard appears.
Select Java in the Categories list, and then select Java Application in the Projects list.
Click Next.
The Name and Location window appears.
Enter a name for the project and a name for the main Java class, and then click Finish.
The project is created and a Java file containing the code framework appears in the Java Editor.
If you did not specify the class name in the wizard, you can do the following to rename the Java class:
Do the following to add the required Jersey JAR files to the project:
In the Project window, right-click the Java application, and then select Properties.
The Properties window appears.
Under Categories, select Libraries.
Click the Compile tab, and then click Add Jar/Folder.
Browse to and select the following Jersey JAR files:
jersey-bundle-1.0.3.1.jar
jsr311-api-1.0.jar
You can find these files in glassfish-home/lib.
Click OK.
Define the processing logic for the filter in the Java package that was created for you by the wizard.
Use standard Java methods along with the methods defined in the Jersey API to define the filter, exception, and logging logic. See the example following this procedure for a simple implementation.
The following example illustrates the code for a simple authentication filter that has a user name and password as parameters.
package javafilter; import com.sun.jersey.api.client.ClientHandlerException; import com.sun.jersey.api.client.ClientRequest; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.filter.ClientFilter; import java.util.logging.Level; import java.util.logging.Logger; public class Filter extends ClientFilter { private static final Logger logger = Logger.getLogger(Filter.class.getName()); private String username; private String password; public Filter() { } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } @Override public ClientResponse handle(ClientRequest request) throws ClientHandlerException { logger.log(Level.INFO, "inside handle() method, username=" + username + ", password=" + password); ClientResponse response = getNext().handle(request); return response; } } |