11/*
22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33 *
4- * Copyright (c) 2013-2014 Oracle and/or its affiliates. All rights reserved.
4+ * Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
55 *
66 * The contents of this file are subject to the terms of either the GNU
77 * General Public License Version 2 only ("GPL") or the Common Development
4747import java .lang .annotation .Target ;
4848import java .util .Set ;
4949
50+ import javax .ws .rs .DefaultValue ;
5051import javax .ws .rs .GET ;
52+ import javax .ws .rs .HeaderParam ;
5153import javax .ws .rs .NameBinding ;
5254import javax .ws .rs .Path ;
5355import javax .ws .rs .container .ContainerRequestContext ;
56+ import javax .ws .rs .container .ContainerRequestFilter ;
5457import javax .ws .rs .container .ContainerResponseContext ;
5558import javax .ws .rs .container .ContainerResponseFilter ;
59+ import javax .ws .rs .container .PreMatching ;
5660import javax .ws .rs .core .Application ;
5761import javax .ws .rs .core .Response ;
5862
5963import org .glassfish .jersey .server .ResourceConfig ;
6064import org .glassfish .jersey .test .JerseyTest ;
6165
6266import org .junit .Test ;
67+ import static org .hamcrest .core .Is .is ;
6368import static org .junit .Assert .assertEquals ;
6469import static org .junit .Assert .assertNull ;
70+ import static org .junit .Assert .assertThat ;
6571
6672import jersey .repackaged .com .google .common .collect .Sets ;
6773
6874/**
69- * @author Miroslav Fuksa (miroslav.fuksa at oracle.com)
75+ * Test-suite ensuring the correct functionality of name binding.
7076 *
77+ * @author Miroslav Fuksa
78+ * @author Michal Gajdos (michal.gajdos at oracle.com)
7179 */
7280public class NameBindingTest extends JerseyTest {
81+
7382 @ Override
7483 protected Application configure () {
7584 return new ResourceConfig (Resource .class , FooResource .class , BarResource .class , FooBarResource .class , FooFilter .class ,
76- BarFilter .class , FooBarFilter .class );
85+ BarFilter .class , FooBarFilter .class , PreMatchingFooFilter . class );
7786 }
7887
7988 @ Path ("resource" )
8089 public static class Resource {
90+
8191 @ GET
8292 public String noBinding () {
8393 return "noBinding" ;
@@ -105,35 +115,53 @@ public String foobar() {
105115 return "foobar" ;
106116 }
107117
118+ @ GET
119+ @ Path ("preMatchingNameBinding" )
120+ public String preMatchingNameBinding (@ HeaderParam ("header" ) @ DefaultValue ("bar" ) final String header ) {
121+ return header ;
122+ }
108123 }
109124
110125 @ NameBinding
111126 @ Target ({ElementType .TYPE , ElementType .METHOD })
112127 @ Retention (value = RetentionPolicy .RUNTIME )
113128 public static @interface FooBinding {
129+
114130 }
115131
116132 @ NameBinding
117133 @ Target ({ElementType .TYPE , ElementType .METHOD })
118134 @ Retention (value = RetentionPolicy .RUNTIME )
119135 public static @interface BarBinding {
120- }
121136
137+ }
122138
123139 @ FooBinding
124140 public static class FooFilter implements ContainerResponseFilter {
125141
126142 @ Override
127- public void filter (ContainerRequestContext requestContext , ContainerResponseContext responseContext ) throws IOException {
143+ public void filter (final ContainerRequestContext requestContext , final ContainerResponseContext responseContext ) throws
144+ IOException {
128145 responseContext .getHeaders ().add (this .getClass ().getSimpleName (), "called" );
129146 }
130147 }
131148
149+ @ PreMatching
150+ @ FooBinding
151+ public static class PreMatchingFooFilter implements ContainerRequestFilter {
152+
153+ @ Override
154+ public void filter (final ContainerRequestContext requestContext ) throws IOException {
155+ requestContext .getHeaders ().putSingle ("header" , "foo" );
156+ }
157+ }
158+
132159 @ BarBinding
133160 public static class BarFilter implements ContainerResponseFilter {
134161
135162 @ Override
136- public void filter (ContainerRequestContext requestContext , ContainerResponseContext responseContext ) throws IOException {
163+ public void filter (final ContainerRequestContext requestContext , final ContainerResponseContext responseContext ) throws
164+ IOException {
137165 responseContext .getHeaders ().add (this .getClass ().getSimpleName (), "called" );
138166 }
139167 }
@@ -143,12 +171,12 @@ public void filter(ContainerRequestContext requestContext, ContainerResponseCont
143171 public static class FooBarFilter implements ContainerResponseFilter {
144172
145173 @ Override
146- public void filter (ContainerRequestContext requestContext , ContainerResponseContext responseContext ) throws IOException {
174+ public void filter (final ContainerRequestContext requestContext , final ContainerResponseContext responseContext ) throws
175+ IOException {
147176 responseContext .getHeaders ().add (this .getClass ().getSimpleName (), "called" );
148177 }
149178 }
150179
151-
152180 private static final Set <Class <?>> FILTERS = initialize ();
153181
154182 private static Set <Class <?>> initialize () {
@@ -159,22 +187,20 @@ private static Set<Class<?>> initialize() {
159187 return set ;
160188 }
161189
162-
163- private void checkCalled (Response response , Class <?>... filtersThatShouldBeCalled ) {
164- Set <Class <?>> positiveFilters = Sets .newHashSet (filtersThatShouldBeCalled );
165- for (Class <?> filter : FILTERS ) {
190+ private void checkCalled (final Response response , final Class <?>... filtersThatShouldBeCalled ) {
191+ final Set <Class <?>> positiveFilters = Sets .newHashSet (filtersThatShouldBeCalled );
192+ for (final Class <?> filter : FILTERS ) {
166193 if (positiveFilters .contains (filter )) {
167- assertEquals ("Filter '" + filter .getSimpleName () + "' should be called." ,
168- "called" , response . getHeaders () .getFirst (filter .getSimpleName ()));
194+ assertEquals ("Filter '" + filter .getSimpleName () + "' should be called." , "called" , response . getHeaders ()
195+ .getFirst (filter .getSimpleName ()));
169196 } else {
170- assertNull ("Filter '" + filter .getSimpleName () + "' should not be called." ,
171- response . getHeaders (). get ( filter .getSimpleName ()));
197+ assertNull ("Filter '" + filter .getSimpleName () + "' should not be called." , response . getHeaders (). get ( filter
198+ .getSimpleName ()));
172199 }
173200 }
174201 }
175202
176-
177- private Response _getResponse (String path ) {
203+ private Response _getResponse (final String path ) {
178204 final Response response = target ().path (path ).request ().get ();
179205 assertEquals (200 , response .getStatus ());
180206 return response ;
@@ -190,6 +216,18 @@ public void testResourceFooBinding() {
190216 checkCalled (_getResponse ("resource/foo" ), FooFilter .class );
191217 }
192218
219+ /**
220+ * Reproducer for JERSEY-2739. Name bound annotation on a pre-matching filter should be ignored and the filter should be
221+ * invoked for each resource method (globally).
222+ */
223+ @ Test
224+ public void preMatchingNameBinding () {
225+ final Response response = _getResponse ("resource/preMatchingNameBinding" );
226+
227+ // Request filter - applied, even when the filter is name bound and the resource method is not.
228+ assertThat ("Name binding on a @PreMatching filter not ignored." , response .readEntity (String .class ), is ("foo" ));
229+ }
230+
193231 @ Test
194232 public void testResourceBarBinding () {
195233 checkCalled (_getResponse ("resource/bar" ), BarFilter .class );
@@ -200,10 +238,10 @@ public void testResourceFooBarBinding() {
200238 checkCalled (_getResponse ("resource/foobar" ), FooFilter .class , BarFilter .class , FooBarFilter .class );
201239 }
202240
203-
204241 @ Path ("foo-resource" )
205242 @ FooBinding
206243 public static class FooResource extends Resource {
244+
207245 }
208246
209247 @ Test
@@ -229,6 +267,7 @@ public void testFooResourceFooBarBinding() {
229267 @ Path ("bar-resource" )
230268 @ BarBinding
231269 public static class BarResource extends Resource {
270+
232271 }
233272
234273 @ Test
@@ -251,11 +290,11 @@ public void testBarResourceFooBarBinding() {
251290 checkCalled (_getResponse ("bar-resource/foobar" ), BarFilter .class , FooFilter .class , FooBarFilter .class );
252291 }
253292
254-
255293 @ Path ("foobar-resource" )
256294 @ BarBinding
257295 @ FooBinding
258296 public static class FooBarResource extends Resource {
297+
259298 }
260299
261300 @ Test
@@ -278,5 +317,4 @@ public void testFooBarResourceFooBarBinding() {
278317 checkCalled (_getResponse ("foobar-resource/foobar" ), BarFilter .class , FooFilter .class , FooBarFilter .class );
279318 }
280319
281-
282320}
0 commit comments