Skip to content

Commit

Permalink
Various fixes for DSpace deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
markpatton committed Nov 22, 2024
1 parent c70f959 commit fec504f
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ protected List<DepositFileResource> resolveCustodialResources(List<DepositFile>
} else if (Objects.nonNull(dfr.getDepositFile().getPassFileId())) {
String passFileId = dfr.getDepositFile().getPassFileId();
LOG.trace("Returning PassFileResource for Pass File {}", passFileId);
delegateResource = new PassFileResource(passClient, passFileId);
delegateResource = new PassFileResource(passClient, passFileId, dfr.getDepositFile().getName());
} else if (location.startsWith(HTTP_PREFIX) || location.startsWith(HTTPS_PREFIX) ||
location.startsWith(JAR_PREFIX)) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ public class PassFileResource extends AbstractResource {

private final PassClient passClient;
private final String passFileId;
private final String filename;

public PassFileResource(PassClient passClient, String passFileId) {
public PassFileResource(PassClient passClient, String passFileId, String filename) {
this.passClient = passClient;
this.passFileId = passFileId;
this.filename = filename;
}

@Override
Expand All @@ -54,4 +56,9 @@ public String getContentAsString(Charset charset) throws IOException {
public String getDescription() {
return "PassFileResource File ID: " + passFileId;
}

@Override
public String getFilename() {
return filename;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,69 +59,81 @@ public String patchWorkspaceItem(DepositSubmission submission) {
String title = manuscriptMd.getTitle();

// Required by DSpace
metadata.add(element("traditionalpageone", "dc.title", title));
metadata.add(add_array("traditionalpageone", "dc.title", title));

if (journalMd != null && journalMd.getPublisherName() != null) {
metadata.add(element("traditionalpageone", "dc.publisher", journalMd.getPublisherName()));
metadata.add(add_array("traditionalpageone", "dc.publisher", journalMd.getPublisherName()));
}

if (articleMd.getDoi() != null) {
metadata.add(element("traditionalpageone", "dc.identifier.doi", articleMd.getDoi().toString()));
metadata.add(add_array("traditionalpageone", "dc.identifier.doi", articleMd.getDoi().toString()));
}

if (manuscriptMd.getMsAbstract() != null) {
metadata.add(element("traditionalpageone", "dc.description.abstract", manuscriptMd.getMsAbstract()));
metadata.add(add_array("traditionalpageone", "dc.description.abstract", manuscriptMd.getMsAbstract()));
}

String citation = createCitation(submission);

if (!citation.isEmpty()) {
metadata.add(element("traditionalpageone", "dc.identifier.citation", citation));
metadata.add(add_array("traditionalpageone", "dc.identifier.citation", citation));
}

// TODO: Must format YYYY-MM-DD
// Required by DSpace
String publicationDate = journalMd.getPublicationDate();
metadata.add(element("traditionalpageone", "dc.date.issued", publicationDate));
metadata.add(add_array("traditionalpageone", "dc.date.issued", publicationDate));

for (Person p: depositMd.getPersons()) {
if (p.getType() != DepositMetadata.PERSON_TYPE.submitter) {
metadata.add(element("traditionalpageone", "dc.contributor", p.getName()));
}
}
// Add non-submitters as authors
// TODO This is different from before
String[] authors = depositMd.getPersons().stream().filter(
p -> p.getType() != DepositMetadata.PERSON_TYPE.submitter).
map(Person::getName).toArray(String[]::new);

metadata.add(add_array("traditionalpageone", "dc.contributor.author", authors));

ZonedDateTime embargoLiftDate = articleMd.getEmbargoLiftDate();

if (embargoLiftDate != null) {
String liftDate = embargoLiftDate.format(DateTimeFormatter.ISO_LOCAL_DATE);

metadata.add(element("traditionalpageone", dspaceFieldEmbargoLift, liftDate));
metadata.add(element("traditionalpageone", dspaceFieldEmbargoTerms, liftDate));
metadata.add(add_array("traditionalpageone", dspaceFieldEmbargoLift, liftDate));
metadata.add(add_array("traditionalpageone", dspaceFieldEmbargoTerms, liftDate));
}

// Required by DSpace
metadata.add(element("license", "granted", "true"));
metadata.add(add("license", "granted", "true"));

return metadata.toString();
}

private JSONObject element(String section, String key, String... values) {
private JSONObject add(String section, String key, String value) {
JSONObject op = new JSONObject();

op.put("op", "add");
op.put("path", "/sections/" + section + "/" + key);
op.put("value", value);

return op;
}

private JSONObject add_array(String section, String key, String... values) {
JSONObject op = new JSONObject();

op.put("op", "add");
op.put("path", "/sections/" + section + "/" + key);

JSONArray op_value = new JSONArray();
for (String value : values) {
op_value.add(elementValue(value));
op_value.add(array_value(value));
}

op.put("value", op_value);

return op;
}

private JSONObject elementValue(String value) {
private JSONObject array_value(String value) {
JSONObject obj = new JSONObject();
obj.put("value", value);
return obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +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;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -57,6 +55,9 @@ public class DspaceDepositService {
@Value("${dspace.api.url}")
private String dspaceApiUrl;

@Value("${dspace.website.url}")
private String dspaceWebsiteUrl;

@Value("${dspace.user}")
private String dspaceUsername;

Expand All @@ -69,10 +70,6 @@ public class DspaceDepositService {
@Value("${dspace.collection.handle}")
private String dspaceCollectionHandle;

private String dspaceCollectionUuid;

private final String dspaceServerScheme;

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

public DspaceDepositService(@Value("${dspace.api.url}") String dspaceApiUrl) {
Expand All @@ -95,12 +92,6 @@ public DspaceDepositService(@Value("${dspace.api.url}") String dspaceApiUrl) {
.requestFactory(new HttpComponentsClientHttpRequestFactory(httpClient))
.baseUrl(dspaceApiUrl)
.build();

try {
this.dspaceServerScheme = new URI(dspaceApiUrl).getScheme();
} catch (URISyntaxException e) {
throw new RuntimeException("Could not get scheme: " + dspaceApiUrl);
}
}

/**
Expand Down Expand Up @@ -181,7 +172,7 @@ private String parseHandleFilter(URI accessUrl) {
}

public String createAccessUrlFromItemUuid(String itemUuid) {
return dspaceServerScheme + "://" + dspaceServer + "/items/" + itemUuid;
return dspaceWebsiteUrl + "/items/" + itemUuid;
}

private String findItemUuid(String handleValue, AuthContext authContext, String submissionTitle) {
Expand Down Expand Up @@ -272,43 +263,23 @@ public String getUuidForHandle(String handle, AuthContext authContext) {
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 -> {
for (DepositFileResource res: files) {
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 uuid = getUuidForHandle(dspaceCollectionHandle, authContext);

String json = restClient.post()
.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 @@ -62,8 +62,9 @@ public TransportResponse send(PackageStream packageStream, Map<String, String> m
// TODO Set workspace item id on Deposit.depositStatusRef so can check if it already exists.
// TODO Then check metadata to see if it needs to be patched.

int workspaceItemId = JsonPath.parse(workspaceItemJson).read("$.id");
String itemUuid = JsonPath.parse(workspaceItemJson).read("$.item.uuid");
int workspaceItemId = JsonPath.parse(workspaceItemJson).read("$._embedded.workspaceitems[0].id");
String itemUuid = JsonPath.parse(workspaceItemJson).read(
"$._embedded.workspaceitems[0]._embedded.item.uuid");

LOG.debug("Patching WorkspaceItem to add metadata {}", patchJson);
dspaceDepositService.patchWorkspaceItem(workspaceItemId, patchJson, authContext);
Expand All @@ -73,7 +74,6 @@ public TransportResponse send(PackageStream packageStream, Map<String, String> m

String accessUrl = dspaceDepositService.createAccessUrlFromItemUuid(itemUuid);

// TODO Is this published immediately?
// TODO 422 indicates validation errors. Should mark the deposit as failed and not retry.

LOG.warn("Completed DSpace Deposit for Submission: {}, accessUrl: {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pmc.ftp.password=${PMC_FTP_PASSWORD}

dspace.server=${DSPACE_SERVER}
dspace.api.url=${DSPACE_API_URL}
dspace.website.url=${DSPACE_WEBSITE_URL}
dspace.user=${DSPACE_USER}
dspace.password=${DSPACE_PASSWORD}
dspace.collection.handle=${DSPACE_COLLECTION_HANDLE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void testGetInputStream() throws IOException {
file.setUri(fileUri);
passClient.createObject(file);

PassFileResource passFileResource = new PassFileResource(passClient, file.getId());
PassFileResource passFileResource = new PassFileResource(passClient, file.getId(), file.getName());

// WHEN
InputStream inputStream = passFileResource.getInputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
dspace.password=${DSPACE_PASSWORD:test-dspace-pw}
dspace.server=localhost:8000
dspace.api.url=http://localhost:8000/api
dspace.website.url=http://localhost:8000/website
dspace.collection.handle=1234/1
"""
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"inveniordm.api.baseUrl=http://localhost:9030/api",
"dspace.server=localhost:9030",
"dspace.api.url=http://localhost:9030/dspace/api",
"dspace.website.url=http://localhost:9030/dspace/website",
"dspace.collection.handle=collectionhandle"
})
@WireMockTest(httpPort = 9030)
Expand Down Expand Up @@ -400,7 +401,8 @@ private void initDSpaceApiStubs() throws IOException {
.willReturn(ok(searchJson)));

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

stubFor(patch("/dspace/api/submission/workspaceitems/1").willReturn(WireMock.ok()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
@TestPropertySource(properties = {
"dspace.server=localhost:9020",
"dspace.api.url=http://localhost:9020/server/api",
"dspace.website.url=http://localhost:9020/website",
"pass.test.data.job.enabled=true",
"pass.test.data.policy.title=test-policy-title",
"pass.test.data.user.email=test-user-email@foo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dspace.server=localhost:8000
dspace.user=user
dspace.password=test
dspace.api.url=http://localhost:8000/api
dspace.website.url=http://localhost:8000/website
dspace.collection.handle=1234/1
dspace.field.embargo.lift=local.embargo.lift
dspace.field.embargo.terms=local.embargo.terms

0 comments on commit fec504f

Please sign in to comment.