Skip to content

Commit

Permalink
Switch DSpace deposit collection configuration from uuid to handle
Browse files Browse the repository at this point in the history
  • Loading branch information
markpatton committed Nov 21, 2024
1 parent 7914fb3 commit c70f959
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.eclipse.pass.deposit.support.dspace;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
Expand Down Expand Up @@ -65,10 +66,12 @@ public class DspaceDepositService {
@Value("${dspace.server}")
private String dspaceServer;

@Value("${dspace.collection.uuid}")
@Value("${dspace.collection.handle}")
private String dspaceCollectionHandle;

private String dspaceCollectionUuid;

private String dspaceServerScheme;
private final String dspaceServerScheme;

public record AuthContext(String xsrfToken, String authToken){}

Expand Down Expand Up @@ -245,22 +248,67 @@ private void deleteItem(String itemUuid, AuthContext authContext) {
LOG.warn("Deleted item UUID={}", itemUuid);
}

public String getUuidForHandle(String handle, AuthContext authContext) {
LOG.debug("Search Dspace for object with handle={}", handle);

String searchResponse = restClient.get()
.uri("/discover/search/objects?query=handle:{handleValue}", handle)
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", authContext.authToken())
.retrieve()
.body(String.class);

List<Map<String, ?>> searchArray = JsonPath.parse(searchResponse).read("$..indexableObject[?(@.handle)]");

if (searchArray.size() == 1) {
Map<String, ?> itemMap = searchArray.get(0);
String uuid = itemMap.get("uuid").toString();

LOG.debug("Found object UUID={} with handle={}", uuid, handle);

return uuid;
}

throw new RuntimeException("Unable to find object with handle: " + handle);
}

public String getCollectionUuid(AuthContext authContext) {
if (dspaceCollectionUuid == null) {
dspaceCollectionUuid = getUuidForHandle(dspaceCollectionHandle, authContext);
}

return dspaceCollectionUuid;
}

public String createWorkspaceItem(List<DepositFileResource> files, AuthContext authContext) {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();

files.forEach(res -> {
body.add("file", res.getResource());
System.err.println(res.getFilename());
try {
System.err.println(res.getResource().contentLength());
} catch (IOException e) {
throw new RuntimeException(e);
}
});

String uuid = getCollectionUuid(authContext);

System.err.println("Moo, creating wsi in collection " + uuid);

String json = restClient.post()
.uri("/submission/workspaceitems?owningCollection={collectionUuid}", dspaceCollectionUuid)
.uri("/submission/workspaceitems?owningCollection={collectionUuid}", uuid)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.MULTIPART_FORM_DATA)
.header("Authorization", authContext.authToken())
.header("X-XSRF-TOKEN", authContext.xsrfToken())
.header("Cookie", "DSPACE-XSRF-COOKIE=" + authContext.xsrfToken())
.body(body)
.retrieve().body(String.class);

System.err.println("result: " + json);

return json;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dspace.server=${DSPACE_SERVER}
dspace.api.url=${DSPACE_API_URL}
dspace.user=${DSPACE_USER}
dspace.password=${DSPACE_PASSWORD}
dspace.collection.uuid=${DSPACE_COLLECTION_UUID}
dspace.collection.handle=${DSPACE_COLLECTION_HANDLE}
dspace.field.embargo.lift=${DSPACE_FIELD_EMBARGO_LIFT:local.embargo.lift}
dspace.field.embargo.terms=${DSPACE_FIELD_EMBARGO_TERMS:local.embargo.terms}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
dspace.password=${DSPACE_PASSWORD:test-dspace-pw}
dspace.server=localhost:8000
dspace.api.url=http://localhost:8000/api
dspace.collection.uuid=none
dspace.collection.handle=1234/1
"""
)
@ContextConfiguration(initializers = ConfigDataApplicationContextInitializer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"inveniordm.api.baseUrl=http://localhost:9030/api",
"dspace.server=localhost:9030",
"dspace.api.url=http://localhost:9030/dspace/api",
"dspace.collection.uuid=collectionuuid"
"dspace.collection.handle=collectionhandle"
})
@WireMockTest(httpPort = 9030)
public class SubmissionProcessorIT extends AbstractSubmissionIT {
Expand Down Expand Up @@ -378,6 +378,27 @@ private void initDSpaceApiStubs() throws IOException {
withHeader("DSPACE-XSRF-TOKEN", "csrftoken")));
stubFor(post("/dspace/api/authn/login").willReturn(WireMock.ok().withHeader("Authorization", "authtoken")));

String searchJson = "{\n"
+ " \"_embedded\": {\n"
+ " \"searchResult\": {\n"
+ " \"_embedded\": {\n"
+ " \"objects\": [\n"
+ " {\n"
+ " \"_embedded\": {\n"
+ " \"indexableObject\": {\n"
+ " \"handle\": \"collectionhandle\",\n"
+ " \"uuid\": \"collectionuuid\"\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}\n";
stubFor(get("/dspace/api/discover/search/objects?query=handle:collectionhandle")
.willReturn(ok(searchJson)));

stubFor(post("/dspace/api/submission/workspaceitems?owningCollection=collectionuuid")
.willReturn(WireMock.ok("{\"id\": 1, \"item\": {\"uuid\": \"uuid\"}}")));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ dspace.server=localhost:8000
dspace.user=user
dspace.password=test
dspace.api.url=http://localhost:8000/api
dspace.collection.uuid=none
dspace.collection.handle=1234/1
dspace.field.embargo.lift=local.embargo.lift
dspace.field.embargo.terms=local.embargo.terms

0 comments on commit c70f959

Please sign in to comment.