Java

Rough around the edges demo of each call that can be made to the service.


package com.services.cro.ie;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.*;
import java.net.*;

public class CroApi {
	
	public static void main(String[] args) {
		
		HttpURLConnection connection = null;	    
		OutputStreamWriter wr = null;
		BufferedReader rd  = null;	     
		StringBuilder sb = null;	     
		String line = null;

		URL serverAddress = null;

		try {

			//******************************************************************************
			//   Demonstration of a company search

			//******************************************************************************
			serverAddress = new URL("https://services.cro.ie/cws/companies?company_name=ryanair");

			//set up out communications stuff
			connection = null;

			//Set up the initial connection
			connection = (HttpURLConnection)serverAddress.openConnection();
			connection.setRequestMethod("GET");
			connection.setRequestProperty("Authorization", "Basic YourBase64EncodedEmailAndApiKeyHere");
			connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			connection.setDoOutput(true);
			connection.setReadTimeout(10000);

			connection.connect();

			//read the result from the server
			rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			sb = new StringBuilder();

			while ((line = rd.readLine()) != null)
			{
				sb.append(line + '\n');
			}	        
			System.out.println(sb.toString());

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			//close the connection, set all objects to null
			connection.disconnect();
			rd = null;
			sb = null;
			wr = null;
			connection = null;
		}

		
		try {

			//******************************************************************************
			//  Demonstration of a getting the company details if you know a company number
			//  This gives slightly more complete info than doing a company search (NARD is calculated,
			//  and other dates are returned)
			//******************************************************************************
			serverAddress = new URL("https://services.cro.ie/cws/company/22045/C");

			//Set up the initial connection
			connection = (HttpURLConnection)serverAddress.openConnection();
			connection.setRequestMethod("GET");
			connection.setRequestProperty("Authorization", "Basic YourBase64EncodedEmailAndApiKeyHere");
			connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			connection.setDoOutput(true);
			connection.setReadTimeout(10000);

			connection.connect();

			//read the result from the server
			rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			sb = new StringBuilder();

			while ((line = rd.readLine()) != null)
			{
				sb.append(line + '\n');
			}	        
			System.out.println(sb.toString());

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			//close the connection, set all objects to null
			connection.disconnect();
			rd = null;
			sb = null;
			wr = null;
			connection = null;
		}


		
		try {

			//******************************************************************************
			// Demonstration of a getting the count of submissions, and the first page 
			// of submissions for a given company.  Paging is calculated based on "skip" and max".  
			// An initial call to "submissioncount" is necessary first if you want to enable paging			
			//******************************************************************************
			
			serverAddress = new URL("https://services.cro.ie/cws/submissioncount?company_num=22045&company_bus_ind=c");
			
			connection = (HttpURLConnection)serverAddress.openConnection();
			connection.setRequestMethod("GET");
			connection.setRequestProperty("Authorization", "Basic YourBase64EncodedEmailAndApiKeyHere");
			connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			connection.setDoOutput(true);
			connection.setReadTimeout(10000);

			connection.connect();

			//read the result from the server
			rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			sb = new StringBuilder();

			while ((line = rd.readLine()) != null)
			{
				sb.append(line + '\n');
			}	        
			System.out.println("The number of submissions is " + sb.toString());
			
			
			// Work out your own paging logic based on the count returned from above.... 
			// Note that for a very large resultset, a max of 500 rows is returned... 
			serverAddress = new URL("https://services.cro.ie/cws/company/22045/C/submissions?skip=0&max=25");
			// Or: serverAddress = new URL("https://services.cro.ie/cws/submissions?company_num=22045&company_bus_ind=c");

			connection = (HttpURLConnection)serverAddress.openConnection();
			connection.setRequestMethod("GET");
			connection.setRequestProperty("Authorization", "Basic YourBase64EncodedEmailAndApiKeyHere");
			connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			connection.setDoOutput(true);
			connection.setReadTimeout(10000);

			connection.connect();

			//read the result from the server
			rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			sb = new StringBuilder();

			while ((line = rd.readLine()) != null)
			{
				sb.append(line + '\n');
			}	        
			System.out.println(sb.toString());

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			//close the connection, set all objects to null
			connection.disconnect();
			rd = null;
			sb = null;
			wr = null;
			connection = null;
		}

		
		

		try {

			//******************************************************************************
			// Demonstration of a getting the account balance.
			// Read the notes... you need to be a signed up customer of the CRO, you will need the
			// Account Number and PIN.  And the credentials must be in the header.			
			//******************************************************************************
			
			// Step 1: Get the Auth Key by running "permissionrequest":
			serverAddress = new URL("https://services.cro.ie/cws/customerapi/account/permissionrequest");

			//set up out communications stuff
			connection = null;

			//Set up the initial connection
			connection = (HttpURLConnection)serverAddress.openConnection();
			
			// ******   NB --- note the POST method.
			connection.setRequestMethod("POST");
			
			// Set your credentials as you would for the free stuff:
			connection.setRequestProperty("Authorization", "Basic YourBase64EncodedEmailAndApiKeyHere");
			connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			connection.setDoOutput(true);
			connection.setReadTimeout(10000);


			// here is the tedious way of building the request header...
			// you could use a JSONObject class
			byte[] outputBytes = "{\"customer_num\":\"1234\",\"customer_pin\":\"9D5D61\",\"requested_method\":\"balance\",\"time_to_live\":\"300\"}".getBytes("UTF-8");
			connection.setRequestProperty("Content-Length", Integer.toString(outputBytes.length));
			
			java.io.OutputStream os = connection.getOutputStream();
			os.write(outputBytes);
			os.close();
			
			connection.connect();
	
			
			//read the result from the server
			rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			sb = new StringBuilder();

			while ((line = rd.readLine()) != null)
			{
				sb.append(line + '\n');
			}	        
			
			// Now pull the string out of it:
			String authKeyForBalance = sb.toString();

			System.out.println(authKeyForBalance);
			
			
			// Now we need to build the URL for the Balance:
			// Step 2: Build the URL and get the balance using a http GET
			//  Don't forget to include the key in the querystring
			serverAddress = new URL("https://services.cro.ie/cws/customerapi/account/1234/balance?key=" + 
					authKeyForBalance.replaceAll("\"", ""));

			System.out.println(serverAddress.toString());
			
			//set up out communications stuff
			connection = null;

			//Set up the initial connection
			connection = (HttpURLConnection)serverAddress.openConnection();			
			connection.setRequestMethod("GET");
			
			connection.setRequestProperty("Authorization", "Basic YourBase64EncodedEmailAndApiKeyHere");
			connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			connection.setDoOutput(true);
			connection.setReadTimeout(10000);
			connection.connect();
	
			
			//read the result from the server
			rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			sb = new StringBuilder();

			while ((line = rd.readLine()) != null)
			{
				sb.append(line + '\n');
			}	        
					
			// double accountBalance = Double.parseDouble(sb.toString());						
			System.out.println("The balance is: " + sb.toString());				
			
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			
			
			// Try putting in some invalid credentials or values in the headers and you will get here			
			try{
				
				System.out.println(connection.getResponseCode());
				System.out.println(connection.getResponseMessage());

				
				//read the result from the server
				rd  = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
				sb = new StringBuilder();

				while ((line = rd.readLine()) != null)
				{
					sb.append(line + '\n');
				}	        				

				// The error stream details...  
				System.out.println(sb.toString());
				
			}
			catch (Exception ex)
			{
				
			}
			
			e.printStackTrace();
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		finally
		{
			//close the connection, set all objects to null
			connection.disconnect();
			rd = null;
			sb = null;
			wr = null;
			connection = null;
		}

		

		try {

			//******************************************************************************
			// Demonstration of a getting a document
			// Read the notes... you need to be a signed up customer of the CRO, you will need the
			// Account Number and PIN.  And the credentials must be in the header.			
			//******************************************************************************
			
			// Step 1: Get the Auth Key by running "permissionrequest":
			serverAddress = new URL("https://services.cro.ie/cws/customerapi/account/permissionrequest");

			//set up out communications stuff
			connection = null;

			//Set up the initial connection
			connection = (HttpURLConnection)serverAddress.openConnection();
			
			// ******   NB --- note the POST method.
			connection.setRequestMethod("POST");
			// ******
						
			// The usual header here:
			connection.setRequestProperty("Authorization", "Basic YourBase64EncodedEmailAndApiKeyHere");
			connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			connection.setDoOutput(true);
			connection.setReadTimeout(10000);

			// Create the Request Header.
			// Note that requested_method must be document and doc_id is mandatory
			
			int sampleDocId = 56146377;
			String requestBody = String.format("{\"customer_num\":\"1234\",\"customer_pin\":\"9060BB\",\"requested_method\":\"document\",\"doc_id\":\"%d\",\"time_to_live\":\"300\"}", sampleDocId);
			System.out.println(requestBody);
			byte[] outputBytes = requestBody.getBytes("UTF-8");
			connection.setRequestProperty("Content-Length", Integer.toString(outputBytes.length));
			java.io.OutputStream os = connection.getOutputStream();
			os.write(outputBytes);
			os.close();
			
			connection.connect();
	
			
			//read the result from the server
			rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			sb = new StringBuilder();

			while ((line = rd.readLine()) != null)
			{
				sb.append(line + '\n');
			}	        
			
			// Now pull the string out of it:
			String authKeyForDocument = sb.toString();

			// you will need to strip out the quotes as it is wrapped.
			System.out.println(authKeyForDocument);
			
						
			// Now we need to build the URL for the document
			// Step 2: Build the URL and get the byte stream using a http GET
			//  Don't forget to include the key in the querystring
			serverAddress = new URL(String.format("https://services.cro.ie/cws/documentapi/document/%d?key=%s", sampleDocId,  
					authKeyForDocument.replaceAll("\"", "") ) ) ;

			System.out.println(serverAddress.toString());
			
			//set up out communications stuff
			connection = null;

			//Set up the initial connection
			connection = (HttpURLConnection)serverAddress.openConnection();			
			connection.setRequestMethod("GET");
			
						
			connection.setRequestProperty("Authorization", "Basic YourBase64EncodedEmailAndApiKeyHere");
			// connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			connection.setDoOutput(true);
			connection.setReadTimeout(10000);
			connection.connect();
	

			// Since you get a URLConnection, use it to get the InputStream
			InputStream in = connection.getInputStream();
		    
			// Now that the InputStream is open, get the content length
			int contentLength = connection.getContentLength();

			// To avoid having to resize the array over and over and over as
			// bytes are written to the array, provide an accurate estimate of
			// the ultimate size of the byte array
			ByteArrayOutputStream tmpOut;
			if (contentLength != -1) {
			        tmpOut = new ByteArrayOutputStream(contentLength);
			} else {
				tmpOut = new ByteArrayOutputStream(16384); // Pick some appropriate size
			}

			byte[] buf = new byte[512];
			while (true) {
				int len = in.read(buf);
				if (len == -1) {
				 break;
				}
				tmpOut.write(buf, 0, len);
			}
			in.close();
			tmpOut.close(); // No effect, but good to do anyway to keep the metaphor alive
    
			byte[] array = tmpOut.toByteArray();
		       
		       // Don't forget to build up your own repository of documents after it is purchased the first time!
			FileOutputStream fos = new FileOutputStream(String.format("C:\\_scans\\%d.tif", sampleDocId));
			fos.write(array);
			fos.close();

		    
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			
			
			// Try putting in some invalid credentials or values in the headers and you will get here			
			try{
				
				System.out.println(connection.getResponseCode());
				System.out.println(connection.getResponseMessage());

				
				//read the result from the server
				rd  = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
				sb = new StringBuilder();

				while ((line = rd.readLine()) != null)
				{
					sb.append(line + '\n');
				}	        				

				// The error stream details...  
				System.out.println(sb.toString());
				
			}
			catch (Exception ex)
			{
				
			}
			
			e.printStackTrace();
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		finally
		{
			//close the connection, set all objects to null
			connection.disconnect();
			rd = null;
			sb = null;
			wr = null;
			connection = null;
		}		
		
	}
}