Java for Exporting Data from Rossum API

This Java code shows how to download XML data captured in a given Rossum queue for the previous calendar day.

This Java code shows how to download XML data captured in a given queue for the previous calendar day – as part of the regular export routine. Update the parameters in the main function based on your scenario.

The export API is more powerful - you can filter by different criteria or download the data e.g. as a JSON instead - see the full docs.

import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;

public class RossumApiExport {

    private String apiPrefix;
    private String authToken;

    public RossumApiExport(String userName, String password) throws IOException {
        this(userName, password, "https://example.rossum.app/api/v1/");
    }

    public RossumApiExport(String userName, String password, String apiPrefix) throws IOException {
        this.apiPrefix = apiPrefix;
        authToken = getApiToken(userName, password);
    }

    private String getApiToken(String userName, String password) throws IOException {
        JSONObject requestJson = new JSONObject();
        requestJson.put("username", userName);
        requestJson.put("password", password);

        HttpClient client = HttpClients.custom().build();
        HttpUriRequest request = RequestBuilder.post(apiPrefix + "auth/login")
            .setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
            .setEntity(new StringEntity(requestJson.toString()))
            .build();
        HttpResponse response = client.execute(request);
        String responseStr = EntityUtils.toString(response.getEntity());

        if (response.getStatusLine().getStatusCode() == 200) {
            JSONObject responseJson = new JSONObject(responseStr);
            return responseJson.getString("key");
        } else {
            throw new RuntimeException("Unable to login. " + responseStr);
        }
    }

    private String exportDocument(int queueId, String dateStart, String dateEnd) throws IOException {
        String urlTemplate = apiPrefix + "queues/%s/export?format=xml&exported_at_after=%s&exported_at_before=%s&page_size=100&page=1";
        String url = String.format(urlTemplate, queueId, dateStart, dateEnd);
        HttpClient client = HttpClients.custom().build();
        HttpUriRequest request = RequestBuilder.get(url)
            .setHeader(HttpHeaders.CONTENT_TYPE, "application/xml")
            .setHeader("Authorization", String.format("token %s", this.authToken))
            .build();
        HttpResponse response = client.execute(request);
        return EntityUtils.toString(response.getEntity());
    }

    public static void main(String[] args) throws IOException {
        String userName = "[email protected]";
        String password = "secret.password";
        int queueId = 12345;

       LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);

        RossumApiExport instance = new RossumApiExport(userName, password);
        String content = instance.exportDocument(queueId, yesterday.toString(), today.toString());

        String exportPath = String.format("rossum_export_%s.xml", today.toString());
        BufferedWriter writer = new BufferedWriter(new FileWriter(exportPath));
        writer.write(content);
        writer.close();
    }

}

This example will export at most 100 documents. To export more documents, you need to modify the code download multiple pages one by one, until it reaches the page count (or it can simply stop when an empty set of results is finally downloaded).