Pages

Thursday, 3 July 2014

Using ClientRequestFilters in JDeveloper REST Clients

Usecase:
This sample demonstrates the use of ClientRequestFilters in REST Clients developed using JDeveloper.
For this, we will be creating a JAXRS client with a ClientRequestFilter which sets the header param ‘Client-Name’ and passes it on in the request.
Pre-Requisite:
JDev 12.1.3.0.0 and JAXRS 2.0 support
Steps:
Download the service here and run EmployeeService.java. Copy the WADL URL thus obtained.
Here, I will explain just the key points in the service.
This is a JAXRS 2.0 based service with a single GET method (as shown), which fetches employee details. 
    @HeaderParam("Client-Name") String client;    
    @GET
    @Produces("application/xml")
    public Response getEmpList() {
        if (client == null || client == "") {
            System.out.println("Client :" + client);
            return Response.status(404).entity("Client details required").build();
        } else if (client.equalsIgnoreCase("My Client")) {
            System.out.println("Client-Name:" + client);
            return Response.status(200).entity(new EmployeeList(employeeList)).build();
        } else {
            return Response.status(401).entity("Not authorized to view the details").build();
        }
}
@HeaderParam is used here to extract the value of the header field ‘Client-Name’ received in the request. Based on this value, the service returns the status of the response as:
               404 – header value missing
               200 – header value matches ‘My Client’
               401 – Some invalid value has been supplied

Creating the client:
Create a new custom application. Call it ‘ClientFilterDemo’.




Invoke the RESTful Client and Proxy wizard.

Select JAXRS 2.0 style.

Paste the WADL URL copied previously.


Click Next. Change the Class name to a more convenient name ‘ClientDemoClass’.


Finish the wizard to create the client.
Open ClientDemoClassClient.java. Add the following code to invoke the GET operation of the service where it says //add your code here.
 System.out.println(clientdemoclassemployee.getAsXml(String.class));

Next, create a new Java class called ‘HeaderSetFilter’ which implements ‘ClientRequestFilter’.






Write the following code in this HeaderSetFilter class.
package project1;
import java.io.IOException;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.ext.Provider;
@Provider
public class HeaderSetFilter implements ClientRequestFilter {
    public HeaderSetFilter() {
        super();
    }
    @Override
    public void filter(ClientRequestContext clientRequestContext) throws IOException {
        // TODO Implement this method
        if (clientRequestContext.getHeaders().get("Client-Name") == null) {
            clientRequestContext.getHeaders().putSingle("Client-Name", "My Client");
        }
    }
}
In this code, we have implemented the filter method. If Client-Name has not been set, we are setting it to ‘My Client’. We have also added the @Provider annotation to ensure that this class is auto-discovered. This class will be executed just before the request is sent by the client. Hence, the header Client-Name will be added to the request before transmitting it.
However, we need to register this class to the client code. This is done in ClientDemoClass.java. Find the method customizeClientConfiguration. Add the following line to it to register the filter class:
   cc.register(HeaderSetFilter.class);    
That’s it.
Now, run the client and you will see the employee data in the console.
If you change the value of Client-Name in filter() method of HeaderSetFilter, 401 will be seen.

No comments:

Post a Comment