This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
PortalRequestHandler.java
302 lines (263 loc) · 12.9 KB
/
PortalRequestHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
* Copyright (C) 2009 eXo Platform SAS.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.exoplatform.portal.application;
import java.util.List;
import java.util.Locale;
import java.util.ServiceLoader;
import java.util.regex.Pattern;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.exoplatform.commons.utils.I18N;
import org.exoplatform.commons.utils.Safe;
import org.exoplatform.container.PortalContainer;
import org.exoplatform.portal.config.DataStorage;
import org.exoplatform.portal.config.StaleModelException;
import org.exoplatform.portal.config.model.PortalConfig;
import org.exoplatform.portal.config.model.PortalProperties;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.web.ControllerContext;
import org.exoplatform.web.WebAppController;
import org.exoplatform.web.WebRequestHandler;
import org.exoplatform.web.application.ApplicationLifecycle;
import org.exoplatform.web.application.ApplicationRequestPhaseLifecycle;
import org.exoplatform.web.application.Phase;
import org.exoplatform.web.application.RequestFailure;
import org.exoplatform.web.controller.QualifiedName;
import org.exoplatform.webui.application.WebuiRequestContext;
import org.exoplatform.webui.core.UIApplication;
/**
* Created by The eXo Platform SAS Dec 9, 2006
*
* This is 'portal' handler, it handle the request of URLs that are routed by navigation controller (using urls parameter).
* This handler is registered to WebAppController by xml configuration.
* @see WebAppController
*/
public class PortalRequestHandler extends WebRequestHandler {
protected static Log log = ExoLogger.getLogger("portal:PortalRequestHandler");
private static PortalApplicationFactory appProvider;
/** . */
public static final QualifiedName REQUEST_PATH = QualifiedName.create("gtn", "path");
/** . */
public static final QualifiedName REQUEST_SITE_TYPE = QualifiedName.create("gtn", "sitetype");
/** . */
public static final QualifiedName REQUEST_SITE_NAME = QualifiedName.create("gtn", "sitename");
/** . */
public static final QualifiedName LANG = QualifiedName.create("gtn", "lang");
/** Used to sanitize the CacheControl HTTP header */
private static final Pattern CACHE_CONTROL_SANITIZE_PATTERN = Pattern.compile("[\\r\\n]");
static {
ServiceLoader<PortalApplicationFactory> loader = ServiceLoader.load(PortalApplicationFactory.class);
for (PortalApplicationFactory factory : loader) {
// We are expecting only one
appProvider = factory;
break;
}
}
public String getHandlerName() {
return "portal";
}
/**
* Dispatched from WebAppController, after the portal servlet init function called, this method create and register
* PortalApplication to WebAppController
*
* PortalApplication creation can be customized by registering PortalApplicationFactory implementation using ServiceLoader
*
* @see PortalApplication
*/
@Override
public void onInit(WebAppController controller, ServletConfig sConfig) throws Exception {
PortalApplication application;
if (appProvider != null) {
application = appProvider.createApplication(sConfig);
} else {
application = new PortalApplication(sConfig);
}
application.onInit();
controller.addApplication(application);
}
/**
* This method will handle incoming portal request. It gets a reference to the WebAppController
*
* Here are the steps done in the method:
*
* 1) Get the PortalApplication reference from the controller <br/>
* 2) Create a PortalRequestContext object that is a convenient wrapper on all the request information <br/>
* 3) Get the collection of ApplicationLifecycle referenced in the PortalApplication and defined in
* the webui-configuration.xml of the portal application <br/>
* 4) Set that context in a ThreadLocal to easily access it <br/>
* 5) Check if user have permission to access portal, if not, send 403 status code,
* if user has not login, redirect to login page <br/>
* 6) dispatch to processRequest method, this is protected method, we can extend and override this method to
* write a new requestHandler base on PortalRequestHandler (@see {@link StandaloneAppRequestHandler})
* 7) set the header Cache-Control to no-cache or to the value specified in the portal configuration <br/>
*
*/
@SuppressWarnings("unchecked")
@Override
public boolean execute(ControllerContext controllerContext) throws Exception {
HttpServletRequest req = controllerContext.getRequest();
HttpServletResponse res = controllerContext.getResponse();
log.debug("Session ID = " + req.getSession().getId());
// watch out: this might get overriden later, if the portal itself has a configuration for this value
res.setHeader("Cache-Control", "no-cache");
//
String requestPath = controllerContext.getParameter(REQUEST_PATH);
String requestSiteType = controllerContext.getParameter(REQUEST_SITE_TYPE);
String requestSiteName = controllerContext.getParameter(REQUEST_SITE_NAME);
//
Locale requestLocale;
String lang = controllerContext.getParameter(LANG);
if (lang == null || lang.length() == 0) {
requestLocale = null;
} else {
requestLocale = I18N.parseTagIdentifier(lang);
}
if (requestSiteName == null) {
res.sendRedirect(req.getContextPath());
return true;
}
PortalApplication app = controllerContext.getController().getApplication(PortalApplication.PORTAL_APPLICATION_ID);
PortalRequestContext context = new PortalRequestContext(app, controllerContext, requestSiteType, requestSiteName,
requestPath, requestLocale);
DataStorage storage = (DataStorage) PortalContainer.getComponent(DataStorage.class);
PortalConfig persistentPortalConfig = storage.getPortalConfig(requestSiteType, requestSiteName);
if (context.getUserPortalConfig() == null) {
if (persistentPortalConfig == null) {
return false;
} else if (req.getRemoteUser() == null) {
context.requestAuthenticationLogin();
} else {
context.sendError(HttpServletResponse.SC_FORBIDDEN);
}
} else {
if (persistentPortalConfig != null) {
String cacheControl = persistentPortalConfig.getProperty(PortalProperties.CACHE_CONTROL);
if (cacheControl != null) {
// GTNPORTAL-3361
// Previously, the Cache-Control was set to no-cache at all times, the reason for that being unclear.
// A feature request to allow portals to set their own policy caused this change, but we might
// revert if there are bad side-effects. If so, please replace this comment with the background information,
// so that it gets documented why the no-cache setting is forced.
res.setHeader("Cache-Control", getSanitizedCacheControl(cacheControl));
}
}
processRequest(context, app);
}
return true;
}
/**
* This method do the main job on processing a portal request:
*
* 1) Call onStartRequest() on each ApplicationLifecycle object <br/>
* 2) Get the StateManager object from the PortalApplication (also referenced in the XML file) <br/>
* 3) Use the StateManager to get a reference on the root UI component: UIApplication;
* the method used is restoreUIRootComponent(context) <br/>
* 4) If the UI component is not the current one in used in the PortalContextRequest, then replace it <br/>
* 5) Process decode on the PortalApplication <br/>
* 6) Process Action on the PortalApplication <br/>
* 7) Process Render on the UIApplication UI component <br/>
* 8) call onEndRequest on all the ApplicationLifecycle <br/>
* 9) Release the context from the thread
*
* @param context
* @param app
* @throws Exception
*/
@SuppressWarnings("unchecked")
protected void processRequest(PortalRequestContext context, PortalApplication app) throws Exception {
WebuiRequestContext.setCurrentInstance(context);
PortalRequestImpl.createInstance(context);
UIApplication uiApp = app.getStateManager().restoreUIRootComponent(context);
List<ApplicationLifecycle> lifecycles = app.getApplicationLifecycle();
try {
if (context.getUIApplication() != uiApp)
context.setUIApplication(uiApp);
for (ApplicationLifecycle lifecycle : lifecycles)
lifecycle.onStartRequest(app, context);
if (uiApp != null) {
uiApp.processDecode(context);
}
if (!context.isResponseComplete() && !context.getProcessRender()) {
startRequestPhaseLifecycle(app, context, lifecycles, Phase.ACTION);
uiApp.processAction(context);
endRequestPhaseLifecycle(app, context, lifecycles, Phase.ACTION);
}
if (!context.isResponseComplete()) {
startRequestPhaseLifecycle(app, context, lifecycles, Phase.RENDER);
uiApp.processRender(context);
endRequestPhaseLifecycle(app, context, lifecycles, Phase.RENDER);
}
if (uiApp != null)
uiApp.setLastAccessApplication(System.currentTimeMillis());
// Store ui root
app.getStateManager().storeUIRootComponent(context);
} catch (StaleModelException staleModelEx) {
// Minh Hoang TO:
// At the moment, this catch block is never reached, as the StaleModelException is intercepted temporarily
// in UI-related code
for (ApplicationLifecycle lifecycle : lifecycles) {
lifecycle.onFailRequest(app, context, RequestFailure.CONCURRENCY_FAILURE);
}
} catch (Exception NonStaleModelEx) {
// We want to ignore the ClientAbortException since this is caused by the users
// browser closing the connection and is not something we should be logging.
if(!NonStaleModelEx.getClass().toString().contains("ClientAbortException")) {
log.error("Error while handling request", NonStaleModelEx);
}
} finally {
// We close the writer here once and for all
Safe.close(context.getWriter());
//
try {
for (ApplicationLifecycle lifecycle : lifecycles)
lifecycle.onEndRequest(app, context);
} catch (Exception exception) {
log.error("Error while ending request on all ApplicationLifecycle", exception);
}
PortalRequestImpl.clearInstance();
WebuiRequestContext.setCurrentInstance(null);
}
}
private void startRequestPhaseLifecycle(PortalApplication app, PortalRequestContext context,
List<ApplicationLifecycle> lifecycles, Phase phase) {
for (ApplicationLifecycle lifecycle : lifecycles) {
if (lifecycle instanceof ApplicationRequestPhaseLifecycle)
((ApplicationRequestPhaseLifecycle) lifecycle).onStartRequestPhase(app, context, phase);
}
}
private void endRequestPhaseLifecycle(PortalApplication app, PortalRequestContext context,
List<ApplicationLifecycle> lifecycles, Phase phase) {
for (ApplicationLifecycle lifecycle : lifecycles) {
if (lifecycle instanceof ApplicationRequestPhaseLifecycle)
((ApplicationRequestPhaseLifecycle) lifecycle).onEndRequestPhase(app, context, phase);
}
}
@Override
protected boolean getRequiresLifeCycle() {
return true;
}
public String getSanitizedCacheControl(String cacheControl) {
if (null == cacheControl) {
return null;
}
return CACHE_CONTROL_SANITIZE_PATTERN.matcher(cacheControl).replaceAll("");
}
}