Search in sources :

Example 1 with AbstractManagementException

use of io.gravitee.am.service.exception.AbstractManagementException in project gravitee-access-management by gravitee-io.

the class ErrorHandler method handle.

@Override
public void handle(RoutingContext routingContext) {
    if (routingContext.failed()) {
        Throwable throwable = routingContext.failure();
        // management exception (resource not found, server error, ...)
        if (throwable instanceof AbstractManagementException) {
            AbstractManagementException technicalManagementException = (AbstractManagementException) throwable;
            handleException(routingContext, technicalManagementException.getHttpStatusCode(), technicalManagementException.getMessage());
        // oauth2 exception (token invalid exception)
        } else if (throwable instanceof OAuth2Exception) {
            OAuth2Exception oAuth2Exception = (OAuth2Exception) throwable;
            handleException(routingContext, oAuth2Exception.getHttpStatusCode(), oAuth2Exception.getMessage());
        } else if (throwable instanceof PolicyChainException) {
            PolicyChainException policyChainException = (PolicyChainException) throwable;
            handleException(routingContext, policyChainException.statusCode(), policyChainException.key() + " : " + policyChainException.getMessage());
        } else if (throwable instanceof HttpException) {
            HttpException httpStatusException = (HttpException) throwable;
            handleException(routingContext, httpStatusException.getStatusCode(), httpStatusException.getPayload());
        } else {
            logger.error(throwable.getMessage(), throwable);
            if (routingContext.statusCode() != -1) {
                routingContext.response().setStatusCode(routingContext.statusCode()).end();
            } else {
                routingContext.response().setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end();
            }
        }
    }
}
Also used : AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) HttpException(io.vertx.ext.web.handler.HttpException) PolicyChainException(io.gravitee.am.gateway.policy.PolicyChainException) OAuth2Exception(io.gravitee.am.common.exception.oauth2.OAuth2Exception)

Example 2 with AbstractManagementException

use of io.gravitee.am.service.exception.AbstractManagementException in project gravitee-access-management by gravitee-io.

the class ErrorHandler method handle.

@Override
public void handle(RoutingContext routingContext) {
    if (routingContext.failed()) {
        Throwable throwable = routingContext.failure();
        // management exception (resource not found, server error, ...)
        if (throwable instanceof AbstractManagementException) {
            AbstractManagementException technicalManagementException = (AbstractManagementException) throwable;
            handleException(routingContext, "technical_error", technicalManagementException.getMessage());
        // oauth2 exception (token invalid exception)
        } else if (throwable instanceof OAuth2Exception) {
            OAuth2Exception oAuth2Exception = (OAuth2Exception) throwable;
            handleException(routingContext, oAuth2Exception.getOAuth2ErrorCode(), oAuth2Exception.getMessage());
        } else if (throwable instanceof PolicyChainException) {
            PolicyChainException policyChainException = (PolicyChainException) throwable;
            handleException(routingContext, policyChainException.key(), policyChainException.getMessage());
        } else if (throwable instanceof HttpException) {
            HttpException httpStatusException = (HttpException) throwable;
            handleException(routingContext, httpStatusException.getMessage(), httpStatusException.getPayload());
        } else {
            logger.error("An exception occurs while handling incoming request", throwable);
            if (routingContext.statusCode() != -1) {
                routingContext.response().setStatusCode(routingContext.statusCode()).end();
            } else {
                routingContext.response().setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end();
            }
        }
    }
}
Also used : AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) HttpException(io.vertx.ext.web.handler.HttpException) PolicyChainException(io.gravitee.am.gateway.policy.PolicyChainException) OAuth2Exception(io.gravitee.am.common.exception.oauth2.OAuth2Exception)

Example 3 with AbstractManagementException

use of io.gravitee.am.service.exception.AbstractManagementException in project gravitee-access-management by gravitee-io.

the class ServiceResourceServiceImpl method create.

