Search in sources :

Example 16 with ResourceMethod

use of org.glassfish.jersey.server.model.ResourceMethod in project metrics by dropwizard.

the class InstrumentedResourceMethodApplicationListener method registerExceptionMeteredAnnotations.

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

Example 17 with ResourceMethod

use of org.glassfish.jersey.server.model.ResourceMethod in project jersey by jersey.

the class MonitoringStatisticsProcessor method processRequestItems.

private void processRequestItems() {
    final Queue<MonitoringEventListener.RequestStats> requestQueuedItems = monitoringEventListener.getRequestQueuedItems();
    final FloodingLogger floodingLogger = new FloodingLogger(requestQueuedItems);
    while (!requestQueuedItems.isEmpty()) {
        floodingLogger.conditionallyLogFlooding();
        final MonitoringEventListener.RequestStats event = requestQueuedItems.remove();
        final MonitoringEventListener.TimeStats requestStats = event.getRequestStats();
        statisticsBuilder.addRequestExecution(requestStats.getStartTime(), requestStats.getDuration());
        final MonitoringEventListener.MethodStats methodStat = event.getMethodStats();
        if (methodStat != null) {
            final ResourceMethod method = methodStat.getMethod();
            statisticsBuilder.addExecution(event.getRequestUri(), method, methodStat.getStartTime(), methodStat.getDuration(), requestStats.getStartTime(), requestStats.getDuration());
        }
    }
}
Also used : ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 18 with ResourceMethod

use of org.glassfish.jersey.server.model.ResourceMethod in project jersey by jersey.

the class MethodSelectingRouter method determineResponseMediaType.

/**
     * Determine the {@link MediaType} of the {@link Response} based on writers suitable for the given entity class,
     * pre-selected method and acceptable media types.
     *
     * @param entityClass          entity class to determine the media type for.
     * @param entityType           entity type for writers.
     * @param selectedMethod       pre-selected (invoked) method.
     * @param acceptableMediaTypes acceptable media types from request.
     * @return media type of the response.
     */
private MediaType determineResponseMediaType(final Class<?> entityClass, final Type entityType, final RequestSpecificConsumesProducesAcceptor selectedMethod, final List<AcceptableMediaType> acceptableMediaTypes) {
    // Return pre-selected MediaType.
    if (usePreSelectedMediaType(selectedMethod, acceptableMediaTypes)) {
        return selectedMethod.produces.combinedType;
    }
    final ResourceMethod resourceMethod = selectedMethod.methodRouting.method;
    final Invocable invocable = resourceMethod.getInvocable();
    // Entity class can be null when considering HEAD method || empty entity.
    final Class<?> responseEntityClass = entityClass == null ? invocable.getRawRoutingResponseType() : entityClass;
    final Method handlingMethod = invocable.getHandlingMethod();
    // Media types producible by method.
    final List<MediaType> methodProducesTypes = !resourceMethod.getProducedTypes().isEmpty() ? resourceMethod.getProducedTypes() : Collections.singletonList(MediaType.WILDCARD_TYPE);
    // Applicable entity providers
    final List<WriterModel> writersForEntityType = workers.getWritersModelsForType(responseEntityClass);
    CombinedMediaType selected = null;
    for (final MediaType acceptableMediaType : acceptableMediaTypes) {
        for (final MediaType methodProducesType : methodProducesTypes) {
            if (!acceptableMediaType.isCompatible(methodProducesType)) {
                // no need to go deeper if acceptable and method produces type are incompatible
                continue;
            }
            // Use writers suitable for entity class to determine the media type.
            for (final WriterModel model : writersForEntityType) {
                for (final MediaType writerProduces : model.declaredTypes()) {
                    if (!writerProduces.isCompatible(acceptableMediaType) || !methodProducesType.isCompatible(writerProduces)) {
                        continue;
                    }
                    final CombinedMediaType.EffectiveMediaType effectiveProduces = new CombinedMediaType.EffectiveMediaType(MediaTypes.mostSpecific(methodProducesType, writerProduces), false);
                    final CombinedMediaType candidate = CombinedMediaType.create(acceptableMediaType, effectiveProduces);
                    if (candidate != CombinedMediaType.NO_MATCH) {
                        // Look for a better compatible worker.
                        if (selected == null || CombinedMediaType.COMPARATOR.compare(candidate, selected) < 0) {
                            if (model.isWriteable(responseEntityClass, entityType, handlingMethod.getDeclaredAnnotations(), candidate.combinedType)) {
                                selected = candidate;
                            }
                        }
                    }
                }
            }
        }
    }
    // Found media type for current writer.
    if (selected != null) {
        return selected.combinedType;
    }
    // so it can be written.
    return selectedMethod.produces.combinedType;
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) WriterModel(org.glassfish.jersey.message.WriterModel) MediaType(javax.ws.rs.core.MediaType) AcceptableMediaType(org.glassfish.jersey.message.internal.AcceptableMediaType) HttpMethod(javax.ws.rs.HttpMethod) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Method(java.lang.reflect.Method) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 19 with ResourceMethod

