Search in sources :

Example 11 with JobSecurityException

use of javax.batch.operations.JobSecurityException in project dataverse by IQSS.

the class ImportFromFileSystemCommand method execute.

@Override
public JsonObject execute(CommandContext ctxt) throws CommandException {
    JsonObjectBuilder bld = jsonObjectBuilder();
    /**
     * batch import as-individual-datafiles is disabled in this iteration;
     * only the import-as-a-package is allowed. -- L.A. Feb 2 2017
     */
    String fileMode = FileRecordWriter.FILE_MODE_PACKAGE_FILE;
    try {
        /**
         * Current constraints: 1. only supports merge and replace mode 2.
         * valid dataset 3. valid dataset directory 4. valid user & user has
         * edit dataset permission 5. only one dataset version 6. dataset
         * version is draft
         */
        if (!mode.equalsIgnoreCase("MERGE") && !mode.equalsIgnoreCase("REPLACE")) {
            String error = "Import mode: " + mode + " is not currently supported.";
            logger.info(error);
            throw new IllegalCommandException(error, this);
        }
        if (!fileMode.equals(FileRecordWriter.FILE_MODE_INDIVIDUAL_FILES) && !fileMode.equals(FileRecordWriter.FILE_MODE_PACKAGE_FILE)) {
            String error = "File import mode: " + fileMode + " is not supported.";
            logger.info(error);
            throw new IllegalCommandException(error, this);
        }
        File directory = new File(System.getProperty("dataverse.files.directory") + File.separator + dataset.getAuthority() + File.separator + dataset.getIdentifier());
        if (!isValidDirectory(directory)) {
            String error = "Dataset directory is invalid. " + directory;
            logger.info(error);
            throw new IllegalCommandException(error, this);
        }
        if (Strings.isNullOrEmpty(uploadFolder)) {
            String error = "No uploadFolder specified";
            logger.info(error);
            throw new IllegalCommandException(error, this);
        }
        File uploadDirectory = new File(System.getProperty("dataverse.files.directory") + File.separator + dataset.getAuthority() + File.separator + dataset.getIdentifier() + File.separator + uploadFolder);
        if (!isValidDirectory(uploadDirectory)) {
            String error = "Upload folder is not a valid directory.";
            logger.info(error);
            throw new IllegalCommandException(error, this);
        }
        if (dataset.getVersions().size() != 1) {
            String error = "Error creating FilesystemImportJob with dataset with ID: " + dataset.getId() + " - Dataset has more than one version.";
            logger.info(error);
            throw new IllegalCommandException(error, this);
        }
        if (dataset.getLatestVersion().getVersionState() != DatasetVersion.VersionState.DRAFT) {
            String error = "Error creating FilesystemImportJob with dataset with ID: " + dataset.getId() + " - Dataset isn't in DRAFT mode.";
            logger.info(error);
            throw new IllegalCommandException(error, this);
        }
        try {
            long jid;
            Properties props = new Properties();
            props.setProperty("datasetId", dataset.getId().toString());
            props.setProperty("userId", getUser().getIdentifier().replace("@", ""));
            props.setProperty("mode", mode);
            props.setProperty("fileMode", fileMode);
            props.setProperty("uploadFolder", uploadFolder);
            if (totalSize != null && totalSize > 0) {
                props.setProperty("totalSize", totalSize.toString());
            }
            JobOperator jo = BatchRuntime.getJobOperator();
            jid = jo.start("FileSystemImportJob", props);
            if (jid > 0) {
                bld.add("executionId", jid).add("message", "FileSystemImportJob in progress");
                return bld.build();
            } else {
                String error = "Error creating FilesystemImportJob with dataset with ID: " + dataset.getId();
                logger.info(error);
                throw new CommandException(error, this);
            }
        } catch (JobStartException | JobSecurityException ex) {
            String error = "Error creating FilesystemImportJob with dataset with ID: " + dataset.getId() + " - " + ex.getMessage();
            logger.info(error);
            throw new IllegalCommandException(error, this);
        }
    } catch (Exception e) {
        bld.add("message", "Import Exception - " + e.getMessage());
        return bld.build();
    }
}
Also used : JobSecurityException(javax.batch.operations.JobSecurityException) JobStartException(javax.batch.operations.JobStartException) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) JobOperator(javax.batch.operations.JobOperator) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) JsonObjectBuilder(javax.json.JsonObjectBuilder) Properties(java.util.Properties) File(java.io.File) JobStartException(javax.batch.operations.JobStartException) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) JobSecurityException(javax.batch.operations.JobSecurityException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException)

Example 12 with JobSecurityException

use of javax.batch.operations.JobSecurityException in project wildfly by wildfly.

the class BatchSubsystemSecurityTestCase method testAbandon_NotAllowed.

/**
 * Abandoning an execution by a user who doesn't have the permission to do it.
 */
@Test
public void testAbandon_NotAllowed() throws Exception {
    final SecurityIdentity user1 = getSecurityIdentity("user1", "password1");
    final SecurityIdentity user2 = getSecurityIdentity("user2", "password2");
    final Long id = user1.runAs((Callable<Long>) () -> operator.start("assert-identity", new Properties()));
    waitForJobEnd(id, 10);
    try {
        user2.runAs(() -> operator.abandon(id));
        Assert.fail("user2 should not be allowed to abandon job executions");
    } catch (JobSecurityException e) {
    // OK
    }
    Assert.assertEquals(operator.getJobExecution(id).getBatchStatus(), BatchStatus.COMPLETED);
}
Also used : SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) JobSecurityException(javax.batch.operations.JobSecurityException) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

JobSecurityException (javax.batch.operations.JobSecurityException)12 Test (org.junit.Test)10 SecurityIdentity (org.wildfly.security.auth.server.SecurityIdentity)10 Properties (java.util.Properties)9 JobOperator (javax.batch.operations.JobOperator)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)1 JobExecutionEntity (edu.harvard.iq.dataverse.batch.entities.JobExecutionEntity)1 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)1 IllegalCommandException (edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException)1 File (java.io.File)1 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 JobStartException (javax.batch.operations.JobStartException)1 NoSuchJobExecutionException (javax.batch.operations.NoSuchJobExecutionException)1 JobExecution (javax.batch.runtime.JobExecution)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1