@Override
public Single<ServiceResource> create(String domain, NewServiceResource newServiceResource, User principal) {
    LOGGER.debug("Create a new resource {} for domain {}", newServiceResource, domain);
    ServiceResource resource = new ServiceResource();
    resource.setId(newServiceResource.getId() == null ? RandomString.generate() : newServiceResource.getId());
    resource.setReferenceId(domain);
    resource.setReferenceType(ReferenceType.DOMAIN);
    resource.setName(newServiceResource.getName());
    resource.setType(newServiceResource.getType());
    resource.setConfiguration(newServiceResource.getConfiguration());
    resource.setCreatedAt(new Date());
    resource.setUpdatedAt(resource.getCreatedAt());
    return serviceResourceRepository.create(resource).flatMap(resource1 -> {
        // send sync event to refresh plugins that are using this resource
        Event event = new Event(Type.RESOURCE, new Payload(resource1.getId(), resource1.getReferenceType(), resource1.getReferenceId(), Action.CREATE));
        return eventService.create(event).flatMap(__ -> Single.just(resource1));
    }).onErrorResumeNext(ex -> {
        if (ex instanceof AbstractManagementException) {
            return Single.error(ex);
        }
        LOGGER.error("An error occurs while trying to create a resource", ex);
        return Single.error(new TechnicalManagementException("An error occurs while trying to create a resource", ex));
    });
}
Also used : Date(java.util.Date) NewServiceResource(io.gravitee.am.service.model.NewServiceResource) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.am.service.AuditService) Single(io.reactivex.Single) Event(io.gravitee.am.model.common.event.Event) Type(io.gravitee.am.common.event.Type) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Flowable(io.reactivex.Flowable) User(io.gravitee.am.identityprovider.api.User) AuditBuilder(io.gravitee.am.service.reporter.builder.AuditBuilder) ReferenceType(io.gravitee.am.model.ReferenceType) EventService(io.gravitee.am.service.EventService) ServiceResourceAuditBuilder(io.gravitee.am.service.reporter.builder.management.ServiceResourceAuditBuilder) ServiceResourceNotFoundException(io.gravitee.am.service.exception.ServiceResourceNotFoundException) Action(io.gravitee.am.common.event.Action) ServiceResourceService(io.gravitee.am.service.ServiceResourceService) Logger(org.slf4j.Logger) ServiceResourceCurrentlyUsedException(io.gravitee.am.service.exception.ServiceResourceCurrentlyUsedException) ServiceResource(io.gravitee.am.model.resource.ServiceResource) EventType(io.gravitee.am.common.audit.EventType) FactorService(io.gravitee.am.service.FactorService) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) RandomString(io.gravitee.am.common.utils.RandomString) Component(org.springframework.stereotype.Component) Payload(io.gravitee.am.model.common.event.Payload) UpdateServiceResource(io.gravitee.am.service.model.UpdateServiceResource) Lazy(org.springframework.context.annotation.Lazy) ServiceResourceRepository(io.gravitee.am.repository.management.api.ServiceResourceRepository) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) Event(io.gravitee.am.model.common.event.Event) NewServiceResource(io.gravitee.am.service.model.NewServiceResource) ServiceResource(io.gravitee.am.model.resource.ServiceResource) UpdateServiceResource(io.gravitee.am.service.model.UpdateServiceResource) Payload(io.gravitee.am.model.common.event.Payload) Date(java.util.Date) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException)

Example 4 with AbstractManagementException

use of io.gravitee.am.service.exception.AbstractManagementException in project gravitee-access-management by gravitee-io.

the class TagServiceImpl method create.

