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

Commit 1cae8c3

Browse files
Marek PotociarGerrit Code Review
authored andcommitted
Merge "JERSEY-2132: Entity Filtering - Add support for JSON via Jackson"
2 parents 4dbe88a + a7d624b commit 1cae8c3

File tree

54 files changed

+3407
-254
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+3407
-254
lines changed

docs/src/main/docbook/entity-filtering.xml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
55
6-
Copyright (c) 2013-2014 Oracle and/or its affiliates. All rights reserved.
6+
Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
77
88
The contents of this file are subject to the terms of either the GNU
99
General Public License Version 2 only ("GPL") or the Common Development
@@ -44,6 +44,7 @@
4444
<!ENTITY % ents SYSTEM "jersey.ent" > %ents;
4545
<!ENTITY jersey.github.ef.example.link "<link xlink:href='&jersey.github.examples.uri;/entity-filtering'>Entity Filtering example</link>">
4646
<!ENTITY jersey.github.ef.security.example.link "<link xlink:href='&jersey.github.examples.uri;/entity-filtering-security'>Entity Filtering example (with security annotations)</link>">
47+
<!ENTITY jersey.github.ef.selectable.example.link "<link xlink:href='&jersey.github.examples.uri;/entity-filtering-selectable'>Entity Filtering example (based on dynamic and configurable query parameters)</link>">
4748
]>
4849

