Skip to content

Commit

Permalink
feat(jans-core): added Discovery.java script and sample external service
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriyz committed May 25, 2022
1 parent 91614e4 commit 440f2dd
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.jans.service.custom.script.test.java;

import io.jans.model.custom.script.CustomScriptType;
import io.jans.model.custom.script.conf.CustomScriptConfiguration;
import io.jans.model.custom.script.type.discovery.DiscoveryType;
import io.jans.service.custom.script.ExternalScriptService;
import org.json.JSONObject;

/**
* @author Yuriy Zabrovarnyy
*/
public class SampleDiscoveryExternalScriptService extends ExternalScriptService {

public SampleDiscoveryExternalScriptService() {
super(CustomScriptType.DISCOVERY);
}

public boolean modifyDiscovery(JSONObject jsonObject, Object context) {
final CustomScriptConfiguration script = getDefaultExternalCustomScript();
if (script == null) {
log.trace("No discovery java script set.");
return false;
}

try {
log.trace("Executing java 'modifyDiscovery' method, script name: {}, jsonWebResponse: {}, context: {}", script.getName(), jsonObject, context);

DiscoveryType discoveryType = (DiscoveryType) script.getExternalType();
final boolean result = discoveryType.modifyResponse(jsonObject, context);
log.trace("Finished 'modifyDiscovery' method, script name: {}, jsonWebResponse: {}, context: {}, result: {}", script.getName(), jsonObject, context, result);

return result;
} catch (Exception e) {
log.error(e.getMessage(), e);
saveScriptError(script.getCustomScript(), e);
}

return false;
}
}
50 changes: 50 additions & 0 deletions jans-core/script/src/test/resources/Discovery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import io.jans.model.SimpleCustomProperty;
import io.jans.model.custom.script.model.CustomScript;
import io.jans.model.custom.script.type.discovery.DiscoveryType;
import io.jans.service.custom.script.CustomScriptManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.json.JSONObject;

import java.util.Map;

/**
* @author Yuriy Zabrovarnyy
*/
public class Discovery implements DiscoveryType {

private static final Logger log = LoggerFactory.getLogger(Discovery.class);
private static final Logger scriptLogger = LoggerFactory.getLogger(CustomScriptManager.class);

@Override
public boolean init(Map<String, SimpleCustomProperty> configurationAttributes) {
log.info("Init of Discovery Java custom script");
return true;
}

@Override
public boolean init(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) {
log.info("Init of Discovery Java custom script");
return true;
}

@Override
public boolean destroy(Map<String, SimpleCustomProperty> configurationAttributes) {
log.info("Destroy of Discovery Java custom script");
return true;
}

@Override
public int getApiVersion() {
log.info("getApiVersion Discovery Java custom script: 11");
return 11;
}

@Override
public boolean modifyResponse(Object responseAsJsonObject, Object context) {
scriptLogger.info("write to script logger");
JSONObject response = (JSONObject) responseAsJsonObject;
response.accumulate("key_from_java", "value_from_script_on_java");
return true;
}
}

0 comments on commit 440f2dd

Please sign in to comment.