Friday, January 24, 2025

Create case

 import javax.net.ssl.*;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.ProtocolException;
import java.net.MalformedURLException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;


public class Main {
    static String createCase() throws IOException, NoSuchAlgorithmException, KeyManagementException {
        String caseId=null;

        String messageContent = "";
        BufferedReader br = new BufferedReader(new FileReader(new File("CreateCase.json")));
        String line=null;
        while((line=br.readLine())!= null){
            messageContent += line;
        }
        // Printing the message
        System.out.println(messageContent);
        // URL of the API or Server
        String url = "<<URL>>";
        URL urlObj = new URL(url);

        TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        }
        };

        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };

        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

        HttpsURLConnection postCon = (HttpsURLConnection) urlObj.openConnection();
        //postCon.setSSLSocketFactory(SSLCertificateSocketFactory.getInsecure(0, null));
        //postCon.setHostnameVerifier(getHostnameVerifier());
        postCon.setRequestMethod("POST");

        // Setting the message content type as JSON
        postCon.setRequestProperty("Content-Type", "application/json");
        postCon.setDoOutput(true);
        // for writing the message content to the server
        OutputStream osObj = postCon.getOutputStream();
        osObj.write(messageContent.getBytes());
        // closing the output stream
        osObj.flush();
        osObj.close();
        int respCode = postCon.getResponseCode();
        System.out.println("Response from the server is: \n");
        System.out.println("The POST Request Response Code :  " + respCode);
        System.out.println("The POST Request Response Message : " + postCon.getResponseMessage());
        if (respCode == HttpURLConnection.HTTP_CREATED)
        {
        // reaching here means the connection has been established
        // By default, InputStream is attached to a keyboard.
        // Therefore, we have to direct the InputStream explicitly
        // towards the response of the server
            InputStreamReader irObj = new InputStreamReader(postCon.getInputStream());
            br = new BufferedReader(irObj);
            String input = null;
            StringBuffer sb = new StringBuffer();
            while ((input = br .readLine()) != null)
            {
                sb.append(input);
            }
            br.close();
            postCon.disconnect();
            // printing the response
            String strResponse=sb.toString();
            caseId=strResponse.substring(strResponse.indexOf(" ")+1,strResponse.indexOf("\","));
        }
        else
        {
            // connection was not successful
            System.out.println("POST Request did not work.");
        }
        return caseId;
    }

    private static HostnameVerifier getHostnameVerifier() {
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                HostnameVerifier hv =
                        HttpsURLConnection.getDefaultHostnameVerifier();
                return hv.verify("com.example.com", session);
            }
        };
        return hostnameVerifier;
    }

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, KeyManagementException {
        String caseId = createCase();
        System.out.println("CaseId="+caseId);
    }

}







################CreateCase.json################
{
  "caseTypeID": "ING-WB-PAM-Work-BillableAgreement",
  "content": {
    "Country":"CzechRepublic",
    "AccountDetails":{
      "AccountBIC":"INGBCZPPXXX",
      "AccountName":"NABUURS S.R.O.",
      "AccountNumber":"",
      "AccountStartDate":"20170424",
      "AccountStatus":"O",
      "AccountType":"CACC",
      "BankCode":"3500",
      "Currency":"CZK",
      "StartDate":"20170424T000000.000 GMT",
      "SubAccountType":""
    },
    "AccountIdentifiers":{
      "BBAN":"CZ1535000000001000427519",
      "Currency":"CZK",
      "IBAN":"CZ1535000000001000427519"
    },
    "GridDetails":{
      "GRIDID":"479527870",
      "IBSSRNr":"",
      "pyID":"47952787"
    }
  }
}