use of org.gluu.model.custom.script.model.CustomScript in project oxTrust by GluuFederation.
the class CustomScriptWebResourceTest method createScriptTest.
@Test
public void createScriptTest() {
String name = "MyScript";
CustomScript script = getCustomScript(name);
HttpPost request = new HttpPost(BASE_URL + ApiConstants.BASE_API_URL + ApiConstants.CONFIGURATION + ApiConstants.SCRIPTS);
try {
String json = mapper.writeValueAsString(script);
HttpEntity entity = new ByteArrayEntity(json.toString().getBytes("UTF-8"), ContentType.APPLICATION_FORM_URLENCODED);
request.setEntity(entity);
String CONTENT_TYPE = "Content-Type";
request.setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON);
HttpResponse response = handle(request);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
HttpEntity result = response.getEntity();
CustomScript myScript = mapper.readValue(EntityUtils.toString(result), CustomScript.class);
Assert.assertEquals(myScript.getName(), name);
Assert.assertEquals(myScript.getDescription(), name);
} catch (ParseException | IOException e) {
e.printStackTrace();
Assert.assertTrue(false);
}
}
use of org.gluu.model.custom.script.model.CustomScript in project oxTrust by GluuFederation.
the class CustomScriptWebResource method deleteCustomScript.
@DELETE
@Path(ApiConstants.INUM_PARAM_PATH)
@Operation(summary = "Delete custom script", description = "Delete an custom script")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Success"), @ApiResponse(responseCode = "500", description = "Server error") })
@ProtectedApi(scopes = { WRITE_ACCESS })
public Response deleteCustomScript(@PathParam(ApiConstants.INUM) @NotNull String inum) {
log(logger, "Delete custom script" + inum);
try {
Objects.requireNonNull(inum);
CustomScript existingScript = customScriptService.getScriptByInum(inum);
if (existingScript != null) {
customScriptService.remove(existingScript);
return Response.ok().build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
} catch (Exception e) {
log(logger, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
use of org.gluu.model.custom.script.model.CustomScript in project oxCore by GluuFederation.
the class CustomScriptManager method clearScriptErrorImpl.
protected void clearScriptErrorImpl(CustomScript customScript) {
// Load entry from DN
String customScriptDn = customScript.getDn();
Class<? extends CustomScript> scriptType = customScript.getScriptType().getCustomScriptModel();
CustomScript loadedCustomScript = customScriptService.getCustomScriptByDn(scriptType, customScriptDn);
// Check if there is no error
ScriptError currError = loadedCustomScript.getScriptError();
if (currError == null) {
return;
}
// Save error into script entry
loadedCustomScript.setScriptError(null);
customScriptService.update(loadedCustomScript);
}
use of org.gluu.model.custom.script.model.CustomScript in project oxCore by GluuFederation.
the class CustomScriptManager method reloadCustomScriptConfigurations.
private ReloadResult reloadCustomScriptConfigurations(Map<String, CustomScriptConfiguration> customScriptConfigurations, List<CustomScript> newCustomScripts) {
Map<String, CustomScriptConfiguration> newCustomScriptConfigurations;
boolean modified = false;
if (customScriptConfigurations == null) {
newCustomScriptConfigurations = new HashMap<String, CustomScriptConfiguration>();
modified = true;
} else {
// Clone old map to avoid reload not changed scripts because it's time and CPU
// consuming process
newCustomScriptConfigurations = new HashMap<String, CustomScriptConfiguration>(customScriptConfigurations);
}
List<String> newSupportedCustomScriptInums = new ArrayList<String>();
for (CustomScript newCustomScript : newCustomScripts) {
if (!newCustomScript.isEnabled()) {
continue;
}
if (ScriptLocationType.FILE == newCustomScript.getLocationType()) {
// Replace script revision with file modification time. This should allow to
// reload script automatically after changing location_type
long fileModifiactionTime = getFileModificationTime(newCustomScript.getLocationPath());
newCustomScript.setRevision(fileModifiactionTime);
}
String newSupportedCustomScriptInum = StringHelper.toLowerCase(newCustomScript.getInum());
newSupportedCustomScriptInums.add(newSupportedCustomScriptInum);
CustomScriptConfiguration prevCustomScriptConfiguration = newCustomScriptConfigurations.get(newSupportedCustomScriptInum);
if (prevCustomScriptConfiguration == null || prevCustomScriptConfiguration.getCustomScript().getRevision() != newCustomScript.getRevision()) {
// Destroy old version properly before creating new one
if (prevCustomScriptConfiguration != null) {
destroyCustomScript(prevCustomScriptConfiguration);
}
// Load script entry with all attributes
CustomScript loadedCustomScript = customScriptService.getCustomScriptByDn(newCustomScript.getScriptType().getCustomScriptModel(), newCustomScript.getDn());
// Prepare configuration attributes
Map<String, SimpleCustomProperty> newConfigurationAttributes = new HashMap<String, SimpleCustomProperty>();
List<SimpleExtendedCustomProperty> simpleCustomProperties = loadedCustomScript.getConfigurationProperties();
if (simpleCustomProperties == null) {
simpleCustomProperties = new ArrayList<SimpleExtendedCustomProperty>(0);
}
for (SimpleCustomProperty simpleCustomProperty : simpleCustomProperties) {
newConfigurationAttributes.put(simpleCustomProperty.getValue1(), simpleCustomProperty);
}
if (ScriptLocationType.FILE == loadedCustomScript.getLocationType()) {
// Replace script revision with file modification time. This should allow to
// reload script automatically after changing location_type
long fileModifiactionTime = getFileModificationTime(loadedCustomScript.getLocationPath());
loadedCustomScript.setRevision(fileModifiactionTime);
if (fileModifiactionTime != 0) {
String scriptFromFile = loadFromFile(loadedCustomScript.getLocationPath());
if (StringHelper.isNotEmpty(scriptFromFile)) {
loadedCustomScript.setScript(scriptFromFile);
}
}
}
// Automatic package update '.xdi' --> '.org'
// TODO: Remove in CE 5.0
String scriptCode = loadedCustomScript.getScript();
if (scriptCode != null) {
scriptCode = scriptCode.replaceAll(".xdi", ".gluu");
loadedCustomScript.setScript(scriptCode);
}
// Load script
BaseExternalType newCustomScriptExternalType = createExternalType(loadedCustomScript, newConfigurationAttributes);
CustomScriptConfiguration newCustomScriptConfiguration = new CustomScriptConfiguration(loadedCustomScript, newCustomScriptExternalType, newConfigurationAttributes);
// Store configuration and script
newCustomScriptConfigurations.put(newSupportedCustomScriptInum, newCustomScriptConfiguration);
modified = true;
}
}
// Remove old external scripts configurations
for (Iterator<Entry<String, CustomScriptConfiguration>> it = newCustomScriptConfigurations.entrySet().iterator(); it.hasNext(); ) {
Entry<String, CustomScriptConfiguration> externalAuthenticatorConfigurationEntry = it.next();
String prevSupportedCustomScriptInum = externalAuthenticatorConfigurationEntry.getKey();
if (!newSupportedCustomScriptInums.contains(prevSupportedCustomScriptInum)) {
// Destroy old authentication method
destroyCustomScript(externalAuthenticatorConfigurationEntry.getValue());
it.remove();
modified = true;
}
}
return new ReloadResult(newCustomScriptConfigurations, modified);
}
use of org.gluu.model.custom.script.model.CustomScript in project oxCore by GluuFederation.
the class CustomScriptManager method saveScriptErrorImpl.
protected void saveScriptErrorImpl(CustomScript customScript, Exception exception, boolean overwrite) {
// Load entry from DN
String customScriptDn = customScript.getDn();
Class<? extends CustomScript> scriptType = customScript.getScriptType().getCustomScriptModel();
CustomScript loadedCustomScript = customScriptService.getCustomScriptByDn(scriptType, customScriptDn);
// Check if there is error value already
ScriptError currError = loadedCustomScript.getScriptError();
if (!overwrite && (currError != null)) {
return;
}
// Save error into script entry
StringBuilder builder = new StringBuilder();
builder.append(ExceptionUtils.getStackTrace(exception));
String message = exception.getMessage();
if (message != null && !StringUtils.isEmpty(message)) {
builder.append("\n==================Further details============================\n");
builder.append(message);
}
loadedCustomScript.setScriptError(new ScriptError(new Date(), builder.toString()));
customScriptService.update(loadedCustomScript);
}
Aggregations