Search in sources :

Example 6 with OrcidDeprecatedException

use of org.orcid.core.exception.OrcidDeprecatedException in project ORCID-Source by ORCID.

the class T2OrcidApiServiceVersionedDelegatorImpl method checkDeprecation.

/**
     * Checks if an account is deprecated
     * 
     * @param orcidMessage
     *            OrcidMessage, for it we can get the orcid to check for
     *            deprecation
     * @throws DeprecatedException
     *             if the account is deprecated
     * */
private void checkDeprecation(String orcid) {
    ProfileEntity entity = profileEntityCacheManager.retrieve(orcid);
    if (entity != null) {
        if (entity.getDeprecatedDate() != null) {
            Map<String, String> params = new HashMap<String, String>();
            StringBuffer primary = new StringBuffer(orcidUrlManager.getBaseUrl()).append("/").append(entity.getPrimaryRecord().getId());
            params.put(OrcidDeprecatedException.ORCID, primary.toString());
            if (entity.getDeprecatedDate() != null) {
                XMLGregorianCalendar calendar = DateUtils.convertToXMLGregorianCalendar(entity.getDeprecatedDate());
                params.put(OrcidDeprecatedException.DEPRECATED_DATE, calendar.toString());
            }
            throw new OrcidDeprecatedException(params);
        }
    }
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) HashMap(java.util.HashMap) OrcidDeprecatedException(org.orcid.core.exception.OrcidDeprecatedException) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity)

Example 7 with OrcidDeprecatedException

use of org.orcid.core.exception.OrcidDeprecatedException in project ORCID-Source by ORCID.

the class OrcidExceptionMapper method getOrcidErrorResponse.

private Response getOrcidErrorResponse(Object orcidError, Throwable t) {
    int statusCode = 0;
    if (org.orcid.jaxb.model.error_rc1.OrcidError.class.isAssignableFrom(orcidError.getClass())) {
        statusCode = ((org.orcid.jaxb.model.error_rc1.OrcidError) orcidError).getResponseCode();
    } else if (org.orcid.jaxb.model.error_rc2.OrcidError.class.isAssignableFrom(orcidError.getClass())) {
        statusCode = ((org.orcid.jaxb.model.error_rc2.OrcidError) orcidError).getResponseCode();
    } else if (org.orcid.jaxb.model.error_rc3.OrcidError.class.isAssignableFrom(orcidError.getClass())) {
        statusCode = ((org.orcid.jaxb.model.error_rc3.OrcidError) orcidError).getResponseCode();
    } else if (org.orcid.jaxb.model.error_rc4.OrcidError.class.isAssignableFrom(orcidError.getClass())) {
        statusCode = ((org.orcid.jaxb.model.error_rc4.OrcidError) orcidError).getResponseCode();
    } else if (org.orcid.jaxb.model.error_v2.OrcidError.class.isAssignableFrom(orcidError.getClass())) {
        statusCode = ((org.orcid.jaxb.model.error_v2.OrcidError) orcidError).getResponseCode();
    }
    if (OrcidDeprecatedException.class.isAssignableFrom(t.getClass())) {
        OrcidDeprecatedException exception = (OrcidDeprecatedException) t;
        Map<String, String> params = exception.getParams();
        String location = null;
        if (params != null) {
            if (params.containsKey(OrcidDeprecatedException.ORCID)) {
                location = getPrimaryRecordLocation(params);
            }
        }
        Response response = null;
        if (location != null) {
            response = Response.status(statusCode).header(LOCATION_HEADER, location).entity(orcidError).build();
        } else {
            response = Response.status(statusCode).entity(orcidError).build();
        }
        return response;
    }
    return Response.status(statusCode).entity(orcidError).build();
}
Also used : Response(javax.ws.rs.core.Response) OrcidDeprecatedException(org.orcid.core.exception.OrcidDeprecatedException)

Example 8 with OrcidDeprecatedException

use of org.orcid.core.exception.OrcidDeprecatedException in project ORCID-Source by ORCID.

the class OrcidExceptionMapper method legacyErrorResponse.

