Search in sources :

Example 6 with Metered

use of com.codahale.metrics.annotation.Metered in project cas by apereo.

the class DefaultCentralAuthenticationService method validateServiceTicket.

@Audit(action = "SERVICE_TICKET_VALIDATE", actionResolverName = "VALIDATE_SERVICE_TICKET_RESOLVER", resourceResolverName = "VALIDATE_SERVICE_TICKET_RESOURCE_RESOLVER")
@Timed(name = "VALIDATE_SERVICE_TICKET_TIMER")
@Metered(name = "VALIDATE_SERVICE_TICKET_METER")
@Counted(name = "VALIDATE_SERVICE_TICKET_COUNTER", monotonic = true)
@Override
public Assertion validateServiceTicket(final String serviceTicketId, final Service service) throws AbstractTicketException {
    if (!isTicketAuthenticityVerified(serviceTicketId)) {
        LOGGER.info("Service ticket [{}] is not a valid ticket issued by CAS.", serviceTicketId);
        throw new InvalidTicketException(serviceTicketId);
    }
    final ServiceTicket serviceTicket = this.ticketRegistry.getTicket(serviceTicketId, ServiceTicket.class);
    if (serviceTicket == null) {
        LOGGER.info("Service ticket [{}] does not exist.", serviceTicketId);
        throw new InvalidTicketException(serviceTicketId);
    }
    try {
        /*
             * Synchronization on ticket object in case of cache based registry doesn't serialize
             * access to critical section. The reason is that cache pulls serialized data and
             * builds new object, most likely for each pull. Is this synchronization needed here?
             */
        synchronized (serviceTicket) {
            if (serviceTicket.isExpired()) {
                LOGGER.info("ServiceTicket [{}] has expired.", serviceTicketId);
                throw new InvalidTicketException(serviceTicketId);
            }
            if (!serviceTicket.isValidFor(service)) {
                LOGGER.error("Service ticket [{}] with service [{}] does not match supplied service [{}]", serviceTicketId, serviceTicket.getService().getId(), service);
                throw new UnrecognizableServiceForServiceTicketValidationException(serviceTicket.getService());
            }
        }
        final Service selectedService = resolveServiceFromAuthenticationRequest(service);
        LOGGER.debug("Resolved service [{}] from the authentication request", selectedService);
        final RegisteredService registeredService = this.servicesManager.findServiceBy(selectedService);
        LOGGER.debug("Located registered service definition [{}] from [{}] to handle validation request", registeredService, selectedService);
        RegisteredServiceAccessStrategyUtils.ensureServiceAccessIsAllowed(selectedService, registeredService);
        final TicketGrantingTicket root = serviceTicket.getGrantingTicket().getRoot();
        final Authentication authentication = getAuthenticationSatisfiedByPolicy(root.getAuthentication(), new ServiceContext(selectedService, registeredService));
        final Principal principal = authentication.getPrincipal();
        final RegisteredServiceAttributeReleasePolicy attributePolicy = registeredService.getAttributeReleasePolicy();
        LOGGER.debug("Attribute policy [{}] is associated with service [{}]", attributePolicy, registeredService);
        @SuppressWarnings("unchecked") final Map<String, Object> attributesToRelease = attributePolicy != null ? attributePolicy.getAttributes(principal, registeredService) : new HashMap<>();
        final String principalId = registeredService.getUsernameAttributeProvider().resolveUsername(principal, selectedService);
        final Principal modifiedPrincipal = this.principalFactory.createPrincipal(principalId, attributesToRelease);
        final AuthenticationBuilder builder = DefaultAuthenticationBuilder.newInstance(authentication);
        builder.setPrincipal(modifiedPrincipal);
        final Authentication finalAuthentication = builder.build();
        AuthenticationCredentialsLocalBinder.bindCurrent(finalAuthentication);
        final Assertion assertion = new ImmutableAssertion(finalAuthentication, serviceTicket.getGrantingTicket().getChainedAuthentications(), selectedService, serviceTicket.isFromNewLogin());
        doPublishEvent(new CasServiceTicketValidatedEvent(this, serviceTicket, assertion));
        return assertion;
    } finally {
        if (serviceTicket.isExpired()) {
            this.ticketRegistry.deleteTicket(serviceTicketId);
        } else {
            this.ticketRegistry.updateTicket(serviceTicket);
        }
    }
}
Also used : RegisteredService(org.apereo.cas.services.RegisteredService) DefaultAuthenticationBuilder(org.apereo.cas.authentication.DefaultAuthenticationBuilder) AuthenticationBuilder(org.apereo.cas.authentication.AuthenticationBuilder) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) ServiceContext(org.apereo.cas.services.ServiceContext) UnrecognizableServiceForServiceTicketValidationException(org.apereo.cas.ticket.UnrecognizableServiceForServiceTicketValidationException) Assertion(org.apereo.cas.validation.Assertion) ImmutableAssertion(org.apereo.cas.validation.ImmutableAssertion) RegisteredService(org.apereo.cas.services.RegisteredService) Service(org.apereo.cas.authentication.principal.Service) ServiceTicket(org.apereo.cas.ticket.ServiceTicket) ImmutableAssertion(org.apereo.cas.validation.ImmutableAssertion) Authentication(org.apereo.cas.authentication.Authentication) CasServiceTicketValidatedEvent(org.apereo.cas.support.events.ticket.CasServiceTicketValidatedEvent) InvalidTicketException(org.apereo.cas.ticket.InvalidTicketException) Principal(org.apereo.cas.authentication.principal.Principal) RegisteredServiceAttributeReleasePolicy(org.apereo.cas.services.RegisteredServiceAttributeReleasePolicy) Audit(org.apereo.inspektr.audit.annotation.Audit) Counted(com.codahale.metrics.annotation.Counted) Metered(com.codahale.metrics.annotation.Metered) Timed(com.codahale.metrics.annotation.Timed)

