Search in sources :

Example 1 with OrcidClientNotFoundException

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

the class T2OrcidApiServiceDelegatorImpl method registerWebhook.

/**
 * Register a new webhook to the profile. As with all calls, if the message
 * contains any other elements, a 400 Bad Request will be returned.
 *
 * @param orcid
 *            the identifier of the profile to add the webhook
 * @param uriInfo
 *            an uri object containing the webhook
 * @return If successful, returns a 2xx.
 */
@Override
@AccessControl(requiredScope = ScopePathType.WEBHOOK)
public Response registerWebhook(UriInfo uriInfo, String orcid, String webhookUri) {
    @SuppressWarnings("unused") URI validatedWebhookUri = null;
    try {
        validatedWebhookUri = new URI(webhookUri);
    } catch (URISyntaxException e) {
        Object[] params = { webhookUri };
        throw new OrcidBadRequestException(localeManager.resolveMessage("apiError.badrequest_incorrect_webhook.exception", params));
    }
    ProfileEntity profile = profileEntityCacheManager.retrieve(orcid);
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    ClientDetailsEntity clientDetails = null;
    String clientId = null;
    if (OAuth2Authentication.class.isAssignableFrom(authentication.getClass())) {
        OAuth2Request authorizationRequest = ((OAuth2Authentication) authentication).getOAuth2Request();
        clientId = authorizationRequest.getClientId();
        clientDetails = clientDetailsManager.findByClientId(clientId);
    }
    if (profile != null && clientDetails != null) {
        WebhookEntityPk webhookPk = new WebhookEntityPk(profile, webhookUri);
        WebhookEntity webhook = webhookManager.find(webhookPk);
        boolean isNew = webhook == null;
        if (isNew) {
            webhook = new WebhookEntity();
            webhook.setProfile(profile);
            webhook.setDateCreated(new Date());
            webhook.setEnabled(true);
            webhook.setUri(webhookUri);
            webhook.setClientDetails(clientDetails);
        }
        webhookManager.update(webhook);
        return isNew ? Response.created(uriInfo.getAbsolutePath()).build() : Response.noContent().build();
    } else if (profile == null) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("orcid", orcid);
        throw new OrcidNotFoundException(params);
    } else {
        Map<String, String> params = new HashMap<String, String>();
        params.put("client", clientId);
        throw new OrcidClientNotFoundException(params);
    }
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) WebhookEntityPk(org.orcid.persistence.jpa.entities.keys.WebhookEntityPk) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Date(java.util.Date) SubmissionDate(org.orcid.jaxb.model.message.SubmissionDate) OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OrcidBadRequestException(org.orcid.core.exception.OrcidBadRequestException) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) WebhookEntity(org.orcid.persistence.jpa.entities.WebhookEntity) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) OrcidNotFoundException(org.orcid.core.exception.OrcidNotFoundException) Map(java.util.Map) HashMap(java.util.HashMap) OrcidClientNotFoundException(org.orcid.core.exception.OrcidClientNotFoundException) AccessControl(org.orcid.core.security.visibility.aop.AccessControl)

Aggregations

URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OrcidBadRequestException (org.orcid.core.exception.OrcidBadRequestException)1 OrcidClientNotFoundException (org.orcid.core.exception.OrcidClientNotFoundException)1 OrcidNotFoundException (org.orcid.core.exception.OrcidNotFoundException)1 AccessControl (org.orcid.core.security.visibility.aop.AccessControl)1 SubmissionDate (org.orcid.jaxb.model.message.SubmissionDate)1 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)1 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)1 WebhookEntity (org.orcid.persistence.jpa.entities.WebhookEntity)1 WebhookEntityPk (org.orcid.persistence.jpa.entities.keys.WebhookEntityPk)1 Authentication (org.springframework.security.core.Authentication)1 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)1 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)1