mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-18 07:23:08 +00:00
Merge pull request #2052 from tantz001/DS-3905
DS-3905; add a custom RestController
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.app.rest;
|
||||
|
||||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.atteo.evo.inflector.English;
|
||||
|
||||
import org.dspace.app.rest.converter.DSpaceObjectConverter;
|
||||
import org.dspace.app.rest.model.DSpaceObjectRest;
|
||||
import org.dspace.app.rest.utils.ContextUtil;
|
||||
import org.dspace.content.DSpaceObject;
|
||||
import org.dspace.core.Context;
|
||||
import org.dspace.identifier.IdentifierNotFoundException;
|
||||
import org.dspace.identifier.IdentifierNotResolvableException;
|
||||
import org.dspace.identifier.factory.IdentifierServiceFactory;
|
||||
import org.dspace.identifier.service.IdentifierService;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.TemplateVariable;
|
||||
import org.springframework.hateoas.TemplateVariable.VariableType;
|
||||
import org.springframework.hateoas.TemplateVariables;
|
||||
import org.springframework.hateoas.UriTemplate;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/" + IdentifierRestController.CATEGORY)
|
||||
public class IdentifierRestController implements InitializingBean {
|
||||
public static final String CATEGORY = "pid";
|
||||
|
||||
public static final String ACTION = "find";
|
||||
|
||||
public static final String PARAM = "id";
|
||||
|
||||
private static final Logger log =
|
||||
Logger.getLogger(IdentifierRestController.class);
|
||||
|
||||
@Autowired
|
||||
private List<DSpaceObjectConverter> converters;
|
||||
|
||||
@Autowired
|
||||
private DiscoverableEndpointsService discoverableEndpointsService;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
discoverableEndpointsService
|
||||
.register(this,
|
||||
Arrays.asList(
|
||||
new Link(
|
||||
new UriTemplate("/api/" + CATEGORY + "/" + ACTION,
|
||||
new TemplateVariables(
|
||||
new TemplateVariable(PARAM, VariableType.REQUEST_PARAM))),
|
||||
CATEGORY)));
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = ACTION, params = PARAM)
|
||||
@SuppressWarnings("unchecked")
|
||||
public void getDSObyIdentifier(HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
@RequestParam("id") String id)
|
||||
throws IOException, SQLException {
|
||||
|
||||
DSpaceObject dso = null;
|
||||
Context context = ContextUtil.obtainContext(request);
|
||||
IdentifierService identifierService = IdentifierServiceFactory
|
||||
.getInstance().getIdentifierService();
|
||||
try {
|
||||
dso = identifierService.resolve(context, id);
|
||||
if (dso != null) {
|
||||
DSpaceObjectRest dsor = convertDSpaceObject(dso);
|
||||
URI link = linkTo(dsor.getController(), dsor.getCategory(),
|
||||
English.plural(dsor.getType()))
|
||||
.slash(dsor.getId()).toUri();
|
||||
response.setStatus(HttpServletResponse.SC_FOUND);
|
||||
response.sendRedirect(link.toString());
|
||||
} else {
|
||||
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
}
|
||||
} catch (IdentifierNotFoundException e) {
|
||||
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
} catch (IdentifierNotResolvableException e) {
|
||||
response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
|
||||
} finally {
|
||||
context.abort();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a DSpaceObject in its REST representation using a suitable converter
|
||||
*/
|
||||
private DSpaceObjectRest convertDSpaceObject(DSpaceObject dspaceObject) {
|
||||
for (DSpaceObjectConverter converter : converters) {
|
||||
if (converter.supportsModel(dspaceObject)) {
|
||||
return converter.fromModel(dspaceObject);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.app.rest;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.dspace.app.rest.builder.CommunityBuilder;
|
||||
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Integration test for the identifier resolver
|
||||
*
|
||||
* @author Andrea Bollini (andrea.bollini at 4science.it)
|
||||
*/
|
||||
public class IdentifierRestControllerIT extends AbstractControllerIntegrationTest {
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidIdentifier() throws Exception {
|
||||
//We turn off the authorization system in order to create the structure as defined below
|
||||
context.turnOffAuthorisationSystem();
|
||||
|
||||
// We create a top community to receive an identifier
|
||||
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||
.withName("Parent Community")
|
||||
.build();
|
||||
|
||||
context.restoreAuthSystemState();
|
||||
|
||||
String handle = parentCommunity.getHandle();
|
||||
String communityDetail = REST_SERVER_URL + "core/communities/" + parentCommunity.getID();
|
||||
|
||||
getClient().perform(get("/api/pid/find?id={handle}",handle))
|
||||
.andExpect(status().isFound())
|
||||
//We expect a Location header to redirect to the community details
|
||||
.andExpect(header().string("Location", communityDetail));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnexistentIdentifier() throws Exception {
|
||||
getClient().perform(get("/api/pid/find?{id}","fakeIdentifier"))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user