Search in sources :

Example 1 with WorkspaceItem

use of org.dspace.content.WorkspaceItem in project DSpace by DSpace.

the class WorkflowItemRestRepositoryIT method whenWorkspaceitemBecomeWorkflowitemWithAccessConditionsTheBitstremMustBeDownloableTest.

@Test
public void whenWorkspaceitemBecomeWorkflowitemWithAccessConditionsTheBitstremMustBeDownloableTest() throws Exception {
    context.turnOffAuthorisationSystem();
    EPerson submitter = EPersonBuilder.createEPerson(context).withEmail("submitter@example.com").withPassword(password).build();
    EPerson reviewer1 = EPersonBuilder.createEPerson(context).withEmail("reviewer1@example.com").withPassword(password).build();
    parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
    Collection collection1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").withWorkflowGroup(1, reviewer1).build();
    Bitstream bitstream = null;
    WorkspaceItem witem = null;
    String bitstreamContent = "0123456789";
    AtomicReference<Integer> idRef = new AtomicReference<Integer>();
    try (InputStream is = IOUtils.toInputStream(bitstreamContent, Charset.defaultCharset())) {
        context.setCurrentUser(submitter);
        witem = WorkspaceItemBuilder.createWorkspaceItem(context, collection1).withTitle("Test WorkspaceItem").withIssueDate("2019-10-01").build();
        bitstream = BitstreamBuilder.createBitstream(context, witem.getItem(), is).withName("Test bitstream").withDescription("This is a bitstream to test range requests").withMimeType("text/plain").build();
        context.restoreAuthSystemState();
        String tokenEPerson = getAuthToken(eperson.getEmail(), password);
        String tokenSubmitter = getAuthToken(submitter.getEmail(), password);
        String tokenReviewer1 = getAuthToken(reviewer1.getEmail(), password);
        // submitter can download the bitstream
        getClient(tokenSubmitter).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")).andExpect(status().isOk()).andExpect(header().string("Accept-Ranges", "bytes")).andExpect(header().string("ETag", "\"" + bitstream.getChecksum() + "\"")).andExpect(content().contentType("text/plain")).andExpect(content().bytes(bitstreamContent.getBytes()));
        // reviewer can't still download the bitstream
        getClient(tokenReviewer1).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")).andExpect(status().isForbidden());
        // others can't download the bitstream
        getClient(tokenEPerson).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")).andExpect(status().isForbidden());
        // create a list of values to use in add operation
        List<Operation> addAccessCondition = new ArrayList<>();
        List<Map<String, String>> accessConditions = new ArrayList<Map<String, String>>();
        Map<String, String> value = new HashMap<>();
        value.put("name", "administrator");
        accessConditions.add(value);
        addAccessCondition.add(new AddOperation("/sections/upload/files/0/accessConditions", accessConditions));
        String patchBody = getPatchContent(addAccessCondition);
        getClient(tokenSubmitter).perform(patch("/api/submission/workspaceitems/" + witem.getID()).content(patchBody).contentType(MediaType.APPLICATION_JSON_PATCH_JSON)).andExpect(status().isOk()).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].name", is("administrator"))).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].startDate", nullValue())).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].endDate", nullValue()));
        // verify that the patch changes have been persisted
        getClient(tokenSubmitter).perform(get("/api/submission/workspaceitems/" + witem.getID())).andExpect(status().isOk()).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].name", is("administrator"))).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].startDate", nullValue())).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].endDate", nullValue()));
        // submit the workspaceitem to start the workflow
        getClient(tokenSubmitter).perform(post(BASE_REST_SERVER_URL + "/api/workflow/workflowitems").content("/api/submission/workspaceitems/" + witem.getID()).contentType(textUriContentType)).andExpect(status().isCreated()).andDo(result -> idRef.set(read(result.getResponse().getContentAsString(), "$.id")));
        // check that the workflowitem is persisted
        getClient(tokenSubmitter).perform(get("/api/workflow/workflowitems/" + idRef.get())).andExpect(status().isOk()).andExpect(jsonPath("$", Matchers.is(WorkflowItemMatcher.matchItemWithTitleAndDateIssued(null, "Test WorkspaceItem", "2019-10-01")))).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].name", is("administrator"))).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].startDate", nullValue())).andExpect(jsonPath("$.sections.upload.files[0].accessConditions[0].endDate", nullValue()));
        // reviewer can download the bitstream
        getClient(tokenReviewer1).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")).andExpect(status().isOk()).andExpect(header().string("Accept-Ranges", "bytes")).andExpect(header().string("ETag", "\"" + bitstream.getChecksum() + "\"")).andExpect(content().contentType("text/plain")).andExpect(content().bytes(bitstreamContent.getBytes()));
        // submitter can download the bitstream
        getClient(tokenSubmitter).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")).andExpect(status().isOk()).andExpect(header().string("Accept-Ranges", "bytes")).andExpect(header().string("ETag", "\"" + bitstream.getChecksum() + "\"")).andExpect(content().contentType("text/plain")).andExpect(content().bytes(bitstreamContent.getBytes()));
        // others can't download the bitstream
        getClient(tokenEPerson).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content")).andExpect(status().isForbidden());
    } finally {
        // remove the workflowitem if any
        WorkflowItemBuilder.deleteWorkflowItem(idRef.get());
    }
}
Also used : Bitstream(org.dspace.content.Bitstream) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReplaceOperation(org.dspace.app.rest.model.patch.ReplaceOperation) RemoveOperation(org.dspace.app.rest.model.patch.RemoveOperation) AddOperation(org.dspace.app.rest.model.patch.AddOperation) Operation(org.dspace.app.rest.model.patch.Operation) AddOperation(org.dspace.app.rest.model.patch.AddOperation) EPerson(org.dspace.eperson.EPerson) Collection(org.dspace.content.Collection) Map(java.util.Map) HashMap(java.util.HashMap) WorkspaceItem(org.dspace.content.WorkspaceItem) AbstractControllerIntegrationTest(org.dspace.app.rest.test.AbstractControllerIntegrationTest) Test(org.junit.Test)

