Skip to main content
Version: ADONIS 15/ADOIT 16/ADOGRC 12

Retrieve a process model's model image

This article shows how to use the ADOXX API to retrieve a model image.

The Java snippet below will fetch the image of a model whose ID we retrieved (e.g. via a search call). Based on the passed Accept header, the model data is either retrieved as JSON, or - in our case - as png image. The image is then written to a file on our hard disk.

note

The Java snippet below uses Basic Authentication.

Replace the values in angle brackets with values that fit your setup, e.g. instead of <HOST> use the host at which you access your ADONIS instance.

Click to view the code!

Source Code

public class GetModelImage
{
public static void main (final String [] args) throws ClientProtocolException, IOException
{
final CloseableHttpClient aClient = HttpClients.createDefault ();

final String sUser = "<USER>";
final String sPass = "<PASSWORD>";
final String sRepoID = "<REPO_ID>";
final String sModelID = "<MODEL_ID>";
final String sFilePath = "<FILE_PATH>"; // E.g. D:\\model.png
String sPath = "http://<HOST>:<PORT>/ADONIS/rest/2.0/repos/" +
sRepoID +
"/models/" +
sModelID +
"?";

final List <Entry <String, String>> aSearchParameters = new ArrayList <Map.Entry <String, String>> ();
aSearchParameters.add (new AbstractMap.SimpleEntry <> ("dpi", "150"));

// Iterate through the search parameters and construct the URL
for (final Entry <String, String> aParam : aSearchParameters)
{
final String sParamName = aParam.getKey ();
final String sParamValue = aParam.getValue ();
sPath += sParamName + "=" + URLEncoder.encode (sParamValue, "UTF-8") + "&";
}
final HttpGet aMethod = new HttpGet (sPath);

// The headers controlling the return type and the language do not have to be considered for
// token generation
aMethod.addHeader ("Accept", "image/png");
aMethod.addHeader ("Accept-Language", "en");

// Construction of the header to pass the basic authentication information
aMethod.addHeader ("Authorization",
"Basic " +
DatatypeConverter.printBase64Binary ((sUser + ":" + sPass).getBytes ()));

final CloseableHttpResponse aResponse = aClient.execute (aMethod);


final InputStream aInput = aResponse.getEntity ().getContent ();
try
{
final StatusLine aStatusLine = aResponse.getStatusLine ();
System.out.println ("Status Code: " + aStatusLine.getStatusCode ());

final byte [] aBytes = IOUtils.toByteArray (aInput);
final FileOutputStream aFos = new FileOutputStream (new File(sFilePath));
try
{
aFos.write (aBytes);

System.out.println ("Image written to '"+sFilePath+"'.");
}
finally
{
aFos.flush ();
aFos.close ();
}
}

finally
{
aInput.close ();
}
}
}

The result should look similar to the following:

Status Code: 200
Image written to '<FILE_PATH>'.