Cloud 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 and then plug your Docmosis Cloud access key into the code below, then run.

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 Docmosis to get sample data suitable to populate
 * the  "WelcomeTemplate.docx" template.  
 *  
 * The response is a JSON structure and it contains a sub-structure of data matching
 * the template.
 * 
 * Copyright Docmosis 2017
 */
public class GetTemplateSampleDataExample 
{

	// End-point in the USA
	private static final String URL = "https://dws2.docmosis.com/services/rs/getSampleData";
	// End-point in the EU
	//private static final String URL = "https://eu-west.dws2.docmosis.com/services/rs/getSampleData";

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

		if (accessKey.isEmpty()) {
			System.err.println("You must set the access key");
			System.exit(1);
		}

		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 = "samples/WelcomeTemplate.docx";
			
			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.

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) - - DWS2