Skip to content

Commit

Permalink
added new authentication / client class , reafactoring, logging
Browse files Browse the repository at this point in the history
Issue #211
  • Loading branch information
rsoika committed Jul 15, 2024
1 parent 7b30d6e commit 5d37cdf
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,14 @@ public class BackupService {
@RegistryScope(scope = MetricRegistry.APPLICATION_SCOPE)
MetricRegistry metricRegistry;

DocumentClient documentClient = null;
EventLogClient eventLogClient = null;

@PostConstruct
public void init() {
// init timer....
if (workflowServiceEndpoint.isPresent()) {
logger.info("init BackupService endpoint: " + workflowServiceEndpoint.get().toString());
// Registering a non-persistent Timer Service.
try {
startScheduler(true);
Expand Down Expand Up @@ -166,12 +170,13 @@ public void onTimeout(jakarta.ejb.Timer _timer) {
int total = 0;
int success = 0;
int errors = 0;
long duration = System.currentTimeMillis();
try {
backupStatusHandler.setTimer(_timer);

logger.info("Processing backup events...");
// init rest clients....
DocumentClient documentClient = restClientHelper.getDocumentClient();
EventLogClient eventLogClient = restClientHelper.getEventLogClient(documentClient);
DocumentClient documentClient = restClientHelper.createDocumentClient();
EventLogClient eventLogClient = restClientHelper.createEventLogClient(documentClient);
if (documentClient == null || eventLogClient == null) {
logController.warning(BackupApi.TOPIC_BACKUP,
"Unable to connect to workflow instance endpoint - please verify configuration!");
Expand All @@ -192,43 +197,42 @@ public void onTimeout(jakarta.ejb.Timer _timer) {
eventLogClient.setPageSize(100);
List<ItemCollection> events = eventLogClient.searchEventLog(BackupApi.EVENTLOG_TOPIC_BACKUP);

for (ItemCollection eventLogEntry : events) {
total++;
topic = eventLogEntry.getItemValueString("topic");
id = eventLogEntry.getItemValueString("id");
ref = eventLogEntry.getItemValueString("ref");

try {
// first try to lock the eventLog entry....
eventLogClient.lockEventLogEntry(id);

// pull the snapshotEvent ...
logger.finest("......pull snapshot " + ref + "....");
// eventCache.add(eventLogEntry);
ItemCollection snapshot = pullSnapshot(eventLogEntry, documentClient, eventLogClient);

ftpConnector.put(snapshot);

// finally remove the event log entry...
eventLogClient.deleteEventLogEntry(id);
success++;

countMetric(METRIC_EVENTS_PROCESSED);

} catch (InvalidAccessException | EJBException | BackupException | RestAPIException e) {
// we also catch EJBExceptions here because we do not want to cancel the
// ManagedScheduledExecutorService
logController.warning(BackupApi.TOPIC_BACKUP,
"SnapshotEvent " + id + " backup failed: " + e.getMessage());
errors++;
countMetric(METRIC_EVENTS_ERRORS);

if (events != null && events.size() > 0) {
logger.info(" -> " + events.size() + " backup events found...");
for (ItemCollection eventLogEntry : events) {
total++;
topic = eventLogEntry.getItemValueString("topic");
id = eventLogEntry.getItemValueString("id");
ref = eventLogEntry.getItemValueString("ref");
try {
// first try to lock the eventLog entry....
eventLogClient.lockEventLogEntry(id);
// pull the snapshotEvent ...
logger.finest("......pull snapshot " + ref + "....");
// eventCache.add(eventLogEntry);
ItemCollection snapshot = pullSnapshot(eventLogEntry, documentClient, eventLogClient);
ftpConnector.put(snapshot);
// finally remove the event log entry...
eventLogClient.deleteEventLogEntry(id);
success++;
countMetric(METRIC_EVENTS_PROCESSED);

} catch (InvalidAccessException | EJBException | BackupException | RestAPIException e) {
// we also catch EJBExceptions here because we do not want to cancel the
// ManagedScheduledExecutorService
logController.warning(BackupApi.TOPIC_BACKUP,
"SnapshotEvent " + id + " backup failed: " + e.getMessage());
errors++;
countMetric(METRIC_EVENTS_ERRORS);

}
}
}
// print log
logController.info(BackupApi.TOPIC_BACKUP, success + " snapshots backed up in "
+ (System.currentTimeMillis() - duration) + " ms - " + errors + " errors...");

// print log
if (total > 0) {
logController.info(BackupApi.TOPIC_BACKUP, success + " snapshots backed up, " + errors + " errors...");
} else {
logger.info(" -> no backup events found.");
}

backupStatusHandler.setStatus(BackupStatusHandler.STATUS_SCHEDULED);
Expand All @@ -237,7 +241,7 @@ public void onTimeout(jakarta.ejb.Timer _timer) {
// In case of a exception during processing the event log
// the timer service will automatically restarted. This is important
// to resolve restarts of the workflow engine.
logController.warning(BackupApi.TOPIC_BACKUP, "processsing EventLog failed: " + e.getMessage());
logController.warning(BackupApi.TOPIC_BACKUP, "processing EventLog failed: " + e.getMessage());
try {
restartScheduler();
} catch (BackupException e1) {
Expand All @@ -252,7 +256,6 @@ public void onTimeout(jakarta.ejb.Timer _timer) {
* @param name
*/
private void countMetric(String name) {

try {
Metadata metadata = Metadata.builder().withName(name)
.withDescription("Imixs-Backup Service - processed backup events").build();
Expand Down Expand Up @@ -291,22 +294,24 @@ public void releaseDeadLocks(EventLogClient eventLogClient) throws RestAPIExcept
*/
public ItemCollection pullSnapshot(ItemCollection eventLogEntry, DocumentClient documentClient,
EventLogClient eventLogClient) throws BackupException {
String ref = null;
String id = null;
try {
if (eventLogEntry == null || documentClient == null || eventLogClient == null) {
// no client object
logger.fine("...no eventLogClient available!");
return null;
}

if (eventLogEntry == null || documentClient == null || eventLogClient == null) {
// no client object
logger.fine("...no eventLogClient available!");
return null;
}
ref = eventLogEntry.getItemValueString("ref");
id = eventLogEntry.getItemValueString("id");
logger.fine("......pullSnapshot ref " + ref + "...");
// lookup the snapshot...
ItemCollection snapshot;

String ref = eventLogEntry.getItemValueString("ref");
String id = eventLogEntry.getItemValueString("id");
logger.finest("......pullSnapshot ref " + ref + "...");
// lookup the snapshot...
ItemCollection snapshot;
try {
snapshot = documentClient.getDocument(ref);
if (snapshot != null) {
logger.finest("...write snapshot into backup store...");
logger.fine("...write snapshot into backup store...");
return snapshot;
}
} catch (RestAPIException e) {
Expand All @@ -316,8 +321,13 @@ public ItemCollection pullSnapshot(ItemCollection eventLogEntry, DocumentClient
try {
eventLogClient.deleteEventLogEntry(id);
} catch (RestAPIException e1) {
throw new BackupException("REMOTE_EXCEPTION", "Unable to delte eventLogEntry: " + id, e1);
throw new BackupException("REMOTE_EXCEPTION", "Unable to delete eventLogEntry: " + id, e1);
}

} catch (RuntimeException e) {
// can occur in case of a 404
logController.warning(BackupApi.TOPIC_BACKUP, "Failed to pull Snapshot " + ref + " -> " + e.getMessage());
throw new BackupException("REMOTE_EXCEPTION", "Failed to pull Snapshot: " + e.getMessage(), e);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void onTimeout(jakarta.ejb.Timer _timer) throws BackupException {
restoreStatusHandler.setTimer(_timer);

// init rest clients....
DocumentClient documentClient = restClientHelper.getDocumentClient();
DocumentClient documentClient = restClientHelper.createDocumentClient();

if (documentClient == null) {
logController.warning(BackupApi.TOPIC_RESTORE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.backup.BackupApi;
Expand All @@ -15,7 +16,7 @@
import org.imixs.melman.RestAPIException;
import org.imixs.melman.WorkflowClient;

import jakarta.enterprise.context.RequestScoped;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.ws.rs.client.ClientRequestFilter;
Expand All @@ -29,9 +30,11 @@
*
*/
@Named
@RequestScoped
@ApplicationScoped
public class RestClientHelper implements Serializable {

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

private static final long serialVersionUID = 1L;

@Inject
Expand All @@ -50,15 +53,24 @@ public class RestClientHelper implements Serializable {
@ConfigProperty(name = BackupApi.WORKFLOW_SERVICE_AUTHMETHOD)
Optional<String> instanceAuthmethod;

DocumentClient documentClient = null;
EventLogClient eventLogClient = null;

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

DocumentClient documentClient = null;
// 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());
Expand All @@ -71,6 +83,9 @@ public DocumentClient getDocumentClient() throws RestAPIException {
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(""));
Expand All @@ -79,7 +94,11 @@ public DocumentClient getDocumentClient() throws RestAPIException {

}
if ("COOKIE".equals(auttype)) {
Cookie cookie = new Cookie(instanceUser.orElse(""), instancePassword.orElse(""));
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);
}
Expand All @@ -96,10 +115,17 @@ public DocumentClient getDocumentClient() throws RestAPIException {
/**
* Creates a EventLogClient form a given DocumentClient instance
*
* @param workflowClient - a existing worklfowClient
* If an instance already exists, we return the existing instance.
*
* @param documentClient - a existing documentClient
* @return - a eventLogClient instance
*/
public EventLogClient getEventLogClient(DocumentClient documentClient) {
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
Expand Down

0 comments on commit 5d37cdf

Please sign in to comment.