Merge branch 'alpha/JAL-3362_Jalview_212_alpha' into alpha/merge_212_JalviewJS_2112
[jalview.git] / src / jalview / ws / rest / RestJobThread.java
index acb7904..f60ff0c 100644 (file)
@@ -59,8 +59,6 @@ import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.entity.mime.HttpMultipartMode;
 import org.apache.http.entity.mime.MultipartEntity;
 import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.protocol.BasicHttpContext;
-import org.apache.http.protocol.HttpContext;
 import org.apache.http.util.EntityUtils;
 
 public class RestJobThread extends AWSThread
@@ -216,9 +214,6 @@ public class RestJobThread extends AWSThread
   protected void doHttpReq(Stage stg, RestJob rj, String postUrl)
           throws Exception
   {
-    StringBuffer respText = new StringBuffer();
-    // con.setContentHandlerFactory(new
-    // jalview.ws.io.mime.HttpContentHandler());
     HttpRequestBase request = null;
     String messages = "";
     if (stg == Stage.SUBMIT)
@@ -253,7 +248,6 @@ public class RestJobThread extends AWSThread
     {
       DefaultHttpClient httpclient = new DefaultHttpClient();
 
-      HttpContext localContext = new BasicHttpContext();
       HttpResponse response = null;
       try
       {
@@ -287,39 +281,21 @@ public class RestJobThread extends AWSThread
         Cache.log.debug("Processing result set.");
         processResultSet(rj, response, request);
         break;
+
       case 202:
-        rj.statMessage = "<br>Job submitted successfully. Results available at this URL:\n"
-                + "<a href=" + rj.getJobId() + "\">" + rj.getJobId()
-                + "</a><br>";
-        rj.running = true;
+        markJobAsRunning(rj);
         break;
+
+      case 201:
+        // Created - redirect may be present. Fallthrough to 302
       case 302:
-        Header[] loc;
-        if (!rj.isSubmitted()
-                && (loc = response
-                        .getHeaders(HTTPConstants.HEADER_LOCATION)) != null
-                && loc.length > 0)
-        {
-          if (loc.length > 1)
-          {
-            Cache.log.warn("Ignoring additional " + (loc.length - 1)
-                    + " location(s) provided in response header ( next one is '"
-                    + loc[1].getValue() + "' )");
-          }
-          rj.setJobId(loc[0].getValue());
-          rj.setSubmitted(true);
-        }
+        extractJobId(rj, response);
         completeStatus(rj, response);
         break;
       case 500:
-        // Failed.
-        rj.setSubmitted(true);
-        rj.setAllowedServerExceptions(0);
-        rj.setSubjobComplete(true);
-        rj.error = true;
-        rj.running = false;
-        completeStatus(rj, response,
-                "" + getStage(stg) + "failed. Reason below:\n");
+        markAsFailed(rj, response);
+        completeStatus(rj, response, "" + getStage(stg)
+                + "failed. Reason below:\n");
         break;
       default:
         // Some other response. Probably need to pop up the content in a window.
@@ -346,6 +322,64 @@ public class RestJobThread extends AWSThread
     }
   }
 
+  private void markAsFailed(RestJob rj, HttpResponse response)
+  {
+    // Failed.
+    rj.setSubmitted(true);
+    rj.setAllowedServerExceptions(0);
+    rj.setSubjobComplete(true);
+    rj.error = true;
+    rj.running = false;
+  }
+
+  /**
+   * set the jobRunning flag and post a link to the physical result page encoded
+   * in rj.getJobId()
+   * 
+   * @param rj
+   */
+  private void markJobAsRunning(RestJob rj)
+  {
+    rj.statMessage = "<br>Job submitted successfully. Results available at this URL:\n"
+            + "<a href="
+            + rj.getJobId()
+            + "\">"
+            + rj.getJobId()
+            + "</a><br>";
+    rj.running = true;
+  }
+
+  /**
+   * extract the job ID URL from the redirect page. Does nothing if job is
+   * already running.
+   * 
+   * @param rj
+   * @param response
+   */
+  private void extractJobId(RestJob rj, HttpResponse response)
+  {
+    Header[] loc;
+    if (!rj.isSubmitted())
+    {
+
+      // redirect URL - typical for IBIVU type jobs.
+      if ((loc = response.getHeaders(HTTPConstants.HEADER_LOCATION)) != null
+              && loc.length > 0)
+      {
+        if (loc.length > 1)
+        {
+          Cache.log
+                  .warn("Ignoring additional "
+                          + (loc.length - 1)
+                          + " location(s) provided in response header ( next one is '"
+                          + loc[1].getValue() + "' )");
+        }
+        rj.setJobId(loc[0].getValue());
+        rj.setSubmitted(true);
+      }
+    }
+  }
+
   /**
    * job has completed. Something valid should be available from con
    * 
@@ -575,12 +609,12 @@ public class RestJobThread extends AWSThread
     /**
      * alignment panels derived from each alignment set returned by service.
      */
-    ArrayList<jalview.gui.AlignmentPanel> destPanels = new ArrayList<jalview.gui.AlignmentPanel>();
+    ArrayList<jalview.gui.AlignmentPanel> destPanels = new ArrayList<>();
     /**
      * list of instructions for how to process each distinct alignment set
      * returned by the job set
      */
-    ArrayList<AddDataTo> resultDest = new ArrayList<AddDataTo>();
+    ArrayList<AddDataTo> resultDest = new ArrayList<>();
     /**
      * when false, zeroth pane is panel derived from input deta.
      */
@@ -599,9 +633,9 @@ public class RestJobThread extends AWSThread
     boolean vsepjobs = restClient.service.isVseparable();
     // total number of distinct alignment sets generated by job set.
     int numAlSets = 0, als = 0;
-    List<AlignmentI> destAls = new ArrayList<AlignmentI>();
-    List<jalview.datamodel.HiddenColumns> destColsel = new ArrayList<jalview.datamodel.HiddenColumns>();
-    List<List<NewickFile>> trees = new ArrayList<List<NewickFile>>();
+    List<AlignmentI> destAls = new ArrayList<>();
+    List<jalview.datamodel.HiddenColumns> destColsel = new ArrayList<>();
+    List<List<NewickFile>> trees = new ArrayList<>();
 
     do
     {
@@ -660,7 +694,7 @@ public class RestJobThread extends AWSThread
 
               if (alset.trees != null)
               {
-                trees.add(new ArrayList<NewickFile>(alset.trees));
+                trees.add(new ArrayList<>(alset.trees));
               }
               else
               {
@@ -688,8 +722,8 @@ public class RestJobThread extends AWSThread
        */
       int vrestjob = 0;
       // Destination alignments for all result data.
-      ArrayList<SequenceGroup> visgrps = new ArrayList<SequenceGroup>();
-      Hashtable<String, SequenceGroup> groupNames = new Hashtable<String, SequenceGroup>();
+      ArrayList<SequenceGroup> visgrps = new ArrayList<>();
+      Hashtable<String, SequenceGroup> groupNames = new Hashtable<>();
       ArrayList<AlignmentAnnotation> visAlAn = null;
       for (nvertsep = 0; nvertsep < nvertseps; nvertsep++)
       {
@@ -953,7 +987,7 @@ public class RestJobThread extends AWSThread
                 }
                 if (visAlAn == null)
                 {
-                  visAlAn = new ArrayList<AlignmentAnnotation>();
+                  visAlAn = new ArrayList<>();
                 }
                 AlignmentAnnotation visan = null;
                 for (AlignmentAnnotation v : visAlAn)
@@ -1037,8 +1071,9 @@ public class RestJobThread extends AWSThread
       HiddenColumns destcs;
       String alTitle = MessageManager
               .formatMessage("label.webservice_job_title_on", new String[]
-              { restClient.service.details.Action,
-                  restClient.service.details.Name, restClient.viewTitle });
+              { restClient.service.details.getAction(),
+                  restClient.service.details.getName(),
+                  restClient.viewTitle });
       switch (action)
       {
       case newAlignment:
@@ -1248,7 +1283,7 @@ public class RestJobThread extends AWSThread
    */
   public boolean isValid()
   {
-    ArrayList<String> _warnings = new ArrayList<String>();
+    ArrayList<String> _warnings = new ArrayList<>();
     boolean validt = true;
     if (jobs != null)
     {