mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
Merge pull request #9668 from kshepherd/request_copy_link_generation
Fix for DSpace#9667: Request-a-copy link generation for base URLs that have sub-paths
This commit is contained in:
@@ -12,15 +12,17 @@ import static org.apache.commons.lang3.StringUtils.isBlank;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.validator.routines.EmailValidator;
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -287,19 +289,24 @@ public class RequestItemRepository
|
||||
* Generate a link back to DSpace, to act on a request.
|
||||
*
|
||||
* @param token identifies the request.
|
||||
* @return URL to the item request API, with the token as request parameter
|
||||
* "token".
|
||||
* @return URL to the item request API, with /request-a-copy/{token} as the last URL segments
|
||||
* @throws URISyntaxException passed through.
|
||||
* @throws MalformedURLException passed through.
|
||||
*/
|
||||
private String getLinkTokenEmail(String token)
|
||||
public String getLinkTokenEmail(String token)
|
||||
throws URISyntaxException, MalformedURLException {
|
||||
final String base = configurationService.getProperty("dspace.ui.url");
|
||||
|
||||
URI link = new URIBuilder(base)
|
||||
.setPathSegments("request-a-copy", token)
|
||||
.build();
|
||||
// Construct the link, making sure to support sub-paths
|
||||
URIBuilder uriBuilder = new URIBuilder(base);
|
||||
List<String> segments = new LinkedList<>();
|
||||
if (StringUtils.isNotBlank(uriBuilder.getPath())) {
|
||||
segments.add(StringUtils.strip(uriBuilder.getPath(), "/"));
|
||||
}
|
||||
segments.add("request-a-copy");
|
||||
segments.add(token);
|
||||
|
||||
return link.toURL().toExternalForm();
|
||||
// Build and return the URL from segments (or throw exception)
|
||||
return uriBuilder.setPathSegments(segments).build().toURL().toExternalForm();
|
||||
}
|
||||
}
|
||||
|
@@ -28,6 +28,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.sql.SQLException;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Date;
|
||||
@@ -55,10 +57,12 @@ import org.dspace.builder.RequestItemBuilder;
|
||||
import org.dspace.content.Bitstream;
|
||||
import org.dspace.content.Collection;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.services.ConfigurationService;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -81,6 +85,12 @@ public class RequestItemRepositoryIT
|
||||
@Autowired(required = true)
|
||||
RequestItemService requestItemService;
|
||||
|
||||
@Autowired
|
||||
ApplicationContext applicationContext;
|
||||
|
||||
@Autowired
|
||||
private ConfigurationService configurationService;
|
||||
|
||||
private Collection collection;
|
||||
|
||||
private Item item;
|
||||
@@ -594,4 +604,39 @@ public class RequestItemRepositoryIT
|
||||
Class instanceClass = instance.getDomainClass();
|
||||
assertEquals("Wrong domain class", RequestItemRest.class, instanceClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that generated links include the correct base URL, where the UI URL has a subpath like /subdir
|
||||
*/
|
||||
@Test
|
||||
public void testGetLinkTokenEmailWithSubPath() throws MalformedURLException, URISyntaxException {
|
||||
RequestItemRepository instance = applicationContext.getBean(
|
||||
RequestItemRest.CATEGORY + '.' + RequestItemRest.PLURAL_NAME,
|
||||
RequestItemRepository.class);
|
||||
String currentDspaceUrl = configurationService.getProperty("dspace.ui.url");
|
||||
String newDspaceUrl = currentDspaceUrl + "/subdir";
|
||||
// Add a /subdir to the url for this test
|
||||
configurationService.setProperty("dspace.ui.url", newDspaceUrl);
|
||||
String expectedUrl = newDspaceUrl + "/request-a-copy/token";
|
||||
String generatedLink = instance.getLinkTokenEmail("token");
|
||||
// The URLs should match
|
||||
assertEquals(expectedUrl, generatedLink);
|
||||
configurationService.reloadConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that generated links include the correct base URL, with NO subpath elements
|
||||
*/
|
||||
@Test
|
||||
public void testGetLinkTokenEmailWithoutSubPath() throws MalformedURLException, URISyntaxException {
|
||||
RequestItemRepository instance = applicationContext.getBean(
|
||||
RequestItemRest.CATEGORY + '.' + RequestItemRest.PLURAL_NAME,
|
||||
RequestItemRepository.class);
|
||||
String currentDspaceUrl = configurationService.getProperty("dspace.ui.url");
|
||||
String expectedUrl = currentDspaceUrl + "/request-a-copy/token";
|
||||
String generatedLink = instance.getLinkTokenEmail("token");
|
||||
// The URLs should match
|
||||
assertEquals(expectedUrl, generatedLink);
|
||||
configurationService.reloadConfig();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user