Example 2 with WorkspaceItem

use of org.dspace.content.WorkspaceItem in project DSpace by DSpace.

the class InprogressSubmissionIndexFactoryImpl method storeInprogressItemFields.

@Override
public void storeInprogressItemFields(Context context, SolrInputDocument doc, InProgressSubmission inProgressSubmission) throws SQLException, IOException {
    final Item item = inProgressSubmission.getItem();
    doc.addField("lastModified", SolrUtils.getDateFormatter().format(item.getLastModified()));
    EPerson submitter = inProgressSubmission.getSubmitter();
    if (submitter != null) {
        addFacetIndex(doc, "submitter", submitter.getID().toString(), submitter.getFullName());
    }
    doc.addField("inprogress.item", new IndexableItem(inProgressSubmission.getItem()).getUniqueIndexID());
    // get the location string (for searching by collection & community)
    List<String> locations = indexableCollectionService.getCollectionLocations(context, inProgressSubmission.getCollection());
    // add the item's owning collection to the location list
    // NOTE: inProgressSubmission.getItem().getCollections() is empty while the item is in-progress.
    locations.add("l" + inProgressSubmission.getCollection().getID());
    // Add item metadata
    List<DiscoveryConfiguration> discoveryConfigurations;
    if (inProgressSubmission instanceof WorkflowItem) {
        discoveryConfigurations = SearchUtils.getAllDiscoveryConfigurations((WorkflowItem) inProgressSubmission);
    } else if (inProgressSubmission instanceof WorkspaceItem) {
        discoveryConfigurations = SearchUtils.getAllDiscoveryConfigurations((WorkspaceItem) inProgressSubmission);
    } else {
        discoveryConfigurations = SearchUtils.getAllDiscoveryConfigurations(item);
    }
    indexableItemService.addDiscoveryFields(doc, context, item, discoveryConfigurations);
    indexableCollectionService.storeCommunityCollectionLocations(doc, locations);
}
Also used : Item(org.dspace.content.Item) WorkflowItem(org.dspace.workflow.WorkflowItem) WorkspaceItem(org.dspace.content.WorkspaceItem) WorkflowItem(org.dspace.workflow.WorkflowItem) DiscoveryConfiguration(org.dspace.discovery.configuration.DiscoveryConfiguration) EPerson(org.dspace.eperson.EPerson) WorkspaceItem(org.dspace.content.WorkspaceItem)

