Merge pull request #9801 from tdonohue/port_9668_to_7x

[Port dspace-7_x] Fix for DSpace#9667: Request-a-copy link generation for base URLs that have sub-paths
This commit is contained in:
Tim Donohue
2024-09-06 16:40:59 -05:00
committed by GitHub
2 changed files with 60 additions and 8 deletions

View File

@@ -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 javax.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
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();
}
}

View File

@@ -29,6 +29,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;
@@ -56,10 +58,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;
/**
*
@@ -82,6 +86,12 @@ public class RequestItemRepositoryIT
@Autowired(required = true)
RequestItemService requestItemService;
@Autowired
ApplicationContext applicationContext;
@Autowired
private ConfigurationService configurationService;
private Collection collection;
private Item item;
@@ -610,4 +620,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.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.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();
}
}