private Response legacyErrorResponse(Throwable t) {
    if (OrcidApiException.class.isAssignableFrom(t.getClass())) {
        return ((OrcidApiException) t).getResponse();
    } else if (OrcidValidationException.class.isAssignableFrom(t.getClass())) {
        OrcidMessage entity = getLegacyOrcidEntity("Bad Request: ", t);
        return Response.status(Response.Status.BAD_REQUEST).entity(entity).build();
    } else if (NotFoundException.class.isAssignableFrom(t.getClass())) {
        OrcidMessage entity = getLegacyOrcidEntity("Please specify a version number (1.2 or higher) : ", t);
        return Response.status(OrcidCoreExceptionMapper.getHttpStatusAndErrorCode(t).getKey()).entity(entity).build();
    } else if (WebApplicationException.class.isAssignableFrom(t.getClass())) {
        OrcidMessage entity = getLegacy500OrcidEntity(t);
        WebApplicationException webException = (WebApplicationException) t;
        return Response.status(webException.getResponse().getStatus()).entity(entity).build();
    } else if (AuthenticationException.class.isAssignableFrom(t.getClass())) {
        OrcidMessage entity = getLegacyOrcidEntity("Authentication problem : ", t);
        return Response.status(Response.Status.UNAUTHORIZED).entity(entity).build();
    } else if (OAuth2Exception.class.isAssignableFrom(t.getClass())) {
        OrcidMessage entity = getLegacyOrcidEntity("OAuth2 problem : ", t);
        return Response.status(Response.Status.UNAUTHORIZED).entity(entity).build();
    } else if (OrcidInvalidScopeException.class.isAssignableFrom(t.getClass())) {
        OrcidMessage entity = getLegacyOrcidEntity("OAuth2 problem : ", t);
        return Response.status(Response.Status.UNAUTHORIZED).entity(entity).build();
    } else if (SecurityException.class.isAssignableFrom(t.getClass())) {
        OrcidMessage entity = getLegacyOrcidEntity("Security problem : ", t);
        return Response.status(Response.Status.FORBIDDEN).entity(entity).build();
    } else if (IllegalStateException.class.isAssignableFrom(t.getClass())) {
        OrcidMessage entity = getLegacyOrcidEntity("Illegal state : ", t);
        return Response.status(Response.Status.FORBIDDEN).entity(entity).build();
    } else if (IllegalArgumentException.class.isAssignableFrom(t.getClass())) {
        OrcidMessage entity = getLegacyOrcidEntity("Bad Request : ", t);
        return Response.status(Response.Status.BAD_REQUEST).entity(entity).build();
    } else if (OrcidDeprecatedException.class.isAssignableFrom(t.getClass())) {
        OrcidDeprecatedException exception = (OrcidDeprecatedException) t;
        OrcidDeprecated depreciatedError = new OrcidDeprecated();
        Map<String, String> params = exception.getParams();
        String location = null;
        if (params != null) {
            if (params.containsKey(OrcidDeprecatedException.ORCID)) {
                PrimaryRecord pr = new PrimaryRecord();
                pr.setOrcid(new Orcid(params.get(OrcidDeprecatedException.ORCID)));
                depreciatedError.setPrimaryRecord(pr);
                location = getPrimaryRecordLocation(params);
            }
            if (params.containsKey(OrcidDeprecatedException.DEPRECATED_DATE)) {
                DeprecatedDate dd = new DeprecatedDate();
                String dateString = params.get(OrcidDeprecatedException.DEPRECATED_DATE);
                dd.setValue(DateUtils.convertToXMLGregorianCalendar(dateString, false));
                depreciatedError.setDate(dd);
            }
        }
        Response response = null;
        if (location != null) {
            response = Response.status(Response.Status.MOVED_PERMANENTLY).header(LOCATION_HEADER, location).entity(depreciatedError).build();
        } else {
            response = Response.status(Response.Status.MOVED_PERMANENTLY).entity(depreciatedError).build();
        }
        return response;
    } else if (LockedException.class.isAssignableFrom(t.getClass())) {
        OrcidMessage entity = getLegacyOrcidEntity("Account locked : ", t);
        return Response.status(Response.Status.CONFLICT).entity(entity).build();
    } else if (NoResultException.class.isAssignableFrom(t.getClass())) {
        OrcidMessage entity = getLegacyOrcidEntity("Not found : ", t);
        return Response.status(Response.Status.NOT_FOUND).entity(entity).build();
    } else if (ExceedMaxNumberOfElementsException.class.isAssignableFrom(t.getClass())) {
        OrcidMessage entity = getLegacyOrcidEntity("This version of the API does not support adding more than 10,000 works to a record. Please consider using the 2.0 API.", null);
        return Response.status(Response.Status.CONFLICT).entity(entity).build();
    } else {
        OrcidMessage entity = getLegacy500OrcidEntity(t);
        return Response.status(OrcidCoreExceptionMapper.getHttpStatusAndErrorCode(t).getKey()).entity(entity).build();
    }
}
Also used : PrimaryRecord(org.orcid.jaxb.model.message.PrimaryRecord) LockedException(org.orcid.core.security.aop.LockedException) WebApplicationException(javax.ws.rs.WebApplicationException) ExceedMaxNumberOfElementsException(org.orcid.core.exception.ExceedMaxNumberOfElementsException) OrcidValidationException(org.orcid.core.exception.OrcidValidationException) Response(javax.ws.rs.core.Response) OrcidDeprecated(org.orcid.jaxb.model.message.OrcidDeprecated) Orcid(org.orcid.jaxb.model.message.Orcid) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) OrcidDeprecatedException(org.orcid.core.exception.OrcidDeprecatedException) DeprecatedDate(org.orcid.jaxb.model.message.DeprecatedDate) OrcidApiException(org.orcid.core.exception.OrcidApiException) OAuth2Exception(org.springframework.security.oauth2.common.exceptions.OAuth2Exception)

