-
Notifications
You must be signed in to change notification settings - Fork 27
/
BaseProjectDetailsAction.java
936 lines (828 loc) · 46.5 KB
/
BaseProjectDetailsAction.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
/*
* Copyright (C) 2014 TopCoder Inc., All Rights Reserved.
*/
package com.cronos.onlinereview.actions.projectdetails;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cronos.onlinereview.Constants;
import com.cronos.onlinereview.actions.DynamicModelDrivenAction;
import com.cronos.onlinereview.actions.event.EventBusServiceClient;
import com.cronos.onlinereview.external.ExternalUser;
import com.cronos.onlinereview.external.UserRetrieval;
import com.cronos.onlinereview.model.DynamicModel;
import com.cronos.onlinereview.model.FormFile;
import com.cronos.onlinereview.phases.AmazonSNSHelper;
import com.cronos.onlinereview.util.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.topcoder.management.deliverable.Submission;
import com.topcoder.management.deliverable.SubmissionStatus;
import com.topcoder.management.deliverable.Upload;
import com.topcoder.management.deliverable.UploadManager;
import com.topcoder.management.deliverable.UploadStatus;
import com.topcoder.management.deliverable.persistence.UploadPersistenceException;
import com.topcoder.management.deliverable.search.SubmissionFilterBuilder;
import com.topcoder.management.project.Project;
import com.topcoder.management.project.ProjectManager;
import com.topcoder.management.resource.Resource;
import com.topcoder.management.review.ReviewManager;
import com.topcoder.management.review.data.Review;
import com.topcoder.message.email.EmailEngine;
import com.topcoder.message.email.TCSEmailMessage;
import com.topcoder.project.phases.Phase;
import com.topcoder.search.builder.SearchBuilderException;
import com.topcoder.search.builder.filter.AndFilter;
import com.topcoder.search.builder.filter.EqualToFilter;
import com.topcoder.search.builder.filter.Filter;
import com.topcoder.servlet.request.ConfigurationException;
import com.topcoder.servlet.request.DisallowedDirectoryException;
import com.topcoder.servlet.request.FileDoesNotExistException;
import com.topcoder.servlet.request.FileUpload;
import com.topcoder.servlet.request.FileUploadResult;
import com.topcoder.servlet.request.PersistenceException;
import com.topcoder.servlet.request.UploadedFile;
import com.topcoder.util.config.ConfigManagerException;
import com.topcoder.util.errorhandling.BaseException;
import com.topcoder.util.file.DocumentGenerator;
import com.topcoder.util.file.Template;
import com.topcoder.util.file.fieldconfig.Condition;
import com.topcoder.util.file.fieldconfig.Field;
import com.topcoder.util.file.fieldconfig.Node;
import com.topcoder.util.file.fieldconfig.TemplateFields;
import com.topcoder.util.file.templatesource.FileTemplateSource;
/**
* This is the base class for project details actions classes.
* It provides the basic functions which will be used by all project detail actions.
* <p>
* <b>Thread Safety:</b>Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues.
* </p>
*
* <p>
* Changes in Version 2.1 - Topcoder - Online Review Update - Post to Event Bus Part 2 v1.0
* - fire the submission upload event when the f2f/assembly/code/checkpoint submissions are uploaded
* </p>
*
* @author TCSASSEMBLER
* @version 2.1
*/
public abstract class BaseProjectDetailsAction extends DynamicModelDrivenAction {
/**
* Represents the project id.
*/
private long pid;
/**
* This method verifies the request for certain conditions to be met. This includes verifying if
* the user has specified an ID of the upload he wants to perform an operation on (most often
* – to download), and whether the ID of the upload specified by user denotes existing
* upload.
*
* @return an instance of the {@link CorrectnessCheckResult} class, which specifies whether the
* check was successful and, in the case it was, contains additional information
* retrieved during the check operation, which might be of some use for the calling
* method.
* @param request
* the http request.
* @param errorMessageKey
* permission to check against, or <code>null</code> if no check is required.
* @throws IllegalArgumentException
* if any of the parameters are <code>null</code>, or if
* <code>errorMessageKey</code> parameter is an empty string.
* @throws BaseException
* if any error occurs.
*/
protected CorrectnessCheckResult checkForCorrectUploadId(HttpServletRequest request, String errorMessageKey) throws BaseException {
// Validate parameters
ActionsHelper.validateParameterNotNull(request, "request");
ActionsHelper.validateParameterStringNotEmpty(errorMessageKey, "errorMessageKey");
// Prepare bean that will be returned as the result
CorrectnessCheckResult result = new CorrectnessCheckResult();
// Verify that Upload ID was specified and denotes correct upload
String uidParam = request.getParameter("uid");
if (uidParam == null || uidParam.trim().length() == 0) {
result.setResult(ActionsHelper.produceErrorReport(
this, request, errorMessageKey, "Error.UploadIdNotSpecified", null));
// Return the result of the check
return result;
}
long uid;
try {
// Try to convert specified uid parameter to its integer representation
uid = Long.parseLong(uidParam, 10);
} catch (NumberFormatException nfe) {
result.setResult(ActionsHelper.produceErrorReport(
this, request, errorMessageKey, "Error.UploadNotFound", null));
// Return the result of the check
return result;
}
// Obtain an instance of Upload Manager
UploadManager upMgr = ActionsHelper.createUploadManager();
// Get Upload by its ID
Upload upload = upMgr.getUpload(uid);
// Verify that upload with given ID exists
if (upload == null) {
result.setResult(ActionsHelper.produceErrorReport(
this, request, errorMessageKey, "Error.UploadNotFound", null));
// Return the result of the check
return result;
}
// Store Upload object in the result bean
result.setUpload(upload);
// Obtain an instance of Project Manager
ProjectManager projMgr = ActionsHelper.createProjectManager();
// Get a Project for this upload
Project project = projMgr.getProject(upload.getProject());
// Store Project object in the result bean
result.setProject(project);
// Place project as attribute in the request
request.setAttribute("project", project);
// Gather the roles the user has for current request
AuthorizationHelper.gatherUserRoles(request, project.getId());
return result;
}
/**
* <p>Sends the content of specified file for downloading by client.</p>
*
* @param upload an <code>Upload</code> providing the details for the filr to be downloaded by client.
* @param request an <code>HttpServletRequest</code> representing the incoming request.
* @param response an <code>HttpServletResponse</code> representing the outgoing response.
* @throws UploadPersistenceException if an unexpected error occurs.
* @throws SearchBuilderException if an unexpected error occurs.
* @throws DisallowedDirectoryException if an unexpected error occurs.
* @throws ConfigurationException if an unexpected error occurs.
* @throws PersistenceException if an unexpected error occurs.
* @throws FileDoesNotExistException if an unexpected error occurs.
* @throws IOException if an unexpected error occurs.
*/
protected void processSubmissionDownload(Upload upload, HttpServletRequest request, HttpServletResponse response) throws UploadPersistenceException, SearchBuilderException, DisallowedDirectoryException,
ConfigurationException, PersistenceException, FileDoesNotExistException, IOException{
// At this point, redirect-after-login attribute should be removed (if it exists)
AuthorizationHelper.removeLoginRedirect(request);
FileUpload fileUpload = ActionsHelper.createFileUploadManager(request);
UploadedFile uploadedFile = fileUpload.getUploadedFile(upload.getParameter());
UploadManager upMgr = ActionsHelper.createUploadManager();
Submission[] submissions = upMgr.searchSubmissions(SubmissionFilterBuilder.createUploadIdFilter(upload.getId()));
Submission submission = (submissions.length != 0) ? submissions[0] : null;
String contentDisposition;
if (submission != null) {
contentDisposition = "attachment; filename=\"submission-" + submission.getId() + "-"
+ uploadedFile.getRemoteFileName() + "\"";
} else {
contentDisposition = "attachment; filename=\"upload-" + upload.getId() + "-"
+ uploadedFile.getRemoteFileName() + "\"";
}
outputDownloadedFile(uploadedFile, contentDisposition, response);
}
/**
*
* <p>Outputs the content of specified file to specified response for downloading by client.</p>
*
* @param uploadedFile an <code>UploadedFile</code> providing the details for the filr to be downloaded by client.
* @param contentDisposition a <code>String</code> providing the value for <code>Content-Disposition</code> header.
* @param response an <code>HttpServletResponse</code> representing the outgoing response.
* @throws PersistenceException if an unexpected error occurs.
* @throws FileDoesNotExistException if an unexpected error occurs.
* @throws IOException if an unexpected error occurs.
*/
protected void outputDownloadedFile(UploadedFile uploadedFile, String contentDisposition, HttpServletResponse response)
throws PersistenceException, FileDoesNotExistException, IOException {
InputStream in = uploadedFile.getInputStream();
response.setHeader("Content-Type", "application/octet-stream");
response.setStatus(HttpServletResponse.SC_OK);
response.setIntHeader("Content-Length", (int) uploadedFile.getSize());
response.setHeader("Content-Disposition", contentDisposition);
response.flushBuffer();
OutputStream out = null;
try {
out = response.getOutputStream();
byte[] buffer = new byte[65536];
for (;;) {
int numOfBytesRead = in.read(buffer);
if (numOfBytesRead == -1) {
break;
}
out.write(buffer, 0, numOfBytesRead);
}
} finally {
in.close();
if (out != null) {
out.close();
}
}
}
/**
* <p>Handles the request for downloading the submission of desired type.</p>
*
*
* @param request an <code>HttpServletRequest</code> providing the details for incoming request.
* @param response an <code>HttpServletResponse</code> providing the details for outgoing response.
* @param errorMessageKey a <code>String</code> providing the key in message bundle for the error to be displayed
* to user.
* @param viewAllSubmissionsPermName a <code>String</code> providing the name for permission for viewing all
* submissions.
* @param viewMySubmissionsPermissionName a <code>String</code> providing the name for permission for viewing own
* submissions
* @param viewSubmissionByScreenerPermissionName a <code>String</code> providing the name for permission for viewing
* submissions by screener.
* @param viewMostRecentSubmissionsPermissionName a <code>String</code> providing the name for permission for
* viewing most recent submissions.
* @param downloadCustomSubmissionPermissionName a <code>String</code> providing the name for permission for
* downloading submission for custom catalog.
* @param viewWinningSubmissionPermissionName a <code>String</code> providing the name for permission for viewing
* winning submissions.
* @param screeningPhaseName a <code>String</code> providing the name for screening phase type.
* @param reviewPhaseName a <code>String</code> providing the name for review phase type.
* @param screenerRoleNames a <code>String</code> array listing the names for screener roles.
* @param reviewerRoleNames a <code>String</code> array listing the names for reviewer roles.
* @param submissionType a <code>long</code> referencing the type of the submission being downloaded.
* @return an <code>ActionForward</code> referencing the next view to be displayed to user in case of errors or
* <code>null</code> if submission is downloaded successfully.
* @throws BaseException if an unexpected error occurs.
* @throws IOException if an I/O error occurs.
*/
protected String handleDownloadSubmission(HttpServletRequest request,
HttpServletResponse response, String errorMessageKey,
String viewAllSubmissionsPermName,
String viewMySubmissionsPermissionName,
String viewSubmissionByScreenerPermissionName,
String viewMostRecentSubmissionsPermissionName,
String downloadCustomSubmissionPermissionName,
String viewWinningSubmissionPermissionName,
String screeningPhaseName,
String reviewPhaseName,
String[] screenerRoleNames,
String[] reviewerRoleNames,
long submissionType)
throws BaseException, IOException {
LoggingHelper.logAction(request);
CorrectnessCheckResult verification = ActionsHelper.checkThrottle(false, request, this);
if (!verification.isSuccessful()) {
return verification.getResult();
}
// Verify that certain requirements are met before processing with the Action
verification = checkForCorrectUploadId(request, errorMessageKey);
// If any error has occurred, return action forward contained in the result bean
if (!verification.isSuccessful()) {
return verification.getResult();
}
// Get an upload the user wants to download
Upload upload = verification.getUpload();
// Verify that upload is a submission
if (!upload.getUploadType().getName().equalsIgnoreCase("Submission")) {
ActionsHelper.logDownloadAttempt(request, upload, false);
return ActionsHelper.produceErrorReport(
this, request, errorMessageKey, "Error.NotASubmission", null);
}
// Verify the status of upload and check whether the user has permission to download old uploads
if (upload.getUploadStatus().getName().equalsIgnoreCase("Deleted")
&& !AuthorizationHelper.hasUserPermission(request, viewAllSubmissionsPermName)) {
ActionsHelper.logDownloadAttempt(request, upload, false);
return ActionsHelper.produceErrorReport(
this, request, errorMessageKey, "Error.UploadDeleted", null);
}
// Get all phases for the current project (needed to do permission checks)
Project project = verification.getProject();
Phase[] phases = ActionsHelper.getPhasesForProject(
ActionsHelper.createPhaseManager(false), verification.getProject());
boolean noRights = true;
// Check if it is the Thurgood server trying to get the submission.
// If it's Thurgood, authorize the request.
boolean useThurgood = !isEmptyOrNull(project.getProperty("Thurgood Platform")) &&
!isEmptyOrNull(project.getProperty("Thurgood Language"));
if (useThurgood && !AuthorizationHelper.isUserLoggedIn(request)) {
if (ConfigHelper.getThurgoodUsername().equals(request.getParameter("username")) &&
ConfigHelper.getThurgoodPassword().equals(request.getParameter("password"))) {
noRights = false;
}
}
if (AuthorizationHelper.hasUserPermission(request, viewAllSubmissionsPermName)) {
noRights = false;
}
if (AuthorizationHelper.hasUserPermission(request, viewMySubmissionsPermissionName)) {
long owningResourceId = upload.getOwner();
Resource[] myResources = (Resource[]) request.getAttribute("myResources");
for (Resource myResource : myResources) {
if (myResource.getId() == owningResourceId) {
noRights = false;
break;
}
}
}
if (noRights && AuthorizationHelper.hasUserPermission(request, viewSubmissionByScreenerPermissionName)) {
// Determine whether Screening phase has already been opened (does not have Scheduled status)
final boolean isScreeningOpen = ActionsHelper.isInOrAfterPhase(phases, 0, screeningPhaseName);
// If screener tries to download submission before Screening phase opens,
// notify him about this wrong-doing and do not let perform the action
if (AuthorizationHelper.hasUserRole(request, screenerRoleNames) && !isScreeningOpen) {
ActionsHelper.logDownloadAttempt(request, upload, false);
return ActionsHelper.produceErrorReport(
this, request, errorMessageKey, "Error.IncorrectPhase", null);
}
noRights = false;
}
if (noRights && AuthorizationHelper.hasUserPermission(request, viewMostRecentSubmissionsPermissionName)) {
// If reviewer tries to download submission before Review phase opens,
// notify him about this wrong-doing and do not let perform the action
if (AuthorizationHelper.hasUserRole(request, reviewerRoleNames)
&& !ActionsHelper.isInOrAfterPhase(phases, 0, reviewPhaseName)) {
ActionsHelper.logDownloadAttempt(request, upload, false);
return ActionsHelper.produceErrorReport(
this, request, errorMessageKey, "Error.IncorrectPhase", null);
}
// Regular reviewers can view checkpoint submissions only after the Review phase has started
if (submissionType == 3
&& !AuthorizationHelper.hasUserRole(request, Constants.CHECKPOINT_REVIEWER_ROLE_NAME)
&& AuthorizationHelper.hasUserRole(request, Constants.REVIEWER_ROLE_NAMES)
&& !ActionsHelper.isInOrAfterPhase(phases, 0, Constants.REVIEW_PHASE_NAME)) {
ActionsHelper.logDownloadAttempt(request, upload, false);
return ActionsHelper.produceErrorReport(
this, request, errorMessageKey, "Error.IncorrectPhase", null);
}
noRights = false;
}
// For the Submitters we only allow to download others' submissions if the user has at least passed screening.
if (noRights && AuthorizationHelper.hasUserRole(request, Constants.SUBMITTER_ROLE_NAME)) {
// Get all submissions for this user.
Resource resource = ActionsHelper.getMyResourceForRole(request, Constants.SUBMITTER_ROLE_NAME);
UploadManager upMgr = ActionsHelper.createUploadManager();
Long[] subIds = resource.getSubmissions();
// Check that the user has a submission that passed screening.
// We don't need to check the current phase because if it is still prior to the Appeals Response
// the user won't be able to download others' submissions anyway.
boolean passedScreening = false;
for (Long id : subIds) {
Submission submission = upMgr.getSubmission(id);
if (submission != null && submission.getSubmissionType().getName().equals(Constants.CONTEST_SUBMISSION_TYPE_NAME) &&
!submission.getSubmissionStatus().getName().equals(Constants.FAILED_SCREENING_SUBMISSION_STATUS_NAME)) {
passedScreening = true;
break;
}
}
if (!passedScreening) {
ActionsHelper.logDownloadAttempt(request, upload, false);
return ActionsHelper.produceErrorReport(
this, request, "ViewSubmission", "Error.NoScreeningPassed", null);
}
// Submitters can download others' contest submissions and checkpoint submissions only
// after the Appeals Response phase (or Review phase if Appeals Response phase is not present)
// is closed and only if at least one submission passed review.
// This also takes care of contests with Iterative Review phase, for which submitters
// can't download others' submissions at all.
if (submissionType == 1 || submissionType == 3) {
Phase reviewPhase = ActionsHelper.findPhaseByTypeName(phases, Constants.APPEALS_RESPONSE_PHASE_NAME);
if (reviewPhase == null) {
reviewPhase = ActionsHelper.findPhaseByTypeName(phases, Constants.REVIEW_PHASE_NAME);
}
boolean isReviewFinished = (reviewPhase != null) && (reviewPhase.getPhaseStatus().getId() == 3);
if (isReviewFinished) {
// Check that at least one submission passed review
Submission[] activeSubmissions = ActionsHelper.getProjectSubmissions(project.getId(),
Constants.CONTEST_SUBMISSION_TYPE_NAME, Constants.ACTIVE_SUBMISSION_STATUS_NAME, false);
noRights = (activeSubmissions.length == 0);
}
}
}
if (noRights && submissionType == 1
&& AuthorizationHelper.hasUserPermission(request, Constants.VIEW_CURRENT_ITERATIVE_REVIEW_SUBMISSION)) {
Submission[] submissions = ActionsHelper.getProjectSubmissions(upload.getProject(),
Constants.CONTEST_SUBMISSION_TYPE_NAME, null, false);
Resource resource = ActionsHelper.getMyResourceForRole(request, Constants.ITERATIVE_REVIEWER_ROLE_NAME);
List<Filter> filters = new ArrayList<Filter>();
Filter filterScorecard = new EqualToFilter("scorecardType", LookupHelper.getScorecardType("Iterative Review").getId());
Filter filterReviewer = new EqualToFilter("reviewer", resource.getId());
filters.add(filterScorecard);
filters.add(filterReviewer);
Filter filterForReviews = new AndFilter(filters);
ReviewManager revMgr = ActionsHelper.createReviewManager();
Review[] reviews = revMgr.searchReviews(filterForReviews, false);
for (Submission submission : submissions) {
// If this reviewer already has a review scorecard submitted for this submission, that reviewer can
// download the submission.
if (submission.getUpload().getId() == upload.getId()) {
for (Review review : reviews) {
if (review.getSubmission() == submission.getId()) {
noRights = false;
}
}
}
}
// If it's the "current" submission (i.e. the next one in the queue), the reviewer can download the submission.
Submission earliestSubmission = ActionsHelper.getEarliestActiveSubmission(
upload.getProject(), Constants.CONTEST_SUBMISSION_TYPE_NAME);
if (earliestSubmission != null && earliestSubmission.getUpload().getId() == upload.getId()) {
noRights = false;
}
}
ActionsHelper.logDownloadAttempt(request, upload, !noRights);
if (noRights) {
return ActionsHelper.produceErrorReport(this, request,
errorMessageKey, "Error.NoPermission", Boolean.FALSE);
}
processSubmissionDownload(upload, request, response);
return null;
}
/**
* <p>Handles the request for uploading a submission to project.</p>
*
* @param uploadSubmissionForm a model providing the form submitted by user.
* @param request an <code>HttpServletRequest</code> providing the details for incoming request.
* @param submissionTypeName a <code>String</code> providing the name of submission type.
* @param submitPermissionName a <code>String</code> providing the permission to be used for authorizing users to
* perform submission upload.
* @param phaseName a <code>String</code> providing the name of the type of project phase which submission maps to.
* @return an <code>ActionForward</code> referencing the next view to be displayed to user.
* @throws BaseException if an unexpected error occurs.
*/
protected String handleUploadSubmission(DynamicModel uploadSubmissionForm, HttpServletRequest request,
String submissionTypeName, String submitPermissionName,
String phaseName)
throws BaseException {
LoggingHelper.logAction(request);
CorrectnessCheckResult verification = ActionsHelper.checkThrottle(false, request, this);
if (!verification.isSuccessful()) {
return verification.getResult();
}
// Determine if this request is a post back
final boolean postBack = (request.getParameter("postBack") != null);
// Verify that certain requirements are met before processing with the Action
verification = ActionsHelper.checkForCorrectProjectId(
this, request, submitPermissionName, postBack);
// If any error has occurred, return action forward contained in the result bean
if (!verification.isSuccessful()) {
return verification.getResult();
}
// Retrieve current project
Project project = verification.getProject();
// Get all phases for the current project
Phase[] phases = ActionsHelper.getPhasesForProject(ActionsHelper.createPhaseManager(false), project);
Phase currentPhase = ActionsHelper.getPhase(phases, true, phaseName);
if (currentPhase == null) {
return ActionsHelper.produceErrorReport(this, request,
submitPermissionName, "Error.IncorrectPhase", null);
}
// We don't allow user to upload contest submissions/checkpoint submissions for studio contest
if ("Studio".equalsIgnoreCase(project.getProjectCategory().getProjectType().getName())
&& (submissionTypeName.equals(Constants.CONTEST_SUBMISSION_TYPE_NAME) ||
submissionTypeName.equals(Constants.CHECKPOINT_SUBMISSION_TYPE_NAME))) {
return ActionsHelper.produceErrorReport(this, request,
submitPermissionName, "Error.UploadForStudio", null);
}
// Get my resource
Resource resource = ActionsHelper.getMyResourceForRole(request, "Submitter");
Submission[] activeSubmissions = ActionsHelper.getResourceSubmissions(resource.getId(), submissionTypeName,
Constants.ACTIVE_SUBMISSION_STATUS_NAME, false);
Boolean allowMultipleSubmissions = Boolean.parseBoolean((String) project.getProperty("Allow multiple submissions"));
if (!postBack) {
// Retrieve some basic project info (such as icons' names) and place it into request
ActionsHelper.retrieveAndStoreBasicProjectInfo(request, verification.getProject(), this);
if (activeSubmissions.length > 0 && !allowMultipleSubmissions) {
request.setAttribute("showMultipleSubmissionsWarning", true);
}
return Constants.DISPLAY_PAGE_FORWARD_NAME;
}
FormFile file = (FormFile) getModel().get("file");
// Disallow uploading of empty files
if (file == null || file.getFileSize() == 0) {
return ActionsHelper.produceErrorReport(this, request,
submitPermissionName, "Error.EmptyFileUploaded", null);
}
StrutsRequestParser parser = new StrutsRequestParser();
parser.AddFile(file);
// Obtain an instance of File Upload Manager
FileUpload fileUpload = ActionsHelper.createFileUploadManager(request);
FileUploadResult uploadResult = fileUpload.uploadFiles(request, parser);
UploadedFile uploadedFile = uploadResult.getUploadedFile("file");
Submission submission = new Submission();
Upload upload = new Upload();
upload.setProject(project.getId());
upload.setProjectPhase(currentPhase.getId());
upload.setOwner(resource.getId());
upload.setUploadStatus(LookupHelper.getUploadStatus("Active"));
upload.setUploadType(LookupHelper.getUploadType("Submission"));
upload.setParameter(uploadedFile.getFileId());
submission.setUpload(upload);
submission.setSubmissionStatus(LookupHelper.getSubmissionStatus("Active"));
submission.setSubmissionType(LookupHelper.getSubmissionType(submissionTypeName));
// Get the name (id) of the user performing the operations
String operator = Long.toString(AuthorizationHelper.getLoggedInUserId(request));
UploadManager upMgr = ActionsHelper.createUploadManager();
upMgr.createUpload(upload, operator);
// set the Thurgood job id if the contest is from CloudSpokes
String thurgoodPlatform = (String) project.getProperty("Thurgood Platform");
String thurgoodLanguage = (String) project.getProperty("Thurgood Language");
// Thurgood will be used only for Contest Submissions
if (!isEmptyOrNull(thurgoodPlatform) && !isEmptyOrNull(thurgoodLanguage)
&& submissionTypeName.equals(Constants.CONTEST_SUBMISSION_TYPE_NAME)) {
Map<String, String> parameters =
buildCreateThurgoodParameters(project, thurgoodPlatform, thurgoodLanguage, upload.getId(), request);
if (parameters != null) {
String thurgoodJobId = submitThurgoodJob(createThurgoodJob(parameters));
submission.setThurgoodJobId(thurgoodJobId);
}
}
upMgr.createSubmission(submission, operator);
resource.addSubmission(submission.getId());
ActionsHelper.createResourceManager().updateResource(resource, operator);
// Now depending on whether the project allows multiple submissions or not mark the old submission
// and the upload as deleted.
if (activeSubmissions.length > 0 && !allowMultipleSubmissions) {
Submission earliestSubmission = ActionsHelper.getEarliestActiveSubmission(project.getId(),
submissionTypeName);
boolean deletedEarliestSubmission = false;
SubmissionStatus deleteSubmissionStatus = LookupHelper.getSubmissionStatus("Deleted");
UploadStatus deleteUploadStatus = LookupHelper.getUploadStatus("Deleted");
for (Submission activeSubmission : activeSubmissions) {
activeSubmission.getUpload().setUploadStatus(deleteUploadStatus);
activeSubmission.setSubmissionStatus(deleteSubmissionStatus);
upMgr.updateUpload(activeSubmission.getUpload(), operator);
upMgr.updateSubmission(activeSubmission, operator);
if (earliestSubmission != null && earliestSubmission.getId() == activeSubmission.getId()) {
deletedEarliestSubmission = true;
}
}
if (deletedEarliestSubmission) {
Submission nextEarliestSubmission = ActionsHelper.getEarliestActiveSubmission(project.getId(),
submissionTypeName);
try {
notifyIterativeReviewers(project, earliestSubmission.getId(),
(String) resource.getProperty("Handle"), nextEarliestSubmission);
} catch (Exception e) {
// log and swallow the exception
LoggingHelper.logException("Error when sending email to iterative reviewers.", e);
}
}
}
AmazonSNSHelper.publishProjectUpdateEvent(project);
// fire the submission creation event
Long devTypeId = EventBusServiceClient.F2F_ASSEMBLY_CODE_ID_MAP.get(project.getProjectCategory().getId());
int submissionTypeId = devTypeId == null ? 0 : devTypeId.intValue();
boolean isCheckPointSubmission = Constants.CHECKPOINT_SUBMISSION_TYPE_NAME.equals(submissionTypeName);
submissionTypeId = isCheckPointSubmission
? EventBusServiceClient.SUBMISSION_TYPE_ID_FOR_CHECKPOINT_SUBMISSION : submissionTypeId;
if (submissionTypeId > 0) {
String fileUrl = isCheckPointSubmission ? String.format(ConfigHelper.getCheckpointSubmissionDownloadUrl(), upload.getId())
: String.format(ConfigHelper.getContestSubmissionDownloadUrl(), upload.getId());
EventBusServiceClient.fireSubmissionCreateEvent(project.getId(),
AuthorizationHelper.getLoggedInUserId(request), upload.getParameter(), fileUrl, submission.getId(), submissionTypeId);
}
this.pid = project.getId();
return Constants.SUCCESS_FORWARD_NAME;
}
/**
* <p>Sends email to Iterative Reviewers to notify them about re-uploaded submission, which was in review.</p>
*
* @param project a <code>Project</code> providing details for project.
* @param submissionId a <code>ID</code> of the deleted submission.
* @param handle a <code>String</code> providing the handle of the member who re-uploaded his submission.
* @param nextEarliestSubmission next active earliest submission in the queue.
* @throws BaseException if an unexpected error occurs.
* @throws ConfigManagerException if there is a problem with configuration.
*/
private void notifyIterativeReviewers(Project project, long submissionId, String handle,
Submission nextEarliestSubmission)
throws BaseException, ConfigManagerException {
List<Long> reviewerUserIds = ActionsHelper.getUserIDsByRoleNames(
new String[] {Constants.ITERATIVE_REVIEWER_ROLE_NAME}, project.getId());
if (reviewerUserIds.size() == 0) {
return;
}
Resource nextSubmitter = ActionsHelper.createResourceManager().getResource(
nextEarliestSubmission.getUpload().getOwner());
String nextSubmitterHandle = (String) nextSubmitter.getProperty("Handle");
List<String> reviewerEmails = ActionsHelper.getEmailsByUserIDs(request, reviewerUserIds);
DocumentGenerator docGenerator = new DocumentGenerator();
docGenerator.setDefaultTemplateSource(new FileTemplateSource());
Template template = docGenerator.getTemplate(ConfigHelper.getF2FSubmissionReuploadedEmailTemplateName());
TemplateFields root = docGenerator.getFields(template);
for (Node node : root.getNodes()) {
if (node instanceof Field) {
Field field = (Field) node;
if ("PROJECT_NAME".equals(field.getName())) {
field.setValue("" + project.getProperty("Project Name"));
} else if ("PROJECT_VERSION".equals(field.getName())) {
field.setValue("" + project.getProperty("Project Version"));
} else if ("OR_LINK".equals(field.getName())) {
field.setValue(ConfigHelper.getProjectDetailsBaseURL() + project.getId());
} else if ("MEMBER_HANDLE".equals(field.getName())) {
field.setValue(handle);
} else if ("SUBMISSION_ID".equals(field.getName())) {
field.setValue("" + submissionId);
} else if ("NEXT_MEMBER_HANDLE".equals(field.getName())) {
field.setValue(nextSubmitterHandle);
} else if ("NEXT_SUBMISSION_ID".equals(field.getName())) {
field.setValue("" + nextEarliestSubmission.getId());
}
}
}
String emailContent = docGenerator.applyTemplate(root);
TCSEmailMessage message = new TCSEmailMessage();
message.setSubject(MessageFormat.format(ConfigHelper.getF2FSubmissionReuploadedEmailTemplateSubject(),
project.getProperty("Project Name"),
project.getProperty("Project Version")));
message.setBody(emailContent);
message.setFromAddress(ConfigHelper.getF2FSubmissionReuploadedEmailFromAddress());
for (String recipient : reviewerEmails) {
message.addToAddress(recipient, TCSEmailMessage.TO);
}
message.setContentType("text/html");
EmailEngine.send(message);
}
/**
* Build the parameters for creating Thurgood job.
* @param project the current project
* @param thurgoodPlatform the Thurgood platform
* @param thurgoodLanguage the Thurgood language
* @param uploadId the upload id
* @param request the http servlet request
* @return the parameters map
* @throws BaseException if any error
*/
private Map<String, String> buildCreateThurgoodParameters(Project project, String thurgoodPlatform,
String thurgoodLanguage, long uploadId, HttpServletRequest request) throws BaseException {
// Get the ID of the sender
long senderId = AuthorizationHelper.getLoggedInUserId(request);
// Obtain an instance of User Retrieval
UserRetrieval userRetrieval = ActionsHelper.createUserRetrieval(request);
// Retrieve information about an external user by its ID
ExternalUser user = userRetrieval.retrieveUser(senderId);
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("email", user.getEmail());
parameters.put("language", thurgoodLanguage);
parameters.put("userId", user.getHandle());
parameters.put("notification", "email");
parameters.put("codeUrl", ConfigHelper.getThurgoodCodeURL() + uploadId);
parameters.put("platform", thurgoodPlatform);
return parameters;
}
/**
* Check if the given object is a null or empty string.
* As it is called, it is guaranteed that the value is of string type.
* @param value the value to be checked.
* @return true if the string object is null or empty.
*/
protected boolean isEmptyOrNull(Object value) {
return value == null || ((String) value).trim().length() == 0;
}
/**
* Submit the Thurgood job.
* @param id the Thurgood job id.
* @return the submitted job id, null if submitting fails
*/
private static String submitThurgoodJob(String id) {
String authHeader = "Token token=\"%s\"";
if (id != null) {
HttpURLConnection con = null;
BufferedReader in = null;
try {
URL obj = new URL(ConfigHelper.getThurgoodApiURL() + "/" + id + "/submit");
con = (HttpURLConnection) obj.openConnection();
// set connection timeout
con.setConnectTimeout(ConfigHelper.getThurgoodTimeout());
// optional default is GET
con.setRequestMethod("PUT");
//add request header
con.setRequestProperty("Authorization", String.format(authHeader, ConfigHelper.getThurgoodApiKey()));
int responseCode = con.getResponseCode();
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
if (responseCode != HttpURLConnection.HTTP_OK) {
LoggingHelper.logError("Server backend error happened with response " + response);
return null;
}
ObjectMapper mapper = new ObjectMapper();
ObjectNode root = (ObjectNode) mapper.readTree(response.toString());
if (root.get("success") == null || !"true".equalsIgnoreCase(root.get("success").asText())) {
LoggingHelper.logError("Submitting Thurgood job failed with response " + response);
return null;
}
return id;
} catch (MalformedURLException e) {
LoggingHelper.logException("The requested url is malformed", e);
} catch (IOException e) {
LoggingHelper.logException("IO error while submitting the Thurgood job", e);
} catch (Exception e) {
LoggingHelper.logException("Error occurs while submitting the Thurgood job", e);
} finally {
if (con != null) {
con.disconnect();
}
closeResource(in);
}
}
return null;
}
/**
* Create the Thurgood Job.
* @param parameters the submitted parameters
* @return the created job id, null if creation fails
*/
private static String createThurgoodJob(Map<String, String> parameters) {
String authHeader = "Token token=\"%s\"";
HttpURLConnection con = null;
DataOutputStream wr = null;
BufferedReader in = null;
try {
URL obj = new URL(ConfigHelper.getThurgoodApiURL());
con = (HttpURLConnection) obj.openConnection();
// set connection timeout
con.setConnectTimeout(ConfigHelper.getThurgoodTimeout());
//set header
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", String.format(authHeader, ConfigHelper.getThurgoodApiKey()));
StringBuilder urlParameters = new StringBuilder();
for (Map.Entry<String, String> entry : parameters.entrySet()) {
urlParameters.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
// Send post request
con.setDoOutput(true);
wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters.toString());
wr.flush();
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
int responseCode = con.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
LoggingHelper.logError("Server backend error happened with response " + response);
return null;
}
//get the job id.
ObjectMapper mapper = new ObjectMapper();
ObjectNode root = (ObjectNode) mapper.readTree(response.toString());
if (root.get("success") == null || !"true".equalsIgnoreCase(root.get("success").asText())
|| root.get("data") == null) {
LoggingHelper.logError("Creating Thurgood job failed with response " + response);
return null;
}
JsonNode idNode = root.get("data").get("_id");
if (idNode == null || idNode.asText().trim().length() == 0) {
LoggingHelper.logError("No Created Job ID returned with response " + response);
return null;
}
return idNode.asText();
} catch (MalformedURLException e) {
LoggingHelper.logException("The requested url is malformed", e);
} catch (IOException e) {
LoggingHelper.logException("IO error while creating the Thurgood job", e);
} catch (Exception e) {
LoggingHelper.logException("Error occurs while creating the Thurgood job", e);
} finally {
if (con != null) {
con.disconnect();
}
closeResource(wr);
closeResource(in);
}
return null;
}
/**
* Close the resource if it is not null.
* @param resource the resource to be closed.
*/
private static void closeResource(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
LoggingHelper.logException("IO error while closing the resource", e);
}
}
}
/**
* Getter of pid.
* @return the pid
*/
public long getPid() {
return pid;
}
/**
* Setter of pid.
* @param pid the pid to set
*/
public void setPid(long pid) {
this.pid = pid;
}
}