Skip to content

Commit

Permalink
When POST, PUT or DELETE on a deprecated account, return a 409
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Feb 28, 2024
1 parent b3660c2 commit 4c4f853
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.orcid.api.common.filter.ApiVersionFilter;
import org.orcid.api.common.util.ApiUtils;
import org.orcid.core.api.OrcidApiConstants;
import org.orcid.core.exception.ApplicationException;
import org.orcid.core.exception.ClientDeactivatedException;
import org.orcid.core.exception.DeactivatedException;
import org.orcid.core.exception.DuplicatedGroupIdRecordException;
Expand Down Expand Up @@ -180,6 +181,7 @@ private Response oAuthErrorResponse(Throwable t) {
return Response.status(error.getResponseStatus()).entity(error).build();
}

@Deprecated
private Response legacyErrorResponse(Throwable t) {
if (OrcidApiException.class.isAssignableFrom(t.getClass())) {
return Response.status(((OrcidApiException) t).getHttpStatus()).build();
Expand Down Expand Up @@ -282,9 +284,7 @@ private OrcidMessage getLegacyOrcidEntity(String prefix, Throwable e) {
}

private Response newStyleErrorResponse(Throwable t, String version) {
if(NotFoundException.class.isAssignableFrom(t.getClass())) {
return getOrcidErrorResponse(t, version);
} else if (WebApplicationException.class.isAssignableFrom(t.getClass())) {
if (WebApplicationException.class.isAssignableFrom(t.getClass())) {
return getOrcidErrorResponse((WebApplicationException) t, version);
} else {
return getOrcidErrorResponse(t, version);
Expand All @@ -310,7 +310,7 @@ private Response getOrcidErrorResponse(Object orcidError, Throwable t) {
int statusCode = 0;

if (org.orcid.jaxb.model.error_v2.OrcidError.class.isAssignableFrom(orcidError.getClass())) {
statusCode = ((org.orcid.jaxb.model.error_v2.OrcidError) orcidError).getResponseCode();
statusCode = ((org.orcid.jaxb.model.error_v2.OrcidError) orcidError).getResponseCode();
} else if (org.orcid.jaxb.model.v3.release.error.OrcidError.class.isAssignableFrom(orcidError.getClass())) {
statusCode = ((org.orcid.jaxb.model.v3.release.error.OrcidError) orcidError).getResponseCode();
}
Expand All @@ -324,7 +324,25 @@ private Response getOrcidErrorResponse(Object orcidError, Throwable t) {
location = getPrimaryRecordLocation(params);
}
}


RequestAttributes reqAttr = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servlReqAttr = (ServletRequestAttributes)reqAttr;
HttpServletRequest req = servlReqAttr.getRequest();
String requestMethod = (req == null) ? null : req.getMethod();

statusCode = Response.Status.MOVED_PERMANENTLY.getStatusCode();
if(requestMethod != null && !requestMethod.equals("GET")) {
statusCode = Response.Status.CONFLICT.getStatusCode();
// Create a new error object
if(org.orcid.jaxb.model.error_v2.OrcidError.class.isAssignableFrom(orcidError.getClass())) {
org.orcid.jaxb.model.error_v2.OrcidError v2Error = (org.orcid.jaxb.model.error_v2.OrcidError) orcidError;
orcidError = orcidCoreExceptionMapper.getDeprecatedOrcidErrorV2(v2Error.getErrorCode(), statusCode, params);
} else {
org.orcid.jaxb.model.v3.release.error.OrcidError v3Error = (org.orcid.jaxb.model.v3.release.error.OrcidError) orcidError;
orcidError = orcidCoreExceptionMapper.getDeprecatedOrcidErrorV3(v3Error.getErrorCode(), statusCode, params);
}
}

Response response = null;
if (location != null) {
response = Response.status(statusCode).header(LOCATION_HEADER, location).entity(orcidError).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,31 @@ public org.orcid.jaxb.model.error_v2.OrcidError getOrcidErrorV2(int errorCode, i
orcidError.setUserMessage(resolveMessage(messageSource.getMessage("apiError." + errorCode + ".userMessage", null, locale), params));
return orcidError;
}


public org.orcid.jaxb.model.error_v2.OrcidError getDeprecatedOrcidErrorV2(int errorCode, int status, Map<String, String> params) {
Locale locale = localeManager.getLocale();
org.orcid.jaxb.model.error_v2.OrcidError orcidError = new org.orcid.jaxb.model.error_v2.OrcidError();
orcidError.setResponseCode(status);
orcidError.setErrorCode(errorCode);
orcidError.setMoreInfo(resolveMessage(messageSource.getMessage("apiError." + errorCode + ".moreInfo", null, locale), params));
String message =resolveMessage(messageSource.getMessage("apiError." + errorCode + ".userMessage", null, locale), params);
orcidError.setDeveloperMessage(message);
orcidError.setUserMessage(message);
return orcidError;
}

public org.orcid.jaxb.model.v3.release.error.OrcidError getDeprecatedOrcidErrorV3(int errorCode, int status, Map<String, String> params) {
Locale locale = localeManager.getLocale();
org.orcid.jaxb.model.v3.release.error.OrcidError orcidError = new org.orcid.jaxb.model.v3.release.error.OrcidError();
orcidError.setResponseCode(status);
orcidError.setErrorCode(errorCode);
orcidError.setMoreInfo(resolveMessage(messageSource.getMessage("apiError." + errorCode + ".moreInfo", null, locale), params));
String message = resolveMessage(messageSource.getMessage("apiError." + errorCode + ".userMessage", null, locale), params);
orcidError.setDeveloperMessage(message);
orcidError.setUserMessage(message);
return orcidError;
}

public org.orcid.jaxb.model.v3.release.error.OrcidError getOrcidErrorV3(int errorCode, int status, Throwable t) {
Locale locale = localeManager.getLocale();
org.orcid.jaxb.model.v3.release.error.OrcidError orcidError = new org.orcid.jaxb.model.v3.release.error.OrcidError();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
*/
public class OrcidDeprecatedException extends ApplicationException {
private static final long serialVersionUID = 1L;

public static final String ORCID = "orcid";
public static final String DEPRECATED_DATE = "deprecated_date";
public static final String DEPRECATED_ORCID = "deprecated_orcid";

public OrcidDeprecatedException() {
super();
}
public OrcidDeprecatedException(Map<String, String> params) {
super(params);
}
super();
}

public OrcidDeprecatedException(Map<String, String> params) {
super(params);
}
}

0 comments on commit 4c4f853

Please sign in to comment.