Tornado Get Template Sample Data using Java

This example shows how to get sample data for a Template in JSON format using Java.  It calls the Docmosis REST API to return sample data suitable to populate the "WelcomeTemplate.docx" template.

The sample code includes the instructions to get started. You will need a Free Trial to install and run Tornado.

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

/**
 * This sample code shows a call to Tornado to get sample data suitable to populate
 * the  "WelcomeTemplate.doc" template.  
 *  
 * The response is a JSON structure and it contains a sub-structure of data matching
 * the template.
 * 
 * This is applicable to Docmosis Tornado version 2.6 onwards. 
 * 
 * Copyright Docmosis 2017
 */
public class GetTemplateSampleDataExample 
{

	private static final String URL = "http://localhost:8080/api/getSampleData";

	/*
	 * Run this example
	 */
	public static void main(String[] args) throws MalformedURLException,
			IOException 
	{
		// Set your access Key if you configure it in Tornado
		String accessKey = "";

		HttpURLConnection conn = null;
		DataOutputStream os = null;
		try {
			conn = (HttpURLConnection) new URL(URL).openConnection();
			System.out.println("Connecting [directly] to " + URL);

			// set connection parameters
			conn.setRequestMethod("POST");
			conn.setUseCaches(false);
			conn.setDoOutput(true);
			conn.setDoInput(true);

			// this example uses JSON format
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

			conn.connect();
			System.out.println("Connected");

			// build request
			final String templateName = "WelcomeTemplate.doc";
			
			final String params = "accessKey=" + accessKey + "&templateName=" + templateName;
			System.out.println("Sending request:" + params);
			final byte[] postData = params.getBytes( Charset.forName("UTF-8") );
			
			// send the parameters
			os = new DataOutputStream(conn.getOutputStream());
			os.write(postData);
			os.flush();
			
			// get the response
			int status = conn.getResponseCode();
			if (status == 200) {
				// successful call, extract the response
				
				BufferedReader br = new BufferedReader(
						new InputStreamReader(conn.getInputStream()));
				StringBuffer sb = new StringBuffer();
				String msg;
				while ((msg = br.readLine()) != null) {
					sb.append(msg);
				}
				System.out.println("response:" + sb.toString());
				
				// add code here to process the response as JSON and 
				// in particular the "templateSampleData" field.
				
				br = null;
			} else {
				// something went wrong - tell the user
				System.err.println("Our call failed: status = " + status);
				System.err.println("message:" + conn.getResponseMessage());
				BufferedReader errorReader = new BufferedReader(
						new InputStreamReader(conn.getErrorStream()));
				String msg;
				while ((msg = errorReader.readLine()) != null) {
					System.err.println(msg);
				}
				errorReader = null;
			}
			
		} catch (ConnectException e) {
			// can't make the connection
			System.err.println("Unable to connect to Docmosis:" + e.getMessage());
			System.exit(2);
		} finally {
			if (os != null) {
				os.close();
			}
			if (conn != null) {
				conn.disconnect();
			}
		}

	}
}

Feedback

Invalid Input

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam. Please also consider contacting support@docmosis.com if you need help with this article.

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Invalid Input

Applies To

Docmosis-Java Tornado Cloud
Version(s) - ALL -