Example 7 with Metered

use of com.codahale.metrics.annotation.Metered in project dropwizard by dropwizard.

the class TaskServlet method add.

public void add(Task task) {
    tasks.put('/' + task.getName(), task);
    TaskExecutor taskExecutor = new TaskExecutor(task);
    try {
        final Method executeMethod = task.getClass().getMethod("execute", ImmutableMultimap.class, PrintWriter.class);
        if (executeMethod.isAnnotationPresent(Timed.class)) {
            final Timed annotation = executeMethod.getAnnotation(Timed.class);
            final String name = chooseName(annotation.name(), annotation.absolute(), task);
            taskExecutor = new TimedTask(taskExecutor, metricRegistry.timer(name));
        }
        if (executeMethod.isAnnotationPresent(Metered.class)) {
            final Metered annotation = executeMethod.getAnnotation(Metered.class);
            final String name = chooseName(annotation.name(), annotation.absolute(), task);
            taskExecutor = new MeteredTask(taskExecutor, metricRegistry.meter(name));
        }
        if (executeMethod.isAnnotationPresent(ExceptionMetered.class)) {
            final ExceptionMetered annotation = executeMethod.getAnnotation(ExceptionMetered.class);
            final String name = chooseName(annotation.name(), annotation.absolute(), task, ExceptionMetered.DEFAULT_NAME_SUFFIX);
            taskExecutor = new ExceptionMeteredTask(taskExecutor, metricRegistry.meter(name), annotation.cause());
        }
    } catch (NoSuchMethodException ignored) {
    }
    taskExecutors.put(task, taskExecutor);
}
Also used : ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) Metered(com.codahale.metrics.annotation.Metered) Timed(com.codahale.metrics.annotation.Timed) Method(java.lang.reflect.Method) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 8 with Metered

use of com.codahale.metrics.annotation.Metered in project metrics by dropwizard.

the class InstrumentedResourceMethodApplicationListener method registerMetricsForModel.

