-
Notifications
You must be signed in to change notification settings - Fork 262
PD-5278 #7495
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
+168
−18
Merged
PD-5278 #7495
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
43 changes: 43 additions & 0 deletions
43
orcid-api-common/src/main/java/org/orcid/api/common/util/v3/PublicRecordUtils.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,43 @@ | ||
| package org.orcid.api.common.util.v3; | ||
|
|
||
| import javax.annotation.Resource; | ||
|
|
||
| import org.orcid.api.publicV3.server.security.PublicAPISecurityManagerV3; | ||
| import org.orcid.core.manager.v3.OrcidSecurityManager; | ||
| import org.orcid.core.manager.v3.read_only.RecordManagerReadOnly; | ||
| import org.orcid.core.utils.v3.SourceUtils; | ||
| import org.orcid.core.version.impl.Api3_0LastModifiedDatesHelper; | ||
| import org.orcid.jaxb.model.v3.release.record.Record; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class PublicRecordUtils { | ||
|
|
||
| @Resource(name = "recordManagerReadOnlyV3") | ||
| private RecordManagerReadOnly recordManagerReadOnly; | ||
|
|
||
| @Resource(name = "orcidSecurityManagerV3") | ||
| private OrcidSecurityManager orcidSecurityManager; | ||
|
|
||
| @Resource(name = "publicAPISecurityManagerV3") | ||
| private PublicAPISecurityManagerV3 publicAPISecurityManagerV3; | ||
|
|
||
| @Resource(name = "sourceUtilsReadOnlyV3") | ||
| private SourceUtils sourceUtilsReadOnly; | ||
|
|
||
| public Record getPublicRecord(String orcid, boolean filterVersionOfIdentifiers) { | ||
| orcidSecurityManager.checkProfile(orcid); | ||
| Record record = recordManagerReadOnly.getPublicRecord(orcid, filterVersionOfIdentifiers); | ||
| publicAPISecurityManagerV3.filter(record); | ||
| if (record.getPerson() != null) { | ||
| sourceUtilsReadOnly.setSourceName(record.getPerson()); | ||
| } | ||
| if (record.getActivitiesSummary() != null) { | ||
| ActivityUtils.cleanEmptyFields(record.getActivitiesSummary()); | ||
| sourceUtilsReadOnly.setSourceName(record.getActivitiesSummary()); | ||
| } | ||
| ElementUtils.setPathToRecord(record, orcid); | ||
| Api3_0LastModifiedDatesHelper.calculateLastModified(record); | ||
| return record; | ||
| } | ||
| } |
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
orcid-api-common/src/main/resources/orcid-api-common-public-v3-context.xml
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,7 @@ | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <beans xmlns="http://www.springframework.org/schema/beans" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation=" | ||
| http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> | ||
| <bean id="publicAPISecurityManagerV3" class="org.orcid.api.publicV3.server.security.impl.PublicAPISecurityManagerV3Impl" /> | ||
| </beans> |
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
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
28 changes: 28 additions & 0 deletions
28
orcid-web/src/main/java/org/orcid/frontend/web/controllers/PublicRecordApiController.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,28 @@ | ||
| package org.orcid.frontend.web.controllers; | ||
|
|
||
| import javax.annotation.Resource; | ||
|
|
||
| import org.orcid.api.common.util.v3.PublicRecordUtils; | ||
| import org.orcid.core.api.OrcidApiConstants; | ||
| import org.orcid.jaxb.model.v3.release.record.Record; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestMethod; | ||
| import org.springframework.web.bind.annotation.ResponseBody; | ||
|
|
||
| @Controller | ||
| public class PublicRecordApiController { | ||
|
|
||
| @Resource | ||
| private PublicRecordUtils publicRecordUtils; | ||
|
|
||
| private final boolean filterVersionOfIdentifiers = false; | ||
|
|
||
| @RequestMapping(value = "/{orcid:(?:\\d{4}-){3,}\\d{3}[\\dX]}/record", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE, | ||
| OrcidApiConstants.ORCID_JSON, OrcidApiConstants.VND_ORCID_JSON }) | ||
| public @ResponseBody Record viewRecord(@PathVariable("orcid") String orcid) { | ||
| return publicRecordUtils.getPublicRecord(orcid, filterVersionOfIdentifiers); | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
...web/src/main/java/org/orcid/frontend/web/controllers/PublicRecordApiExceptionHandler.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,77 @@ | ||
| package org.orcid.frontend.web.controllers; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import javax.annotation.Resource; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.ws.rs.core.Response; | ||
|
|
||
| import org.orcid.core.exception.OrcidCoreExceptionMapper; | ||
| import org.orcid.core.exception.OrcidDeprecatedException; | ||
| import org.orcid.core.manager.impl.OrcidUrlManager; | ||
| import org.orcid.jaxb.model.v3.release.error.OrcidError; | ||
| import org.orcid.utils.OrcidStringUtils; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
|
||
|
|
||
| @RestControllerAdvice(assignableTypes = PublicRecordApiController.class) | ||
| public class PublicRecordApiExceptionHandler { | ||
|
|
||
| private static final String LOCATION_HEADER = "location"; | ||
| private static final String PRIMARY_ORCID_HEADER = "x-orcid-primary"; | ||
|
|
||
| @Resource | ||
| private OrcidCoreExceptionMapper orcidCoreExceptionMapper; | ||
|
|
||
| @ExceptionHandler(Throwable.class) | ||
| public ResponseEntity<Object> handleThrowable(Throwable t, HttpServletRequest request) { | ||
| if (OrcidDeprecatedException.class.isAssignableFrom(t.getClass())) { | ||
| return handleDeprecated((OrcidDeprecatedException) t, request); | ||
| } | ||
| Object orcidError = orcidCoreExceptionMapper.getOrcidError(t, OrcidCoreExceptionMapper.V3); | ||
| int statusCode = ((OrcidError) orcidError).getResponseCode(); | ||
| return ResponseEntity.status(statusCode).body(orcidError); | ||
| } | ||
|
|
||
| private ResponseEntity<Object> handleDeprecated(OrcidDeprecatedException t, HttpServletRequest request) { | ||
| Object orcidError = orcidCoreExceptionMapper.getOrcidError(t, OrcidCoreExceptionMapper.V3); | ||
| Map<String, String> params = t.getParams(); | ||
|
|
||
| int statusCode = Response.Status.MOVED_PERMANENTLY.getStatusCode(); | ||
| String method = request.getMethod(); | ||
| if (method != null && !"GET".equals(method)) { | ||
| statusCode = Response.Status.CONFLICT.getStatusCode(); | ||
| OrcidError v3Error = (OrcidError) orcidError; | ||
| orcidError = orcidCoreExceptionMapper.getDeprecatedOrcidErrorV3(v3Error.getErrorCode(), statusCode, params); | ||
| } | ||
|
|
||
| HttpHeaders headers = new HttpHeaders(); | ||
| if (params != null && params.containsKey(OrcidDeprecatedException.ORCID)) { | ||
| String primaryOrcid = OrcidStringUtils.getOrcidNumber(params.get(OrcidDeprecatedException.ORCID)); | ||
| if (primaryOrcid != null) { | ||
| headers.add(PRIMARY_ORCID_HEADER, primaryOrcid); | ||
| } | ||
| String location = getPrimaryRecordLocation(request, params); | ||
| if (location != null) { | ||
| headers.add(LOCATION_HEADER, location); | ||
| } | ||
| } | ||
| return ResponseEntity.status(statusCode).headers(headers).body(orcidError); | ||
| } | ||
|
|
||
| private String getPrimaryRecordLocation(HttpServletRequest request, Map<String, String> params) { | ||
| String deprecatedOrcid = OrcidStringUtils.getOrcidNumber(request.getRequestURI()); | ||
| String primaryOrcid = OrcidStringUtils.getOrcidNumber(params.get(OrcidDeprecatedException.ORCID)); | ||
| String originalRequest = request.getRequestURL().toString(); | ||
| if (OrcidUrlManager.isSecure(request)) { | ||
| if (originalRequest.startsWith("http:")) { | ||
| originalRequest = originalRequest.replaceFirst("http:", "https:"); | ||
| } | ||
| } | ||
| return originalRequest.replace(deprecatedOrcid, primaryOrcid); | ||
| } | ||
| } | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cryptalith did you tested that this code runs? Im not sure if this code is necessary or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this can be tested by navigating to a deprecated orcid id print page.
As the PublicRecordApiController is a Spring MVC @controller. Requests to
/{orcid}/recordare handled by Spring’s not by the JAX-RS stack where OrcidExceptionMapper run for the public API.So for this endpoint the @RestControllerAdvice is the Spring-side equivalent: it turns exceptions from that controller only into HTTP responses.