Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 904ed19

Browse files
committed
Respecting @priority for custom ExceptionMappers.
Change-Id: I3ee1321b2077b34f0301d53ded04ff23625efba0
1 parent f0a3b32 commit 904ed19

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

core-common/src/main/java/org/glassfish/jersey/internal/inject/Providers.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,10 @@ public static <T> Iterable<T> getAllProviders(
308308
* @return set of all available service provider instances for the contract
309309
*/
310310
public static <T> Collection<ServiceHolder<T>> getAllServiceHolders(InjectionManager injectionManager, Class<T> contract) {
311-
List<ServiceHolder<T>> providers = getServiceHolders(injectionManager, contract, CustomAnnotationLiteral.INSTANCE);
311+
List<ServiceHolder<T>> providers = getServiceHolders(injectionManager,
312+
contract,
313+
Comparator.comparingInt(Providers::getPriority),
314+
CustomAnnotationLiteral.INSTANCE);
312315
providers.addAll(getServiceHolders(injectionManager, contract));
313316

314317
LinkedHashSet<ServiceHolder<T>> providersSet = new LinkedHashSet<>();
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.jersey.tests.e2e.server;
42+
43+
import javax.ws.rs.GET;
44+
import javax.ws.rs.Path;
45+
import javax.ws.rs.core.Application;
46+
import javax.ws.rs.core.Response;
47+
import javax.ws.rs.ext.ExceptionMapper;
48+
import javax.ws.rs.ext.Provider;
49+
50+
import javax.annotation.Priority;
51+
52+
import org.glassfish.jersey.server.ResourceConfig;
53+
import org.glassfish.jersey.test.JerseyTest;
54+
55+
import org.junit.Test;
56+
import static org.hamcrest.core.Is.is;
57+
import static org.junit.Assert.assertThat;
58+
59+
/**
60+
* @author Pavel Bucek (pavel.bucek at oracle.com)
61+
*/
62+
public class ExceptionMapperPriorityTest extends JerseyTest {
63+
64+
@Override
65+
protected Application configure() {
66+
return new ResourceConfig(ExceptionMapperPriorityResource.class,
67+
MyFirstExceptionMapper.class,
68+
MySecondExceptionMapper.class,
69+
MyThirdExceptionMapper.class);
70+
}
71+
72+
@Test
73+
public void priorityTest() {
74+
String response = target().request().get(String.class);
75+
76+
assertThat(response, is(MySecondExceptionMapper.class.getName()));
77+
}
78+
79+
@Path("/")
80+
public static class ExceptionMapperPriorityResource {
81+
82+
@GET
83+
public String get() throws MyException {
84+
throw new MyException();
85+
}
86+
}
87+
88+
public static class MyException extends Exception {
89+
90+
}
91+
92+
@Provider
93+
@Priority(300)
94+
public static class MyFirstExceptionMapper implements ExceptionMapper<MyException> {
95+
96+
@Override
97+
public Response toResponse(MyException exception) {
98+
return Response.ok(MyFirstExceptionMapper.class.getName()).build();
99+
}
100+
}
101+
102+
@Provider
103+
@Priority(100)
104+
public static class MySecondExceptionMapper implements ExceptionMapper<MyException> {
105+
106+
@Override
107+
public Response toResponse(MyException exception) {
108+
return Response.ok(MySecondExceptionMapper.class.getName()).build();
109+
}
110+
}
111+
112+
@Provider
113+
@Priority(200)
114+
public static class MyThirdExceptionMapper implements ExceptionMapper<MyException> {
115+
116+
@Override
117+
public Response toResponse(MyException exception) {
118+
return Response.ok(MyThirdExceptionMapper.class.getName()).build();
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)