Skip to content

Commit d0645e3

Browse files
ZCU-PUB/SWORDv2 the swordv2 url is not composed correctly (#931)
* Removed `swordv2` duplicities from the `edit-media` request * Compose correctly the swordBaseUrl when the `path` is empty * Added better doc
1 parent c7ec2ac commit d0645e3

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

dspace-server-webapp/src/test/java/org/dspace/app/sword2/Swordv2IT.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
import static org.junit.Assert.assertNotNull;
1515

1616
import java.nio.file.Path;
17+
import java.sql.SQLException;
1718
import java.util.List;
1819

1920
import org.dspace.app.rest.test.AbstractWebClientIntegrationTest;
21+
import org.dspace.authorize.AuthorizeException;
2022
import org.dspace.builder.CollectionBuilder;
2123
import org.dspace.builder.CommunityBuilder;
2224
import org.dspace.builder.ItemBuilder;
@@ -228,6 +230,84 @@ public void depositItemWithEmbargo() throws Exception {
228230
assertEquals(ATOM_ENTRY_CONTENT_TYPE, response.getHeaders().getContentType().toString());
229231
}
230232

233+
// Test when the `swordv2-server.url` is null. The swordv2 server URL should be constructed using the default value
234+
@Test
235+
public void editMediaPathTest() throws SQLException, AuthorizeException {
236+
context.turnOffAuthorisationSystem();
237+
// The `swordv2` server URL is constructed, and the default value is used if `swordv2-server.url` is null.
238+
configurationService.setProperty("swordv2-server.url", null);
239+
// The `dspace.server.url` must be updated following the same logic as the `swordv2-server.url`.
240+
// The `getURL` method used a specific port.
241+
String dspaceServerUrl = configurationService.getProperty("dspace.server.url");
242+
configurationService.setProperty("dspace.server.url", getURL(""));
243+
try {
244+
// Create a top level community and one Collection
245+
parentCommunity = CommunityBuilder.createCommunity(context)
246+
.withName("Parent Community")
247+
.build();
248+
// Make sure our Collection allows the "eperson" user to submit into it
249+
Collection collection = CollectionBuilder.createCollection(context, parentCommunity)
250+
.withName("Test SWORDv2 Collection")
251+
.withSubmitterGroup(eperson)
252+
.build();
253+
// Above changes MUST be committed to the database for SWORDv2 to see them.
254+
context.commit();
255+
context.restoreAuthSystemState();
256+
257+
// Add file
258+
LinkedMultiValueMap<Object, Object> multipart = new LinkedMultiValueMap<>();
259+
multipart.add("file", new FileSystemResource(Path.of("src", "test", "resources",
260+
"org", "dspace", "app", "sword2", "example.zip")));
261+
// Add required headers
262+
HttpHeaders headers = new HttpHeaders();
263+
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
264+
headers.setContentDisposition(ContentDisposition.attachment().filename("example.zip").build());
265+
headers.set("Packaging", "http://purl.org/net/sword/package/METSDSpaceSIP");
266+
headers.setAccept(List.of(MediaType.APPLICATION_ATOM_XML));
267+
268+
//----
269+
// STEP 1: Verify upload/submit via SWORDv2 works
270+
//----
271+
// Send POST to upload Zip file via SWORD
272+
ResponseEntity<String> response = postResponseAsString(COLLECTION_PATH + "/" + collection.getHandle(),
273+
eperson.getEmail(), password,
274+
new HttpEntity<>(multipart, headers));
275+
276+
// Expect a 201 CREATED response with ATOM "entry" content returned
277+
assertEquals(HttpStatus.CREATED, response.getStatusCode());
278+
assertEquals(ATOM_ENTRY_CONTENT_TYPE, response.getHeaders().getContentType().toString());
279+
// MUST return a "Location" header which is the "/swordv2/edit/[uuid]" URI of the created item
280+
assertNotNull(response.getHeaders().getLocation());
281+
282+
String editLink = response.getHeaders().getLocation().toString();
283+
284+
// Body should include that link as the rel="edit" URL
285+
assertThat(response.getBody(), containsString("<link href=\"" + editLink + "\" rel=\"edit\"/>"));
286+
287+
//----
288+
// STEP 2: Verify uploaded content can be read via SWORDv2
289+
//----
290+
// Edit URI should work when requested by the EPerson who did the deposit
291+
HttpHeaders authHeaders = new HttpHeaders();
292+
authHeaders.setBasicAuth(eperson.getEmail(), password);
293+
RequestEntity request = RequestEntity.get(editLink)
294+
.accept(MediaType.valueOf("application/atom+xml"))
295+
.headers(authHeaders)
296+
.build();
297+
response = responseAsString(request);
298+
// Expect a 200 response with ATOM feed content returned
299+
assertEquals(HttpStatus.OK, response.getStatusCode());
300+
assertEquals(ATOM_FEED_CONTENT_TYPE, response.getHeaders().getContentType().toString());
301+
// Body should include links to bitstreams from the zip.
302+
// This just verifies at least one /swordv2/edit-media/bitstream/* link exists.
303+
assertThat(response.getBody(), containsString(getURL(MEDIA_RESOURCE_PATH + "/bitstream")));
304+
// Verify Item title also is returned in the body
305+
assertThat(response.getBody(), containsString("Attempts to detect retrotransposition"));
306+
} finally {
307+
configurationService.setProperty("dspace.server.url", dspaceServerUrl);
308+
}
309+
}
310+
231311
/**
232312
* This tests four different SWORDv2 actions, as these all require starting with a new deposit.
233313
* 1. Depositing a new item via SWORD (via POST /collections/[collection-uuid])

dspace-swordv2/src/main/java/org/dspace/sword2/SwordUrlManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.List;
1414

1515
import org.apache.abdera.i18n.iri.IRI;
16+
import org.apache.commons.lang3.StringUtils;
1617
import org.dspace.content.Bitstream;
1718
import org.dspace.content.Bundle;
1819
import org.dspace.content.Collection;
@@ -109,7 +110,7 @@ public String getSwordBaseUrl()
109110
"Unable to construct service document urls, due to missing/invalid " +
110111
"config in sword2.url and/or dspace.server.url");
111112
}
112-
sUrl = buildSWORDUrl("swordv2");
113+
sUrl = buildSWORDUrl("");
113114
}
114115
return sUrl;
115116
}
@@ -496,6 +497,6 @@ public IRI getMediaFeedUrl(Item item)
496497
* @return a sword URL
497498
*/
498499
private String buildSWORDUrl(String path) {
499-
return dspaceUrl + "/" + swordPath + "/" + path;
500+
return dspaceUrl + "/" + swordPath + (StringUtils.isNotBlank(path) ? "/" + path : "");
500501
}
501502
}

0 commit comments

Comments
 (0)