Skip to content

Commit

Permalink
Merge remote-tracking branch '3.0' into '3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
senivam authored Nov 24, 2023
2 parents 71f6a08 + a2f95b4 commit 485d0b3
Show file tree
Hide file tree
Showing 14 changed files with 251 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2020 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 jakarta.annotation.Priority;
import jakarta.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 defaultValue the default priority value if not {@link @Priority) present
* @return the value of Priority annotation if present or the default otherwise.
*/
public static int getPriorityValue(Class<?> prioritized, int defaultValue) {
final Priority priority = prioritized.getAnnotation(Priority.class);
return priority != null ? priority.value() : defaultValue;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2022 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 jakarta.ws.rs.Priorities;
import jakarta.ws.rs.ProcessingException;
import jakarta.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) 2011, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 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 @@ -87,6 +87,7 @@ private RuntimeDelegate findServerDelegate() {
for (RuntimeDelegate delegate : ServiceFinder.find(RuntimeDelegate.class)) {
// try to find runtime delegate from core-server
if (delegate.getClass() != RuntimeDelegateImpl.class) {
RuntimeDelegate.setInstance(delegate);
return delegate;
}
}
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 jakarta.annotation.Priority;
import jakarta.ws.rs.Priorities;
import jakarta.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.getPriorityValue(config1.getClass(), Priorities.USER);
int priority2 = JerseyPriorities.getPriorityValue(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, 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,6 +42,7 @@

import jakarta.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.getPriorityValue(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 jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;

import jakarta.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.getPriorityValue(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 @@ -45,6 +45,7 @@
import jakarta.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 +132,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.getPriorityValue(featureClass, Priorities.USER);
}

/**
Expand Down Expand Up @@ -592,10 +588,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.getPriorityValue(o1.getClass(), Priorities.USER);
final int p2 = JerseyPriorities.getPriorityValue(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, 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 All @@ -23,6 +23,7 @@

import jakarta.annotation.Priority;

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

/**
Expand Down Expand Up @@ -84,11 +85,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.getPriorityValue(clazz, Priorities.USER);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,14 @@ private int parseName(final CharacterIterator ci, int skipGroup) {
// groupCounts.add(1 + skipGroup);

if (variables.hasLength(0)) {
int len = TEMPLATE_VALUE_PATTERN.pattern().length() - 1;
String pattern = TEMPLATE_VALUE_PATTERN.pattern().substring(0, len) + '{' + variables.getLength(0) + '}';
namePattern = Pattern.compile(pattern);
if (variables.getLength(0) != 0) {
int len = TEMPLATE_VALUE_PATTERN.pattern().length() - 1;
String pattern = TEMPLATE_VALUE_PATTERN.pattern().substring(0, len)
+ '{' + variables.getLength(0) + '}';
namePattern = Pattern.compile(pattern);
} else {
namePattern = TEMPLATE_VALUE_PATTERN;
}
templateVariable.setLength(variables.getLength(0));
} else {
namePattern = (!variables.hasRegexp(0))
Expand Down Expand Up @@ -462,7 +467,10 @@ private int parseName(final CharacterIterator ci, int skipGroup) {
if (argIndex != 0) {
regexBuilder.append(")");
}
regexBuilder.append("{0,1}");

if (!variables.hasRegexp(argIndex)) {
regexBuilder.append("{0,1}");
}

argIndex++;
groupCounts.add(2);
Expand Down Expand Up @@ -571,6 +579,7 @@ private void parse(CharacterIterator ci, String template) {

StringBuilder regexBuilder = new StringBuilder();
State state = State.TEMPLATE;
State previousState;
boolean star = false;
boolean whiteSpace = false;
boolean ignoredLastComma = false;
Expand All @@ -579,6 +588,7 @@ private void parse(CharacterIterator ci, String template) {
int regExpRound = 0; // (
boolean reqExpSlash = false; // \
while ((state.value & (State.ERROR.value | State.EXIT.value)) == 0) {
previousState = state;
c = ci.next();
// "\\{(\\w[-\\w\\.]*)
if (Character.isLetterOrDigit(c)) {
Expand Down Expand Up @@ -702,8 +712,8 @@ private void parse(CharacterIterator ci, String template) {
regexps.add(regex);
}
} else {
regexps.add(null);
lengths.add(null);
regexps.add(previousState == State.REGEXP ? "" : null);
lengths.add(previousState == State.REGEXP ? 0 : null);
}

names.add(nameBuilder.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -257,7 +259,7 @@ void _testMatching(final String template, final String uri, final String... valu
final Map<String, String> m = new HashMap<String, String>();

boolean isMatch = t.match(uri, m);
assertTrue(isMatch);
assertTrue(isMatch, "No match for '" + uri + "' & params '" + Arrays.toString(values) + "`");
assertEquals(values.length, t.getTemplateVariables().size());

final Iterator<String> names = t.getTemplateVariables().iterator();
Expand Down Expand Up @@ -982,13 +984,20 @@ void testRfc6570MultiplePathArgs() {
_testTemplateNames("/{a,b,c}", "a", "b", "c");
_testMatching("/uri/{a}", "/uri/hello", "hello");
_testMatching("/uri/{a,b}", "/uri/hello,world", "hello", "world");
_testMatching("/uri/{a,b}", "/uri/x", "x", null);
_testMatching("/uri{?a,b}", "/uri?a=hello&b=world", "hello", "world");
_testMatching("/uri/{a,b,c}", "/uri/hello,world,!", "hello", "world", "!");
_testMatching("/uri/{a,b,c}", "/uri/hello,world", "hello", "world", null);
_testMatching("/uri/{a,b,c}", "/uri/hello", "hello", null, null);
_testMatching("/uri/{a,b,c}", "/uri/", null, null, null);
}

@Test
public void testRegularExpressionIsNotOptional() {
Assertions.assertThrows(AssertionFailedError.class,
() -> _testMatching("/{name: [a-z0-9]{3,128}}", "/", new String[]{null}));
}

@Test
void testRfc6570PathLength() {
_testMatching("/uri/{a:5}", "/uri/hello", "hello");
Expand Down
Loading

0 comments on commit 485d0b3

Please sign in to comment.