Open FX Forward Contract
We will first import the "java.io", "java.util", "java.net" and "org.json" packages into the solution.
Next, we create a public class "OpenFXForwardContract".
Inside the public class, we will create a public static void, and declare all the input and output variables.
Then we create a try clause, and create both the header and content object.
Then, we will connect to the API serviceName, build the request parameters, send the request, and get the response object.
Then we will parse the Content, ServiceResponse, and ServiceRespHeader, and store the "GlobalErrorID", "ErrorText" and "ErrorDetails" in "globalErrorID", "errorText" and "errorDetails" respectively.
If the globalErrorID is '010041', it means that the OTP provided has expired. We will then display the errorText and errorDetails as well as the message 'OTP has expired, new OTP will be sent'.
If it is not '010000', we will display the ErrorText and the ErrorDetails.
Else, it means that there is no error, and we will display the result.
Entire Code:
// OpenFXForwardContract.java
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class OpenFXForwardContract {
public static void main(String args[])
{
boolean verbose = true;
// api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
//response parameters
String serviceRespTag = "FX_Forward_Contract_ReadResponse";
String globalErrorID;
String errorText;
String errorDetails;
String openDate = "2019-08-01";
String maturityDate = "2020-02-01";
String period = "6M";
String amount = "6000";
String openDate2 = "2020-02-01";
String maturityDate2 = "2020-05-01";
String period2 = "3M";
String amount2 = "3000";
String openDate3 = "2020-05-01";
String maturityDate3 = "2020-06-01";
String period3 = "1M";
String amount3 = "1000";
String openDate4 = "2020-06-01";
String maturityDate4 = "2020-07-01";
String period4 = "1M";
String amount4 = "1000";
String referenceNumber = "testing";
String baseCurrencyAccountID = "468";
String quoteCurrencyAccountID = "5458";
String baseCurrency = "USD";
String quoteCurrency = "GBP";
String serviceName = "openFXForwardContract";
String userID = "alan";
String PIN = "987654";
String OTP = "987654";
String FX_ForwardID;
try {
// build header
JSONObject jo = new JSONObject();
jo.put("serviceName", "openFXForwardContract");
jo.put("userID", userID);
jo.put("PIN", PIN);
jo.put("OTP", OTP);
JSONObject headerObj = new JSONObject();
headerObj.put("Header", jo);
String header = headerObj.toString();
JSONArray ja = new JSONArray();
// this example has 3 segments: 1st for 3M 3000, 2nd for 1M 1000, and 3rd for 1M 1000, for a total of 5M 5000
// FXForwardSegment 1 (manditory)
jo = new JSONObject();
jo.put("openDate", openDate);
jo.put("maturityDate", maturityDate);
jo.put("period", period);
jo.put("amount", amount);
ja.put(jo);
// FXForwardSegment 2 (optional)
jo = new JSONObject();
jo.put("openDate", openDate2);
jo.put("maturityDate", maturityDate2);
jo.put("period", period2);
jo.put("amount", amount2);
ja.put(jo);
// FXForwardSegment 3 (optional)
jo = new JSONObject();
jo.put("openDate", openDate3);
jo.put("maturityDate", maturityDate3);
jo.put("period", period3);
jo.put("amount", amount3);
ja.put(jo);
// FXForwardSegment 4 (optional, 4 segments max)
jo = new JSONObject();
jo.put("openDate", openDate4);
jo.put("maturityDate", maturityDate4);
jo.put("period", period4);
jo.put("amount", amount4);
ja.put(jo);
// build content
jo = new JSONObject();
jo.put("referenceNumber", referenceNumber);
jo.put("baseCurrencyAccountID", baseCurrencyAccountID);
jo.put("quoteCurrencyAccountID", quoteCurrencyAccountID);
jo.put("baseCurrency", baseCurrency);
jo.put("quoteCurrency", quoteCurrency);
jo.put("FXForwardSegment", ja);
JSONObject contentObj = new JSONObject();
contentObj.put("Content", jo);
String content = contentObj.toString();
// connect to API service
HttpURLConnection urlConnection = (HttpURLConnection) new URL(apiServiceUrl).openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
// build request parameters
String parameters
= "Header="+header+"&"
+ "Content="+content;
// send request
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream()));
br.write(parameters);
br.close();
// get response
String response = "";
Scanner s = new Scanner(urlConnection.getInputStream());
while (s.hasNextLine()){
response += s.nextLine();
}
s.close();
// get response object
boolean apiError;
JSONObject responseObj = new JSONObject(response);
responseObj = new JSONObject(response);
if (verbose){
System.out.println(parameters);
System.out.println(responseObj.toString(4)); // indent 4 spaces
System.out.println();
}
// parse {"Content"}
contentObj = responseObj.getJSONObject("Content");
// parse {"ServiceResponse"}
JSONObject serviceRespObj = contentObj.getJSONObject("ServiceResponse");
// parse {"ServiceRespHeader"}
JSONObject serviceRespHeaderObj = serviceRespObj.getJSONObject("ServiceRespHeader");
globalErrorID = serviceRespHeaderObj.getString("GlobalErrorID");
errorText = serviceRespHeaderObj.getString("ErrorText");
errorDetails = serviceRespHeaderObj.getString("ErrorDetails");
if(globalErrorID.equals("010041"))
{
System.out.println(errorText);
System.out.println(errorDetails);
System.out.println("OTP expired, new OTP will be sent");
}
else if(!globalErrorID.equals("010000"))
{
System.out.println(errorText);
System.out.println(errorDetails);
}
else
{
JSONObject fxObj = serviceRespObj.getJSONObject("FXForwardContract");
FX_ForwardID = fxObj.getString("FX_ForwardID");
System.out.println("You have successfully opened a FX Forward Contract. The FX Forward ID is " +FX_ForwardID);
return;
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Single source CHM, PDF, DOC and HTML Help creation