Search in sources :

Example 21 with CustomScript

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);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CustomScript(org.gluu.model.custom.script.model.CustomScript) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 22 with CustomScript

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();
    }
}
Also used : CustomScript(org.gluu.model.custom.script.model.CustomScript) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ProtectedApi(org.gluu.oxtrust.service.filter.ProtectedApi) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses)

Example 23 with CustomScript

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);
}
Also used : ScriptError(org.gluu.model.custom.script.model.ScriptError) CustomScript(org.gluu.model.custom.script.model.CustomScript)

Example 24 with CustomScript

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);
}
Also used : CustomScript(org.gluu.model.custom.script.model.CustomScript) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleExtendedCustomProperty(org.gluu.model.SimpleExtendedCustomProperty) Entry(java.util.Map.Entry) BaseExternalType(org.gluu.model.custom.script.type.BaseExternalType) SimpleCustomProperty(org.gluu.model.SimpleCustomProperty) CustomScriptConfiguration(org.gluu.model.custom.script.conf.CustomScriptConfiguration)

Example 25 with CustomScript

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);
}
Also used : ScriptError(org.gluu.model.custom.script.model.ScriptError) CustomScript(org.gluu.model.custom.script.model.CustomScript) Date(java.util.Date)

Aggregations

CustomScript (org.gluu.model.custom.script.model.CustomScript)42 ArrayList (java.util.ArrayList)20 AuthenticationCustomScript (org.gluu.model.custom.script.model.auth.AuthenticationCustomScript)11 CustomScriptType (org.gluu.model.custom.script.CustomScriptType)8 IOException (java.io.IOException)7 SimpleCustomProperty (org.gluu.model.SimpleCustomProperty)6 HttpEntity (org.apache.http.HttpEntity)5 HttpResponse (org.apache.http.HttpResponse)5 ParseException (org.apache.http.ParseException)5 SimpleExtendedCustomProperty (org.gluu.model.SimpleExtendedCustomProperty)5 Test (org.junit.Test)5 List (java.util.List)4 CustomScriptConfiguration (org.gluu.model.custom.script.conf.CustomScriptConfiguration)4 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)3 SelectableEntity (org.gluu.model.SelectableEntity)3 BasePersistenceException (org.gluu.persist.exception.BasePersistenceException)3 Filter (org.gluu.search.filter.Filter)3 Operation (io.swagger.v3.oas.annotations.Operation)2 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)2