1616
1717package org .labkey .cluster ;
1818
19+ import org .apache .commons .io .FilenameUtils ;
1920import org .apache .commons .lang3 .StringUtils ;
2021import org .apache .logging .log4j .LogManager ;
2122import org .apache .logging .log4j .Logger ;
2223import org .labkey .api .action .ConfirmAction ;
24+ import org .labkey .api .action .SimpleRedirectAction ;
2325import org .labkey .api .action .SpringActionController ;
2426import org .labkey .api .data .DbSchema ;
2527import org .labkey .api .data .DbSchemaType ;
2628import org .labkey .api .data .Table ;
2729import org .labkey .api .data .TableInfo ;
30+ import org .labkey .api .pipeline .PipeRoot ;
2831import org .labkey .api .pipeline .PipelineJob ;
2932import org .labkey .api .pipeline .PipelineJobException ;
3033import org .labkey .api .pipeline .PipelineJobService ;
3538import org .labkey .api .security .RequiresPermission ;
3639import org .labkey .api .security .RequiresSiteAdmin ;
3740import org .labkey .api .security .permissions .AdminPermission ;
41+ import org .labkey .api .security .permissions .ReadPermission ;
42+ import org .labkey .api .settings .AppProps ;
43+ import org .labkey .api .settings .ResourceURL ;
3844import org .labkey .api .util .HtmlString ;
3945import org .labkey .api .util .PageFlowUtil ;
4046import org .labkey .api .util .URLHelper ;
@@ -110,9 +116,10 @@ public URLHelper getSuccessURL(JobIdsForm form)
110116
111117 public ModelAndView getConfirmView (JobIdsForm form , BindException errors ) throws Exception
112118 {
119+
113120 return new HtmlView (HtmlString .unsafe ("This will change the status of the pipeline job with the provided ID to Cancelled. It is intended to help the situation when the normal UI leave a job in a perpetual 'Cancelling' state." +
114121 "To continue, enter a comma-delimited list of Job IDs and hit submit:<br><br>" +
115- "<label>Enter Job ID(s): </label><input name=\" jobIds\" ><br>" ));
122+ "<label>Enter Job ID(s): </label><input name=\" jobIds\" value = \" " + form . getJobIds () + " \" ><br>" ));
116123 }
117124
118125 public boolean handlePost (JobIdsForm form , BindException errors ) throws Exception
@@ -274,7 +281,7 @@ public ModelAndView getConfirmView(JobIdsForm form, BindException errors) throws
274281 {
275282 return new HtmlView (HtmlString .unsafe ("This will attempt to re-queue existing pipeline jobs using their serialized JSON text files. It is intended as a workaround for the situation where a job has been marked complete." +
276283 "To continue, enter a comma-delimited list of Job IDs and hit submit:<br><br>" +
277- "<label>Enter Job ID(s): </label><input name=\" jobIds\" ><br>" ));
284+ "<label>Enter Job ID(s): </label><input name=\" jobIds\" value= \" " + form . getJobIds () + " \" ><br>" ));
278285 }
279286
280287 public boolean handlePost (JobIdsForm form , BindException errors ) throws Exception
@@ -330,4 +337,96 @@ public boolean handlePost(JobIdsForm form, BindException errors) throws Exceptio
330337 return true ;
331338 }
332339 }
340+
341+ @ RequiresPermission (ReadPermission .class )
342+ public static class ViewJavaLogAction extends SimpleRedirectAction <ViewJavaLogForm >
343+ {
344+ @ Override
345+ public void validate (ViewJavaLogForm viewJavaLogForm , BindException errors )
346+ {
347+ super .validate (viewJavaLogForm , errors );
348+
349+ if (viewJavaLogForm .getJobId () == null )
350+ {
351+ errors .reject (ERROR_MSG , "Must provide JobId" );
352+ }
353+
354+ PipelineStatusFile sf = PipelineService .get ().getStatusFile (viewJavaLogForm .getJobId ());
355+ if (sf == null )
356+ {
357+ errors .reject (ERROR_MSG , "Unknown job: " + viewJavaLogForm .getJobId ());
358+ }
359+ else if (!sf .lookupContainer ().hasPermission (getUser (), ReadPermission .class ))
360+ {
361+ errors .reject (ERROR_MSG , "The current user does not have permission to view the folder: " + sf .lookupContainer ().getPath ());
362+ }
363+ }
364+
365+ @ Override
366+ public URLHelper getRedirectURL (ViewJavaLogForm viewJavaLogForm ) throws Exception
367+ {
368+ PipelineStatusFile sf = PipelineService .get ().getStatusFile (viewJavaLogForm .getJobId ());
369+ File parentDir = new File (sf .getFilePath ()).getParentFile ();
370+ if (!parentDir .exists ())
371+ {
372+ throw new IllegalArgumentException ("Log directory doesnt exist: " + parentDir .getPath ());
373+ }
374+
375+ File [] javaLogs = parentDir .listFiles ((dir , name ) -> {
376+ return name .endsWith (".java.log" );
377+ });
378+
379+ if (javaLogs == null || javaLogs .length == 0 )
380+ {
381+ throw new IllegalArgumentException ("No files ending with java.log found: " + parentDir .getPath ());
382+ }
383+
384+ long lastModifiedTime = Long .MIN_VALUE ;
385+ File chosenFile = null ;
386+ for (File file : javaLogs )
387+ {
388+ if (file .lastModified () > lastModifiedTime )
389+ {
390+ chosenFile = file ;
391+ lastModifiedTime = file .lastModified ();
392+ }
393+ }
394+
395+ PipeRoot root = PipelineService .get ().getPipelineRootSetting (sf .lookupContainer ());
396+ if (root == null )
397+ {
398+ throw new IllegalArgumentException ("Unable to find pipeline root for folder: " + sf .lookupContainer ().getPath ());
399+ }
400+
401+ if (!root .isUnderRoot (chosenFile ))
402+ {
403+ throw new IllegalArgumentException ("Log file is not under the pipeline root for folder: " + sf .lookupContainer ().getPath ());
404+ }
405+
406+ String relPath = root .relativePath (chosenFile );
407+ if (relPath == null )
408+ {
409+ throw new IllegalArgumentException ("Unable to find log file path for folder: " + sf .lookupContainer ().getPath ());
410+ }
411+
412+ relPath = org .labkey .api .util .Path .parse (FilenameUtils .separatorsToUnix (relPath )).encode ();
413+
414+ return new ResourceURL (AppProps .getInstance ().getBaseServerUrl () + root .getWebdavURL () + relPath );
415+ }
416+ }
417+
418+ public static class ViewJavaLogForm
419+ {
420+ private Integer _jobId ;
421+
422+ public Integer getJobId ()
423+ {
424+ return _jobId ;
425+ }
426+
427+ public void setJobId (Integer jobId )
428+ {
429+ _jobId = jobId ;
430+ }
431+ }
333432}
0 commit comments