AnsweredAssumed Answered

Want to run a standloane java code which will call a REST api in IBM ODM from activity designer

Question asked by anupamdasibm on Mar 14, 2018

I want to call this  REST api from Activity Designer through a groovy script or though a java class (after reading several times this Activiti User Guide ) am not able to understand to be honest what the clear cut java code

 

"https://odmdemo1.bpm.ibmcloud.com/odm/dev/DecisionService/rest/v1/AnupamsStuffDemoCOnfig/AnupamStuffPriceService"

which requires a basic authentication which i do have

 

    public  static String testStuffRules(String endpointURI,String userName,String password, String payload ) throws ClientProtocolException, IOException{
        
        String credentials = userName + ":" + password;
        String encodedValue = Base64.encodeBase64String(credentials.getBytes());
        String authorization = "Basic " + new String(encodedValue);

 

        String contentType = "application/json";
        
        CloseableHttpClient client = HttpClients.createDefault();
        
        try {
            HttpPost httpPost = new HttpPost(endpointURI);
            // Add the basic authentication header
            httpPost.addHeader("Authorization", authorization);
            httpPost.addHeader("Content-Type", contentType);
            httpPost.setEntity(new StringEntity(payload));
            CloseableHttpResponse response = client.execute(httpPost);
            try {
                if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                    System.err.println("Status Code: " + response.getStatusLine().getStatusCode());
                    System.err.println("Status Line: " + response.getStatusLine());
                    String responseEntity = EntityUtils.toString(response.getEntity());
                    System.err.println("Response Entity: " + responseEntity);

 

                    throw new RuntimeException(
                            "An error occurred when invoking Decision Service at: "
                                    + endpointURI
                                    + "\n"
                                    + response.getStatusLine() + ": " + responseEntity);
                } else {
                    String result = EntityUtils.toString(response.getEntity());
                    System.out.println("Result: " + result);
                    return result;
                }
            } finally {
                if (response != null) {
                    response.close();
                }
            }
        } finally {
            client.close();
        }
    }
   

 

public static void main(String[] args) throws ClientProtocolException, IOException {
        
        // Replace <vhostname> with the name of the host of the cloud portal
        String endpointURI = "https://odmdemo1.bpm.ibmcloud.com/odm/dev/DecisionService/rest/v1/AnupamsStuffDemoCOnfig/AnupamStuffPriceService";

 

        
        //odm On Prem
        //String endpointURI ="http://inmbzp7147.in.dst.ibm.com:9090/DecisionService/rest/v1/StuffPriceDevConfig/StuffPrizeService" ;
        
        String payload = "{\"theOrder\": {" +
                "\"stuffType\": \"Meat Lovers\"," +
                "\"stuffSize\": \"Large\"," +
                "\"customerState\": \"Texas\"}" +
                "}" ;
All i want to run this piece of java code from activity designer 

I need a straight cut standalone java program from where i can run it from activity designer .Is it possible ?If yes then please inform me with proper code .no more documentation please .              
  Hope you understand .

Outcomes