ie.aib.nbp.zosconnect.ZosRestServicePerformanceRunner.java Source code

Java tutorial

Introduction

Here is the source code for ie.aib.nbp.zosconnect.ZosRestServicePerformanceRunner.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ie.aib.nbp.zosconnect;

import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONObject;

/**
 *
 * @author 58128
 */
public class ZosRestServicePerformanceRunner {

    private final ZosConnector connector;

    public ZosRestServicePerformanceRunner(String host, int port, PrintStream out) {
        this.connector = new ZosConnector();
        this.connector.authenticate(host, port, out);
    }

    public long executeServices(String URI, String username, String password, String payload, PrintStream out,
            boolean attachResponse, boolean formatResponse) {
        long elapsedTime = 0L;
        //SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss z");
        //out.print("Start DateTime: "+sdf.format(new Date())+"   response: ");
        out.print("Call succeeded, response: ");
        if (formatResponse)
            out.println();
        long startTime = System.currentTimeMillis();
        if (this.connector.isReadyToGo()) {
            String response = this.connector.makePostRequest(URI, username, password, payload);
            try {
                JSONObject result = new JSONObject(response);
                if (attachResponse) {
                    String output = formatResponse ? result.toString(4) : result.toString();
                    out.println(output);
                }

                //calculate execution time
                elapsedTime = System.currentTimeMillis() - startTime;
                //out.print("End DateTime: "+sdf.format(new Date())+"     ");                
                out.println("service call completed in " + elapsedTime + " miliseconds.");

            } catch (Exception ex) {
                out.println("execution failed or hit the timeout !!!");
            }
        }
        return elapsedTime;
    }
}