REST Binding Component User's Guide

Implementing Jersey Client Filters

You can use Jersey client filters to modify a REST request or response for an outbound REST client interaction. For example, you can define a filter for generating the appropriate authentication header based on user-supplied information. To implement filters, you need to define the logic

ProcedureTo Define the Jersey Filter

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:

  1. Right-click in the Projects window of the NetBeans IDE, and then select New Project.

    The New Project Wizard appears.

  2. Select Java in the Categories list, and then select Java Application in the Projects list.

  3. Click Next.

    The Name and Location window appears.

  4. 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.

    Figure shows the generated framework for a Java
Application.
  5. If you did not specify the class name in the wizard, you can do the following to rename the Java class:

    1. In the Projects window under the Java project, right-click Main.java.

    2. Point to Refactor and then select Rename.

    3. Enter the new name, and then click Refactor.

  6. Do the following to add the required Jersey JAR files to the project:

    1. In the Project window, right-click the Java application, and then select Properties.

      The Properties window appears.

    2. Under Categories, select Libraries.

    3. Click the Compile tab, and then click Add Jar/Folder.

    4. Browse to and select the following Jersey JAR files:

      • jersey-bundle-1.0.3.1.jar

      • jsr311-api-1.0.jar


        Tip –

        You can find these files in glassfish-home/lib.


      Figure shows the required Jersey JAR files added
to the project properties.
    5. Click OK.

  7. 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.


Example 2 Simple Authentication Filter

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;
   }
}

ProcedureTo Add the Filter to the Composite Application

Before You Begin

Before you can perform this step, you need to complete the following steps:

  1. Open the composite application for the project in which you want to implement filters.

  2. On the CASA Editor, right-click the REST outbound endpoint to which you want to add the filter, and then click Properties.

    Figure shows the REST endpoint in a composite
application.

    The Properties Editor appears.

    Figure shows the endpoint Properties Editor.
  3. Click the ellipsis next to the Filters property under JAX-RS (Jersey) Extension.

    The Filters Editor appears.

  4. On the Filters Editor, click Add.

  5. From the dialog box that appears, navigate to and select the Java Application project you created to define the filter logic. Click Open.

    The Select Java Libraries dialog box appears.

  6. Click OK on the dialog box. Do not select the Jersey libraries.

    The information from the Java Application project is populated into the Filter Editor.

    Figure shows the Filter Editor for REST filters.
  7. If you defined parameters for the filter, enter the parameter values in the Parameters section at the bottom of the editor.

  8. Click OK.

    The filter name appears in the Filters property.

  9. Click Close on the Properties Editor.