1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jasperreports.jsf.context;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26 import javax.faces.application.ViewHandler;
27 import javax.faces.context.ExternalContext;
28
29 import javax.servlet.ServletContext;
30 import net.sf.jasperreports.jsf.Constants;
31 import net.sf.jasperreports.jsf.InvalidEnvironmentException;
32
33 import net.sf.jasperreports.jsf.component.UIReport;
34 import net.sf.jasperreports.jsf.renderkit.ReportRenderer;
35
36
37
38
39
40
41
42 public abstract class ExternalContextHelper {
43
44
45 private static final String PORTLET_CLASS = "javax.portlet.Portlet";
46
47
48 private static final String PORTLET_RESOURCEURL_CLASS =
49 "javax.portlet.ResourceURL";
50
51
52 private static final String BRIDGE_CLASS = "javax.portlet.faces.Bridge";
53
54 protected static final int BUFFER_SIZE = 2014;
55
56
57
58
59
60
61
62 protected static ExternalContextHelper newInstance (
63 final ExternalContext context)
64 throws InvalidEnvironmentException {
65 ExternalContextHelper instance = null;
66 if (isServletContext(context)) {
67 instance = new ServletContextHelper();
68 } else if (isPortletAvailable()) {
69 String portletVersion = getPortletVersion();
70
71 if (!"2.0".equals(portletVersion)) {
72 throw new InvalidEnvironmentException("Incorrect portlet"
73 + " version: " + portletVersion);
74 }
75
76 if (!isFacesBridgeAvailable()) {
77 throw new InvalidEnvironmentException("Portlet 2.0"
78 + " environment detected but not Faces' bridge has"
79 + " found. Please use a portlet faces bridge"
80 + " compliant with JSR-329.");
81 }
82
83 instance = new PortletContextHelper();
84 } else {
85 throw new IllegalArgumentException(
86 "Unrecognized application context: " +
87 context.getContext().getClass().getName());
88 }
89 return instance;
90 }
91
92
93
94
95
96
97
98 public static boolean isFacesBridgeAvailable() {
99 boolean bridgeAvailable = false;
100 try {
101 Class.forName(BRIDGE_CLASS);
102 bridgeAvailable = true;
103 } catch (final ClassNotFoundException e) {
104 bridgeAvailable = false;
105 } catch (final NoClassDefFoundError e) {
106 bridgeAvailable = false;
107 }
108 return bridgeAvailable;
109 }
110
111
112
113
114
115
116
117 public static boolean isPortletAvailable() {
118 boolean portletAvailable = false;
119 try {
120 Class.forName(PORTLET_CLASS);
121 portletAvailable = true;
122 } catch (final ClassNotFoundException e) {
123 portletAvailable = false;
124 } catch (final NoClassDefFoundError e) {
125 portletAvailable = false;
126 }
127 return portletAvailable;
128 }
129
130
131
132
133
134
135 public static String getPortletVersion() {
136 final boolean portletAvailable = isPortletAvailable();
137
138 String portletVersion = null;
139 try {
140 Class.forName(PORTLET_RESOURCEURL_CLASS);
141 portletVersion = "2.0";
142 } catch (final ClassNotFoundException e) {
143 portletVersion = portletAvailable ? "1.0" : null;
144 } catch (final NoClassDefFoundError e) {
145 portletVersion = portletAvailable ? "1.0" : null;
146 }
147
148 return portletVersion;
149 }
150
151
152
153
154
155
156
157
158 public static boolean isServletContext(final ExternalContext context) {
159 final Object ctx = context.getContext();
160 return (ctx instanceof ServletContext);
161 }
162
163
164
165
166 protected ExternalContextHelper() { }
167
168 public abstract Collection<ContentType> getAcceptedContentTypes(
169 final ExternalContext context);
170
171
172
173
174
175
176
177
178 public abstract ReportRenderRequest restoreReportRequest(
179 ExternalContext context);
180
181
182
183
184
185
186
187 public abstract String getRequestURI(final ExternalContext context);
188
189
190
191
192
193
194
195 public abstract String getRequestServerName(final ExternalContext context);
196
197
198
199
200
201
202
203
204 public abstract String getResourceRealPath(
205 final ExternalContext context, String name);
206
207
208
209
210
211
212
213
214 @SuppressWarnings({ "unchecked", "rawtypes" })
215 public final Map<String, String> getViewCacheMap(
216 final ExternalContext context) {
217 Map<String, String> cacheMap = (Map) context.getSessionMap()
218 .get(Constants.VIEW_CACHE_KEY);
219 if (cacheMap == null) {
220 cacheMap = new HashMap<String, String>();
221 context.getSessionMap().put(
222 Constants.VIEW_CACHE_KEY, cacheMap);
223 }
224 return cacheMap;
225 }
226
227
228
229
230
231
232
233 public final String getViewId(final ExternalContext context) {
234 String pathInfo = context.getRequestPathInfo();
235 String servletPath = context.getRequestServletPath();
236 if (pathInfo == null) {
237 String suffix = context.getInitParameter(
238 ViewHandler.DEFAULT_SUFFIX_PARAM_NAME);
239 if (suffix == null || suffix.length() == 0) {
240 suffix = ViewHandler.DEFAULT_SUFFIX;
241 }
242 return servletPath.substring(0, servletPath
243 .lastIndexOf('.')) + suffix;
244 } else {
245 return pathInfo;
246 }
247 }
248
249
250
251
252
253
254
255
256
257
258
259 public abstract void writeHeaders(ExternalContext context,
260 ReportRenderer renderer, UIReport report) throws IOException;
261
262
263
264
265
266
267
268
269
270 public abstract void writeResponse(
271 final ExternalContext context,
272 final ContentType contentType,
273 final InputStream stream)
274 throws IOException;
275
276 }