Wednesday, 9 April 2014

Passing Header parameters in WebService Data Control


About

In this blog, I will be explaining on how to pass header parameters in WebServiceDataControl

Implementation:

In this example, i have webservice where based on the Header parameter value the system will greet the user such as any value other than John will be treated as USER.

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

@WebService(serviceName = "HeaderService")
public class HeaderService {
    public HeaderService() {
        super();
    }
    @Resource
    WebServiceContext ctx ;

    public String greetUser() {
        try{
        String tenantID = null;
        MessageContext context = ctx.getMessageContext();
        Map requestHeaders =
            ((Map)context.get(MessageContext.HTTP_REQUEST_HEADERS));
        if (requestHeaders != null && requestHeaders.get("TenentID") != null) {
            tenantID =
                    ((List)requestHeaders.get("TenentID")).get(0).toString();
        }
        if (tenantID.equalsIgnoreCase("John")) {
            tenantID = "ADMIN";
        } else {
            tenantID = "USER";
        }
            return "Hello " + tenantID;
        }catch(Exception e)
        {
        e.printStackTrace();
        }
       return null;
    }
}

In JDeveloper, create a New Fusion Web Application and Invoke the WebService DataControl from the New Gallery


Provide the WSDL of the above service deployed and click on Next. In the DataControl Operations page select the method to exposed in our case the greetUser and select the Include Http Headers Check box.


Click on finish and complete the wizard. Now Expand the DC palette we can observe that the method exposes  the Http Header Parameter (Map Type) as input parameter for the method and the output as String.



In the ViewController project create a jspx page with bacing bean or you can also create java class and associate it to the page.I will go with the latter approach as it is easy

Now open the Backing Bean java Class and add a Map variable with getters and setters. Then set the value for the Map field and initialize in the setf1 method as it is executed when the page loads.
 private Map httpheadermap;
public void setHttpheadermap(Map httpheadermap) {
        this.httpheadermap = httpheadermap;
    }

public Map getHttpheadermap() {
        return httpheadermap;
    }
// Here we are setting the value to the Map , you can also set in //the setters of the Map

public void setF1(RichForm f1) {
        this.f1 = f1;
        setHttpheadermap(new HashMap());
        httpheadermap.put("TenentID", "John");
    }



After creating the jspx page, Drag and Drop the method from the DC palette onto the page as ADF parameter form
On dropping it you will get the Edit Form Fields dialog where delete the HttpHeader Field that is exposed

In  the Edit Action Binding Dialog for the Http Header Parameter bind it to  the Map defined in the Backing bean 


Now Run the page.  Click on the GreetUser, we can see that it says as Hello ADMIN as we have set the value as JOHN in  the backing bean


No comments:

Post a Comment