01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */package com.google.inject.servlet;
16:
17: import com.google.inject.AbstractModule;
18: import com.google.inject.TypeLiteral;
19: import com.google.inject.Provider;
20: import static com.google.inject.servlet.ServletScopes.REQUEST;
21: import static com.google.inject.servlet.ServletScopes.SESSION;
22:
23: import java.util.Map;
24:
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: /**
32: * Configures the servlet scopes and creates bindings for the servlet API
33: * objects so you can inject the request, response, session, etc.
34: *
35: * @author crazybob@google.com (Bob Lee)
36: */
37: public class ServletModule extends AbstractModule {
38:
39: protected void configure() {
40: // Scopes.
41: bindScope(RequestScoped.class, REQUEST);
42: bindScope(SessionScoped.class, SESSION);
43:
44: // Bind request.
45: Provider<HttpServletRequest> requestProvider = new Provider<HttpServletRequest>() {
46: public HttpServletRequest get() {
47: return GuiceFilter.getRequest();
48: }
49:
50: public String toString() {
51: return "RequestProvider";
52: }
53: };
54: bind(HttpServletRequest.class).toProvider(requestProvider);
55: bind(ServletRequest.class).toProvider(requestProvider);
56:
57: // Bind response.
58: Provider<HttpServletResponse> responseProvider = new Provider<HttpServletResponse>() {
59: public HttpServletResponse get() {
60: return GuiceFilter.getResponse();
61: }
62:
63: public String toString() {
64: return "ResponseProvider";
65: }
66: };
67: bind(HttpServletResponse.class).toProvider(responseProvider);
68: bind(ServletResponse.class).toProvider(responseProvider);
69:
70: // Bind session.
71: bind(HttpSession.class).toProvider(new Provider<HttpSession>() {
72: public HttpSession get() {
73: return GuiceFilter.getRequest().getSession();
74: }
75:
76: public String toString() {
77: return "SessionProvider";
78: }
79: });
80:
81: // Bind request parameters.
82: bind(new TypeLiteral<Map<String, String[]>>() {
83: }).annotatedWith(RequestParameters.class).toProvider(
84: new Provider<Map<String, String[]>>() {
85: @SuppressWarnings({"unchecked"})
86: public Map<String, String[]> get() {
87: return GuiceFilter.getRequest()
88: .getParameterMap();
89: }
90:
91: public String toString() {
92: return "RequestParametersProvider";
93: }
94: });
95: }
96: }
|