Get FX Forward Contracts
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "GetFXForwardContracts".
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 the header 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 a 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 use the "instanceof" operator to check whether the "forwardcontracts" object is an JSONArray or JSONObject, and display the results accordingly.
Entire Code:
// GetFXForwardContracts.java
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class GetFXForwardContracts {
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_Contracts_ReadResponse";
String globalErrorID;
String errorText;
String errorDetails;
String serviceName = "getFXForwardContracts";
String userID = "alan";
String PIN = "987654";
String OTP = "987654";
String fxForwardID;
String amount;
String baseCurrency;
String baseCurrencyAccountID;
String baseCurrencyInterestRate;
String customerID;
String forwardRate;
String maturityDate;
String openDate;
String period;
String quoteCurrency;
String quoteCurrencyAccountID;
String quoteCurrencyInterestRate;
String referenceNumber;
String spotRate;
String status;
try {
// build header
JSONObject jo = new JSONObject();
jo.put("serviceName", serviceName);
jo.put("userID", userID);
jo.put("PIN", PIN);
jo.put("OTP", OTP);
JSONObject headerObj = new JSONObject();
headerObj.put("Header", jo);
String header = headerObj.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;
// 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();
responseObj = new JSONObject(response);
if (verbose)
{
//System.out.println(parameters);
System.out.println(responseObj.toString(4));
System.out.println();
}
//parse {"Content"}
JSONObject contentObj = new JSONObject();
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 forwardcontractsObj = serviceRespObj.getJSONObject("FXForwardContractList");
Object testObj = forwardcontractsObj.get("FXForwardContract");
if (testObj instanceof JSONArray)
{
JSONArray fxObj = forwardcontractsObj.getJSONArray("FXForwardContract");
for (int i = 0; i < fxObj.length(); i++)
{
JSONObject fxobj = fxObj.getJSONObject(i);
fxForwardID = fxobj.getString("FX_ForwardID");
amount = fxobj.getString("amount");
baseCurrency = fxobj.getString("baseCurrency");
baseCurrencyAccountID = fxobj.getString("baseCurrencyAccountID");
baseCurrencyInterestRate = fxobj.getString("baseCurrencyInterestRate");
customerID = fxobj.getString("customerID");
forwardRate = fxobj.getString("forward_rate");
maturityDate = fxobj.getString("maturity_date");
openDate = fxobj.getString("open_date");
period = fxobj.getString("period");
quoteCurrency = fxobj.getString("quoteCurrency");
quoteCurrencyAccountID = fxobj.getString("quoteCurrencyAccountID");
quoteCurrencyInterestRate = fxobj.getString("quoteCurrencyInterestRate");
referenceNumber = fxobj.getString("referenceNumber");
spotRate = fxobj.getString("spot_rate");
status = fxobj.getString("status");
System.out.println("FX Forward ID = " +fxForwardID);
System.out.println("Amount = " +amount);
System.out.println("Base Currency = " +baseCurrency);
System.out.println("Base Currency Account ID = " +baseCurrencyAccountID);
System.out.println("Base Currency Interest Rate = " +baseCurrencyInterestRate);
System.out.println("Customer ID = " +customerID);
System.out.println("Forward Rate = " +forwardRate);
System.out.println("Maturity Date = " +maturityDate);
System.out.println("Open Date = " +openDate);
System.out.println("Period = " +period);
System.out.println("Quote Currency = " +quoteCurrency);
System.out.println("Quote Currency Account ID = " +quoteCurrencyAccountID);
System.out.println("Quote Currency Interest Rate = " +quoteCurrencyInterestRate);
System.out.println("Reference Number = " +referenceNumber);
System.out.println("Spot Rate = " +spotRate);
System.out.println("Status = " +status);
}
}
else if(testObj instanceof JSONObject)
{
//parse product
JSONObject fxObj = forwardcontractsObj.getJSONObject("FXForwardContract");
fxForwardID = fxObj.getString("FX_ForwardID");
amount = fxObj.getString("amount");
baseCurrency = fxObj.getString("baseCurrency");
baseCurrencyAccountID = fxObj.getString("baseCurrencyAccountID");
baseCurrencyInterestRate = fxObj.getString("baseCurrencyInterestRate");
customerID = fxObj.getString("customerID");
forwardRate = fxObj.getString("forward_rate");
maturityDate = fxObj.getString("maturity_date");
openDate = fxObj.getString("open_date");
period = fxObj.getString("period");
quoteCurrency = fxObj.getString("quoteCurrency");
quoteCurrencyAccountID = fxObj.getString("quoteCurrencyAccountID");
quoteCurrencyInterestRate = fxObj.getString("quoteCurrencyInterestRate");
referenceNumber = fxObj.getString("referenceNumber");
spotRate = fxObj.getString("spot_rate");
status = fxObj.getString("status");
System.out.println("FX Forward ID = " +fxForwardID);
System.out.println("Amount = " +amount);
System.out.println("Base Currency = " +baseCurrency);
System.out.println("Base Currency Account ID = " +baseCurrencyAccountID);
System.out.println("Base Currency Interest Rate = " +baseCurrencyInterestRate);
System.out.println("Customer ID = " +customerID);
System.out.println("Forward Rate = " +forwardRate);
System.out.println("Maturity Date = " +maturityDate);
System.out.println("Open Date = " +openDate);
System.out.println("Period = " +period);
System.out.println("Quote Currency = " +quoteCurrency);
System.out.println("Quote Currency Account ID = " +quoteCurrencyAccountID);
System.out.println("Quote Currency Interest Rate = " +quoteCurrencyInterestRate);
System.out.println("Reference Number = " +referenceNumber);
System.out.println("Spot Rate = " +spotRate);
System.out.println("Status = " +status);
}
else
{
System.out.println("Customer has no FX Forward Contracts.");
}
return;
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Free Web Help generator