use of org.glassfish.jersey.server.model.ResourceMethod in project jersey by jersey.

the class AbstractErrorTemplateMapper method getErrorTemplate.

/**
     * Get an {@link ErrorTemplate} annotation from resource method / class the throwable was raised from.
     *
     * @return an error template annotation or {@code null} if the method is not annotated.
     */
private ErrorTemplate getErrorTemplate() {
    final ExtendedUriInfo uriInfo = uriInfoProvider.get();
    final ResourceMethod matchedResourceMethod = uriInfo.getMatchedResourceMethod();
    if (matchedResourceMethod != null) {
        final Invocable invocable = matchedResourceMethod.getInvocable();
        ErrorTemplate errorTemplate = invocable.getHandlingMethod().getAnnotation(ErrorTemplate.class);
        if (errorTemplate == null) {
            Class<?> handlerClass = invocable.getHandler().getHandlerClass();
            if (invocable.isInflector() && TemplateInflector.class.isAssignableFrom(invocable.getHandler().getHandlerClass())) {
                handlerClass = ((TemplateInflector) invocable.getHandler().getInstance(null)).getModelClass();
            }
            errorTemplate = handlerClass.getAnnotation(ErrorTemplate.class);
        }
        return errorTemplate;
    }
    return null;
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) ErrorTemplate(org.glassfish.jersey.server.mvc.ErrorTemplate) TemplateInflector(org.glassfish.jersey.server.mvc.internal.TemplateInflector) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) ExtendedUriInfo(org.glassfish.jersey.server.ExtendedUriInfo)

Example 20 with ResourceMethod

use of org.glassfish.jersey.server.model.ResourceMethod in project jersey by jersey.

the class ResourceMethodDispatcherFactoryTest method testBasicDispatchers.

@Test
public void testBasicDispatchers() throws InterruptedException, ExecutionException {
    final Resource.Builder rb = Resource.builder();
    final Method[] methods = this.getClass().getDeclaredMethods();
    for (Method method : methods) {
        if (Modifier.isPrivate(method.getModifiers())) {
            // class-based
            rb.addMethod("GET").handledBy(this.getClass(), method);
            // instance-based
            rb.addMethod("GET").handledBy(this, method);
        }
    }
    for (ResourceModelComponent component : rb.build().getComponents()) {
        if (component instanceof ResourceMethod) {
            Invocable invocable = ((ResourceMethod) component).getInvocable();
            assertNotNull("No dispatcher found for invocable " + invocable.toString(), rmdf.create(invocable, rmihf.create(invocable), null));
        }
    }
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) Resource(org.glassfish.jersey.server.model.Resource) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Method(java.lang.reflect.Method) ResourceModelComponent(org.glassfish.jersey.server.model.ResourceModelComponent) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) Test(org.junit.Test)

Aggregations

ResourceMethod (org.glassfish.jersey.server.model.ResourceMethod)22 Resource (org.glassfish.jersey.server.model.Resource)9 Method (java.lang.reflect.Method)6 Invocable (org.glassfish.jersey.server.model.Invocable)5 MediaType (javax.ws.rs.core.MediaType)4 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)3 HashSet (java.util.HashSet)3 AcceptableMediaType (org.glassfish.jersey.message.internal.AcceptableMediaType)3 RuntimeResource (org.glassfish.jersey.server.model.RuntimeResource)3 Metered (com.codahale.metrics.annotation.Metered)2 Timed (com.codahale.metrics.annotation.Timed)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 HttpMethod (javax.ws.rs.HttpMethod)2 ExtendedUriInfo (org.glassfish.jersey.server.ExtendedUriInfo)2 ResourceModelComponent (org.glassfish.jersey.server.model.ResourceModelComponent)2 ResourceMethodStatistics (org.glassfish.jersey.server.monitoring.ResourceMethodStatistics)2 Test (org.junit.Test)2