4950
<chapter xmlns="http://docbook.org/ns/docbook"
@@ -900,12 +901,17 @@ public class FilteringMoxyJsonProvider extends ConfigurableMoxyJsonProvider {
900901
<link linkend='json.moxy'>MOXy</link>
901902
</para>
902903
</listitem>
904+
<listitem>
905+
<para>
906+
<link linkend='json.jackson'>Jackson (2.x)</link>
907+
</para>
908+
</listitem>
903909
</itemizedlist>
904910
</para>
905911
<para>
906912
In order to use Entity Filtering in mentioned modules you need to explicitly register either
907-
&jersey.message.filtering.EntityFilteringFeature; or &jersey.message.filtering.SecurityEntityFilteringFeature;
908-
to activate Entity Filtering for particular module.
913+
&jersey.message.filtering.EntityFilteringFeature;, &jersey.message.filtering.SecurityEntityFilteringFeature;
914+
or &jersey.message.filtering.SelectableEntityFilteringFeature; to activate Entity Filtering for particular module.
909915
</para>
910916
</section>
911917

@@ -922,6 +928,9 @@ public class FilteringMoxyJsonProvider extends ConfigurableMoxyJsonProvider {
922928
<listitem>
923929
<para>&jersey.github.ef.security.example.link;</para>
924930
</listitem>
931+
<listitem>
932+
<para>&jersey.github.ef.selectable.example.link;</para>
933+
</listitem>
925934
</itemizedlist>
926935
</para>
927936
</section>
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
<!doctype html>
2+
<!--
3+
4+
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5+
6+
Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
7+
8+
The contents of this file are subject to the terms of either the GNU
9+
General Public License Version 2 only ("GPL") or the Common Development
10+
and Distribution License("CDDL") (collectively, the "License"). You
11+
may not use this file except in compliance with the License. You can
12+
obtain a copy of the License at
13+
http://glassfish.java.net/public/CDDL+GPL_1_1.html
14+
or packager/legal/LICENSE.txt. See the License for the specific
15+
language governing permissions and limitations under the License.
16+
17+
When distributing the software, include this License Header Notice in each
18+
file and include the License file at packager/legal/LICENSE.txt.
19+
20+
GPL Classpath Exception:
21+
Oracle designates this particular file as subject to the "Classpath"
22+
exception as provided by Oracle in the GPL Version 2 section of the License
23+
file that accompanied this code.
24+
25+
Modifications:
26+
If applicable, add the following below the License Header, with the fields
27+
enclosed by brackets [] replaced by your own identifying information:
28+
"Portions Copyright [year] [name of copyright owner]"
29+
30+
Contributor(s):
31+
If you wish your version of this file to be governed by only the CDDL or
32+
only the GPL Version 2, indicate your decision by adding "[Contributor]
33+
elects to include this software in this distribution under the [CDDL or GPL
34+
Version 2] license." If you don't indicate a single choice of license, a
35+
recipient has the option to distribute your version of this file under
36+
either the CDDL, the GPL Version 2 or to extend the choice of license to
37+
its licensees as provided above. However, if you add GPL Version 2 code
38+
and therefore, elected the GPL Version 2 license, then the option applies
39+
and therefore, elected the GPL Version 2 license, then the option applies
40+
only if the new code is made subject to such option by the copyright
41+
holder.
42+
43+
-->
44+
<html lang="en">
45+
<head>
46+
<meta charset="utf-8">
47+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
48+
49+
<title>Entity Data Filtering - Role-based Entity Filtering</title>
50+
51+
<!-- Bootstrap -->
52+
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
53+
</head>
54+
<body>
55+
56+
<div class="container">
57+
<div class="row">
58+
<div class="col-lg-offset-1 col-lg-10">
59+
<header class="page-header">
60+
<h1>Entity Data Filtering <small>Role-based Entity Filtering using security annotations</small></h1>
61+
</header>
62+
63+
<p>
64+
This example demonstrates how to use entity filtering feature together with security annotations (from
65+
<code>javax.annotation.security</code> package) and how to apply them on domain classes as well as on
66+
JAX-RS resource classes or JAX-RS resource methods.
67+
</p>
68+
<p>
69+
In addition to domain classes and JAX-RS resources (with security annotations applied) there is also one
70+
(pre-matching) container request filter, <code>SecurityRequestFilter</code>. The filter sets security
71+
context for each incoming request as if the request was invoked by a user in role "manager".
72+
</p>
73+
<p>
74+
The full description how Entity Data Filtering can be found in Jersey User Guide, chapter
75+
<a href="https://jersey.java.net/documentation/latest/entity-filtering.html" target="_blank">Entity Data Filtering</a>.
76+
Sections relevant to this example (describing this exact example) are:
77+
<ul>
78+
<li><a href="https://jersey.java.net/documentation/latest/entity-filtering.html#d0e13911" target="_blank">Enabling and configuring Entity Filtering in your application</a></li>
79+
<li><a href="https://jersey.java.net/documentation/latest/entity-filtering.html#ef.security.annotations" target="_blank">Role-based Entity Filtering using (javax.annotation.security) annotations</a></li>
80+
</ul>
81+
</p>
82+
83+
<h2>Contents</h2>
84+
85+
<p>
86+
The mapping of the URI path space is presented in the following table:
87+
</p>
88+
89+
<table class="table table-bordered">
90+
<thead>
91+
<tr>
92+
<th>URI path</th>
93+
<th>Resource class</th>
94+
<th>HTTP methods</th>
95+
<th>Allowed values</th>
96+
<th>Notes</th>
97+
</tr>
98+
</thead>
99+
100+
<tbody>
101+
<tr>
102+
<td><code>/restricted-resource/denyAll</code></td>
103+
<td>RestrictedResource</td>
104+
<td>GET</td>
105+
<td>N/A</td>
106+
<td>@DenyAll annotation used - returns HTTP 403, Forbidden response</td>
107+
</tr>
108+
<tr>
109+
<td><code>/restricted-resource/permitAll</code></td>
110+
<td>RestrictedResource</td>
111+
<td>GET</td>
112+
<td>N/A</td>
113+
<td>
114+
@PermitAll annotation used<br/>
115+
Role-based view on RestrictedEntity class - permitAll, simpleField
116+
</td>
117+
</tr>
118+
<tr>
119+
<td><code>/restricted-resource/rolesAllowed</code></td>
120+
<td>RestrictedResource</td>
121+
<td>GET</td>
122+
<td>N/A</td>
123+
<td>
124+
@RolesAllowed({"manager"}) annotation used, user in role "manager"<br/>
125+
Role-based view on RestrictedEntity class - permitAll, simpleField, mixedField.managerField
126+
</td>
127+
</tr>
128+
<tr>
129+
<td><code>/unrestricted-resource</code></td>
130+
<td>UnrestrictedResource</td>
131+
<td>GET</td>
132+
<td>N/A</td>
133+
<td>
134+
No security annotation used, user in role "manager"<br/>
135+
Role-based view on RestrictedEntity class - permitAll, simpleField, mixedField.managerField
136+
</td>
137+
</tr>
138+
</tbody>
139+
</table>
140+
141+
<p>
142+
Application is based on Grizzly container (see <code>App</code>). Everything needed (resources/providers)
143+
is registered in <code>SecurityEntityFilteringApplication</code>.
144+
</p>
145+
146+
<h2>Running the Example</h2>
147+
148+
<p>Run the example as follows:</p>
149+
<blockquote>
150+
<pre>mvn clean package exec:java</pre>
151+
</blockquote>
152+
153+
<p>
154+
This deploys current example using Grizzly. You can access the application at:
155+
<ul>
156+
<li><a href="http://localhost:8080/unrestricted-resource">http://localhost:8080/unrestricted-resource</a></li>
157+
<li><a href="http://localhost:8080/restricted-resource/denyAll">http://localhost:8080/restricted-resource/denyAll</a></li>
158+
<li><a href="http://localhost:8080/restricted-resource/permitAll">http://localhost:8080/restricted-resource/permitAll</a></li>
159+
<li><a href="http://localhost:8080/restricted-resource/rolesAllowed">http://localhost:8080/restricted-resource/rolesAllowed</a></li>
160+
<li><a href="http://localhost:8080/restricted-resource/runtimeRolesAllowed?roles=manager,user">http://localhost:8080/restricted-resource/runtimeRolesAllowed?roles=manager,user</a></li>
161+
</ul>
162+
</p>
163+
164+
<h2>Using Jackson instead of MOXy</h2>
165+
166+
<p>
167+
This examples uses by default Entity Data Filtering feature together with MOXy. To switch MOXy JSON provider
168+
to Jackson (2.x) JSON provider simply
169+
170+
<ul>
171+
<li>
172+
comment registration of MOXy ContextResolver, and<br/>
173+
<code>register(new MoxyJsonConfig().setFormattedOutput(true).resolver())</code>
174+
</li>
175+
<li>
176+
uncomment registration of JacksonFeature<br/>
177+
<code>register(JacksonFeature.class)</code>
178+
</li>
179+
</ul>
180+
181+
in <code>SecurityEntityFilteringApplication</code> class.
182+
</p>
183+
184+
</div>
185+
</div>
186+
</div>
187+
188+
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
189+
</body>
190+
</html>

examples/entity-filtering-security/pom.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,21 @@
6161
<artifactId>jersey-server</artifactId>
6262
</dependency>
6363
<dependency>
64-
<groupId>org.glassfish.jersey.media</groupId>
65-
<artifactId>jersey-media-moxy</artifactId>
64+
<groupId>org.glassfish.jersey.containers</groupId>
65+
<artifactId>jersey-container-grizzly2-http</artifactId>
6666
</dependency>
6767
<dependency>
6868
<groupId>org.glassfish.jersey.ext</groupId>
6969
<artifactId>jersey-entity-filtering</artifactId>
7070
</dependency>
7171

7272
<dependency>
73-
<groupId>org.glassfish.jersey.containers</groupId>
74-
<artifactId>jersey-container-grizzly2-http</artifactId>
73+
<groupId>org.glassfish.jersey.media</groupId>
74+
<artifactId>jersey-media-moxy</artifactId>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.glassfish.jersey.media</groupId>
78+
<artifactId>jersey-media-json-jackson</artifactId>
7579
</dependency>
7680

7781
<dependency>

examples/entity-filtering-security/src/main/java/org/glassfish/jersey/examples/entityfiltering/security/SecurityEntityFilteringApplication.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013 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
@@ -41,6 +41,7 @@
4141

4242
import javax.ws.rs.ApplicationPath;
4343

44+
import org.glassfish.jersey.jackson.JacksonFeature;
4445
import org.glassfish.jersey.message.filtering.SecurityEntityFilteringFeature;
4546
import org.glassfish.jersey.moxy.json.MoxyJsonConfig;
4647
import org.glassfish.jersey.server.ResourceConfig;
@@ -60,7 +61,10 @@ public SecurityEntityFilteringApplication() {
6061
// Register entity-filtering security feature.
6162
register(SecurityEntityFilteringFeature.class);
6263

63-
// Configure MOXy Json provider.
64+
// Configure MOXy Json provider. Comment this line to use Jackson. Uncomment to use MOXy.
6465
register(new MoxyJsonConfig().setFormattedOutput(true).resolver());
66+
67+
// Configure Jackson Json provider. Comment this line to use MOXy. Uncomment to use Jackson.
68+
// register(JacksonFeature.class);
6569
}
6670
}

examples/entity-filtering-security/src/test/java/org/glassfish/jersey/examples/entityfiltering/security/RestrictedResourceTest.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013 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
@@ -40,15 +40,23 @@
4040

4141
package org.glassfish.jersey.examples.entityfiltering.security;
4242

43-
import javax.ws.rs.core.Application;
43+
import java.util.Arrays;
44+
45+
import javax.ws.rs.core.Feature;
4446
import javax.ws.rs.core.Response;
4547

4648
import org.glassfish.jersey.examples.entityfiltering.security.domain.RestrictedEntity;
4749
import org.glassfish.jersey.examples.entityfiltering.security.domain.RestrictedSubEntity;
50+
import org.glassfish.jersey.jackson.JacksonFeature;
51+
import org.glassfish.jersey.message.filtering.SecurityEntityFilteringFeature;
52+
import org.glassfish.jersey.moxy.json.MoxyJsonFeature;
53+
import org.glassfish.jersey.server.ResourceConfig;
4854
import org.glassfish.jersey.test.JerseyTest;
4955
import org.glassfish.jersey.test.TestProperties;
5056

5157
import org.junit.Test;
58+
import org.junit.runner.RunWith;
59+
import org.junit.runners.Parameterized;
5260
import static org.hamcrest.CoreMatchers.equalTo;
5361
import static org.hamcrest.CoreMatchers.notNullValue;
5462
import static org.hamcrest.CoreMatchers.nullValue;
@@ -59,14 +67,21 @@
5967
*
6068
* @author Michal Gajdos (michal.gajdos at oracle.com)
6169
*/
70+
@RunWith(Parameterized.class)
6271
public class RestrictedResourceTest extends JerseyTest {
6372

64-
@Override
65-
protected Application configure() {
66-
enable(TestProperties.LOG_TRAFFIC);
67-
enable(TestProperties.DUMP_ENTITY);
73+
@Parameterized.Parameters(name = "Provider: {0}")
74+
public static Iterable<Class[]> providers() {
75+
return Arrays.asList(new Class[][]{{MoxyJsonFeature.class}, {JacksonFeature.class}});
76+
}
6877

69-
return new SecurityEntityFilteringApplication();
78+
public RestrictedResourceTest(final Class<Feature> filteringProvider) {
79+
super(new ResourceConfig(SecurityEntityFilteringFeature.class)
80+
.packages("org.glassfish.jersey.examples.entityfiltering.security")
81+
.register(filteringProvider));
82+
83+
enable(TestProperties.DUMP_ENTITY);
84+
enable(TestProperties.LOG_TRAFFIC);
7085
}
7186

7287
@Test

0 commit comments

Comments
 (0)