Debian Codegraph @

samples/jersey-guice-filter/README.html

raw
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <!--
3
4 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5
6 Copyright (c) 2010-2011 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 only if the new code is made subject to such option by the copyright
40 holder.
41
42 -->
43 <html><head><title>Jersey-Guice Example</title></head>
44
45 <!--
46 super(new WebAppDescriptor.Builder("com.sun.jersey.samples.guice.resources")
47 .contextListenerClass(com.sun.jersey.samples.guice.GuiceServletConfig.class)
48 .filterClass(com.google.inject.servlet.GuiceFilter.class)
49 .contextPath("jersey-guice-filter")
50 .servletPath("/")
51 .build());
52 -->
53
54 <body>
55 <h1>Jersey Guice Example</h1>
56
57 <p>This example demonstrates how to develop Restful WebService sample using Guice Servlet Extensions with a servlet filter.
58
59 <h2>Contents of jersey-guice-filter example</h2>
60 <dl>
61 <dd>
62 <h3>web.xml and GuiceServletConfig </h3>
63 You must register both the GuiceFilter and your subclass of GuiceServletContextListener in your application's web.xml file.
64
65 Add the following to web.xml so the servlet container triggers this class when the app is deployed.
66
67 <pre>
68 &lt;listener&gt;
69 &lt;listener-class&gt;com.sun.jersey.samples.guice.GuiceServletConfig&lt;/listener-class&gt;
70 &lt;/listener&gt;
71 </pre>
72
73 GuiceServletConfig is class that extends GuiceServletContextListener and overrides the getInjector() method to return a Guice injector which has it's configureServlets method overridden to filter all request "/*".</p>
74
75 Next, place GuiceFilter after this <code>&lt;/listener&gt; </code> element in your .web.xml file:
76 <pre>
77 &lt;filter&gt;
78 &lt;filter-name>guiceFilter&lt;/filter-name&gt;
79 &lt;filter-class>com.google.inject.servlet.GuiceFilter&lt;/filter-class&gt;
80 &lt;/filter&gt;
81
82 &lt;filter-mapping&gt;
83 &lt;filter-name>guiceFilter&lt;/filter-name&gt;
84 &lt;url-pattern>/*&lt;/url-pattern&gt;
85 &lt;/filter-mapping&gt;
86 </pre>
87 This tells the Servlet Container to re-route all requests through GuiceFilter.
88
89 <h3>Installing a Servlet Module</h3>
90 Now that you have GuiceFilter configured, Guice Servlet is set up. Next, you will need to install an instance of ServletModule in order really use Guice Servlet.
91 We do this in <code>GuiceServletConfig.java</code> file by overriding <code>getInjector()</code> to return our new instance of ServletModule:
92
93 <pre>@Override
94 protected Injector getInjector() {
95 return Guice.createInjector(new ServletModule() {</pre>
96
97 You can think of the ServletModule as an in-code replacement for the web.xml deployment descriptor.
98
99 <h3>The Binding Language</h3>
100 Filters and servlets are configured here in ServletModule using normal Java method calls. This module sets up the request and session scopes. Here we are registering a servlet during creation of Guice injector:
101
102 <pre>@Override
103 protected void configureServlets() {
104 // Bind classes
105 bind(PerRequestResource.class);
106
107 serve("/*").with(GuiceContainer.class);
108 }
109 });
110 </pre>
111 The module also provides a place to configure your filters and servlets from.
112 We have chosen to create the injector in our ServletContextListener.
113 (Feel free to create the injector from any place you choose).
114 A ServletContextListener is a Java servlet component that is triggered as soon
115 as a web application is deployed, and before any requests begin to arrive.
116 Guice Servlet provides a convenience utility that you can subclass in order
117 to register your own ServletContextListeners:
118
119 <pre>public class GuiceServletConfig extends GuiceServletContextListener {
120
121 @Override
122 protected Injector getInjector() {
123 return Guice.createInjector(new ServletModule() {
124 </pre>
125 The final class looks like this:
126 <pre>
127 public class GuiceServletConfig extends GuiceServletContextListener {
128
129 @Override
130 protected Injector getInjector() {
131 return Guice.createInjector(new ServletModule() {
132
133 @Override
134 protected void configureServlets() {
135 // Bind classes
136 bind(PerRequestResource.class);
137
138 serve("/*").with(GuiceContainer.class);
139 }
140 });
141 }
142 }
143 </pre>
144
145 <h3>Creating resource class</h3>
146 We have created resource class, <code>PerRequestResource</code> which binds path <code>@Path("bound/perrequest")</code> to scope using guice <code>@RequestScoped</code> as shown in the following code:
147 <pre>//Create resource class, @Path("bound/perrequest"), using guice @RequestScoped
148 @Path("bound/perrequest")
149 @RequestScoped
150 public class PerRequestResource {</pre>
151
152 <h3>Injecting URI info/query parameter</h3>
153 We Inject URI info and a query parameter via code:
154 <pre>//Inject URI info and a query parameter
155 @Context UriInfo ui;
156
157 @QueryParam("x") String x;</pre>
158
159 <h3>Singleton Component creation and Injection into resource</h3>
160 Finally, we create singleton component (see <code>SingletonComponent.java</code>) and inject into resource <code>PerRequestResource</code> at construction time via code:
161 <pre>private final SingletonComponent sc;
162
163 //Create singleton component and inject into resource at construction
164 @Inject
165 public PerRequestResource(SingletonComponent sc) {
166 this.sc = sc;
167 }</pre>
168 </dd>
169
170 Details here <a href="http://code.google.com/p/google-guice/wiki/ServletModule">google-guice/ServletModule</a> were copied and applied to our specific example.
171
172 </dl>
173 <p>The mapping of the URI path space is presented in the following table:</p>
174 <table border="1">
175 <tbody>
176 <tr>
177 <th>URI path</th>
178 <th>Resource class</th>
179 <th>HTTP method</th>
180 <th>Description</th>
181 </tr>
182 <tr>
183 <td>/jersey-guice-filter/bound/perrequest</td>
184 <td>PerRequestResource</td>
185 <td>GET</td>
186 <td>Returns string representing PerRequestResource Context URI info path along with QueryParam and Singleton component's hashcode integer value converted to hex, prefixed by SINGLETON: </td>
187 </tr>
188 </tbody>
189 </table>
190
191
192 <h2>Running the Example</h2>
193
194 <p>You can run the example using embedded GlassFish as follows:</p>
195
196 <p>Build and deploy the project by executing maven from the project directory</p>
197 <blockquote><code>mvn clean package embedded-glassfish:run</code></blockquote>
198
199 <p>From a web browser, visit:</p>
200 <blockquote><code><a href="http://localhost:8080/jersey-guice-filter/bound/perrequest">http://localhost:8080/jersey-guice-filter/bound/perrequest</a></code>
201 </blockquote>
202
203 </body>
204 </html>