forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create handle without dspace object * new controller for import handle without dspace object * create handle for workspaceitem * using of existing handle if it exists (#316) * checkstyle * fix failed test * added whitespace after for * review * removed authorization for handle creation * remove unneeded import * checkstyle
- Loading branch information
1 parent
9fe1b52
commit 624378c
Showing
7 changed files
with
215 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ver-webapp/src/main/java/org/dspace/app/rest/repository/ClarinHandleImportController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* 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.repository; | ||
|
||
import static org.dspace.app.rest.utils.ContextUtil.obtainContext; | ||
|
||
import java.io.IOException; | ||
import java.sql.SQLException; | ||
import java.util.Objects; | ||
import javax.servlet.ServletInputStream; | ||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.dspace.app.rest.converter.ConverterService; | ||
import org.dspace.app.rest.exception.UnprocessableEntityException; | ||
import org.dspace.app.rest.model.HandleRest; | ||
import org.dspace.app.rest.utils.Utils; | ||
import org.dspace.authorize.AuthorizeException; | ||
import org.dspace.core.Context; | ||
import org.dspace.handle.Handle; | ||
import org.dspace.handle.service.HandleClarinService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* Specialized controller created for Clarin-Dspace import handle without dspace object. | ||
* | ||
* @author Michaela Paurikova (michaela.paurikova at dataquest.sk) | ||
*/ | ||
@RestController | ||
@RequestMapping("/api/clarin/import") | ||
public class ClarinHandleImportController { | ||
@Autowired | ||
private HandleClarinService handleClarinService; | ||
@Autowired | ||
private ConverterService converter; | ||
@Autowired | ||
private Utils utils; | ||
|
||
/** | ||
* Endpoint for import handle without dspace object. | ||
* The mapping for requested endpoint, for example | ||
* <pre> | ||
* {@code | ||
* https://<dspace.server.url>/api/clarin/import/handle | ||
* } | ||
* </pre> | ||
* @param request request | ||
* @return handle converted to the rest | ||
* @throws AuthorizeException if authorization error | ||
* @throws SQLException if database error | ||
*/ | ||
@PreAuthorize("hasAuthority('ADMIN')") | ||
@RequestMapping(method = RequestMethod.POST, value = "/handle") | ||
public HandleRest importHandle(HttpServletRequest request) throws AuthorizeException, SQLException { | ||
Context context = obtainContext(request); | ||
if (Objects.isNull(context)) { | ||
throw new RuntimeException("Context is null!"); | ||
} | ||
ObjectMapper mapper = new ObjectMapper(); | ||
HandleRest handleRest = null; | ||
try { | ||
ServletInputStream input = request.getInputStream(); | ||
handleRest = mapper.readValue(input, HandleRest.class); | ||
} catch (IOException e1) { | ||
throw new UnprocessableEntityException("Error parsing request body", e1); | ||
} | ||
|
||
Handle handle; | ||
try { | ||
handle = handleClarinService.createHandle(context, handleRest.getHandle()); | ||
//set handle attributes | ||
handle.setResourceTypeId(handleRest.getResourceTypeID()); | ||
// Save created handle | ||
handleClarinService.save(context, handle); | ||
} catch (SQLException e) { | ||
throw new RuntimeException("Error while trying to create new Handle and update it", e); | ||
} | ||
handleRest = converter.toRest(handle, utils.obtainProjection()); | ||
context.commit(); | ||
return handleRest; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinHandleImportControllerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* 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.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.dspace.app.rest.model.HandleRest; | ||
import org.dspace.app.rest.test.AbstractControllerIntegrationTest; | ||
import org.dspace.handle.Handle; | ||
import org.dspace.handle.service.HandleClarinService; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
|
||
/** | ||
* Integration test to test the /api/clarin/import/handle endpoint | ||
* | ||
* @author Michaela Paurikova (michaela.paurikova at dataquest.sk) | ||
*/ | ||
public class ClarinHandleImportControllerIT extends AbstractControllerIntegrationTest { | ||
@Autowired | ||
private HandleClarinService handleService; | ||
|
||
@Test | ||
public void createHandleWithoutDSpaceObjectTest() throws Exception { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
HandleRest handleRest = new HandleRest(); | ||
handleRest.setHandle("123456789/1"); | ||
handleRest.setResourceTypeID(2); | ||
Integer handleId = null; | ||
String adminToken = getAuthToken(admin.getEmail(), password); | ||
MvcResult mvcResult = getClient(adminToken).perform(post("/api/clarin/import/handle") | ||
.content(mapper.writeValueAsBytes(handleRest)) | ||
.contentType(contentType)) | ||
.andExpect(status().isOk()) | ||
.andReturn(); | ||
|
||
String content = mvcResult.getResponse().getContentAsString(); | ||
Map<String, Object> map = mapper.readValue(content, Map.class); | ||
handleId = Integer.valueOf(String.valueOf(map.get("id"))); | ||
//find created handle | ||
Handle handle = handleService.findByID(context, handleId); | ||
//control | ||
assertEquals(handle.getHandle(), "123456789/1"); | ||
assertEquals(handle.getResourceTypeId(), (Integer)2); | ||
assertNull(handle.getUrl()); | ||
assertNull(handle.getDSpaceObject()); | ||
//clean all | ||
context.turnOffAuthorisationSystem(); | ||
List<Handle> handles = handleService.findAll(context); | ||
for (Handle h: handles) { | ||
handleService.delete(context, h); | ||
} | ||
context.restoreAuthSystemState(); | ||
} | ||
} |