Merge branch 'mmw/JAL-4199-web-services-testing' into development/Release_2_12_Branch
[jalview.git] / src / jalview / ws2 / client / slivka / SlivkaWSClient.java
index b32c188..7dcdae1 100644 (file)
@@ -15,6 +15,7 @@ import java.util.regex.Pattern;
 
 import jalview.api.FeatureColourI;
 import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
@@ -45,10 +46,10 @@ public class SlivkaWSClient implements WebServiceClientI
 
   final SlivkaClient client;
 
-  SlivkaWSClient(SlivkaService service)
+  SlivkaWSClient(SlivkaClient client, SlivkaService service)
   {
     this.service = service;
-    this.client = service.getClient();
+    this.client = client;
   }
 
   @Override
@@ -71,7 +72,7 @@ public class SlivkaWSClient implements WebServiceClientI
   public WebServiceJobHandle submit(List<SequenceI> sequences,
       List<ArgumentI> args, Credentials credentials) throws IOException
   {
-    var request = new uk.ac.dundee.compbio.slivkaclient.JobRequest();
+    var request = new uk.ac.dundee.compbio.slivkaclient.RequestValues();
     for (Parameter param : service.getParameters())
     {
       // TODO: restrict input sequences parameter name to "sequences"
@@ -94,7 +95,7 @@ public class SlivkaWSClient implements WebServiceClientI
         }
         if (format == null)
         {
-          Cache.log.warn(String.format(
+          Console.warn(String.format(
               "Unknown input format %s, assuming fasta.",
               fileParam.getMediaType()));
           format = FileFormat.Fasta;
@@ -115,7 +116,7 @@ public class SlivkaWSClient implements WebServiceClientI
         Parameter param = service.getParameter(paramId);
         if (param instanceof Parameter.FlagParameter)
         {
-          if (arg.getValue() != null && !arg.getValue().isBlank())
+          if (arg.getValue() != null && !arg.getValue().isEmpty())
             request.addData(paramId, true);
           else
             request.addData(paramId, false);
@@ -130,8 +131,8 @@ public class SlivkaWSClient implements WebServiceClientI
         }
       }
     }
-    var job = service.submitJob(request);
-    return createJobHandle(job.getId());
+    var jobId = client.submitJob(service, request);
+    return createJobHandle(jobId);
   }
 
   protected WebServiceJobHandle createJobHandle(String jobId)
@@ -144,8 +145,7 @@ public class SlivkaWSClient implements WebServiceClientI
   @Override
   public JobStatus getStatus(WebServiceJobHandle job) throws IOException
   {
-    var slivkaJob = client.getJob(job.getJobId());
-    return statusMap.getOrDefault(slivkaJob.getStatus(), JobStatus.UNKNOWN);
+    return statusMap.getOrDefault(client.fetchJobStatus(job.getJobId()), JobStatus.UNKNOWN);
   }
 
   protected static final EnumMap<Job.Status, JobStatus> statusMap = new EnumMap<>(Job.Status.class);
@@ -167,14 +167,13 @@ public class SlivkaWSClient implements WebServiceClientI
   @Override
   public String getLog(WebServiceJobHandle job) throws IOException
   {
-    var slivkaJob = client.getJob(job.getJobId());
-    for (var f : slivkaJob.getResults())
+    for (var f : client.fetchFilesList(job.getJobId()))
     {
       if (f.getLabel().equals("log"))
       {
         ByteArrayOutputStream stream = new ByteArrayOutputStream();
-        f.writeTo(stream);
-        return stream.toString(StandardCharsets.UTF_8);
+        client.writeFileTo(f, stream);
+        return stream.toString("UTF-8");
       }
     }
     return "";
@@ -183,14 +182,13 @@ public class SlivkaWSClient implements WebServiceClientI
   @Override
   public String getErrorLog(WebServiceJobHandle job) throws IOException
   {
-    var slivkaJob = client.getJob(job.getJobId());
-    for (var f : slivkaJob.getResults())
+    for (var f : client.fetchFilesList(job.getJobId()))
     {
       if (f.getLabel().equals("error-log"))
       {
         ByteArrayOutputStream stream = new ByteArrayOutputStream();
-        f.writeTo(stream);
-        return stream.toString(StandardCharsets.UTF_8);
+        client.writeFileTo(f, stream);
+        return stream.toString("UTF-8");
       }
     }
     return "";
@@ -200,7 +198,7 @@ public class SlivkaWSClient implements WebServiceClientI
   public void cancel(WebServiceJobHandle job)
       throws IOException, UnsupportedOperationException
   {
-    throw new UnsupportedOperationException(
+    Console.warn(
         "slivka client does not support job cancellation");
   }
 }
@@ -209,16 +207,15 @@ class SlivkaAlignmentWSClient extends SlivkaWSClient
     implements AlignmentWebServiceClientI
 {
 
-  SlivkaAlignmentWSClient(SlivkaService service)
+  SlivkaAlignmentWSClient(SlivkaClient client, SlivkaService service)
   {
-    super(service);
+    super(client, service);
   }
 
   @Override
   public AlignmentI getAlignment(WebServiceJobHandle job) throws IOException
   {
-    var slivkaJob = client.getJob(job.getJobId());
-    for (var f : slivkaJob.getResults())
+    for (var f : client.fetchFilesList(job.getJobId()))
     {
       // TODO: restrict result file label to "alignment"
       FileFormat format;
@@ -235,7 +232,7 @@ class SlivkaAlignmentWSClient extends SlivkaWSClient
       return new FormatAdapter().readFile(f.getContentUrl().toString(),
           DataSourceType.URL, format);
     }
-    Cache.log.warn("No alignment found on the server");
+    Console.warn("No alignment found on the server");
     throw new IOException("no alignment found");
   }
 
@@ -244,9 +241,9 @@ class SlivkaAlignmentWSClient extends SlivkaWSClient
 class SlivkaAnnotationWSClient extends SlivkaWSClient
     implements AnnotationWebServiceClientI
 {
-  SlivkaAnnotationWSClient(SlivkaService service)
+  SlivkaAnnotationWSClient(SlivkaClient client, SlivkaService service)
   {
-    super(service);
+    super(client, service);
   }
 
   @Override
@@ -254,10 +251,9 @@ class SlivkaAnnotationWSClient extends SlivkaWSClient
       List<SequenceI> sequences, Map<String, FeatureColourI> colours,
       Map<String, FeatureMatcherSetI> filters) throws IOException
   {
-    var slivkaJob = client.getJob(job.getJobId());
     var aln = new Alignment(sequences.toArray(new SequenceI[sequences.size()]));
     boolean featPresent = false, annotPresent = false;
-    for (var f : slivkaJob.getResults())
+    for (var f : client.fetchFilesList(job.getJobId()))
     {
       // TODO: restrict file label to "annotations" or "features"
       var match = mediaTypePattern.matcher(f.getMediaType());
@@ -270,7 +266,7 @@ class SlivkaAnnotationWSClient extends SlivkaWSClient
             aln, service.getId(), f.getContentUrl().toString(),
             DataSourceType.URL);
         if (annotPresent)
-          Cache.log.debug(format("loaded annotations for %s", service.getId()));
+          Console.debug(format("loaded annotations for %s", service.getId()));
       }
       else if (fmt.equalsIgnoreCase("jalview-features"))
       {
@@ -278,13 +274,13 @@ class SlivkaAnnotationWSClient extends SlivkaWSClient
             DataSourceType.URL);
         featPresent = ff.parse(aln, colours, true);
         if (featPresent)
-          Cache.log.debug(format("loaded features for %s", service.getId()));
+          Console.debug(format("loaded features for %s", service.getId()));
       }
     }
     if (!annotPresent)
-      Cache.log.debug(format("no annotations found for %s", service.getId()));
+      Console.debug(format("no annotations found for %s", service.getId()));
     if (!featPresent)
-      Cache.log.debug(format("no features found for %s", service.getId()));
+      Console.debug(format("no features found for %s", service.getId()));
     return aln.getAlignmentAnnotation() != null ? Arrays.asList(aln.getAlignmentAnnotation())
         : Collections.emptyList();
   }