mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-18 15:33:09 +00:00
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:
@@ -12,15 +12,17 @@ import static org.apache.commons.lang3.StringUtils.isBlank;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.validator.routines.EmailValidator;
|
import org.apache.commons.validator.routines.EmailValidator;
|
||||||
import org.apache.http.client.utils.URIBuilder;
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
@@ -287,19 +289,24 @@ public class RequestItemRepository
|
|||||||
* Generate a link back to DSpace, to act on a request.
|
* Generate a link back to DSpace, to act on a request.
|
||||||
*
|
*
|
||||||
* @param token identifies the request.
|
* @param token identifies the request.
|
||||||
* @return URL to the item request API, with the token as request parameter
|
* @return URL to the item request API, with /request-a-copy/{token} as the last URL segments
|
||||||
* "token".
|
|
||||||
* @throws URISyntaxException passed through.
|
* @throws URISyntaxException passed through.
|
||||||
* @throws MalformedURLException passed through.
|
* @throws MalformedURLException passed through.
|
||||||
*/
|
*/
|
||||||
private String getLinkTokenEmail(String token)
|
public String getLinkTokenEmail(String token)
|
||||||
throws URISyntaxException, MalformedURLException {
|
throws URISyntaxException, MalformedURLException {
|
||||||
final String base = configurationService.getProperty("dspace.ui.url");
|
final String base = configurationService.getProperty("dspace.ui.url");
|
||||||
|
|
||||||
URI link = new URIBuilder(base)
|
// Construct the link, making sure to support sub-paths
|
||||||
.setPathSegments("request-a-copy", token)
|
URIBuilder uriBuilder = new URIBuilder(base);
|
||||||
.build();
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -29,6 +29,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -56,10 +58,12 @@ import org.dspace.builder.RequestItemBuilder;
|
|||||||
import org.dspace.content.Bitstream;
|
import org.dspace.content.Bitstream;
|
||||||
import org.dspace.content.Collection;
|
import org.dspace.content.Collection;
|
||||||
import org.dspace.content.Item;
|
import org.dspace.content.Item;
|
||||||
|
import org.dspace.services.ConfigurationService;
|
||||||
import org.hamcrest.Matchers;
|
import org.hamcrest.Matchers;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -82,6 +86,12 @@ public class RequestItemRepositoryIT
|
|||||||
@Autowired(required = true)
|
@Autowired(required = true)
|
||||||
RequestItemService requestItemService;
|
RequestItemService requestItemService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigurationService configurationService;
|
||||||
|
|
||||||
private Collection collection;
|
private Collection collection;
|
||||||
|
|
||||||
private Item item;
|
private Item item;
|
||||||
@@ -610,4 +620,39 @@ public class RequestItemRepositoryIT
|
|||||||
Class instanceClass = instance.getDomainClass();
|
Class instanceClass = instance.getDomainClass();
|
||||||
assertEquals("Wrong domain class", RequestItemRest.class, instanceClass);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user