Cloud (DWS3) Render URL-Encoded Form using Java

This example shows how to use Docmosis to render a url-encoded form using Java.  It calls the Docmosis REST API to merge the data with the template and stream the result back.

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.File;
import java.io.FileOutputStream;
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.net.URLEncoder;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * This sample code shows how to use Docmosis to render a url-encoded form.
 * 
 * Copyright Docmosis 2019
 */
public class JavaRenderURLEncoded 
{

	// Set your region here.
	// End-point in the USA
	private static final String DWS_RENDER_URL = "https://us.dws3.docmosis.com/api/renderForm";
	// End-point in the EU
	//private static final String DWS_RENDER_URL = "https://eu.dws3.docmosis.com/api/renderForm";
	// End-point in AUS
	//private static final String DWS_RENDER_URL = "https://au.dws3.docmosis.com/api/renderForm";
	
	// Set your access key here. This is visible in your cloud account in the Docmosis Web Site.
	// It is your key to accessing your service - keep it private and safe.
	private static final String ACCESS_KEY = "XXX";
	
	
	
	/*
	 * Run this example
	 */
	public static void main(String[] args) throws MalformedURLException,
			IOException 
	{
		final String outputFileName = "myWelcome.pdf";
		
		if ("XXX".equals(ACCESS_KEY)) {
			System.err.println("You must set the access key");
			System.exit(1);
		}
		
		HttpURLConnection conn = null;
		DataOutputStream os = null;
		try {
			conn = (HttpURLConnection) new URL(DWS_RENDER_URL).openConnection();
			System.out.println("Connecting [directly] to " + DWS_RENDER_URL);

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

			// use a url-encoded form.
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

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

			// build request
			final String templateName = "samples/WelcomeTemplate.docx";
			
			String title = "Hello Cloud my Java form post";
			String message1 = "This cloud experience is better than I thought.";
			String message2 = "The sun is shining.";
			String message3 = "Right, now back to work.";
			
			final String params = 
				// docmosis parameters
				"accessKey=" + URLEncoder.encode(ACCESS_KEY, "UTF-8") 
				+ "&templateName=" + URLEncoder.encode(templateName, "UTF-8") 
				+ "&outputName=" + URLEncoder.encode(outputFileName, "UTF-8")
				// data parameters
				+ "&title=" + URLEncoder.encode(title, "UTF-8")
				// we use an "indexed string" to supply an array of "messages", each with a "msg"
				+ "&messages.0.msg=" + URLEncoder.encode(message1, "UTF-8")
				+ "&messages.1.msg=" + URLEncoder.encode(message2, "UTF-8")
				+ "&messages.2.msg=" + URLEncoder.encode(message3, "UTF-8")
				;
			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

				// successful render,
				// save our document to a file
				byte[] buff = new byte[4096];
				int bytesRead = 0;

				File file = new File(outputFileName);
				FileOutputStream fos = new FileOutputStream(file);
				try {
					while ((bytesRead = conn.getInputStream().read(buff, 0, buff.length)) != -1) {
						fos.write(buff, 0, bytesRead);
					}
				} finally {
					fos.close();
				}

				System.out.println("Created file:" + file.getAbsolutePath());
			} 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) - - DWS3