KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > web > ServletWebRequest


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.web;
16
17 import java.io.IOException JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Locale JavaDoc;
20
21 import javax.servlet.RequestDispatcher JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.http.HttpSession JavaDoc;
26
27 import org.apache.hivemind.ApplicationRuntimeException;
28 import org.apache.hivemind.HiveMind;
29 import org.apache.hivemind.util.Defense;
30 import org.apache.tapestry.describe.DescriptionReceiver;
31
32 /**
33  * Adapter from {@link javax.servlet.http.HttpServletRequest} to
34  * {@link org.apache.tapestry.web.WebRequest}.
35  *
36  * @author Howard M. Lewis Ship
37  * @since 4.0
38  */

39 public class ServletWebRequest implements WebRequest
40 {
41     private final HttpServletRequest JavaDoc _servletRequest;
42
43     private final HttpServletResponse JavaDoc _servletResponse;
44
45     private WebSession _webSession;
46
47     public ServletWebRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
48     {
49         Defense.notNull(request, "request");
50         Defense.notNull(response, "response");
51
52         _servletRequest = request;
53         _servletResponse = response;
54     }
55
56     public List JavaDoc getParameterNames()
57     {
58         return WebUtils.toSortedList(_servletRequest.getParameterNames());
59     }
60
61     public String JavaDoc getParameterValue(String JavaDoc parameterName)
62     {
63         return _servletRequest.getParameter(parameterName);
64     }
65
66     public String JavaDoc[] getParameterValues(String JavaDoc parameterName)
67     {
68         return _servletRequest.getParameterValues(parameterName);
69     }
70
71     public String JavaDoc getContextPath()
72     {
73         return _servletRequest.getContextPath();
74     }
75
76     public WebSession getSession(boolean create)
77     {
78         if (_webSession != null)
79             return _webSession;
80
81         HttpSession JavaDoc session = _servletRequest.getSession(create);
82
83         if (session != null)
84             _webSession = new ServletWebSession(session);
85
86         return _webSession;
87     }
88
89     public List JavaDoc getAttributeNames()
90     {
91         return WebUtils.toSortedList(_servletRequest.getAttributeNames());
92     }
93
94     public Object JavaDoc getAttribute(String JavaDoc name)
95     {
96         return _servletRequest.getAttribute(name);
97     }
98
99     public void setAttribute(String JavaDoc name, Object JavaDoc attribute)
100     {
101         if (attribute == null)
102             _servletRequest.removeAttribute(name);
103         else
104             _servletRequest.setAttribute(name, attribute);
105     }
106
107     public String JavaDoc getScheme()
108     {
109         return _servletRequest.getScheme();
110     }
111
112     public String JavaDoc getServerName()
113     {
114         return _servletRequest.getServerName();
115     }
116
117     public int getServerPort()
118     {
119         return _servletRequest.getServerPort();
120     }
121
122     public String JavaDoc getRequestURI()
123     {
124         return _servletRequest.getRequestURI();
125     }
126
127     public void forward(String JavaDoc URL)
128     {
129         if (HiveMind.isBlank(URL))
130         {
131             performForward("/");
132             return;
133         }
134
135         boolean internal = !(URL.startsWith("/") || URL.indexOf("://") > 0);
136
137         if (internal)
138             performForward("/" + URL);
139         else
140             sendRedirect(URL);
141     }
142
143     private void sendRedirect(String JavaDoc URL)
144     {
145         String JavaDoc finalURL = _servletResponse.encodeRedirectURL(URL);
146
147         try
148         {
149             _servletResponse.sendRedirect(finalURL);
150         }
151         catch (IOException JavaDoc ex)
152         {
153             throw new ApplicationRuntimeException(WebMessages.unableToRedirect(URL, ex), ex);
154         }
155
156     }
157
158     private void performForward(String JavaDoc URL)
159     {
160         RequestDispatcher JavaDoc dispatcher = _servletRequest.getRequestDispatcher(URL);
161
162         if (dispatcher == null)
163             throw new ApplicationRuntimeException(WebMessages.unableToFindDispatcher(URL));
164
165         try
166         {
167             dispatcher.forward(_servletRequest, _servletResponse);
168         }
169         catch (ServletException JavaDoc ex)
170         {
171             throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
172         }
173         catch (IOException JavaDoc ex)
174         {
175             throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
176         }
177     }
178
179     /**
180      * Returns {@link HttpServletRequest#getServletPath()}.
181      */

182     public String JavaDoc getActivationPath()
183     {
184         String JavaDoc servletPath = _servletRequest.getServletPath();
185         String JavaDoc pathInfo = _servletRequest.getPathInfo();
186
187         return pathInfo == null ? servletPath : servletPath + pathInfo;
188     }
189
190     public Locale JavaDoc getLocale()
191     {
192         return _servletRequest.getLocale();
193     }
194
195     public void describeTo(DescriptionReceiver receiver)
196     {
197         receiver.describeAlternate(_servletRequest);
198     }
199
200     public String JavaDoc getHeader(String JavaDoc name)
201     {
202         return _servletRequest.getHeader(name);
203     }
204 }
Popular Tags