Skip to content

Commit

Permalink
fix remarks in #242 review about hooks (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coutadeur committed Oct 27, 2023
1 parent 2f2ecb6 commit 68cd544
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 119 deletions.
13 changes: 2 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<staging.dir>${project.build.directory}/staging</staging.dir>
<jmockit-version>1.5</jmockit-version>
<graalvm.version>22.3.1</graalvm.version>
<jackson.version>2.9.6</jackson.version>
<compiler.dir>${project.build.directory}/compiler</compiler.dir>
</properties>

Expand Down Expand Up @@ -872,20 +873,10 @@
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
<version>${jackson.version}</version>
</dependency>
</dependencies>

Expand Down
98 changes: 40 additions & 58 deletions src/main/java/org/lsc/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
import java.lang.ProcessBuilder;
import java.io.OutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.util.Optional;
import org.lsc.utils.output.LdifLayout;
import org.lsc.beans.syncoptions.ISyncOptions.OutputFormat;
import com.fasterxml.jackson.databind.ObjectMapper; // For encoding object to JSON
import com.fasterxml.jackson.databind.ObjectWriter;

Expand All @@ -58,62 +58,33 @@
*/
public class Hooks {

static final Logger LOGGER = LoggerFactory.getLogger(AbstractSynchronize.class);
static final Logger LOGGER = LoggerFactory.getLogger(Hooks.class);

private static final ObjectMapper Mapper = new ObjectMapper();
/**
* Method calling a postSyncHook if necessary
*
* return nothing
*/
public final static void postSyncHook(final String hook, final String outputFormat, final LscModifications lm) {
public final void postSyncHook(final Optional<String> hook, final OutputFormat outputFormat, final LscModifications lm) {

if( hook != null && ! hook.equals("") )
if( hook != null && hook.isPresent() )
{

String format = "";
if( outputFormat.equals("json") ) {
format = "json";
}
else
{
format = "ldif";
}

// Compute json/ldif modifications
String modifications = null;

switch (lm.getOperation()) {
case CREATE_OBJECT:
if( format.equals("json") ) {
modifications = getJsonModifications(lm);
}
else {
modifications = LdifLayout.format(lm);
}
callHook("create", hook, lm.getMainIdentifier(), format, modifications);
callHook("create", hook.get(), lm.getMainIdentifier(), outputFormat, lm);
break;

case UPDATE_OBJECT:
if( format.equals("json") ) {
modifications = getJsonModifications(lm);
}
else {
modifications = LdifLayout.format(lm);
}
callHook("update", hook, lm.getMainIdentifier(), format, modifications);
callHook("update", hook.get(), lm.getMainIdentifier(), outputFormat, lm);
break;

case CHANGE_ID:
if( format.equals("json") ) {
modifications = getJsonModifications(lm);
}
else {
modifications = LdifLayout.format(lm);
}
callHook("changeId", hook, lm.getMainIdentifier(), format, modifications);
callHook("changeId", hook.get(), lm.getMainIdentifier(), outputFormat, lm);
break;

case DELETE_OBJECT:
callHook("delete", hook, lm.getMainIdentifier(), format, modifications);
callHook("delete", hook.get(), lm.getMainIdentifier(), outputFormat, lm);
break;

default:
Expand All @@ -122,18 +93,30 @@ public final static void postSyncHook(final String hook, final String outputForm
}
}

/**
* Method calling the hook
*
* return nothing
*/
public final static void callHook( String operationType,
String hook,
String identifier,
String format,
String modifications) {
OutputFormat outputFormat,
LscModifications lm) {

LOGGER.info("Calling {} posthook {} with format {} for {}",
operationType,
hook,
outputFormat.toString(),
identifier);

String modifications = null;
// Compute modifications only in a create / update / changeid operation
if( ! operationType.equals("delete") )
{
if( outputFormat == OutputFormat.JSON ) {
modifications = getJsonModifications(lm);
}
else {
modifications = LdifLayout.format(lm);
}
}

LOGGER.info("Calling {} posthook {} with format {} for {}", operationType, hook, format, identifier);
try {
if( modifications != null ) {
Process p = new ProcessBuilder(
Expand All @@ -158,11 +141,12 @@ public final static void callHook( String operationType,
}
}
catch(IOException e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
LOGGER.error("Error while calling {} posthook {} with format {} for {}: {}",
operationType, hook, format, identifier, sw.toString());
LOGGER.error("Error while calling {} posthook {} with format {} for {}",
operationType,
hook,
outputFormat.toString(),
identifier);
LOGGER.error("posthook error: ", e);
}
}

Expand All @@ -172,16 +156,14 @@ public final static void callHook( String operationType,
* @return modifications in a json String
*/
public final static String getJsonModifications(final LscModifications lm) {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
ObjectWriter ow = Mapper.writer().withDefaultPrettyPrinter();
String json = "";
try {
json = ow.writeValueAsString(lm.getLscAttributeModifications());
}
catch(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
LOGGER.error("Error while encoding LSC modifications to json", sw.toString());
LOGGER.error("Error while encoding LSC modifications to json");
LOGGER.error("encoding error: ", e);
}
return json;
}
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/org/lsc/beans/syncoptions/ForceSyncOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import java.util.List;
import java.util.Set;
import java.util.Optional;

import org.lsc.LscModificationType;
import org.lsc.configuration.PolicyType;
Expand Down Expand Up @@ -128,28 +129,28 @@ public String getCondition(LscModificationType operation) {
return DEFAULT_CONDITION;
}

public String getPostHookOutputFormat() {
return "";
public OutputFormat getPostHookOutputFormat() {
return OutputFormat.LDIF;
}

public String getCreatePostHook() {
return "";
public Optional<String> getCreatePostHook() {
return Optional.ofNullable(null);
}

public String getDeletePostHook() {
return "";
public Optional<String> getDeletePostHook() {
return Optional.ofNullable(null);
}

public String getUpdatePostHook() {
return "";
public Optional<String> getUpdatePostHook() {
return Optional.ofNullable(null);
}

public String getChangeIdPostHook() {
return "";
public Optional<String> getChangeIdPostHook() {
return Optional.ofNullable(null);
}

public String getPostHook(LscModificationType operation) {
return "";
public Optional<String> getPostHook(LscModificationType operation) {
return Optional.ofNullable(null);
}

public String getDn() {
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/org/lsc/beans/syncoptions/ISyncOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import java.util.List;
import java.util.Set;
import java.util.Optional;

import org.lsc.LscModificationType;
import org.lsc.configuration.PolicyType;
Expand All @@ -60,6 +61,9 @@ public interface ISyncOptions {

/** default condition if none is given */
public static final String DEFAULT_CONDITION = "true";

/** list of output formats */
public enum OutputFormat { LDIF, JSON };

/**
* Initialize the synchronization options policy.
Expand Down Expand Up @@ -156,40 +160,40 @@ public interface ISyncOptions {
/**
* Returns the posthook output format
*
* @return the posthook output format or "" if none is specified (default)
* @return the posthook output format (default = OutputFormat.LDIF)
*/
String getPostHookOutputFormat();
OutputFormat getPostHookOutputFormat();

/**
* Returns the posthook for a creation
*
* @return the posthook or "" if none is specified (default)
*/
String getCreatePostHook();
Optional<String> getCreatePostHook();

/**
* Returns the posthook for an update
*
* @return the posthook or "" if none is specified (default)
*/
String getUpdatePostHook();
Optional<String> getUpdatePostHook();

/**
* Returns the posthook for a delete
*
* @return the posthook or "" if none is specified (default)
*/
String getDeletePostHook();
Optional<String> getDeletePostHook();

/**
* Returns the posthook for a id change
*
* @return the posthook or "" if none is specified (default)
*/
String getChangeIdPostHook();
Optional<String> getChangeIdPostHook();


String getPostHook(LscModificationType operation);
Optional<String> getPostHook(LscModificationType operation);

/**
* Return the expression used to infer the new object DN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Optional;

import org.lsc.LscModificationType;
import org.lsc.configuration.DatasetType;
Expand Down Expand Up @@ -197,43 +198,41 @@ public String getCondition(LscModificationType operation) {
return result;
}

public String getPostHookOutputFormat() {
public OutputFormat getPostHookOutputFormat() {
// default: returns LDIF format
if (conf.getHooks() == null || conf.getHooks().getOutputFormat() == null) {
return "";
return OutputFormat.LDIF;
}
switch(conf.getHooks().getOutputFormat()){
case "json":
return OutputFormat.JSON;
default:
return OutputFormat.LDIF;
}
return conf.getHooks().getOutputFormat();
}

public String getCreatePostHook() {
if (conf.getHooks() == null || conf.getHooks().getCreatePostHook() == null) {
return "";
}
return conf.getHooks().getCreatePostHook();
public Optional<String> getCreatePostHook() {
Optional<String> hook = Optional.ofNullable(conf.getHooks().getCreatePostHook()).filter(s -> !s.isEmpty());
return hook;
}

public String getDeletePostHook() {
if (conf.getHooks() == null || conf.getHooks().getDeletePostHook() == null) {
return "";
}
return conf.getHooks().getDeletePostHook();
public Optional<String> getDeletePostHook() {
Optional<String> hook = Optional.ofNullable(conf.getHooks().getDeletePostHook()).filter(s -> !s.isEmpty());
return hook;
}

public String getUpdatePostHook() {
if (conf.getHooks() == null || conf.getHooks().getUpdatePostHook() == null) {
return "";
}
return conf.getHooks().getUpdatePostHook();
public Optional<String> getUpdatePostHook() {
Optional<String> hook = Optional.ofNullable(conf.getHooks().getUpdatePostHook()).filter(s -> !s.isEmpty());
return hook;
}

public String getChangeIdPostHook() {
if (conf.getHooks() == null || conf.getHooks().getChangeIdPostHook() == null) {
return "";
}
return conf.getHooks().getChangeIdPostHook();
public Optional<String> getChangeIdPostHook() {
Optional<String> hook = Optional.ofNullable(conf.getHooks().getChangeIdPostHook()).filter(s -> !s.isEmpty());
return hook;
}

public String getPostHook(LscModificationType operation) {
String result = "";
public Optional<String> getPostHook(LscModificationType operation) {
Optional<String> result = Optional.ofNullable(null);
switch (operation) {
case CREATE_OBJECT:
result = this.getCreatePostHook();
Expand Down
Loading

0 comments on commit 68cd544

Please sign in to comment.