Hello,
I’m trying to write some Java script that uses Bonita engine API to interact with a Bonita docker container. The docker-compose file and the Java code are as follows.
docker-compose.yml: (followed instructions on https://hub.docker.com/_/bonita)
# Use tech_user/secret as user/password credentials
version: "3"
services:
# Database
db:
image: postgres:9.3
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=example
command:
- -c
- max_connections=300
- -c
- max_prepared_transactions=300
ports:
- 5050:5432
# Bonita
bonita:
image: bonita:7.8.3
ports:
- 8080:8080
environment:
- POSTGRES_ENV_POSTGRES_PASSWORD=example
- DB_VENDOR=postgres
- DB_HOST=db
- TENANT_LOGIN=tech_user
- TENANT_PASSWORD=secret
- PLATFORM_LOGIN=pfadmin
- PLATFORM_PASSWORD=pfsecret
- HTTP_API=true
- REST_API_DYN_AUTH_CHECKS=false
depends_on:
- db
entrypoint:
- bash
- -c
- |
set -e
echo 'Waiting for Postgres to be available'
export PGPASSWORD="$$POSTGRES_ENV_POSTGRES_PASSWORD"
maxTries=10
while [ "$$maxTries" -gt 0 ] && ! psql -h "$$DB_HOST" -U 'postgres' -c '\l'; do
let maxTries--
sleep 1
done
echo
if [ "$$maxTries" -le 0 ]; then
echo >&2 'error: unable to contact Postgres after 10 tries'
exit 1
fi
exec /opt/files/startup.sh
Java code: (followed instructions on https://documentation.bonitasoft.com/bonita/7.8/configure-client-of-bonita-bpm-engine)
package com.mycompany.app;
import java.util.HashMap;
import java.util.Map;
import org.bonitasoft.engine.api.ApiAccessType;
import org.bonitasoft.engine.api.LoginAPI;
import org.bonitasoft.engine.api.TenantAPIAccessor;
import org.bonitasoft.engine.api.TenantAdministrationAPI;
import org.bonitasoft.engine.exception.BonitaHomeNotSetException;
import org.bonitasoft.engine.exception.ServerAPIException;
import org.bonitasoft.engine.exception.UnknownAPITypeException;
import org.bonitasoft.engine.platform.LoginException;
import org.bonitasoft.engine.session.APISession;
import org.bonitasoft.engine.util.APITypeManager;
public class App {
public static void main(String[] args) {
final String host = "http://localhost:8080";
// final String host = "http://localhost:1760";
final String username = "tech_user";
// final String username = "install";
final String password = "secret";
// final String password = "install";
Map<String, String> settings = new HashMap<String, String>();
settings.put("server.url", host);
settings.put("application.name", "bonita");
APITypeManager.setAPITypeAndParams(ApiAccessType.HTTP, settings);
// get the LoginAPI using the TenantAPIAccessor
try {
LoginAPI loginAPI = TenantAPIAccessor.getLoginAPI();
// log in to the tenant to create a session
APISession apiSession = loginAPI.login(username, password);
System.out.println(apiSession.getUserName());
System.out.println(apiSession.isTechnicalUser());
TenantAdministrationAPI tenantApi = TenantAPIAccessor.getTenantAdministrationAPI(apiSession);
// System.out.println(tenantApi.isPaused() ? "Paused" : "Running");
// tenantApi.pause();
// System.out.println(tenantApi.isPaused() ? "Paused" : "Running");
// tenantApi.resume();
// System.out.println(tenantApi.isPaused() ? "Paused" : "Running");
} catch (LoginException e) {
e.printStackTrace();
} catch (BonitaHomeNotSetException e) {
e.printStackTrace();
} catch (ServerAPIException e) {
e.printStackTrace();
} catch (UnknownAPITypeException e) {
e.printStackTrace();
}
}
}
PROBLEM
The script works for the Bonita Studio’s embedded server (hosted at localhost:1760). Logging in as “install” user, pausing and unpausing BPM service work successfully (commented out code).
However, when I change the connection to Bonita docker service (hosted at localhost:8080), the following error is shown while the script tries to login:
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy0.login(Unknown Source)
at com.mycompany.app.App.main(App.java:42)
App.java:42
Caused by: java.io.IOException: Error while executing POST request (http code: 404) <POST http://localhost:8080/bonita/serverAPI/org.bonitasoft.engine.api.LoginAPI/login HTTP/1.1>
at org.bonitasoft.engine.api.HTTPServerAPI.invokeMethod(HTTPServerAPI.java:128)
HTTPServerAPI.java:128
at org.bonitasoft.engine.api.impl.ClientInterceptor.invoke(ClientInterceptor.java:86)
ClientInterceptor.java:86
at com.sun.proxy.$Proxy0.login(Unknown Source)
at com.mycompany.app.App.main(App.java:42)
App.java:42
at < ========== Beginning of the server stack trace ========== >. ( )
at org.bonitasoft.engine.api.HTTPServerAPI.executeHttpPost(HTTPServerAPI.java:160)
HTTPServerAPI.java:160
at org.bonitasoft.engine.api.HTTPServerAPI.invokeMethod(HTTPServerAPI.java:123)
HTTPServerAPI.java:123
at org.bonitasoft.engine.api.impl.ClientInterceptor.invoke(ClientInterceptor.java:86)
ClientInterceptor.java:86
... 2 more
Caused by: org.apache.http.client.HttpResponseException:
at org.apache.http.impl.client.AbstractResponseHandler.handleResponse(AbstractResponseHandler.java:69)
AbstractResponseHandler.java:69
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:65)
BasicResponseHandler.java:65
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:51)
BasicResponseHandler.java:51
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:222)
CloseableHttpClient.java:222
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164)
CloseableHttpClient.java:164
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:139)
CloseableHttpClient.java:139
at org.bonitasoft.engine.api.HTTPServerAPI.executeHttpPost(HTTPServerAPI.java:152)
HTTPServerAPI.java:152
... 4 more
QUESTION
HOW TO MAKE THE LOGIN API CALL IN ABOVE JAVA CODE TO RUN SUCCESSFULLY WITH THE DOCKER CONTAINER? (the one at localhost:8080) AM I MISSING SOME CONFIGURATION?
APISession apiSession = loginAPI.login(username, password); // Failing
SUSPICION
I suspect that Bonita docker image did not configure the class path correctly, so that the loginAPI implementation was not found.
Anyway, can some professional lend a helping hand?