Skip to content

Commit

Permalink
Allow @priority for ExceptionMapper
Browse files Browse the repository at this point in the history
Centralized working with @priority value.

Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol committed Nov 16, 2023
1 parent 5616760 commit c76c88a
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,6 +16,7 @@

package org.glassfish.jersey;

import javax.annotation.Priority;
import javax.ws.rs.Priorities;

/**
Expand All @@ -36,4 +37,15 @@ private JerseyPriorities() {
* processing after the components with {@code Priorities.ENTITY_CODER} are processed.
*/
public static final int POST_ENTITY_CODER = Priorities.ENTITY_CODER + 100;

/**
* Return the value of priority annotation on a given class, if exists. Return the default value if not present.
* @param prioritized the provider class that potentially has a priority.
* @param _default the default priority if not {@link @Priority) present
* @return the value of Priority annotation if present or the default otherwise.
*/
public static int getPriority(Class<?> prioritized, int _default) {
final Priority priority = prioritized.getAnnotation(Priority.class);
return priority != null ? priority.value() : _default;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -28,9 +28,11 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.Priorities;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.ext.ExceptionMapper;

import org.glassfish.jersey.JerseyPriorities;
import org.glassfish.jersey.internal.inject.Bindings;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.inject.InstanceBinding;
Expand Down Expand Up @@ -111,19 +113,18 @@ public <T extends Throwable> ExceptionMapper<T> find(final Class<T> type) {
private <T extends Throwable> ExceptionMapper<T> find(final Class<T> type, final T exceptionInstance) {
ExceptionMapper<T> mapper = null;
int minDistance = Integer.MAX_VALUE;
int priority = Priorities.USER;

for (final ExceptionMapperType mapperType : exceptionMapperTypes.get()) {
final int d = distance(type, mapperType.exceptionType);
if (d >= 0 && d <= minDistance) {
final ExceptionMapper<T> candidate = mapperType.mapper.getInstance();
final int p = mapperType.mapper.getRank() > 0 ? mapperType.mapper.getRank() : Priorities.USER;

if (isPreferredCandidate(exceptionInstance, candidate, d == minDistance)) {
if (isPreferredCandidate(exceptionInstance, candidate, d == minDistance && p >= priority)) {
mapper = candidate;
minDistance = d;
if (d == 0) {
// slight optimization: if the distance is 0, it is already the best case, so we can exit
return mapper;
}
priority = p;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,11 +16,11 @@

package org.glassfish.jersey.internal.config;

import org.glassfish.jersey.JerseyPriorities;
import org.glassfish.jersey.internal.ServiceFinder;
import org.glassfish.jersey.spi.ExternalConfigurationModel;
import org.glassfish.jersey.spi.ExternalConfigurationProvider;

import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.core.Configurable;
import java.util.ArrayList;
Expand Down Expand Up @@ -152,19 +152,8 @@ private static class ConfigComparator implements Comparator<ExternalConfiguratio

@Override
public int compare(ExternalConfigurationProvider config1, ExternalConfigurationProvider config2) {

boolean config1PriorityPresent = config1.getClass().isAnnotationPresent(Priority.class);
boolean config2PriorityPresent = config2.getClass().isAnnotationPresent(Priority.class);

int priority1 = Priorities.USER;
int priority2 = Priorities.USER;

if (config1PriorityPresent) {
priority1 = config1.getClass().getAnnotation(Priority.class).value();
}
if (config2PriorityPresent) {
priority2 = config2.getClass().getAnnotation(Priority.class).value();
}
int priority1 = JerseyPriorities.getPriority(config1.getClass(), Priorities.USER);
int priority2 = JerseyPriorities.getPriority(config2.getClass(), Priorities.USER);

if (priority1 == priority2) {
return config1.getClass().getName().compareTo(config2.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -42,6 +42,7 @@

import javax.annotation.Priority;

import org.glassfish.jersey.JerseyPriorities;
import org.glassfish.jersey.internal.LocalizationMessages;
import org.glassfish.jersey.model.ContractProvider;
import org.glassfish.jersey.model.internal.RankedComparator;
Expand Down Expand Up @@ -357,13 +358,7 @@ private static <T> T holder2service(ServiceHolder<T> holder) {
}

private static int getPriority(Class<?> serviceClass) {
Priority annotation = serviceClass.getAnnotation(Priority.class);
if (annotation != null) {
return annotation.value();
}

// default priority
return Priorities.USER;
return JerseyPriorities.getPriority(serviceClass, /* default priority */ Priorities.USER);
}

private static <T> Class<T> getImplementationClass(Class<T> contract, ServiceHolder<T> serviceHolder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import javax.annotation.Priority;

import org.glassfish.jersey.JerseyPriorities;
import org.glassfish.jersey.internal.PropertiesDelegate;

/**
Expand Down Expand Up @@ -315,8 +314,9 @@ private static String formatInstance(final Object instance) {
} else {
textSB.append('[');
formatInstance(instance, textSB);
if (instance.getClass().isAnnotationPresent(Priority.class)) {
textSB.append(" #").append(instance.getClass().getAnnotation(Priority.class).value());
final int priority = JerseyPriorities.getPriority(instance.getClass(), -1);
if (priority != -1) {
textSB.append(" #").append(priority);
}
if (instance instanceof WebApplicationException) {
formatResponse(((WebApplicationException) instance).getResponse(), textSB);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -42,9 +42,8 @@
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;

import javax.annotation.Priority;

import org.glassfish.jersey.ExtendedConfig;
import org.glassfish.jersey.JerseyPriorities;
import org.glassfish.jersey.internal.LocalizationMessages;
import org.glassfish.jersey.internal.ServiceFinder;
import org.glassfish.jersey.internal.inject.Binder;
Expand Down Expand Up @@ -131,12 +130,7 @@ private static int priority(Class<? extends Feature> featureClass, int priority)
if (priority != ContractProvider.NO_PRIORITY) {
return priority;
}
final Priority priorityAnnotation = featureClass.getAnnotation(Priority.class);
if (priorityAnnotation != null) {
return priorityAnnotation.value();
} else {
return Priorities.USER;
}
return JerseyPriorities.getPriority(featureClass, Priorities.USER);
}

/**
Expand Down Expand Up @@ -592,10 +586,8 @@ public void configureAutoDiscoverableProviders(final InjectionManager injectionM
// Check whether meta providers have been initialized for a config this config has been loaded from.
if (!disableMetaProviderConfiguration) {
final Set<AutoDiscoverable> providers = new TreeSet<>((o1, o2) -> {
final int p1 = o1.getClass().isAnnotationPresent(Priority.class)
? o1.getClass().getAnnotation(Priority.class).value() : Priorities.USER;
final int p2 = o2.getClass().isAnnotationPresent(Priority.class)
? o2.getClass().getAnnotation(Priority.class).value() : Priorities.USER;
final int p1 = JerseyPriorities.getPriority(o1.getClass(), Priorities.USER);
final int p2 = JerseyPriorities.getPriority(o2.getClass(), Priorities.USER);

return (p1 < p2 || p1 == p2) ? -1 : 1;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -21,8 +21,7 @@

import javax.ws.rs.Priorities;

import javax.annotation.Priority;

import org.glassfish.jersey.JerseyPriorities;
import org.glassfish.jersey.model.ContractProvider;

/**
Expand Down Expand Up @@ -84,11 +83,7 @@ private int computeRank(final T provider, final int rank) {
clazz = clazz.getSuperclass();
}

if (clazz.isAnnotationPresent(Priority.class)) {
return clazz.getAnnotation(Priority.class).value();
} else {
return Priorities.USER;
}
return JerseyPriorities.getPriority(clazz, Priorities.USER);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2021 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -39,7 +39,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Priority;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.ws.rs.Priorities;
Expand All @@ -61,6 +60,7 @@
import org.eclipse.microprofile.rest.client.ext.QueryParamStyle;
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
import org.eclipse.microprofile.rest.client.spi.RestClientListener;
import org.glassfish.jersey.JerseyPriorities;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.Initializable;
Expand Down Expand Up @@ -299,8 +299,7 @@ private int getProviderPriority(Class<?> restClientInterface, Class<?> providerC
} else if (providerPriorityJersey instanceof Integer) {
return (int) providerPriorityJersey;
}
Priority priority = providerClass.getAnnotation(Priority.class);
return priority == null ? -1 : priority.value();
return JerseyPriorities.getPriority(providerClass, -1);
}

@Override
Expand Down Expand Up @@ -524,9 +523,7 @@ public AsyncInvocationInterceptor newInterceptor() {

Integer getPriority() {
if (priority == null) {
priority = Optional.ofNullable(factory.getClass().getAnnotation(Priority.class))
.map(Priority::value)
.orElse(Priorities.USER);
priority = JerseyPriorities.getPriority(factory.getClass(), Priorities.USER);
}
return priority;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -23,7 +23,6 @@
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Priority;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.spi.CreationalContext;
Expand All @@ -36,6 +35,7 @@
import javax.inject.Singleton;
import javax.ws.rs.RuntimeType;

import org.glassfish.jersey.JerseyPriorities;
import org.glassfish.jersey.internal.inject.Binding;
import org.glassfish.jersey.internal.inject.PerLookup;
import org.glassfish.jersey.internal.inject.PerThread;
Expand Down Expand Up @@ -161,15 +161,13 @@ public int getRank() {
return binding.getRank();
}

int _default = 1;
Class<T> type = binding.getImplementationType();
if (type != null) {
Priority priority = type.getAnnotation(Priority.class);
if (priority != null) {
return priority.value();
}
return JerseyPriorities.getPriority(type, _default);
}

return 1;
return _default;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -22,7 +22,6 @@
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Priority;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.spi.CreationalContext;
Expand All @@ -34,6 +33,7 @@
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Singleton;

import org.glassfish.jersey.JerseyPriorities;
import org.glassfish.jersey.internal.inject.Binding;
import org.glassfish.jersey.internal.inject.PerLookup;
import org.glassfish.jersey.internal.inject.PerThread;
Expand Down Expand Up @@ -151,16 +151,14 @@ public int getRank() {
if (binding.getRank() != null) {
return binding.getRank();
}
int _default = 1;

Class<T> type = binding.getImplementationType();
if (type != null) {
Priority priority = type.getAnnotation(Priority.class);
if (priority != null) {
return priority.value();
}
return JerseyPriorities.getPriority(type, _default);
}

return 1;
return _default;
}

@Override
Expand Down
Loading

0 comments on commit c76c88a

Please sign in to comment.