Friday 16 November 2018

Rest Web Service Call in Oracle Forms

Getting  Locations from Google using Rest Web Service Call in Oracle Forms

Steps to call Web Service in Oracle Forms

1. Open SoapUI tool and create new REST project.
2. Now add the web service url in the url path and click on import WADL.

3. Generated file is placed in the folder and the path is mentioned in the main java source code as below.

    @Generated(value = { "wadl|file:///C:/2.wadl", "run|21a8cd13-a951-4a69-921a-51a6e3f9f412" }

4. Open jdeveloper and build the following java source as new project.
Google maps API is used in this code,to add other web service url modify the url in the _uriBuilder and finally archive the project.

package rest_test;

import com.sun.jersey.api.client.Client;
import javax.annotation.Generated;
@Generated({ "Oracle JDeveloper", "run|21a8cd13-a951-4a69-921a-51a6e3f9f412" })
public class MapsGoogleapisComClient {
public static void main(String[] args) {
Client client = MapsGoogleapisCom.createClient();

MapsGoogleapisCom.MapsApiPlaceAutocompleteJson mapsgoogleapiscommapsApiPlaceAutocompleteJson =
MapsGoogleapisCom.mapsApiPlaceAutocompleteJson(client,"123");

// add your code here
String str;
str = mapsgoogleapiscommapsApiPlaceAutocompleteJson.get();
}
    
public static String function_call(String input) {
Client client = MapsGoogleapisCom.createClient();
MapsGoogleapisCom.MapsApiPlaceAutocompleteJson mapsgoogleapiscommapsApiPlaceAutocompleteJson =
MapsGoogleapisCom.mapsApiPlaceAutocompleteJson(client,input);
String str;
str = mapsgoogleapiscommapsApiPlaceAutocompleteJson.get();  
return str;
}  
}

package rest_test;

import java.net.URI; 
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder; 
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

@Generated(value = { "wadl|file:///C:/2.wadl", "run|21a8cd13-a951-4a69-921a-51a6e3f9f412" },
comments = "wadl2java, http://wadl.java.net", date = "2018-08-03T08:24:33.033+05:30")
public class MapsGoogleapisCom {
/**
* The base URI for the resource represented by this proxy
*
*/
public final static URI BASE_URI;
static {
URI originalURI = URI.create("https://maps.googleapis.com");
// Look up to see if we have any indirection in the local copy
// of META-INF/java-rs-catalog.xml file, assuming it will be in the
// oasis:name:tc:entity:xmlns:xml:catalog namespace or similar duck type
java.io.InputStream is = MapsGoogleapisCom.class.getResourceAsStream("/META-INF/jax-rs-catalog.xml");
if (is != null) {
try {
// Ignore the namespace in the catalog, can't use wildcard until
// we are sure we have XPath 2.0
String found =javax.xml.xpath.XPathFactory.newInstance().newXPath().evaluate("/*[name(.) = 'catalog']/*[name(.) = 'uri' and @name ='" +
originalURI + "']/@uri",
new org.xml.sax.InputSource(is));

if (found != null && found.length() > 0) {
originalURI = java.net.URI.create(found);
}

} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
is.close();
} catch (java.io.IOException e) {
}
}
}
BASE_URI = originalURI;
}
    /**
     * Json.
     *
     */
public static MapsGoogleapisCom.MapsApiPlaceAutocompleteJson mapsApiPlaceAutocompleteJson(Client client,
URI baseURI,String input) {
        return new MapsGoogleapisCom.MapsApiPlaceAutocompleteJson(client, baseURI, input);
    }

    /**
     * Template method to allow tooling to customize the new Client
     *
     */
    private static void customizeClientConfiguration(ClientConfig cc) {
    }

    /**
     * Template method to allow tooling to override Client factory
     *
     */
    private static Client createClientInstance(ClientConfig cc) {
        return Client.create(cc);
    }

    /**
     * Create a new Client instance
     *
     */
    public static Client createClient() {
        ClientConfig cc = new DefaultClientConfig();
        customizeClientConfiguration(cc);
        return createClientInstance(cc);
    }

    /**
     * Json.
     *
     */
    public static MapsGoogleapisCom.MapsApiPlaceAutocompleteJson mapsApiPlaceAutocompleteJson(String input) {
        return mapsApiPlaceAutocompleteJson(createClient(), BASE_URI,input);
    }

    /**
     * Json.
     *
     */
public static MapsGoogleapisCom.MapsApiPlaceAutocompleteJson mapsApiPlaceAutocompleteJson(Client client,String input) {
return mapsApiPlaceAutocompleteJson(client, BASE_URI,input);
}
 /**
 * Json.
     *
     */