Example 3 with WorkspaceItem

use of org.dspace.content.WorkspaceItem in project DSpace by DSpace.

the class EPersonServiceImpl method getDeleteConstraints.

@Override
public List<String> getDeleteConstraints(Context context, EPerson ePerson) throws SQLException {
    List<String> tableList = new ArrayList<>();
    // check for eperson in item table
    Iterator<Item> itemsBySubmitter = itemService.findBySubmitter(context, ePerson, true);
    if (itemsBySubmitter.hasNext()) {
        tableList.add("item");
    }
    WorkspaceItemService workspaceItemService = ContentServiceFactory.getInstance().getWorkspaceItemService();
    List<WorkspaceItem> workspaceBySubmitter = workspaceItemService.findByEPerson(context, ePerson);
    if (workspaceBySubmitter.size() > 0) {
        tableList.add("workspaceitem");
    }
    ResourcePolicyService resourcePolicyService = AuthorizeServiceFactory.getInstance().getResourcePolicyService();
    if (resourcePolicyService.find(context, ePerson).size() > 0) {
        tableList.add("resourcepolicy");
    }
    WorkflowService workflowService = WorkflowServiceFactory.getInstance().getWorkflowService();
    List<String> workflowConstraints = workflowService.getEPersonDeleteConstraints(context, ePerson);
    tableList.addAll(workflowConstraints);
    // explaining to the user why the eperson cannot be deleted.
    return tableList;
}
Also used : WorkspaceItem(org.dspace.content.WorkspaceItem) Item(org.dspace.content.Item) WorkflowService(org.dspace.workflow.WorkflowService) XmlWorkflowService(org.dspace.xmlworkflow.service.XmlWorkflowService) ArrayList(java.util.ArrayList) WorkspaceItemService(org.dspace.content.service.WorkspaceItemService) WorkspaceItem(org.dspace.content.WorkspaceItem) ResourcePolicyService(org.dspace.authorize.service.ResourcePolicyService)

Example 4 with WorkspaceItem

use of org.dspace.content.WorkspaceItem in project DSpace by DSpace.

the class LogicalFilterTest method init.

/**
 * This method will be run before every test as per @Before. It will
 * initialize resources required for the tests.
 *
 * Other methods can be annotated with @Before here or in subclasses
 * but no execution order is guaranteed
 */
