forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 2
75-BE/missing-handles #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
14a714a
create handle without dspace object
Paurikova2 5319232
new controller for import handle without dspace object
Paurikova2 57fb076
create handle for workspaceitem
Paurikova2 67d36b7
using of existing handle if it exists (#316)
Paurikova2 b1b7e4a
checkstyle
Paurikova2 8c86151
fix failed test
Paurikova2 893fefc
added whitespace after for
Paurikova2 c5728c2
review
Paurikova2 3f17151
removed authorization for handle creation
Paurikova2 643e16a
remove unneeded import
Paurikova2 8e9b825
checkstyle
Paurikova2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.