public static class MapsApiPlaceAutocompleteJson {
private Client _client;
private UriBuilder _uriBuilder;
private Map<String, Object> _templateAndMatrixParameterValues;

private MapsApiPlaceAutocompleteJson(Client client, UriBuilder uriBuilder, Map<String, Object> map) {
_client = client;
_uriBuilder = uriBuilder.clone();
_templateAndMatrixParameterValues = map;
}
/**
* Create new instance using existing Client instance, and a base URI and any parameters
         *
         */
public MapsApiPlaceAutocompleteJson(Client client, URI baseUri,String input) {
_client = client;
_uriBuilder = UriBuilder.fromUri(baseUri);
_uriBuilder = _uriBuilder.path("/maps/api/place/autocomplete/json");
_uriBuilder =
_uriBuilder.queryParam("input", input);
_uriBuilder =
_uriBuilder.queryParam("key", "AIzaSyApl0Qp1v94eBH0qqDGFhdUbz4Zr5gX7hs");
_uriBuilder =
_uriBuilder.queryParam("sensor", "false");
_templateAndMatrixParameterValues = new HashMap<String, Object>();
}
//        /**
//         * Json.
//         *
//         */
//        public ClientResponse get() {
//            UriBuilder localUriBuilder = _uriBuilder.clone();
//            WebResource resource = _client.resource(localUriBuilder.buildFromMap(_templateAndMatrixParameterValues));
//            com.sun.jersey.api.client.WebResource.Builder resourceBuilder = resource.getRequestBuilder();
//            ClientResponse response;
//            response = resourceBuilder.method("GET", ClientResponse.class);
//           
//            return response;
//        }
 /**
* Json new trial.
*
*/
public String get() {
String Concatenated_desc = new String();
UriBuilder localUriBuilder = _uriBuilder.clone();
WebResource resource = _client.resource(localUriBuilder.buildFromMap(_templateAndMatrixParameterValues));
com.sun.jersey.api.client.WebResource.Builder resourceBuilder = resource.getRequestBuilder();
ClientResponse response;
response = resourceBuilder.method("GET", ClientResponse.class);
String output;
output = response.getEntity(String.class);
try {
for (int i=0; i<5; i++) {
JSONObject json = new JSONObject(output);
JSONArray pred_array = (JSONArray) json.get("predictions");
JSONObject pred_desc= (JSONObject) pred_array.get(i);
//   System.out.println("The First Description is : "+ pred_desc.get("description"));
if (i >0) { Concatenated_desc = Concatenated_desc + "||"+pred_desc.get("description");}
else
{ Concatenated_desc = Concatenated_desc +pred_desc.get("description");}
if (i == 4){
System.out.println(Concatenated_desc );
}
}
} catch (JSONException e) {
e.printStackTrace();
//  System.out.println(e);
}
return Concatenated_desc;
}
/**
* Json.
*
*/
public <T> T get(GenericType<T> returnType) {
UriBuilder localUriBuilder = _uriBuilder.clone();
WebResource resource = _client.resource(localUriBuilder.buildFromMap(_templateAndMatrixParameterValues));
com.sun.jersey.api.client.WebResource.Builder resourceBuilder = resource.getRequestBuilder();
ClientResponse response;
response = resourceBuilder.method("GET", ClientResponse.class);
if (response.getStatus() >= 400) {
throw new MapsGoogleapisCom.WebApplicationExceptionMessage(Response.status(response.getClientResponseStatus()).build());
}
return response.getEntity(returnType);
}
/**
* Json.
*
 */
 public <T> T get(Class<T> returnType) {
 UriBuilder localUriBuilder = _uriBuilder.clone();
 WebResource resource = _client.resource(localUriBuilder.buildFromMap(_templateAndMatrixParameterValues));
com.sun.jersey.api.client.WebResource.Builder resourceBuilder = resource.getRequestBuilder();
ClientResponse response;
response = resourceBuilder.method("GET", ClientResponse.class);
if (!ClientResponse.class.isAssignableFrom(returnType)) {
if (response.getStatus() >= 400) {
throw new MapsGoogleapisCom.WebApplicationExceptionMessage(Response.status(response.getClientResponseStatus()).build());
}
}
if (!ClientResponse.class.isAssignableFrom(returnType)) {
return response.getEntity(returnType);
} else {
return returnType.cast(response);
}
}
}
    /**
     * Workaround for JAX_RS_SPEC-312
     *
     */
 private static class WebApplicationExceptionMessage extends WebApplicationException {
private WebApplicationExceptionMessage(Response response) {
super(response);
}
/**
* Workaround for JAX_RS_SPEC-312
*
*/
public String getMessage() {
Response response = getResponse();
Response.Status status = Response.Status.fromStatusCode(response.getStatus());
if (status != null) {
return (response.getStatus() + (" " + status.getReasonPhrase()));
} else {
return Integer.toString(response.getStatus());
}
}
public String toString() {
String s = "javax.ws.rs.WebApplicationException";
String message = getLocalizedMessage();
return (s + (": " + message));
}
}
}

5. Create new system variable in system properties,the variable name as CLASSPATH and mention all the dependency jar files and archive file path in variable value.Dependent jar files are attached in the below Link

https://drive.google.com/open?id=1JKo_fDoSHH5oYJF0cEv3JdPTT-LCC67K

6. Open the Forms builder and goto Program -> Import java classes.Import the java class used in the java project.Find the imported java class in the program units.Now we can call the java class from forms builder.
For example here function_call method is used to invoke the webservice and return the response back,the value from combo box is passed to java and return the response to forms.
DECLARE
   jo   ora_java.jobject;
   rv   VARCHAR2 (32000);
BEGIN
   jo := mapsgoogleapiscomclient.NEW;
   rv := mapsgoogleapiscomclient.function_call (:cbox);
END;


No comments:

Post a Comment