[DURACOM-288] Build endpoint map according to request origin, in order to get internal urls if used

This commit is contained in:
Giuseppe Digilio
2025-01-30 15:27:51 +01:00
parent 648d9f6972
commit e6f565e90f
4 changed files with 48 additions and 11 deletions

View File

@@ -44,6 +44,6 @@ public class RootRestResourceController {
@RequestMapping(method = RequestMethod.GET)
public RootResource listDefinedEndpoint(HttpServletRequest request) {
return converter.toResource(rootRestRepository.getRoot());
return converter.toResource(rootRestRepository.getRoot(request));
}
}

View File

@@ -9,6 +9,7 @@ package org.dspace.app.rest.converter;
import static org.dspace.app.util.Util.getSourceVersion;
import jakarta.servlet.http.HttpServletRequest;
import org.dspace.app.rest.model.RootRest;
import org.dspace.services.ConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,12 +24,21 @@ public class RootConverter {
@Autowired
private ConfigurationService configurationService;
public RootRest convert() {
public RootRest convert(HttpServletRequest request) {
RootRest rootRest = new RootRest();
rootRest.setDspaceName(configurationService.getProperty("dspace.name"));
rootRest.setDspaceUI(configurationService.getProperty("dspace.ui.url"));
rootRest.setDspaceServer(configurationService.getProperty("dspace.server.url"));
String requestUrl = request.getRequestURL().toString();
String dspaceUrl = configurationService.getProperty("dspace.server.url");
String dspaceSSRUrl = configurationService.getProperty("dspace.server.ssr.url", dspaceUrl);
if (dspaceUrl.equals(dspaceSSRUrl) || requestUrl.startsWith(dspaceUrl)) {
rootRest.setDspaceServer(dspaceUrl);
} else {
rootRest.setDspaceServer(dspaceSSRUrl);
}
rootRest.setDspaceVersion("DSpace " + getSourceVersion());
return rootRest;
}
}

View File

@@ -7,6 +7,7 @@
*/
package org.dspace.app.rest.repository;
import jakarta.servlet.http.HttpServletRequest;
import org.dspace.app.rest.converter.RootConverter;
import org.dspace.app.rest.model.RootRest;
import org.springframework.beans.factory.annotation.Autowired;
@@ -21,7 +22,7 @@ public class RootRestRepository {
@Autowired
RootConverter rootConverter;
public RootRest getRoot() {
return rootConverter.convert();
public RootRest getRoot(HttpServletRequest request) {
return rootConverter.convert(request);
}
}

View File

@@ -20,6 +20,7 @@ import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.web.MockHttpServletRequest;
/**
* This class' purpose is to test the RootConvertor class.
@@ -33,30 +34,55 @@ public class RootConverterTest {
@Mock
private ConfigurationService configurationService;
private MockHttpServletRequest request = new MockHttpServletRequest();
private String serverURL = "https://dspace-rest/server";
private String serverSSRURL = "http://internal-rest:8080/server";
@Before
public void setUp() throws Exception {
when(configurationService.getProperty("dspace.ui.url")).thenReturn("dspaceurl");
when(configurationService.getProperty("dspace.name")).thenReturn("dspacename");
when(configurationService.getProperty("dspace.server.url")).thenReturn("rest");
when(configurationService.getProperty("dspace.server.url")).thenReturn(serverURL);
when(configurationService.getProperty("dspace.server.ssr.url", serverURL)).thenReturn(serverSSRURL);
}
@Test
public void testReturnCorrectClass() throws Exception {
assertEquals(rootConverter.convert().getClass(), RootRest.class);
assertEquals(rootConverter.convert(request).getClass(), RootRest.class);
}
@Test
public void testCorrectPropertiesSetFromConfigurationService() throws Exception {
String restUrl = "rest";
RootRest rootRest = rootConverter.convert();
String restUrl = "/server/api";
request.setScheme("https");
request.setServerName("dspace-rest");
request.setServerPort(443);
request.setRequestURI(restUrl);
RootRest rootRest = rootConverter.convert(request);
assertEquals("dspaceurl", rootRest.getDspaceUI());
assertEquals("dspacename", rootRest.getDspaceName());
assertEquals(restUrl, rootRest.getDspaceServer());
assertEquals(serverURL, rootRest.getDspaceServer());
assertEquals("DSpace " + Util.getSourceVersion(), rootRest.getDspaceVersion());
}
@Test
public void testReturnNotNull() throws Exception {
assertNotNull(rootConverter.convert());
assertNotNull(rootConverter.convert(request));
}
@Test
public void testCorrectInternalUrlSetFromConfigurationService() throws Exception {
String restUrl = "/server/api";
request.setScheme("http");
request.setServerName("internal-rest");
request.setServerPort(8080);
request.setRequestURI(restUrl);
RootRest rootRest = rootConverter.convert(request);
assertEquals("dspaceurl", rootRest.getDspaceUI());
assertEquals("dspacename", rootRest.getDspaceName());
assertEquals(serverSSRURL, rootRest.getDspaceServer());
assertEquals("DSpace " + Util.getSourceVersion(), rootRest.getDspaceVersion());
}
}