1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.lib.jsf;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.lang.reflect.Method;
24
25 import javax.faces.context.ExternalContext;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public final class ExternalContextUtils
48 {
49
50 private ExternalContextUtils()
51 {
52 }
53
54
55
56
57
58
59
60
61 public static final int getContentLength(ExternalContext externalContext)
62 {
63 RequestType type = getRequestType(externalContext);
64
65 if(type.isRequestFromClient())
66 {
67 try
68 {
69 Object request = externalContext.getRequest();
70 Method contentLenMethod = request.getClass().getMethod("getContentLength",new Class[]{});
71 return ((Integer) contentLenMethod.invoke(request,new Object[]{})).intValue();
72 }
73 catch(Exception e)
74 {
75 _LOG.error("Unsupported request type.", e);
76 }
77 }
78
79 return -1;
80 }
81
82
83
84
85
86
87
88
89 public static final InputStream getRequestInputStream(ExternalContext externalContext)
90 throws IOException
91 {
92 RequestType type = getRequestType(externalContext);
93
94 if(type.isRequestFromClient())
95 {
96 try
97 {
98 Object request = externalContext.getRequest();
99
100 Method method = request.getClass().getMethod(
101 type.isPortlet()?"getPortletInputStream":"getInputStream",new Class[]{});
102 return (InputStream) method.invoke(request,new Object[]{});
103 }
104 catch (Exception e)
105 {
106 _LOG.error("Unable to get the request input stream because of an error", e);
107 }
108 }
109 return null;
110 }
111
112
113
114
115
116
117
118
119 public static final RequestType getRequestType(ExternalContext externalContext)
120 {
121
122
123
124
125 if(_PORTLET_CONTEXT_CLASS != null)
126 {
127 if (_PORTLET_CONTEXT_CLASS.isInstance(externalContext.getContext()))
128 {
129
130 Object request = externalContext.getRequest();
131
132 if(_PORTLET_RENDER_REQUEST_CLASS.isInstance(request))
133 {
134 return RequestType.RENDER;
135 }
136
137 if(_PORTLET_RESOURCE_REQUEST_CLASS != null)
138 {
139 if(_PORTLET_ACTION_REQUEST_CLASS.isInstance(request))
140 {
141 return RequestType.ACTION;
142 }
143
144
145 if(_PORTLET_RESOURCE_REQUEST_CLASS.isInstance(request))
146 {
147 return RequestType.RESOURCE;
148 }
149
150 return RequestType.EVENT;
151 }
152
153 return RequestType.ACTION;
154 }
155 }
156
157 return RequestType.SERVLET;
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171
172 public static final RequestType getRequestType(Object context, Object request)
173 {
174
175
176
177
178
179 if(_PORTLET_CONTEXT_CLASS != null)
180 {
181 if (_PORTLET_CONFIG_CLASS.isInstance(context) ||
182 _PORTLET_CONTEXT_CLASS.isInstance(context))
183 {
184
185
186 if(_PORTLET_RENDER_REQUEST_CLASS.isInstance(request))
187 {
188 return RequestType.RENDER;
189 }
190
191 if(_PORTLET_RESOURCE_REQUEST_CLASS != null)
192 {
193 if(_PORTLET_ACTION_REQUEST_CLASS.isInstance(request))
194 {
195 return RequestType.ACTION;
196 }
197
198
199 if(_PORTLET_RESOURCE_REQUEST_CLASS.isInstance(request))
200 {
201 return RequestType.RESOURCE;
202 }
203
204 return RequestType.EVENT;
205 }
206
207 return RequestType.ACTION;
208 }
209 }
210
211 return RequestType.SERVLET;
212 }
213
214 private static final Log _LOG = LogFactory.getLog(ExternalContextUtils.class);
215
216 private static final Class _PORTLET_ACTION_REQUEST_CLASS;
217 private static final Class _PORTLET_RENDER_REQUEST_CLASS;
218 private static final Class _PORTLET_RESOURCE_REQUEST_CLASS;
219 private static final Class _PORTLET_CONTEXT_CLASS;
220 private static final Class _PORTLET_CONFIG_CLASS;
221
222 static
223 {
224 Class context;
225 Class config;
226 Class actionRequest;
227 Class renderRequest;
228 Class resourceRequest;
229 try
230 {
231 ClassLoader loader = Thread.currentThread().getContextClassLoader();
232 context = loader.loadClass("javax.portlet.PortletContext");
233 config = loader.loadClass("javax.portlet.PortletConfig");
234 actionRequest = loader.loadClass("javax.portlet.ActionRequest");
235 renderRequest = loader.loadClass("javax.portlet.RenderRequest");
236
237 try
238 {
239 resourceRequest = loader.loadClass("javax.portlet.ResourceRequest");
240 }
241 catch (ClassNotFoundException e)
242 {
243 resourceRequest = null;
244 }
245 }
246 catch (ClassNotFoundException e)
247 {
248 context = null;
249 config = null;
250 actionRequest = null;
251 renderRequest = null;
252 resourceRequest = null;
253 }
254
255 _PORTLET_CONTEXT_CLASS = context;
256 _PORTLET_CONFIG_CLASS = config;
257 _PORTLET_ACTION_REQUEST_CLASS = actionRequest;
258 _PORTLET_RENDER_REQUEST_CLASS = renderRequest;
259 _PORTLET_RESOURCE_REQUEST_CLASS = resourceRequest;
260 }
261 }