1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.commons.util;
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 public final class ExternalContextUtils
39 {
40
41 private ExternalContextUtils()
42 {
43 }
44
45
46
47
48
49
50
51
52 public static final int getContentLength(ExternalContext externalContext)
53 {
54 RequestType type = getRequestType(externalContext);
55
56 if(type.isRequestFromClient())
57 {
58 try
59 {
60 Object request = externalContext.getRequest();
61 Method contentLenMethod = request.getClass().getMethod("getContentLength");
62 return (Integer) contentLenMethod.invoke(request);
63 }
64 catch(Exception e)
65 {
66 _LOG.error("Unsupported request type.", e);
67 }
68 }
69
70 return -1;
71 }
72
73
74
75
76
77
78
79
80 public static final InputStream getRequestInputStream(ExternalContext externalContext)
81 throws IOException
82 {
83 RequestType type = getRequestType(externalContext);
84
85 if(type.isRequestFromClient())
86 {
87 try
88 {
89 Object request = externalContext.getRequest();
90
91 Method method = request.getClass().getMethod(type.isPortlet()?"getPortletInputStream":"getInputStream");
92 return (InputStream) method.invoke(request);
93 }
94 catch (Exception e)
95 {
96 _LOG.error("Unable to get the request input stream because of an error", e);
97 }
98 }
99 return null;
100 }
101
102
103
104
105
106
107
108
109 public static final RequestType getRequestType(ExternalContext externalContext)
110 {
111
112
113
114
115 if(_PORTLET_CONTEXT_CLASS != null)
116 {
117 if (_PORTLET_CONTEXT_CLASS.isInstance(externalContext.getContext()))
118 {
119
120 Object request = externalContext.getRequest();
121
122 if(_PORTLET_RENDER_REQUEST_CLASS.isInstance(request))
123 {
124 return RequestType.RENDER;
125 }
126
127 if(_PORTLET_RESOURCE_REQUEST_CLASS != null)
128 {
129 if(_PORTLET_ACTION_REQUEST_CLASS.isInstance(request))
130 {
131 return RequestType.ACTION;
132 }
133
134
135 if(_PORTLET_RESOURCE_REQUEST_CLASS.isInstance(request))
136 {
137 return RequestType.RESOURCE;
138 }
139
140 return RequestType.EVENT;
141 }
142
143 return RequestType.ACTION;
144 }
145 }
146
147 return RequestType.SERVLET;
148 }
149
150 private static final Log _LOG = LogFactory.getLog(TagUtils.class);
151
152 private static final Class<?> _PORTLET_ACTION_REQUEST_CLASS;
153 private static final Class<?> _PORTLET_RENDER_REQUEST_CLASS;
154 private static final Class<?> _PORTLET_RESOURCE_REQUEST_CLASS;
155 private static final Class<?> _PORTLET_CONTEXT_CLASS;
156
157 static
158 {
159 Class<?> context;
160 Class<?> actionRequest;
161 Class<?> renderRequest;
162 Class<?> resourceRequest;
163 try
164 {
165 ClassLoader loader = Thread.currentThread().getContextClassLoader();
166 context = loader.loadClass("javax.portlet.PortletContext");
167 actionRequest = loader.loadClass("javax.portlet.ActionRequest");
168 renderRequest = loader.loadClass("javax.portlet.RenderRequest");
169
170 try
171 {
172 resourceRequest = loader.loadClass("javax.portlet.ResourceRequest");
173 }
174 catch (ClassNotFoundException e)
175 {
176 resourceRequest = null;
177 }
178 }
179 catch (ClassNotFoundException e)
180 {
181 context = null;
182 actionRequest = null;
183 renderRequest = null;
184 resourceRequest = null;
185 }
186
187 _PORTLET_CONTEXT_CLASS = context;
188 _PORTLET_ACTION_REQUEST_CLASS = actionRequest;
189 _PORTLET_RENDER_REQUEST_CLASS = renderRequest;
190 _PORTLET_RESOURCE_REQUEST_CLASS = resourceRequest;
191 }
192 }