Example 9 with OrcidDeprecatedException

use of org.orcid.core.exception.OrcidDeprecatedException in project ORCID-Source by ORCID.

the class OrcidApiServiceDelegatorImpl method getOrcidMessageResponse.

private Response getOrcidMessageResponse(OrcidMessage orcidMessage, String requestedOrcid) {
    boolean isProfileDeprecated = false;
    if (orcidMessage == null) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("orcid", requestedOrcid);
        throw new OrcidNotFoundException(params);
    }
    OrcidProfile orcidProfile = orcidMessage.getOrcidProfile();
    if (orcidProfile != null) {
        orcidProfile.setOrcidInternal(null);
        // If profile is deprecated
        if (orcidMessage.getOrcidProfile().getOrcidDeprecated() != null) {
            isProfileDeprecated = true;
        }
    }
    Response response = null;
    if (isProfileDeprecated) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(OrcidDeprecatedException.ORCID, orcidProfile.getOrcidDeprecated().getPrimaryRecord().getOrcidIdentifier().getUri());
        if (orcidProfile.getOrcidDeprecated().getDate() != null) {
            XMLGregorianCalendar deprecatedDate = orcidProfile.getOrcidDeprecated().getDate().getValue();
            params.put(OrcidDeprecatedException.DEPRECATED_DATE, deprecatedDate.toString());
        }
        throw new OrcidDeprecatedException(params);
    } else {
        orcidMessageUtil.setSourceName(orcidMessage);
        response = Response.ok(orcidMessage).build();
    }
    return response;
}
Also used : OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) Response(javax.ws.rs.core.Response) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) HashMap(java.util.HashMap) OrcidDeprecatedException(org.orcid.core.exception.OrcidDeprecatedException) OrcidNotFoundException(org.orcid.core.exception.OrcidNotFoundException)

Aggregations

OrcidDeprecatedException (org.orcid.core.exception.OrcidDeprecatedException)9 HashMap (java.util.HashMap)7 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)5 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)5 Response (javax.ws.rs.core.Response)3 LockedException (org.orcid.core.security.aop.LockedException)3 OrcidNotClaimedException (org.orcid.core.exception.OrcidNotClaimedException)2 OrcidMessage (org.orcid.jaxb.model.message.OrcidMessage)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 AccessControlException (java.security.AccessControlException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 NoResultException (javax.persistence.NoResultException)1 HttpSession (javax.servlet.http.HttpSession)1 WebApplicationException (javax.ws.rs.WebApplicationException)1