1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.util; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.lang.reflect.Method; |
20 | |
import java.util.Enumeration; |
21 | |
import java.util.Iterator; |
22 | |
|
23 | |
import javax.faces.FacesException; |
24 | |
import javax.faces.application.ViewHandler; |
25 | |
import javax.faces.context.ExternalContext; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
import javax.servlet.RequestDispatcher; |
28 | |
import javax.servlet.ServletException; |
29 | |
import javax.servlet.ServletRequest; |
30 | |
import javax.servlet.ServletResponse; |
31 | |
import javax.servlet.http.HttpServletRequest; |
32 | |
import javax.servlet.http.HttpServletResponse; |
33 | |
import javax.servlet.http.HttpSession; |
34 | |
|
35 | |
import org.seasar.framework.log.Logger; |
36 | |
import org.seasar.framework.util.AssertionUtil; |
37 | |
import org.seasar.framework.util.Disposable; |
38 | |
import org.seasar.framework.util.DisposableUtil; |
39 | |
import org.seasar.framework.util.EnumerationIterator; |
40 | |
import org.seasar.framework.util.MethodUtil; |
41 | |
import org.seasar.teeda.core.JsfConstants; |
42 | |
import org.seasar.teeda.core.resolver.RedirectUrlResolver; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 2 | public class ServletExternalContextUtil { |
49 | |
|
50 | 1 | private static Logger logger = Logger |
51 | 4 | .getLogger(ServletExternalContextUtil.class); |
52 | |
|
53 | |
private static final String CONTENT_TYPE = "Content-Type"; |
54 | |
|
55 | |
private static final String CHARSET_HEADER = "charset="; |
56 | |
|
57 | 1 | private static final int CHARSET_HEADER_LENGTH = CHARSET_HEADER.length(); |
58 | |
|
59 | 1 | private static RedirectUrlResolver resolver = null; |
60 | |
|
61 | 1 | private static boolean resolverInited = false; |
62 | |
|
63 | |
static { |
64 | 1 | DisposableUtil.add(new Disposable() { |
65 | 1 | public void dispose() { |
66 | 1 | resolver = null; |
67 | 1 | resolverInited = false; |
68 | 1 | } |
69 | |
}); |
70 | 1 | } |
71 | |
|
72 | 0 | private ServletExternalContextUtil() { |
73 | 0 | } |
74 | |
|
75 | |
public static void setCharacterEncoding(ServletRequest request) { |
76 | 16 | Method characterEncodingMethod = getCharacterEncodingMethodFromRequest(); |
77 | 16 | if (characterEncodingMethod == null) { |
78 | 0 | return; |
79 | |
} |
80 | 16 | if (!isHttpServletRequest(request)) { |
81 | |
|
82 | |
|
83 | 3 | return; |
84 | |
} |
85 | 13 | HttpServletRequest httpServletRequest = (HttpServletRequest) request; |
86 | 13 | String contentType = httpServletRequest.getHeader(CONTENT_TYPE); |
87 | 13 | String encoding = getEncodingFromContentType(contentType); |
88 | 13 | if (encoding == null) { |
89 | 12 | encoding = getEncodingFromSession(httpServletRequest); |
90 | |
} |
91 | 13 | if (encoding == null || "null".equals(encoding)) { |
92 | 12 | return; |
93 | |
} |
94 | 1 | MethodUtil.invoke(characterEncodingMethod, httpServletRequest, |
95 | |
new Object[] { encoding }); |
96 | 1 | } |
97 | |
|
98 | |
public static boolean setCharacterEncoding(ServletResponse response) { |
99 | 1 | Method characterEncodingMethod = getCharacterEncodingMethodFromResponse(); |
100 | 1 | if (characterEncodingMethod == null |
101 | |
&& (!isHttpServletResponse(response))) { |
102 | 0 | return false; |
103 | |
} |
104 | 1 | HttpServletResponse httpServletResponse = (HttpServletResponse) response; |
105 | 1 | String contentType = httpServletResponse.getContentType(); |
106 | 1 | String encoding = getEncodingFromContentType(contentType); |
107 | 1 | if (encoding == null) { |
108 | 0 | return false; |
109 | |
} |
110 | 1 | MethodUtil.invoke(characterEncodingMethod, httpServletResponse, |
111 | |
new Object[] { encoding }); |
112 | 1 | return true; |
113 | |
} |
114 | |
|
115 | |
public static boolean isHttpServletRequest(ServletRequest request) { |
116 | 33 | return (request != null) && (request instanceof HttpServletRequest); |
117 | |
} |
118 | |
|
119 | |
public static boolean isHttpServletResponse(ServletResponse response) { |
120 | 17 | return (response != null) && (response instanceof HttpServletResponse); |
121 | |
} |
122 | |
|
123 | |
public static String getEncodingFromContentType(String contentType) { |
124 | 17 | if (contentType == null) { |
125 | 12 | return null; |
126 | |
} |
127 | 5 | String encoding = null; |
128 | 5 | int found = contentType.indexOf(CHARSET_HEADER); |
129 | 5 | if (found == 0) { |
130 | 0 | encoding = contentType.substring(CHARSET_HEADER_LENGTH); |
131 | 5 | } else if (found >= 1) { |
132 | 4 | char charBefore = contentType.charAt(found - 1); |
133 | 4 | if (charBefore == ';' || Character.isWhitespace(charBefore)) { |
134 | 4 | encoding = contentType.substring(found + CHARSET_HEADER_LENGTH); |
135 | |
} |
136 | |
} |
137 | 5 | return encoding; |
138 | |
} |
139 | |
|
140 | |
public static String getEncodingFromSession( |
141 | |
HttpServletRequest servletRequest) { |
142 | 13 | String encoding = null; |
143 | 13 | HttpSession session = servletRequest.getSession(false); |
144 | 13 | if (session != null) { |
145 | 1 | encoding = (String) session |
146 | |
.getAttribute(ViewHandler.CHARACTER_ENCODING_KEY); |
147 | |
} |
148 | 13 | return encoding; |
149 | |
} |
150 | |
|
151 | |
public static void dispatch(String path, ServletRequest request, |
152 | |
ServletResponse response) throws IOException { |
153 | 0 | AssertionUtil.assertNotNull("path is null.", path); |
154 | 0 | RequestDispatcher dispatcher = request.getRequestDispatcher(path); |
155 | |
try { |
156 | 0 | dispatcher.forward(request, response); |
157 | 0 | } catch (ServletException e) { |
158 | 0 | if (e.getMessage() != null) { |
159 | 0 | throw new FacesException(e.getMessage(), e); |
160 | |
} |
161 | 0 | throw new FacesException(e); |
162 | 0 | } |
163 | 0 | } |
164 | |
|
165 | |
public static Iterator getLocales(Enumeration locales) { |
166 | 0 | return new EnumerationIterator(locales); |
167 | |
} |
168 | |
|
169 | |
public static Iterator getRequestParameterNames(Enumeration paramNames) { |
170 | 0 | return new EnumerationIterator(paramNames); |
171 | |
} |
172 | |
|
173 | |
public static void redirect(String url, ServletRequest request, |
174 | |
ServletResponse response) throws IOException { |
175 | 0 | HttpServletResponse httpResponse = (HttpServletResponse) response; |
176 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
177 | 0 | if (!resolverInited) { |
178 | 0 | if (DIContainerUtil.hasContainer()) { |
179 | 0 | resolver = (RedirectUrlResolver) DIContainerUtil |
180 | |
.getComponentNoException(RedirectUrlResolver.class); |
181 | |
} |
182 | 0 | resolverInited = true; |
183 | |
} |
184 | 0 | if (resolver != null) { |
185 | 0 | HttpServletRequest httpRequest = (HttpServletRequest) request; |
186 | 0 | url = resolver.resolveUrl(url, context, httpRequest, httpResponse); |
187 | |
} |
188 | 0 | httpResponse.sendRedirect(url); |
189 | 0 | if (context != null) { |
190 | 0 | context.responseComplete(); |
191 | |
} else { |
192 | 0 | logger |
193 | |
.info("[ServletExternalContextUtil.redirect] facesContext is null."); |
194 | |
} |
195 | 0 | } |
196 | |
|
197 | |
private static Method getCharacterEncodingMethodFromRequest() { |
198 | |
try { |
199 | 16 | Class clazz = ServletRequest.class; |
200 | 16 | return clazz.getMethod("setCharacterEncoding", |
201 | |
new Class[] { String.class }); |
202 | 0 | } catch (Exception e) { |
203 | 0 | logger.log(e); |
204 | 0 | return null; |
205 | |
} |
206 | |
} |
207 | |
|
208 | |
private static Method getCharacterEncodingMethodFromResponse() { |
209 | |
try { |
210 | 1 | Class clazz = ServletResponse.class; |
211 | 1 | return clazz.getMethod("setCharacterEncoding", |
212 | |
new Class[] { String.class }); |
213 | 0 | } catch (Exception e) { |
214 | 0 | logger.log(e); |
215 | 0 | return null; |
216 | |
} |
217 | |
} |
218 | |
|
219 | |
public static HttpServletRequest getRequest(ExternalContext externalContext) { |
220 | 7 | return (HttpServletRequest) externalContext.getRequest(); |
221 | |
} |
222 | |
|
223 | |
public static HttpServletResponse getResponse( |
224 | |
ExternalContext externalContext) { |
225 | 0 | return (HttpServletResponse) externalContext.getResponse(); |
226 | |
} |
227 | |
|
228 | |
public static boolean isGet(ExternalContext externalContext) { |
229 | 0 | return getRequest(externalContext).getMethod().equals("GET"); |
230 | |
} |
231 | |
|
232 | |
public static boolean isPost(ExternalContext externalContext) { |
233 | 6 | return getRequest(externalContext).getMethod().equals("POST"); |
234 | |
} |
235 | |
|
236 | |
public static void storeErrorInfoToAttribute(final ServletRequest request, |
237 | |
final Throwable exception) { |
238 | 1 | AssertionUtil.assertNotNull("request", request); |
239 | 1 | AssertionUtil.assertNotNull("exception", exception); |
240 | 1 | request.setAttribute(JsfConstants.ERROR_EXCEPTION, exception); |
241 | 1 | request.setAttribute(JsfConstants.ERROR_EXCEPTION_TYPE, exception |
242 | |
.getClass()); |
243 | 1 | request |
244 | |
.setAttribute(JsfConstants.ERROR_MESSAGE, exception |
245 | |
.getMessage()); |
246 | |
|
247 | 1 | } |
248 | |
} |