@Before
@Override
public void init() {
    super.init();
    try {
        context.turnOffAuthorisationSystem();
        // Set up logical statement lists for operator testing
        setUpStatements();
        // Set up DSpace resources for condition and filter testing
        // Set up first community, collection and item
        this.communityOne = communityService.create(null, context);
        this.collectionOne = collectionService.create(context, communityOne);
        WorkspaceItem workspaceItem = workspaceItemService.create(context, collectionOne, false);
        this.itemOne = installItemService.installItem(context, workspaceItem);
        // Add one bitstream to item one, but put it in THUMBNAIL bundle
        bundleService.addBitstream(context, bundleService.create(context, itemOne, "THUMBNAIL"), bitstreamService.create(context, new ByteArrayInputStream("Item 1 Thumbnail 1".getBytes(StandardCharsets.UTF_8))));
        // Set up second community, collection and item, and third item
        this.communityTwo = communityService.create(null, context);
        this.collectionTwo = collectionService.create(context, communityTwo);
        // Item two
        workspaceItem = workspaceItemService.create(context, collectionTwo, false);
        this.itemTwo = installItemService.installItem(context, workspaceItem);
        // Add two bitstreams to item two
        Bundle bundleTwo = bundleService.create(context, itemTwo, "ORIGINAL");
        bundleService.addBitstream(context, bundleTwo, bitstreamService.create(context, new ByteArrayInputStream("Item 2 Bitstream 1".getBytes(StandardCharsets.UTF_8))));
        bundleService.addBitstream(context, bundleTwo, bitstreamService.create(context, new ByteArrayInputStream("Item 2 Bitstream 2".getBytes(StandardCharsets.UTF_8))));
        // Item three
        workspaceItem = workspaceItemService.create(context, collectionTwo, false);
        this.itemThree = installItemService.installItem(context, workspaceItem);
        // Add three bitstreams to item three
        Bundle bundleThree = bundleService.create(context, itemThree, "ORIGINAL");
        bundleService.addBitstream(context, bundleThree, bitstreamService.create(context, new ByteArrayInputStream("Item 3 Bitstream 1".getBytes(StandardCharsets.UTF_8))));
        bundleService.addBitstream(context, bundleThree, bitstreamService.create(context, new ByteArrayInputStream("Item 3 Bitstream 2".getBytes(StandardCharsets.UTF_8))));
        bundleService.addBitstream(context, bundleThree, bitstreamService.create(context, new ByteArrayInputStream("Item 3 Bitstream 2".getBytes(StandardCharsets.UTF_8))));
        // Withdraw the second item for later testing
        itemService.withdraw(context, itemTwo);
        // Initialise metadata field for later testing with both items
        this.metadataField = metadataFieldService.findByElement(context, MetadataSchemaEnum.DC.getName(), element, qualifier);
        context.restoreAuthSystemState();
    } catch (AuthorizeException | SQLException | IOException e) {
        log.error("Error encountered during init", e);
        fail("Error encountered during init: " + e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SQLException(java.sql.SQLException) Bundle(org.dspace.content.Bundle) AuthorizeException(org.dspace.authorize.AuthorizeException) IOException(java.io.IOException) WorkspaceItem(org.dspace.content.WorkspaceItem) Before(org.junit.Before)

Example 5 with WorkspaceItem

use of org.dspace.content.WorkspaceItem in project DSpace by DSpace.

the class ITDSpaceAIP method testRestoreRestrictedItem.

/**
 * Test restoration from AIP of an access restricted Item
 */
@Test
public void testRestoreRestrictedItem() throws Exception {
    log.info("testRestoreRestrictedItem() - BEGIN");
    // Locate the test Collection (as a parent)
    Collection parent = (Collection) handleService.resolveToObject(context, testCollectionHandle);
    // Create a brand new Item to test with (since we will be changing policies)
    WorkspaceItem wsItem = workspaceItemService.create(context, parent, false);
    Item item = installItemService.installItem(context, wsItem);
    itemService.addMetadata(context, item, "dc", "title", null, null, "Test Restricted Item");
    // Create a test Bitstream in the ORIGINAL bundle
    File f = new File(testProps.get("test.bitstream").toString());
    Bitstream b = itemService.createSingleBitstream(context, new FileInputStream(f), item);
    b.setName(context, "Test Bitstream");
    bitstreamService.update(context, b);
    itemService.update(context, item);
    // Create a custom resource policy for this Item
    List<ResourcePolicy> policies = new ArrayList<>();
    ResourcePolicy admin_policy = resourcePolicyService.create(context);
    admin_policy.setRpName("Admin Read-Only");
    Group adminGroup = groupService.findByName(context, Group.ADMIN);
    admin_policy.setGroup(adminGroup);
    admin_policy.setAction(Constants.READ);
    policies.add(admin_policy);
    itemService.replaceAllItemPolicies(context, item, policies);
    // Export item AIP
    log.info("testRestoreRestrictedItem() - CREATE Item AIP");
    File aipFile = createAIP(item, null, false);
    // Get item handle, so we can check that it is later restored properly
    String itemHandle = item.getHandle();
    // Now, delete that item
    log.info("testRestoreRestrictedItem() - DELETE Item");
    collectionService.removeItem(context, parent, item);
    // Assert the deleted item no longer exists
    DSpaceObject obj = handleService.resolveToObject(context, itemHandle);
    assertThat("testRestoreRestrictedItem() item " + itemHandle + " doesn't exist", obj, nullValue());
    // Restore Item from AIP (non-recursive)
    log.info("testRestoreRestrictedItem() - RESTORE Item");
    restoreFromAIP(parent, aipFile, null, false);
    // Assert the deleted item is RESTORED
    DSpaceObject objRestored = handleService.resolveToObject(context, itemHandle);
    assertThat("testRestoreRestrictedItem() item " + itemHandle + " exists", objRestored, notNullValue());
    // Assert the number of restored policies is equal
    List<ResourcePolicy> policiesRestored = authorizeService.getPolicies(context, objRestored);
    assertEquals("testRestoreRestrictedItem() restored policy count equal", policies.size(), policiesRestored.size());
    // Assert the restored policy has same name, group and permission settings
    ResourcePolicy restoredPolicy = policiesRestored.get(0);
    assertEquals("testRestoreRestrictedItem() restored policy group successfully", admin_policy.getGroup().getName(), restoredPolicy.getGroup().getName());
    assertEquals("testRestoreRestrictedItem() restored policy action successfully", admin_policy.getAction(), restoredPolicy.getAction());
    assertEquals("testRestoreRestrictedItem() restored policy name successfully", admin_policy.getRpName(), restoredPolicy.getRpName());
    log.info("testRestoreRestrictedItem() - END");
}
Also used : WorkspaceItem(org.dspace.content.WorkspaceItem) Item(org.dspace.content.Item) Group(org.dspace.eperson.Group) Bitstream(org.dspace.content.Bitstream) DSpaceObject(org.dspace.content.DSpaceObject) ArrayList(java.util.ArrayList) Collection(org.dspace.content.Collection) ResourcePolicy(org.dspace.authorize.ResourcePolicy) File(java.io.File) FileInputStream(java.io.FileInputStream) WorkspaceItem(org.dspace.content.WorkspaceItem) AbstractIntegrationTest(org.dspace.AbstractIntegrationTest) Test(org.junit.Test)

Aggregations

WorkspaceItem (org.dspace.content.WorkspaceItem)228 Collection (org.dspace.content.Collection)172 Test (org.junit.Test)162 AbstractControllerIntegrationTest (org.dspace.app.rest.test.AbstractControllerIntegrationTest)135 Community (org.dspace.content.Community)121 ArrayList (java.util.ArrayList)88 Operation (org.dspace.app.rest.model.patch.Operation)80 AddOperation (org.dspace.app.rest.model.patch.AddOperation)78 RemoveOperation (org.dspace.app.rest.model.patch.RemoveOperation)76 ReplaceOperation (org.dspace.app.rest.model.patch.ReplaceOperation)75 Item (org.dspace.content.Item)65 HashMap (java.util.HashMap)52 InputStream (java.io.InputStream)40 XmlWorkflowItem (org.dspace.xmlworkflow.storedcomponents.XmlWorkflowItem)39 EPerson (org.dspace.eperson.EPerson)34 SQLException (java.sql.SQLException)26 Bitstream (org.dspace.content.Bitstream)23 AuthorizeException (org.dspace.authorize.AuthorizeException)22 Workflow (org.dspace.xmlworkflow.state.Workflow)20 Map (java.util.Map)19