@Override
public Single<Tag> create(NewTag newTag, String organizationId, User principal) {
    LOGGER.debug("Create a new tag: {}", newTag);
    String id = humanReadableId(newTag.getName());
    return tagRepository.findById(id, organizationId).isEmpty().flatMap(empty -> {
        if (!empty) {
            throw new TagAlreadyExistsException(newTag.getName());
        } else {
            Tag tag = new Tag();
            tag.setId(id);
            tag.setOrganizationId(organizationId);
            tag.setName(newTag.getName());
            tag.setDescription(newTag.getDescription());
            tag.setCreatedAt(new Date());
            tag.setUpdatedAt(tag.getCreatedAt());
            return tagRepository.create(tag);
        }
    }).onErrorResumeNext(ex -> {
        if (ex instanceof AbstractManagementException) {
            return Single.error(ex);
        }
        LOGGER.error("An error occurs while trying to create a tag", ex);
        return Single.error(new TechnicalManagementException("An error occurs while trying to create a tag", ex));
    }).doOnSuccess(tag -> auditService.report(AuditBuilder.builder(TagAuditBuilder.class).tag(tag).principal(principal).type(EventType.TAG_CREATED))).doOnError(throwable -> auditService.report(AuditBuilder.builder(TagAuditBuilder.class).referenceId(organizationId).principal(principal).type(EventType.TAG_CREATED).throwable(throwable)));
}
Also used : Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) DomainService(io.gravitee.am.service.DomainService) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.am.service.AuditService) TagNotFoundException(io.gravitee.am.service.exception.TagNotFoundException) TagRepository(io.gravitee.am.repository.management.api.TagRepository) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) TagAuditBuilder(io.gravitee.am.service.reporter.builder.management.TagAuditBuilder) User(io.gravitee.am.identityprovider.api.User) NewTag(io.gravitee.am.service.model.NewTag) AuditBuilder(io.gravitee.am.service.reporter.builder.AuditBuilder) TagAlreadyExistsException(io.gravitee.am.service.exception.TagAlreadyExistsException) io.reactivex(io.reactivex) TagService(io.gravitee.am.service.TagService) Tag(io.gravitee.am.model.Tag) Logger(org.slf4j.Logger) UpdateTag(io.gravitee.am.service.model.UpdateTag) Set(java.util.Set) EventType(io.gravitee.am.common.audit.EventType) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) Normalizer(java.text.Normalizer) Component(org.springframework.stereotype.Component) Lazy(org.springframework.context.annotation.Lazy) Pattern(java.util.regex.Pattern) TagAlreadyExistsException(io.gravitee.am.service.exception.TagAlreadyExistsException) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) NewTag(io.gravitee.am.service.model.NewTag) Tag(io.gravitee.am.model.Tag) UpdateTag(io.gravitee.am.service.model.UpdateTag) Date(java.util.Date) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException)

Example 5 with AbstractManagementException

use of io.gravitee.am.service.exception.AbstractManagementException in project gravitee-access-management by gravitee-io.

the class EmailTemplateServiceImpl method create0.

