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));
}
}
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());
}
}
}
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;
}
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;
}
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));
}
}
}
Aggregations