Active IQ Unified Manager Discussions

Calling WFA REST api workflow using HTTP POST method from Java

dhrumins
3,780 Views

Hey

I am trying to execute/preview a workflow from the OnCOmmand Java code.

Following is what I have:

public static void executeWfaApi(URL url) throws IOException, SAXException, ParserConfigurationException {

        URL url = new URL("http://dhrumins-pc.hq.netapp.com/rest/workflows/a89ad6cf-c812-4236-9806-4dd332f47cd0/preview");

        String userPassword = WFA_USERNAME + ":" + WFA_PASSWORD;

        String encoding = new String(Base64.encode(userPassword.getBytes()));

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestProperty("Authorization", "Basic " + encoding);

        /*

         *  Add WFA workflow Inputs.

         */

        StringBuffer workflowInput = new StringBuffer();

        workflowInput.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");

        workflowInput.append("<workflowInput>");

        workflowInput.append("<userInputValues>");

        workflowInput.append("<userInputEntry key=\"ClusterIp\" value=\"10.226.161.198\"/>");

        workflowInput.append("<userInputEntry key=\"VserverName\" value=\"dhrumins_dst_vs\"/>");

        workflowInput.append("<userInputEntry key=\"VolumeName\" value=\"dhrumins_service1_dhrumins_src_vs_dhrumins_src_vol1_vaultNodeC\"/>");

        workflowInput.append("</userInputValues>");

        workflowInput.append("<comments>API Example Execution</comments>");

        workflowInput.append("</workflowInput>");

        connection.setRequestMethod("POST");

        connection.setRequestProperty("Content-Type", "application/xml; charset=UTF-8");

        connection.setRequestProperty("Content-Length", Integer.toString(workflowInput.length()));

        connection.setUseCaches(false);

        connection.setDoInput(true);

        connection.setDoOutput(true);

        //Send request

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());

        wr.writeChars(workflowInput.toString());

        wr.flush();

        wr.close();

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        String str;

        while ((str = reader.readLine()) != null) {

            System.out.println(str);

        }

        reader.close();

    }

But when I execute this code, I get the HTTP 400 error:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: http://dhrumins-pc.hq.netapp.com/rest/workflows/a89ad6cf-c812-4236-9806-4dd332f47cd0/preview

    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1313)

    at com.netapp.dfm.protect.wfa.WfaDataAccessService.executeWfaApi(WfaDataAccessService.java:119)

    at com.netapp.dfm.protect.wfa.WfaDataAccessService.main(WfaDataAccessService.java:41)

So has anyone tried to call a WFA workflow using HTTP POST in java? I am suspecting that my method of creating and passing the workflow inputs might not be correct.

Thanks

Dhrumin

1 REPLY 1

paragp
3,780 Views

Dhrumin, I find using the Apache Http client easier than HttpURLConnection directly. The following code snippet works for me

          

HttpClient client = new DefaultHttpClient();

            HttpPost request = new HttpPost("http://" + server + "/rest/workflows/" + workflow.getUuid() + "/jobs");

            String encodedUserNamePassword = Base64.encodeToString((userName + ":" + password).getBytes(), Base64.NO_WRAP);

            request.addHeader("Authorization", "Basic " + encodedUserNamePassword);

            request.addHeader("Content-type", "application/xml");

            try {           

                request.setEntity(new StringEntity(new WorkflowInput(workflow.getUserInputs()).toXML()));

            } catch (UnsupportedEncodingException e) {

                e.printStackTrace();

            }

            HttpResponse httpResponse;

            InputStream inputStream = null;

            try {

                httpResponse = client.execute(request);

                int responseCode = httpResponse.getStatusLine().getStatusCode();

                String message = httpResponse.getStatusLine().getReasonPhrase();               

                HttpEntity entity = httpResponse.getEntity();

                if (entity != null) {

                    inputStream = entity.getContent();

                    String readFromStream = StreamUtils.readAll(inputStream);                   

                    return String.valueOf(responseCode);

                }

            } catch (IOException e) {

                e.printStackTrace();

            } finally {

                try {

                    inputStream.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

This is the toXML method

    public String toXML() {

        StringBuilder sb = new StringBuilder();

        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

        sb.append("<workflowInput>");

        sb.append("<userInputValues>");

        for (UserInput userInput : userInputAndValues) {

            sb.append("<userInputEntry ");

            sb.append("key=\"").append(userInput.getName()).append("\"").append(" ");

            if ("Query".equals(userInput.getType()) || "Enum".equals(userInput.getType())) {

                sb.append("value=\"").append(((EditText)userInput.getView()).getText()).append("\"");

            } else if ("Boolean".equals(userInput.getType())) {

                sb.append("value=\"").append(((CheckBox) userInput.getView()).isChecked() == true ? "true" : "false").append("\"");

            } else {

                sb.append("value=\"").append(((EditText)userInput.getView()).getText()).append("\"");

            }

            sb.append("/>");

        }

        sb.append("</userInputValues>");

        if (executionDateTime != null && !executionDateTime.isEmpty()) {

            sb.append("<executionDateAndTime>").append(executionDateTime).append("</executionDateAndTime>");

        }

        if (comment != null && !comment.isEmpty()) {

            sb.append("<comments>").append(comment).append("</comments>");

        }

        sb.append("</workflowInput>");

        return sb.toString();

       }

Public