Accessing APTARE Reports with the REST API > Sample Client Side Java Code for Calling the REST API
  
Version 10.3.00P13
Sample Client Side Java Code for Calling the REST API
Base Class for Testing
Class for Paginated Data Extraction from a Tabular Report
Class for Exporting Reports and Dashboards
Base Class for Testing
 
import org.apache.commons.codec.binary.Base64;
 
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
 
public class BaseRest {
 
protected final String baseUrl;
protected final String username;
protected final String password;
 
public BaseRest(String baseUrl, String username, String password) {
this.baseUrl = baseUrl;
this.username = username;
this.password = password;
}
 
 
protected URLConnection setUsernamePassword(URL url) throws IOException {
URLConnection urlConnection = url.openConnection();
String authString = username + ":" + password;
String authStringEnc = new String(Base64.encodeBase64(authString.getBytes()));
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
return urlConnection;
}
}
Class for Paginated Data Extraction from a Tabular Report
 
import com.google.gson.Gson;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
 
public class TestExtractDataFromReport extends BaseRest{
 
public TestExtractDataFromReport(String baseUrl, String username, String password) {
super(baseUrl, username, password);
}
 
String getDataFromServer(String path) {
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(baseUrl + path);
URLConnection urlConnection = setUsernamePassword(url);
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
 
return sb.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
 
 
//Testing csv
public static void main(String[] args) {
TestExtractDataFromReport testRest = new TestExtractDataFromReport("http://xyz.com","userId","******");
String path = "/rest/getReport.ajax?reportId=100063";
boolean hasMore = true;
int page = 0;
while(hasMore) {
Result result = execute(testRest, path + "&format=json&currentPage=" + page);
System.out.println(result.toString());
++page;
hasMore = result.hasMore;
}
}
 
 
private static Result execute(TestExtractDataFromReport testRest, String path) {
String result = testRest.getDataFromServer(path);
System.out.println(result);
Gson gson = new Gson();
Map map = gson.fromJson(result, Map.class);
Result result1 = new Result();
result1.reportName = (String) map.get("report_name");
Map paging = (Map) map.get("paging");
result1.hasMore = (Boolean) paging.get("hasMore");
result1.data = (List<Map>) map.get("data");
return result1;
}
 
static class Result {
String reportName;
boolean hasMore;
List<Map> data;
 
@Override
public String toString() {
return "Result{" +
"reportName='" + reportName + '\'' +
", hasMore=" + hasMore +
", data=" + data.size() +
'}';
}
}
}
Class for Exporting Reports and Dashboards
 
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
 
public class TestRestDownload extends BaseRest {
 
public TestRestDownload(String baseUrl, String username, String password) {
super(baseUrl, username, password);
}
 
void writeDownloadedFile(String path, File folder2Download) {
try {
URL url = new URL(baseUrl + path);
URLConnection urlConnection = setUsernamePassword(url);
String content = urlConnection.getHeaderField("Content-disposition");
String fileName = content.replace("attachment;filename=\"","").replaceAll("\"$","");
File output = getFileThatDoesNotExist(folder2Download, fileName);
FileOutputStream fos = new FileOutputStream(output);
ReadableByteChannel rbc = Channels.newChannel(urlConnection.getInputStream());
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
 
private File getFileThatDoesNotExist(File folder2Download, String fileName) {
File output = new File(folder2Download.getAbsolutePath() + File.separator + fileName);
int index = 0;
while(output.exists()) {
++index;
String[] parts = fileName.split("[.]");
if(parts.length == 1) {
output = new File(folder2Download.getAbsolutePath() + File.separator + fileName + index);
break;
}
String suffix = parts[parts.length - 1];
String fileNameNew = fileName.replaceAll("[.]" +suffix,"(" + index + ")" + "." + suffix);
output = new File(folder2Download.getAbsolutePath() + File.separator + fileNameNew);
}
return output;
}
//Exporting Report
public static void main1(String[] args) throws Exception {
TestRestDownload testRest = new TestRestDownload("http://xyz.com","userId","*****");
String path = "/rest/exportReport.ajax?reportId=100063&exportFormat=csv";
File file = new File("C:\\tmp");
testRest.writeDownloadedFile(path, file);
}
 
//Exporting Dashboard
public static void main(String[] args) throws Exception {
TestRestDownload testRest = new TestRestDownload("http://xyz.com","userId","*****");
String path = "/rest/exportDashboard.ajax?dashboardId=100000&exportFormat=html";
File file = new File("C:\\tmp");
testRest.writeDownloadedFile(path, file);
}
}