2020import htsjdk .variant .variantcontext .Genotype ;
2121import htsjdk .variant .variantcontext .VariantContext ;
2222import htsjdk .variant .vcf .VCFFileReader ;
23+ import org .apache .commons .io .IOUtils ;
2324import org .apache .commons .lang3 .StringUtils ;
2425import org .jetbrains .annotations .NotNull ;
2526import org .json .JSONArray ;
4546import org .labkey .api .data .TableInfo ;
4647import org .labkey .api .data .TableSelector ;
4748import org .labkey .api .exp .api .ExpData ;
49+ import org .labkey .api .module .Module ;
50+ import org .labkey .api .module .ModuleLoader ;
4851import org .labkey .api .pipeline .PipeRoot ;
4952import org .labkey .api .pipeline .PipelineService ;
5053import org .labkey .api .pipeline .PipelineValidationException ;
5154import org .labkey .api .query .FieldKey ;
5255import org .labkey .api .query .QueryService ;
5356import org .labkey .api .query .UserSchema ;
57+ import org .labkey .api .resource .FileResource ;
5458import org .labkey .api .security .RequiresPermission ;
5559import org .labkey .api .security .permissions .AdminOperationsPermission ;
5660import org .labkey .api .security .permissions .AdminPermission ;
5963import org .labkey .api .security .permissions .UpdatePermission ;
6064import org .labkey .api .util .ExceptionUtil ;
6165import org .labkey .api .util .PageFlowUtil ;
66+ import org .labkey .api .util .Path ;
6267import org .labkey .api .view .JspView ;
6368import org .labkey .api .view .NavTree ;
6469import org .labkey .api .view .UnauthorizedException ;
6772import org .labkey .jbrowse .model .Database ;
6873import org .labkey .jbrowse .model .JsonFile ;
6974import org .labkey .jbrowse .pipeline .JBrowseSessionPipelineJob ;
75+ import org .labkey .sequenceanalysis .SequenceAnalysisModule ;
7076import org .springframework .validation .BindException ;
7177import org .springframework .validation .Errors ;
7278import org .springframework .web .servlet .ModelAndView ;
7379
80+ import java .io .File ;
81+ import java .io .FileInputStream ;
82+ import java .io .FileNotFoundException ;
7483import java .io .IOException ;
84+ import java .io .InputStream ;
85+ import java .nio .charset .StandardCharsets ;
7586import java .sql .SQLException ;
7687import java .util .ArrayList ;
7788import java .util .Arrays ;
@@ -704,4 +715,63 @@ public void setStop(Integer stop)
704715 _stop = stop ;
705716 }
706717 }
718+
719+ @ RequiresPermission (ReadPermission .class )
720+ public class GetSessionAction extends ReadOnlyApiAction <GetSessionForm >
721+ {
722+ private static final String DEMO = "demo" ;
723+
724+ @ Override
725+ public void validateForm (GetSessionForm form , Errors errors )
726+ {
727+ if (StringUtils .isEmpty (form .getSession ()))
728+ {
729+ errors .reject (ERROR_MSG , "Must provide the session Id" );
730+ }
731+ }
732+
733+ @ Override
734+ public Object execute (GetSessionForm form , BindException errors ) throws Exception
735+ {
736+ JSONObject resp ;
737+ if (DEMO .equalsIgnoreCase (form .getSession ()))
738+ {
739+ Module module = ModuleLoader .getInstance ().getModule (JBrowseModule .class );
740+ FileResource r = (FileResource )module .getModuleResolver ().lookup (Path .parse ("external/minimalSession.json" ));
741+ File jsonFile = r .getFile ();
742+ if (jsonFile == null )
743+ {
744+ throw new FileNotFoundException ("Unable to find JSON file: external/minimalSession.json" );
745+ }
746+
747+
748+ try (InputStream is = new FileInputStream (jsonFile ))
749+ {
750+ resp = new JSONObject (IOUtils .toString (is , StandardCharsets .UTF_8 ));
751+ }
752+ }
753+ else
754+ {
755+ resp = new JSONObject ();
756+ resp .put ("session" , form .getSession ());
757+ }
758+
759+ return new ApiSimpleResponse (resp );
760+ }
761+ }
762+
763+ public static class GetSessionForm
764+ {
765+ private String session ;
766+
767+ public String getSession ()
768+ {
769+ return session ;
770+ }
771+
772+ public void setSession (String session )
773+ {
774+ this .session = session ;
775+ }
776+ }
707777}
0 commit comments