Tuesday, June 26, 2012

Part I: Verify User – JSON REST API

Untitled Document
Application Download: https://www.dropbox.com/s/s7csgy8svcij3aj/VerifyUserJson.zip

Use Case Scenario

I wanted to be able to invoke a REST service from an application (regardless of it being ADF Mobile, ADF portal, BPEL, or a standard HTML5 JQuery Mobile app). So I built the sample application (using JDev as a JSON REST app using JAX-RS Jersey and standard J2EE. This was deployed to my standalone WebLogic 10.3.5 server. I created a standard J2EE mobile application (using JDev … coming in PART II) to invoke and consume the REST service. Both were deployed to the same server so they could leverage the same domain name. I will discuss more about why they need to be on the same domain (in PART II when we build the mobile app).

Part I: VerifyUserJson App

In JDev they’ve built in support for REST Web Services projects. So we’ll go ahead and use this version of JDEV. We’re still able to package and deploy the application on a standard 10.3.5 instance of WebLogic because we include the .jar libs in our WEB-INF/lib folder (included in the sample app).

After that I created three Java classes (LoginRequest, LoginRequestHandler, and DataHandler).
  • LoginRequest()
    • This java class has the main verifyLogin method which accepts a username and password as input parameters. It returns either true or false back to the calling method.
  • LoginRequestHandler()
    • This is method invokes the LoginRequest method and returns the response as plain/text. You can also return application/xml or application/json depending on how you want to display it in your application. I also used the GET HTTP method to retrieve the “stuff” from the resource.
  • DataHandler()
    • Method to get the connection to the ORACLE XE database for the HR Schema. You can change this to point to any database that has the HR SCHEMA.
We need to modify the web.xml file to include the JERSEY servlet and servlet mapping.We open the web.xml and add these lines of codes before the closing </web-app>

1:   <servlet>  
2:    <servlet-name>jersey</servlet-name>  
3:    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>  
4:    <load-on-startup>1</load-on-startup>  
5:   </servlet>  
6:   <servlet-mapping>  
7:    <servlet-name>jersey</servlet-name>  
8:    <url-pattern>/jersey/*</url-pattern>  
9:   </servlet-mapping>  

I added these .jar libs to my WEB-INF/lib folder so that I don’t have to worry about matching any of the versions on the application server. Here is a list of the libs in the screenshot below. They are also attached to the code in this blog post. Your application structure should look similar to this: You can test the LoginRequest by running it locally in your JDev (main method) before you deploy it to your
 WebLogic server.

After I deploy the application to server we can test it by invoking this URL with the appropriate parameters.

1:  http://localhost:7101/VerifyUserJsonApp/jersey/verifyUser?p_username=SKING&p_password=welcome1  

If you have any suggestions or comments please let me know.