private void registerMetricsForModel(ResourceModel resourceModel) {
    for (final Resource resource : resourceModel.getResources()) {
        final Timed classLevelTimed = getClassLevelAnnotation(resource, Timed.class);
        final Metered classLevelMetered = getClassLevelAnnotation(resource, Metered.class);
        final ExceptionMetered classLevelExceptionMetered = getClassLevelAnnotation(resource, ExceptionMetered.class);
        for (final ResourceMethod method : resource.getAllMethods()) {
            registerTimedAnnotations(method, classLevelTimed);
            registerMeteredAnnotations(method, classLevelMetered);
            registerExceptionMeteredAnnotations(method, classLevelExceptionMetered);
        }
        for (final Resource childResource : resource.getChildResources()) {
            final Timed classLevelTimedChild = getClassLevelAnnotation(childResource, Timed.class);
            final Metered classLevelMeteredChild = getClassLevelAnnotation(childResource, Metered.class);
            final ExceptionMetered classLevelExceptionMeteredChild = getClassLevelAnnotation(childResource, ExceptionMetered.class);
            for (final ResourceMethod method : childResource.getAllMethods()) {
                registerTimedAnnotations(method, classLevelTimedChild);
                registerMeteredAnnotations(method, classLevelMeteredChild);
                registerExceptionMeteredAnnotations(method, classLevelExceptionMeteredChild);
            }
        }
    }
}
Also used : Metered(com.codahale.metrics.annotation.Metered) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) Timed(com.codahale.metrics.annotation.Timed) Resource(org.glassfish.jersey.server.model.Resource) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 9 with Metered

use of com.codahale.metrics.annotation.Metered in project metrics by dropwizard.

the class InstrumentedResourceMethodApplicationListener method registerMeteredAnnotations.

private void registerMeteredAnnotations(final ResourceMethod method, final Metered classLevelMetered) {
    final Method definitionMethod = method.getInvocable().getDefinitionMethod();
    if (classLevelMetered != null) {
        meters.putIfAbsent(definitionMethod, meterMetric(metrics, method, classLevelMetered));
        return;
    }
    final Metered annotation = definitionMethod.getAnnotation(Metered.class);
    if (annotation != null) {
        meters.putIfAbsent(definitionMethod, meterMetric(metrics, method, annotation));
    }
}
Also used : Metered(com.codahale.metrics.annotation.Metered) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Method(java.lang.reflect.Method)

Example 10 with Metered

use of com.codahale.metrics.annotation.Metered in project cas by apereo.

the class AbstractCentralAuthenticationService method getTicket.

/**
     * {@inheritDoc}
     * <p>
     * Note:
     * Synchronization on ticket object in case of cache based registry doesn't serialize
     * access to critical section. The reason is that cache pulls serialized data and
     * builds new object, most likely for each pull. Is this synchronization needed here?
     */
@Transactional(transactionManager = "ticketTransactionManager", noRollbackFor = InvalidTicketException.class)
@Timed(name = "GET_TICKET_TIMER")
@Metered(name = "GET_TICKET_METER")
@Counted(name = "GET_TICKET_COUNTER", monotonic = true)
@Override
public <T extends Ticket> T getTicket(final String ticketId, final Class<T> clazz) throws InvalidTicketException {
    Assert.notNull(ticketId, "ticketId cannot be null");
    final Ticket ticket = this.ticketRegistry.getTicket(ticketId, clazz);
    verifyTicketState(ticket, ticketId, clazz);
    return (T) ticket;
}
Also used : TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) Ticket(org.apereo.cas.ticket.Ticket) Counted(com.codahale.metrics.annotation.Counted) Metered(com.codahale.metrics.annotation.Metered) Timed(com.codahale.metrics.annotation.Timed) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Metered (com.codahale.metrics.annotation.Metered)13 Timed (com.codahale.metrics.annotation.Timed)12 Counted (com.codahale.metrics.annotation.Counted)9 Audit (org.apereo.inspektr.audit.annotation.Audit)7 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)6 Authentication (org.apereo.cas.authentication.Authentication)5 RegisteredService (org.apereo.cas.services.RegisteredService)5 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)4 Principal (org.apereo.cas.authentication.principal.Principal)4 ServiceContext (org.apereo.cas.services.ServiceContext)3 InvalidTicketException (org.apereo.cas.ticket.InvalidTicketException)3 ServiceTicket (org.apereo.cas.ticket.ServiceTicket)3 Method (java.lang.reflect.Method)2 Service (org.apereo.cas.authentication.principal.Service)2 Ticket (org.apereo.cas.ticket.Ticket)2 ProxyGrantingTicket (org.apereo.cas.ticket.proxy.ProxyGrantingTicket)2 ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Meter (com.codahale.metrics.Meter)1 Timer (com.codahale.metrics.Timer)1