private Single<Email> create0(ReferenceType referenceType, String referenceId, String client, NewEmail newEmail, User principal) {
    String emailId = RandomString.generate();
    // check if email is unique
    return checkEmailUniqueness(referenceType, referenceId, client, newEmail.getTemplate().template()).flatMap(irrelevant -> {
        Email email = new Email();
        email.setId(emailId);
        email.setReferenceType(referenceType);
        email.setReferenceId(referenceId);
        email.setClient(client);
        email.setEnabled(newEmail.isEnabled());
        email.setTemplate(newEmail.getTemplate().template());
        email.setFrom(newEmail.getFrom());
        email.setFromName(newEmail.getFromName());
        email.setSubject(newEmail.getSubject());
        email.setContent(newEmail.getContent());
        email.setExpiresAfter(newEmail.getExpiresAfter());
        email.setCreatedAt(new Date());
        email.setUpdatedAt(email.getCreatedAt());
        return emailRepository.create(email);
    }).flatMap(email -> {
        // create event for sync process
        Event event = new Event(Type.EMAIL, new Payload(email.getId(), email.getReferenceType(), email.getReferenceId(), Action.CREATE));
        return eventService.create(event).flatMap(__ -> Single.just(email));
    }).onErrorResumeNext(ex -> {
        if (ex instanceof AbstractManagementException) {
            return Single.error(ex);
        }
        LOGGER.error("An error occurs while trying to create a email", ex);
        return Single.error(new TechnicalManagementException("An error occurs while trying to create a email", ex));
    }).doOnSuccess(email -> auditService.report(AuditBuilder.builder(EmailTemplateAuditBuilder.class).principal(principal).type(EventType.EMAIL_TEMPLATE_CREATED).email(email))).doOnError(throwable -> auditService.report(AuditBuilder.builder(EmailTemplateAuditBuilder.class).principal(principal).type(EventType.EMAIL_TEMPLATE_CREATED).throwable(throwable)));
}
Also used : EmailAlreadyExistsException(io.gravitee.am.service.exception.EmailAlreadyExistsException) Date(java.util.Date) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.am.service.AuditService) Single(io.reactivex.Single) Event(io.gravitee.am.model.common.event.Event) Type(io.gravitee.am.common.event.Type) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Flowable(io.reactivex.Flowable) User(io.gravitee.am.identityprovider.api.User) AuditBuilder(io.gravitee.am.service.reporter.builder.AuditBuilder) ReferenceType(io.gravitee.am.model.ReferenceType) EventService(io.gravitee.am.service.EventService) Email(io.gravitee.am.model.Email) Action(io.gravitee.am.common.event.Action) Logger(org.slf4j.Logger) EmailTemplateService(io.gravitee.am.service.EmailTemplateService) EventType(io.gravitee.am.common.audit.EventType) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) RandomString(io.gravitee.am.common.utils.RandomString) EmailTemplateAuditBuilder(io.gravitee.am.service.reporter.builder.management.EmailTemplateAuditBuilder) UpdateEmail(io.gravitee.am.service.model.UpdateEmail) NewEmail(io.gravitee.am.service.model.NewEmail) Component(org.springframework.stereotype.Component) Template(io.gravitee.am.model.Template) Payload(io.gravitee.am.model.common.event.Payload) EmailNotFoundException(io.gravitee.am.service.exception.EmailNotFoundException) EmailRepository(io.gravitee.am.repository.management.api.EmailRepository) Lazy(org.springframework.context.annotation.Lazy) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) Email(io.gravitee.am.model.Email) UpdateEmail(io.gravitee.am.service.model.UpdateEmail) NewEmail(io.gravitee.am.service.model.NewEmail) EmailTemplateAuditBuilder(io.gravitee.am.service.reporter.builder.management.EmailTemplateAuditBuilder) Event(io.gravitee.am.model.common.event.Event) Payload(io.gravitee.am.model.common.event.Payload) RandomString(io.gravitee.am.common.utils.RandomString) Date(java.util.Date) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException)

Aggregations

AbstractManagementException (io.gravitee.am.service.exception.AbstractManagementException)22 TechnicalManagementException (io.gravitee.am.service.exception.TechnicalManagementException)18 Autowired (org.springframework.beans.factory.annotation.Autowired)17 Logger (org.slf4j.Logger)15 LoggerFactory (org.slf4j.LoggerFactory)15 Maybe (io.reactivex.Maybe)13 Single (io.reactivex.Single)13 Completable (io.reactivex.Completable)12 EventType (io.gravitee.am.common.audit.EventType)11 AuditBuilder (io.gravitee.am.service.reporter.builder.AuditBuilder)11 Date (java.util.Date)11 Lazy (org.springframework.context.annotation.Lazy)11 Component (org.springframework.stereotype.Component)11 Action (io.gravitee.am.common.event.Action)10 ReferenceType (io.gravitee.am.model.ReferenceType)10 Event (io.gravitee.am.model.common.event.Event)10 Payload (io.gravitee.am.model.common.event.Payload)10 AuditService (io.gravitee.am.service.AuditService)10 Type (io.gravitee.am.common.event.Type)9 RandomString (io.gravitee.am.common.utils.RandomString)9