forked from sakaiproject/sakai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSamigoJsfTool.java
454 lines (397 loc) · 19.7 KB
/
SamigoJsfTool.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/**********************************************************************************
* $URL$
* $Id$
**********************************************************************************
*
* Copyright (c) 2005, 2006, 2007, 2008 The Sakai Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.opensource.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**********************************************************************************/
package org.sakaiproject.jsf.util;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.sakaiproject.jsf2.util.JsfTool;
import org.sakaiproject.tool.api.ActiveTool;
import org.sakaiproject.tool.api.Session;
import org.sakaiproject.tool.api.Tool;
import org.sakaiproject.tool.api.ToolException;
import org.sakaiproject.tool.api.ToolSession;
import org.sakaiproject.tool.cover.ActiveToolManager;
import org.sakaiproject.tool.cover.SessionManager;
import org.sakaiproject.tool.cover.ToolManager;
import org.sakaiproject.util.Web;
import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemTextAttachmentIfc;
import org.sakaiproject.tool.assessment.facade.AssessmentFacade;
import org.sakaiproject.tool.assessment.services.assessment.AssessmentService;
import org.sakaiproject.tool.assessment.services.assessment.PublishedAssessmentService;
import org.sakaiproject.tool.assessment.ui.listener.util.ContextUtil;
import org.sakaiproject.tool.assessment.ui.bean.author.AnswerBean;
import org.sakaiproject.tool.assessment.ui.bean.author.AssessmentSettingsBean;
import org.sakaiproject.tool.assessment.ui.bean.author.AssessmentBean;
import org.sakaiproject.tool.assessment.ui.bean.author.AuthorBean;
import org.sakaiproject.tool.assessment.ui.bean.author.ItemAuthorBean;
import org.sakaiproject.tool.assessment.ui.bean.author.PublishedAssessmentSettingsBean;
import org.sakaiproject.tool.assessment.ui.bean.author.SectionBean;
import org.sakaiproject.tool.assessment.ui.bean.delivery.ItemContentsBean;
import org.sakaiproject.tool.assessment.ui.bean.evaluation.QuestionScoresBean;
import org.sakaiproject.tool.assessment.ui.bean.evaluation.StudentScoresBean;
import org.sakaiproject.tool.assessment.ui.bean.evaluation.TotalScoresBean;
import org.sakaiproject.tool.assessment.ui.bean.authz.AuthorizationBean;
import org.sakaiproject.tool.assessment.ui.listener.evaluation.StudentScoreListener;
import org.sakaiproject.tool.assessment.ui.listener.evaluation.SubmissionNavListener;
/**
* <p>
* Customized JsfTool for Samigo - just to workaround the fact that Samigo
* has the JSF URL mapping "*.faces" hard-coded in several places. If
* all instances of "*.faces" were changed to "*.jsf", this class could be removed.
* </p>
*
*/
@Slf4j
public class SamigoJsfTool extends JsfTool {
private static final String HELPER_EXT = ".helper";
private static final String HELPER_SESSION_PREFIX = "session.";
private static final String HELPER_RETURN_NOTIFICATION = "/returnToCaller";
private static final String RESET_ASSESSMENT_BEAN = "/resetAssessmentBean";
/**
* Recognize a path that is a resource request. It must have an "extension", i.e. a dot followed by characters that do not include a slash.
*
* @param path
* The path to check
* @return true if the path is a resource request, false if not.
*/
protected boolean isResourceRequest(String path)
{
log.debug("****0. inside isResourceRequest, path="+path);
// we need some path
if ((path == null) || (path.length() == 0)) return false;
// we need a last dot
int pos = path.lastIndexOf(".");
if (pos == -1) return false;
// we need that last dot to be the end of the path, not burried in the path somewhere (i.e. no more slashes after the last dot)
String ext = path.substring(pos);
log.debug("****1. inside isResourceRequest, ext="+ext);
if (ext.indexOf("/") != -1) return false;
// these are JSF pages, not resources
// THESE LINES OF CODE IS THE ONLY REASON THIS CLASS EXISTS!
if (ext.equals(".jsf")) return false;
if (ext.equals(".faces")) return false;
if (path.startsWith("/faces/")) return false;
if (path.indexOf(".helper") > -1) return false;
// ok, it's a resource request
return true;
}
protected void dispatch(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
// NOTE: this is a simple path dispatching, taking the path as the view id = jsp file name for the view,
// with default used if no path and a path prefix as configured.
// build up the target that will be dispatched to
String target = req.getPathInfo();
log.debug("***0. dispatch, target ="+target);
//To avoid lessons collide url
Session ses = SessionManager.getCurrentSession();
ses.setAttribute("LESSONBUILDER_RETURNURL_SAMIGO", null);
// see if we need to reset the assessmentBean, such as when returning
// from a helper
// TODO: there MUST be a cleaner way to do this!! These dependencies
// shouldn't be here!!
if (target != null && target.startsWith(RESET_ASSESSMENT_BEAN)) {
AssessmentBean assessmentBean = (AssessmentBean) ContextUtil
.lookupBeanFromExternalServlet("assessmentBean", req, res);
if (assessmentBean != null && assessmentBean.getAssessmentId() != null) {
AssessmentIfc assessment;
AuthorBean author = (AuthorBean) ContextUtil.lookupBean("author");
AssessmentService assessmentService;
if (author.getIsEditPendingAssessmentFlow()) {
assessmentService = new AssessmentService();
}
else {
assessmentService = new PublishedAssessmentService();
}
assessment = assessmentService.getAssessment(Long.valueOf(assessmentBean.getAssessmentId()));
assessmentBean.setAssessment(assessment);
}
target = target.replaceFirst(RESET_ASSESSMENT_BEAN, "");
}
// see if this is a helper trying to return to caller
if (HELPER_RETURN_NOTIFICATION.equals(target)) {
ToolSession session = SessionManager.getCurrentToolSession();
target = (String) session.getAttribute(ToolManager.getCurrentTool()
.getId()
+ Tool.HELPER_DONE_URL);
if (target != null) {
session.removeAttribute(ToolManager.getCurrentTool().getId()
+ Tool.HELPER_DONE_URL);
res.sendRedirect(target);
return;
}
}
boolean sendToHelper = sendToHelper(req, res);
boolean isResourceRequest = isResourceRequest(target);
log.debug("***1. dispatch, send to helper ="+sendToHelper);
log.debug("***2. dispatch, isResourceRequest ="+ isResourceRequest);
// see if we have a helper request
if (sendToHelper) {
return;
}
if (isResourceRequest) {
// get a dispatcher to the path
RequestDispatcher resourceDispatcher = getServletContext().getRequestDispatcher(target);
if (resourceDispatcher != null) {
resourceDispatcher.forward(req, res);
return;
}
}
if (target == null || "/".equals(target)) {
target = computeDefaultTarget();
// make sure it's a valid path
if (!target.startsWith("/")){
target = "/" + target;
}
// now that we've messed with the URL, send a redirect to make it official
res.sendRedirect(Web.returnUrl(req, target));
return;
}
// see if we want to change the specifically requested view
String newTarget = redirectRequestedTarget(target);
// make sure it's a valid path
if (!newTarget.startsWith("/")){
newTarget = "/" + newTarget;
}
if (!newTarget.equals(target)){
// now that we've messed with the URL, send a redirect to make it official
res.sendRedirect(Web.returnUrl(req, newTarget));
return;
}
target = newTarget;
// store this
ToolSession toolSession = SessionManager.getCurrentToolSession();
if (toolSession!=null){
toolSession.setAttribute(LAST_VIEW_VISITED, target);
}
log.debug("3a. dispatch: toolSession="+toolSession);
log.debug("3b. dispatch: target="+target);
log.debug("3c. dispatch: lastview?"+m_defaultToLastView);
//check direct URL permissions
AuthorizationBean authBean = (AuthorizationBean) ContextUtil.lookupBeanFromExternalServlet("authorization", req, res);
if (target.indexOf("/jsf/author/") > -1 &&
!authBean.getAdminPrivilege() &&
!authBean.getCreateAssessment() &&
!authBean.getEditAnyAssessment() &&
!authBean.getEditOwnAssessment() &&
!authBean.getDeleteAnyAssessment() &&
!authBean.getDeleteOwnAssessment() &&
!authBean.getPublishAnyAssessment() &&
!authBean.getPublishOwnAssessment()) {
log.debug("***4a. dispatch, authorization error : path="+target);
target = computeDefaultTarget(false);
}
if (target.indexOf("/jsf/evaluation/") > -1 &&
!authBean.getAdminPrivilege() &&
!authBean.getGradeAnyAssessment() &&
!authBean.getGradeOwnAssessment()) {
log.debug("***4b. dispatch, authorization error : path="+target);
target = computeDefaultTarget(false);
}
if (target.indexOf("/jsf/template/") > -1 &&
!authBean.getAdminPrivilege() &&
!authBean.getCreateTemplate() &&
!authBean.getEditOwnTemplate() &&
!authBean.getDeleteOwnTemplate()) {
log.debug("***4c. dispatch, authorization error : path="+target);
target = computeDefaultTarget(false);
}
//based on assessmentHeadings.jsp, "event" and "section-activity" paths are based on questionpool permissions
//TODO : create custom permissions for event and section-activity
if ((target.indexOf("/jsf/questionpool/") > -1 || target.indexOf("/jsf/event/") > -1 || target.indexOf("/jsf/section-activity/") > -1) &&
!authBean.getAdminPrivilege() &&
!authBean.getAdminQuestionPool() &&
!authBean.getCreateQuestionPool() &&
!authBean.getEditOwnQuestionPool() &&
!authBean.getDeleteOwnQuestionPool() &&
!authBean.getCopyOwnQuestionPool()) {
log.debug("***4d. dispatch, authorization error : path="+target);
target = computeDefaultTarget(false);
}
// add the configured folder root and extension (if missing)
target = m_path + target;
// add the default JSF extension (if we have no extension)
int lastSlash = target.lastIndexOf("/");
int lastDot = target.lastIndexOf(".");
if (lastDot < 0 || lastDot < lastSlash){
target += JSF_EXT;
}
// set the information that can be removed from return URLs
req.setAttribute(URL_PATH, m_path);
req.setAttribute(URL_EXT, ".jsp");
// set the sakai request object wrappers to provide the native, not Sakai set up, URL information
// - this assures that the FacesServlet can dispatch to the proper view based on the path info
req.setAttribute(Tool.NATIVE_URL, Tool.NATIVE_URL);
// TODO: Should setting the HTTP headers be moved up to the portal level as well?
res.setContentType("text/html; charset=UTF-8");
res.addHeader("Cache-Control", "no-store");
// dispatch to the target
log.debug("***5. dispatch, dispatching path: " + req.getPathInfo() + " to: " + target + " context: "
+ getServletContext().getServletContextName());
// if this is a return from the file picker and going back to
// case 1: item mofification, then set
// itemAuthorbean.attachmentlist = filepicker list
if (target.indexOf("/jsf/author/item/") > -1
&& ("true").equals(toolSession.getAttribute("SENT_TO_FILEPICKER_HELPER"))){
ItemAuthorBean bean = (ItemAuthorBean) ContextUtil.lookupBeanFromExternalServlet(
"itemauthor", req, res);
// For EMI Item Attachments
AnswerBean emiQAComboItem = bean.getCurrentAnswer();
if (emiQAComboItem == null) {
bean.setItemAttachment();
}
else {
emiQAComboItem.setItemTextAttachment();
}
toolSession.removeAttribute("SENT_TO_FILEPICKER_HELPER");
}
// case 2: part mofification, then set
// sectionBean.attachmentList = filepicker list
else if (target.indexOf("/jsf/author/editPart") > -1
&& ("true").equals(toolSession.getAttribute("SENT_TO_FILEPICKER_HELPER"))){
SectionBean bean = (SectionBean) ContextUtil.lookupBeanFromExternalServlet(
"sectionBean", req, res);
bean.setPartAttachment();
toolSession.removeAttribute("SENT_TO_FILEPICKER_HELPER");
}
// case 3.1: assessment settings mofification, then set assessmentSettingsBean.attachmentList = filepicker list
else if (target.indexOf("/jsf/author/authorSettings") > -1
&& ("true").equals(toolSession.getAttribute("SENT_TO_FILEPICKER_HELPER"))){
AssessmentSettingsBean bean = (AssessmentSettingsBean) ContextUtil.lookupBeanFromExternalServlet(
"assessmentSettings", req, res);
bean.setAssessmentAttachment();
toolSession.removeAttribute("SENT_TO_FILEPICKER_HELPER");
}
// case 3.2: published assessment settings mofification, then set assessmentSettingsBean.attachmentList = filepicker list
else if (target.indexOf("/jsf/author/publishedSettings") > -1
&& ("true").equals(toolSession.getAttribute("SENT_TO_FILEPICKER_HELPER"))){
PublishedAssessmentSettingsBean bean = (PublishedAssessmentSettingsBean) ContextUtil.lookupBeanFromExternalServlet(
"publishedSettings", req, res);
bean.setAssessmentAttachment();
toolSession.removeAttribute("SENT_TO_FILEPICKER_HELPER");
}
else if (target.indexOf("/jsf/evaluation/questionScore") > -1
&& ("true").equals(toolSession.getAttribute("SENT_TO_FILEPICKER_HELPER"))) {
QuestionScoresBean bean = (QuestionScoresBean) ContextUtil.lookupBeanFromExternalServlet("questionScores", req, res);
bean.setAttachment((Long) toolSession.getAttribute("itemGradingId"));
toolSession.removeAttribute("SENT_TO_FILEPICKER_HELPER");
}
else if (target.indexOf("/jsf/evaluation/gradeStudentResult") > -1
&& ("true").equals(toolSession.getAttribute("SENT_TO_FILEPICKER_HELPER"))) {
ItemContentsBean bean = (ItemContentsBean) ContextUtil.lookupBeanFromExternalServlet("itemContents", req, res);
bean.setAttachment((Long) toolSession.getAttribute("itemGradingId"));
toolSession.removeAttribute("SENT_TO_FILEPICKER_HELPER");
}
else if (target.indexOf("/jsf/evaluation/totalScores") > -1
&& ("true").equals(toolSession.getAttribute("SENT_TO_FILEPICKER_HELPER"))) {
TotalScoresBean bean = (TotalScoresBean) ContextUtil.lookupBeanFromExternalServlet("totalScores", req, res);
bean.setAttachment((Long) toolSession.getAttribute("assessmentGradingId"));
toolSession.removeAttribute("SENT_TO_FILEPICKER_HELPER");
}
else if (target.indexOf("/jsf/evaluation/gradeStudentResult") > -1
&& Boolean.TRUE.toString().equals(ContextUtil.lookupParam("resetCache"))) {
StudentScoresBean studentScoresBean = (StudentScoresBean) ContextUtil.lookupBean("studentScores");
String itemGradingId = studentScoresBean.getAssessmentGradingId();
SubmissionNavListener submissionNavListener = new SubmissionNavListener();
submissionNavListener.submissionNav(itemGradingId);
StudentScoreListener studentScoreListener = new StudentScoreListener();
studentScoreListener.studentScores(studentScoresBean.getPublishedId(), itemGradingId, studentScoresBean.getItemId(), false);
}
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(target);
dispatcher.forward(req, res);
// restore the request object
req.removeAttribute(Tool.NATIVE_URL);
req.removeAttribute(URL_PATH);
req.removeAttribute(URL_EXT);
}
protected boolean sendToHelper(HttpServletRequest req, HttpServletResponse res)
throws ToolException {
String path = req.getPathInfo();
if (path == null) path = "/";
// 0 parts means the path was just "/", otherwise parts[0] = "", parts[1] = item id, parts[2]
// if present is "edit"...
String[] parts = path.split("/");
log.debug("***a. sendToHelper.partLength="+parts.length);
String helperPath =null;
String toolPath=null;
// e.g. helper url in Samigo can be /jsf/author/item/sakai.filepicker.helper/tool
// or /sakai.filepicker.helper
if (parts.length > 2){
log.debug("***b. sendToHelper.partLength="+parts.length);
helperPath = parts[parts.length - 2];
toolPath = parts[parts.length - 1];
}
else if (parts.length == 2){
log.debug("***c. sendToHelper.partLength="+parts.length);
helperPath = parts[1];
}
else return false;
if (!helperPath.endsWith(HELPER_EXT)) return false;
log.debug("****d. sendToHelper, part #1="+helperPath);
log.debug("****e. sendToHelper, part #2="+toolPath);
ToolSession toolSession = SessionManager.getCurrentToolSession();
toolSession.setAttribute("SENT_TO_FILEPICKER_HELPER", "true");
Enumeration params = req.getParameterNames();
while (params.hasMoreElements()) {
String paramName = (String)params.nextElement();
if (paramName.startsWith(HELPER_SESSION_PREFIX)) {
String attributeName = paramName.substring(HELPER_SESSION_PREFIX.length());
toolSession.setAttribute(attributeName, req.getParameter(paramName));
}
}
// calc helper id
int posEnd = helperPath.lastIndexOf(".");
String helperId = helperPath.substring(0, posEnd);
log.debug("****f. sendToHelper, helperId="+helperId);
ActiveTool helperTool = ActiveToolManager.getActiveTool(helperId);
String url = req.getContextPath() + req.getServletPath();
if (toolSession.getAttribute(helperTool.getId() + Tool.HELPER_DONE_URL) == null) {
toolSession.setAttribute(helperTool.getId() + Tool.HELPER_DONE_URL,
url + RESET_ASSESSMENT_BEAN + computeDefaultTarget(true));
}
log.debug("****g. sendToHelper, url="+url);
String context = url + "/"+ helperPath;
log.debug("****h. sendToHelper, context="+context);
if (toolPath != null)
helperTool.help(req, res, context, "/"+toolPath);
else
helperTool.help(req, res, context, "");
return true; // was handled as helper call
}
protected String computeDefaultTarget(boolean lastVisited){
// setup for the default view as configured
ToolSession session = SessionManager.getCurrentToolSession();
String target = "/" + m_default;
// if we are doing lastVisit and there's a last-visited view, for this tool placement / user, use that
if (lastVisited) {
String last = (String) session.getAttribute(LAST_VIEW_VISITED);
if (last != null) {
target = last;
}
}
session.removeAttribute(LAST_VIEW_VISITED);
log.debug("***3. computeDefaultTarget()="+target);
return target;
}
}