1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.directwebremoting.guice;
17
18 import com.google.inject.AbstractModule;
19 import com.google.inject.TypeLiteral;
20 import com.google.inject.Provider;
21
22 import java.util.Map;
23
24 import javax.servlet.ServletContext;
25 import javax.servlet.ServletRequest;
26 import javax.servlet.ServletResponse;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.HttpSession;
30
31 import org.directwebremoting.ScriptSession;
32 import org.directwebremoting.ServerContext;
33 import org.directwebremoting.ServerContextFactory;
34 import org.directwebremoting.WebContext;
35 import org.directwebremoting.WebContextFactory;
36 import static org.directwebremoting.guice.DwrScopes.*;
37 import static org.directwebremoting.guice.DwrGuiceUtil.getServletContext;
38
39 /**
40 * Configures DWR scopes and creates bindings for commonly
41 * used objects related to those scopes: request, response,
42 * script session, session, servlet context, and web context.
43 * <p>
44 * Since Guice's ServletModule makes its own bindings for requests,
45 * responses, and sessions, this class has a constructor that lets
46 * the user specify whether to avoid conflicts by not binding these types.
47 * </p>
48 * @author Tim Peierls [tim at peierls dot net]
49 */
50 class DwrGuiceServletModule extends AbstractModule
51 {
52 /**
53 * Creates a module to configure DWR scopes and bindings;
54 * conflicts with the bindings provided by Guice's {@link ServletModule}.
55 * <p>
56 * Normally you would not use this constructor directly, but instead call
57 * {@link AbstractDwrModule#bindDwrScopes() bindDwrScopes} in your binding
58 * code.
59 * </p>
60 */
61 public DwrGuiceServletModule()
62 {
63 this.bindPotentiallyConflictingTypes = true;
64 }
65
66 /**
67 * Creates a module to configure DWR scopes and bindings;
68 * conflicts with the bindings provided by Guice's {@link ServletModule}
69 * unless {@code bindPotentiallyConflictingTypes} is false, in which
70 * case this module will not create its own bindings for requests,
71 * responses, and sessions.
72 * <p>
73 * Normally you would not use this constructor directly, but instead call
74 * {@link AbstractDwrModule#bindDwrScopes(boolean) bindDwrScopes} in your binding
75 * code.
76 * </p>
77 */
78 public DwrGuiceServletModule(boolean bindPotentiallyConflictingTypes)
79 {
80 this.bindPotentiallyConflictingTypes = bindPotentiallyConflictingTypes;
81 }
82
83
84 protected void configure()
85 {
86 bindScope(RequestScoped.class, REQUEST);
87 bindScope(SessionScoped.class, SESSION);
88 bindScope(ScriptSessionScoped.class, SCRIPT);
89 bindScope(ApplicationScoped.class, APPLICATION);
90 bindScope(GlobalApplicationScoped.class, GLOBAL);
91
92 if (bindPotentiallyConflictingTypes)
93 {
94
95
96 Provider<HttpServletRequest> requestProvider =
97 new Provider<HttpServletRequest>()
98 {
99 public HttpServletRequest get()
100 {
101 WebContext webcx = WebContextFactory.get();
102 return webcx.getHttpServletRequest();
103 }
104
105 public String toString()
106 {
107 return "RequestProvider";
108 }
109 };
110 bind(HttpServletRequest.class).toProvider(requestProvider);
111 bind(ServletRequest.class).toProvider(requestProvider);
112
113 Provider<HttpServletResponse> responseProvider =
114 new Provider<HttpServletResponse>()
115 {
116 public HttpServletResponse get()
117 {
118 WebContext webcx = WebContextFactory.get();
119 return webcx.getHttpServletResponse();
120 }
121
122 public String toString()
123 {
124 return "ResponseProvider";
125 }
126 };
127 bind(HttpServletResponse.class).toProvider(responseProvider);
128 bind(ServletResponse.class).toProvider(responseProvider);
129
130 Provider<HttpSession> sessionProvider =
131 new Provider<HttpSession>()
132 {
133 public HttpSession get()
134 {
135 WebContext webcx = WebContextFactory.get();
136 return webcx.getSession();
137 }
138
139 public String toString()
140 {
141 return "SessionProvider";
142 }
143 };
144 bind(HttpSession.class).toProvider(sessionProvider);
145 }
146
147 Provider<Map<String, String[]>> requestParametersProvider =
148 new Provider<Map<String, String[]>>()
149 {
150 @SuppressWarnings({"unchecked"})
151 public Map<String, String[]> get()
152 {
153 WebContext webcx = WebContextFactory.get();
154 return webcx.getHttpServletRequest().getParameterMap();
155 }
156
157 public String toString()
158 {
159 return "RequestParametersProvider";
160 }
161 };
162 bind(new TypeLiteral<Map<String, String[]>>() {})
163 .annotatedWith(RequestParameters.class)
164 .toProvider(requestParametersProvider);
165
166
167 Provider<ScriptSession> scriptSessionProvider =
168 new Provider<ScriptSession>()
169 {
170 public ScriptSession get()
171 {
172 WebContext webcx = WebContextFactory.get();
173 return webcx.getScriptSession();
174 }
175
176 public String toString()
177 {
178 return "ScriptSessionProvider";
179 }
180 };
181 bind(ScriptSession.class).toProvider(scriptSessionProvider);
182
183 Provider<ServletContext> servletContextProvider =
184 new Provider<ServletContext>()
185 {
186 public ServletContext get()
187 {
188
189 return getServletContext();
190 }
191
192 public String toString()
193 {
194 return "ServletContextProvider";
195 }
196 };
197 bind(ServletContext.class).toProvider(servletContextProvider);
198
199 Provider<WebContext> webContextProvider =
200 new Provider<WebContext>()
201 {
202 public WebContext get()
203 {
204 return WebContextFactory.get();
205 }
206
207 public String toString()
208 {
209 return "WebContextProvider";
210 }
211 };
212 bind(WebContext.class).toProvider(webContextProvider);
213
214 Provider<ServerContext> serverContextProvider =
215 new Provider<ServerContext>()
216 {
217 public ServerContext get()
218 {
219 return ServerContextFactory.get(getServletContext());
220 }
221
222 public String toString()
223 {
224 return "ServerContextProvider";
225 }
226 };
227 bind(ServerContext.class).toProvider(serverContextProvider);
228 }
229
230
231 private final boolean bindPotentiallyConflictingTypes;
232 }