Skip to content

Commit

Permalink
added new RestClientHelper class, refactoring
Browse files Browse the repository at this point in the history
Issue #210
  • Loading branch information
rsoika committed Jul 15, 2024
1 parent 5d37cdf commit e7166f5
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import java.util.logging.Logger;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.imixs.melman.BasicAuthenticator;
import org.imixs.archive.service.util.RestClientHelper;
import org.imixs.melman.DocumentClient;
import org.imixs.melman.EventLogClient;
import org.imixs.melman.FormAuthenticator;
import org.imixs.melman.RestAPIException;

import jakarta.annotation.PostConstruct;
Expand All @@ -22,7 +21,6 @@
import jakarta.ejb.TimerConfig;
import jakarta.inject.Inject;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.client.ClientRequestFilter;

/**
* The SyncScheduler starts a TimerService to pull new snapshot events from the
Expand Down Expand Up @@ -70,6 +68,9 @@ public class SyncScheduler {
@Resource
jakarta.ejb.TimerService timerService;

@Inject
RestClientHelper restClientHelper;

private static Logger logger = Logger.getLogger(SyncScheduler.class.getName());

/**
Expand Down Expand Up @@ -105,58 +106,69 @@ public void init() {
public void run(Timer timer) {
DocumentClient documentClient = null;
EventLogClient eventLogClient = null;
ClientRequestFilter authenticator = null;
// ClientRequestFilter authenticator = null;

logger.fine("--- run timeout.... timerinfo= " + timer.getInfo());
logger.fine("--- run timeout.... timerInfo= " + timer.getInfo());
try {
// Test the authentication method and create a corresponding Authenticator
if ("Form".equalsIgnoreCase(workflowServiceAuthMethod.get())) {
// test if a JSESSIONID exists?
String jSessionID = (String) timer.getInfo();
if (jSessionID == null || jSessionID.isEmpty()) {
logger.info("Missing or invalid jSessionID - need new login....");
// no - we need to login first and store the JSESSIONID in a new timer object...
// create a FormAuthenticator
FormAuthenticator formAuth = new FormAuthenticator(workflowServiceEndpoint.get(),
workflowServiceUser.get(), workflowServicePassword.get());
// Authentication successful - do we have a JSESSIONID?
String jsessionID = formAuth.getJsessionID();
if (jsessionID != null && !jsessionID.isEmpty()) {
logger.fine("--- reinitialze timer with new jSessionID : " + jsessionID + " and interval="
+ interval);
// yes - terminate existing timer and create a new one with the JSESSIONID

timer.cancel();
final TimerConfig timerConfig = new TimerConfig();
timerConfig.setInfo(jsessionID);
timerConfig.setPersistent(false);
timerService.createIntervalTimer(interval, interval, timerConfig);
logger.fine("---created new timer");
logger.info(
"successful reconnected: " + workflowServiceEndpoint.get() + " , new Timer created...");
return;
}
} else {
logger.fine("--- reuse jSessionID " + jSessionID + " for login....");
// we have already a jsessionCooke Data object - so create a new
// FormAuthenticator form the JSESSIONID
FormAuthenticator formAuth = new FormAuthenticator(workflowServiceEndpoint.get(), jSessionID);
authenticator = formAuth;
}
} else {
// Default behaviro - use a BasicAuthenticator
BasicAuthenticator basicAuth = new BasicAuthenticator(workflowServiceUser.get(),
workflowServicePassword.get());
authenticator = basicAuth;
}

// init rest clients....
documentClient = restClientHelper.createDocumentClient();
eventLogClient = restClientHelper.createEventLogClient(documentClient);

// // Test the authentication method and create a corresponding Authenticator
// if ("Form".equalsIgnoreCase(workflowServiceAuthMethod.get())) {
// // test if a JSESSIONID exists?
// String jSessionID = (String) timer.getInfo();
// if (jSessionID == null || jSessionID.isEmpty()) {
// logger.info("Missing or invalid jSessionID - need new login....");
// // no - we need to login first and store the JSESSIONID in a new timer
// object...
// // create a FormAuthenticator
// FormAuthenticator formAuth = new
// FormAuthenticator(workflowServiceEndpoint.get(),
// workflowServiceUser.get(), workflowServicePassword.get());
// // Authentication successful - do we have a JSESSIONID?
// String jsessionID = formAuth.getJsessionID();
// if (jsessionID != null && !jsessionID.isEmpty()) {
// logger.fine("--- reinitialze timer with new jSessionID : " + jsessionID + "
// and interval="
// + interval);
// // yes - terminate existing timer and create a new one with the JSESSIONID

// timer.cancel();
// final TimerConfig timerConfig = new TimerConfig();
// timerConfig.setInfo(jsessionID);
// timerConfig.setPersistent(false);
// timerService.createIntervalTimer(interval, interval, timerConfig);
// logger.fine("---created new timer");
// logger.info(
// "successful reconnected: " + workflowServiceEndpoint.get() + " , new Timer
// created...");
// return;
// }
// } else {
// logger.fine("--- reuse jSessionID " + jSessionID + " for login....");
// // we have already a jsessionCooke Data object - so create a new
// // FormAuthenticator form the JSESSIONID
// FormAuthenticator formAuth = new
// FormAuthenticator(workflowServiceEndpoint.get(), jSessionID);
// authenticator = formAuth;
// }
// } else {
// // Default behaviro - use a BasicAuthenticator
// BasicAuthenticator basicAuth = new
// BasicAuthenticator(workflowServiceUser.get(),
// workflowServicePassword.get());
// authenticator = basicAuth;
// }

// do we have a valid authentication?
if (authenticator != null) {
if (documentClient != null) {
// yes - create the client objects
documentClient = new DocumentClient(workflowServiceEndpoint.get());
documentClient.registerClientRequestFilter(authenticator);
eventLogClient = new EventLogClient(workflowServiceEndpoint.get());
eventLogClient.registerClientRequestFilter(authenticator);
// documentClient = new DocumentClient(workflowServiceEndpoint.get());
// documentClient.registerClientRequestFilter(authenticator);
// eventLogClient = new EventLogClient(workflowServiceEndpoint.get());
// eventLogClient.registerClientRequestFilter(authenticator);
logger.fine("--- process event log....");
// release dead locks...
archiveSyncService.releaseDeadLocks(eventLogClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public class SyncService {

@Inject
DataService dataService;

private static Logger logger = Logger.getLogger(SyncService.class.getName());

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package org.imixs.archive.service.util;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.imixs.archive.service.ImixsArchiveApp;
import org.imixs.melman.BasicAuthenticator;
import org.imixs.melman.CookieAuthenticator;
import org.imixs.melman.DocumentClient;
import org.imixs.melman.EventLogClient;
import org.imixs.melman.FormAuthenticator;
import org.imixs.melman.JWTAuthenticator;
import org.imixs.melman.RestAPIException;
import org.imixs.melman.WorkflowClient;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.ws.rs.client.ClientRequestFilter;
import jakarta.ws.rs.core.Cookie;

/**
* The RestClientHelper provides helper method to create a DocumentRestClient
* and a EventLogRestClient.
*
* @author rsoika
*
*/
@Named
@ApplicationScoped
public class RestClientHelper implements Serializable {

private static Logger logger = Logger.getLogger(RestClientHelper.class.getName());

private static final long serialVersionUID = 1L;

@Inject
@ConfigProperty(name = ImixsArchiveApp.WORKFLOW_SERVICE_ENDPOINT)
Optional<String> instanceEndpoint;

@Inject
@ConfigProperty(name = ImixsArchiveApp.WORKFLOW_SERVICE_USER)
Optional<String> instanceUser;

@Inject
@ConfigProperty(name = ImixsArchiveApp.WORKFLOW_SERVICE_PASSWORD)
Optional<String> instancePassword;

@Inject
@ConfigProperty(name = ImixsArchiveApp.WORKFLOW_SERVICE_AUTHMETHOD)
Optional<String> instanceAuthmethod;

DocumentClient documentClient = null;
EventLogClient eventLogClient = null;

/**
* This method creates a new DocumentClient instance.
*
* If an instance already exists, we return the existing instance.
*
* @return
* @throws RestAPIException
*/
public DocumentClient createDocumentClient() throws RestAPIException {

// test if we have already an instance
if (documentClient != null) {
return documentClient;
}
logger.info("RestClientHelper create DocumentClient....");
if (instanceEndpoint.isPresent()) {

documentClient = new WorkflowClient(instanceEndpoint.get());
String auttype = instanceAuthmethod.orElse("BASIC").toUpperCase();
if ("BASIC".equals(auttype)) {
// Create a authenticator
BasicAuthenticator basicAuth = new BasicAuthenticator(instanceUser.orElse(""),
instancePassword.orElse(""));
// register the authenticator
documentClient.registerClientRequestFilter(basicAuth);
}
if ("FORM".equals(auttype)) {

logger.info("RestClientHelper create FormAuthenticator.... instance endpoint="
+ instanceEndpoint.orElse(""));
// Create a authenticator
FormAuthenticator formAuth = new FormAuthenticator(instanceEndpoint.orElse(""), instanceUser.orElse(""),
instancePassword.orElse(""));
// register the authenticator
documentClient.registerClientRequestFilter(formAuth);

}
if ("COOKIE".equals(auttype)) {
Cookie cookie = new Cookie.Builder(instanceUser.orElse("")).path("/").value(instancePassword.orElse(""))
.build();

// Cookie cookie = new Cookie(instanceUser.orElse(""),
// instancePassword.orElse(""), "/", "");
CookieAuthenticator cookieAuth = new CookieAuthenticator(cookie);
documentClient.registerClientRequestFilter(cookieAuth);
}
if ("JWT".equalsIgnoreCase(instancePassword.orElse(""))) {
JWTAuthenticator jwtAuht = new JWTAuthenticator(instancePassword.orElse(""));
documentClient.registerClientRequestFilter(jwtAuht);
}
}

return documentClient;

}

/**
* Creates a EventLogClient form a given DocumentClient instance
*
* If an instance already exists, we return the existing instance.
*
* @param documentClient - a existing documentClient
* @return - a eventLogClient instance
*/
public EventLogClient createEventLogClient(DocumentClient documentClient) {

// test if we have already an instance
if (eventLogClient != null) {
return eventLogClient;
}
if (documentClient != null) {
EventLogClient client = new EventLogClient(documentClient.getBaseURI());
// register all filters from workfow client
List<ClientRequestFilter> filterList = documentClient.getRequestFilterList();
for (ClientRequestFilter filter : filterList) {
client.registerClientRequestFilter(filter);
}
return client;
} else {
// no existing workflow client define!
return null;
}
}

}
8 changes: 5 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.imixs.workflow</groupId>
<artifactId>imixs-archive</artifactId>
Expand Down Expand Up @@ -83,8 +85,8 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<jakarta.version>10.0.0</jakarta.version>
<org.imixs.workflow.version>6.0.4</org.imixs.workflow.version>
<org.imixs.melman.version>2.0.1</org.imixs.melman.version>
<org.imixs.workflow.version>6.0.7-SNAPSHOT</org.imixs.workflow.version>
<org.imixs.melman.version>2.0.2-SNAPSHOT</org.imixs.melman.version>
<org.imixs.mock.version>6.0.0</org.imixs.mock.version>
<apache.pdfbox.version>2.0.21</apache.pdfbox.version>
<microprofile.version>6.0</microprofile.version>
Expand Down

0 comments on commit e7166f5

Please sign in to comment.