How to Publish REST services using Oracle SOA Suite 12c

Hello and welcome!

 

Continuing our deep dive into the Oracle SOA Suite 12c, we will now explore the new REST adapter! That is something clients have been asking for a long time and it is finally here!

For our test I will create a REST POST web-service that will receive either XML, JSON or URL params and pass them to a BPEL process, so as always we will start creating a new SOA pŕoject:

 

ScreenshotRESTBPELCreate

After the process is created we will need to design our data structure for our example to work, to do that we will create a new XSD Schema file! Yes, we are creating a REST service, stay with me, we are going somewhere with this, here is my schema:


<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:vrs="https://vrsbrazil.wordpress.com/RESTSchema"
targetNamespace="https://vrsbrazil.wordpress.com/RESTSchema" elementFormDefault="qualified">
<xsd:element name="restElement">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="dateTime" type="xsd:dateTime"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Important: If you want to create a schema from scratch like I am doing, on the “New Gallery” choose the “XML Schema” option not the “XSD Schema” this one is a wizard for creating XSDs based on file patterns, not covered on this post.

Next we drag a REST Adapter from the Components Palette into the Exposed Services column at the composite.xml, this will bring up the “Create REST Binding” wizard:

ScreenshotRESTBPELWSWizard

At the “Operation Bindings” section, click on the “green plus sign” and then click on  “Add operation binding”, this will bring up the “REST Operation Binding” wizard:

ScreenshotRESTOpBindingWz

At the “Request” tab you will see the options offered for payload: XML, JSON, URL-Encoded and No Payload, we are choosing all BUT No Payload.

You should have also noticed a Schema URL field available, we will click on that one’s search icon and select the  “restElement” element on the XSD we just created. The operation Name and HTTP verb is on the image below:

ScreenshotRESTOpWz2

At the “Request” screen, on the “Payload” section there is a cogs button, this button creates data examples for the payloads we have selected, here are those it created when I clicked:

XML:


<restElement xmlns="https://vrsbrazil.wordpress.com/RESTSchema">
   <name>name44</name>
   <dateTime>2014-07-22T16:06:58.416</dateTime>
</restElement>

JSON:

{
  "name" : "name43",
  "dateTime" : "2014-07-22T16:06:58.415"
}

URL-Encoded:

name=name45&dateTime=2014-07-22T16%3A06%3A58.416

If you want the wizard will save these generated code on your Adapters folder within the project, it can be useful!

Now for our response tab I will also choose our xsd element and choose all data available BUT “No payload” again:

ScreenshotRESTOpWzFinal

We are all set here, click OK to create the operation and OK again on the “Create REST binding” wizard.

ScreenshotRESTBdnCreated

Time to create our Synchronous BPEL process, drag a BPEL component to the composite.xml screen.
On the creation wizard, at the Input/Output section, select our XSD element for both and UNCHECK “Expose as a SOAP Service”:

ScreenshotRESTNewBPEL

Your composite should look like this:

ScreenshotRESTNewBPELComposite

A Mediator is all we need now to link both these guys:

ScreenshotRESTMediatorComp

Make sure you assign the payload values for input and output:

ScreenshotRESTMediatorAsgn

And do the same thing on your BPEL process:

ScreenshotRESTBPELAsgn

If your process looks like this you are probably all set:

ScreenshotRESTBPELProcess

Finally we can deploy and test it, lets start with the basic XML Request and XML Response:

ScreenshotRESTXMLXMLTest
ScreenshotRESTXMLXMLTestResponse

Looking good, how about a JSON/JSON Test:

ScreenshotRESTJSONJSONTest
ScreenshotRESTJSONJSONTestResponse

Let’s mix it up a little with a URL/JSON one:

ScreenshotRESTURLJSonTest

Well this is it, hope I could help you!

Thank you,

Advertisements

Calling a RESTful web service from an Android application

This a simple example of how you can call a RESTful web service from an Android application.
First of all, you need to request a permission to access network. This can be done by adding the following tag to your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

Then we can go to the actual implementation. The easiest way is to use Apache Http Client, which is bundled with Android:

        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

If you want it to run on separate thread I’d recommend extending the AsyncTask:

class RequestTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        //Do anything with response..
    }
}

The AsyncTask class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread.
You then can make a request by adding the following line to your code:

RequestTask task = (RequestTask) new RequestTask().execute("http://www.myserver.com/rest/myservice");

If you are going to access a service from your local machine, keep in mind that the localhost IP address for Android is 10.0.2.2 instead of 127.0.0.1.
After the execute method returns, you just have to call the following method to get the response:

	        try {
				String message = task.get();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

From there, you can manipulate the response message the way you need.

That’s it!
Thanks!