JAL-3210 Improvements to eclipse detection. New src tree and SwingJS updated from...
[jalview.git] / src / jalview / ext / paradise / Annotate3D.java
index 8b74d59..3b7044b 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b1)
- * Copyright (C) 2015 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -20,6 +20,7 @@
  */
 package jalview.ext.paradise;
 
+import jalview.util.JSONUtils;
 import jalview.util.MessageManager;
 import jalview.ws.HttpClientUtils;
 
@@ -32,11 +33,10 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.http.NameValuePair;
 import org.apache.http.message.BasicNameValuePair;
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
 import org.json.simple.parser.ContentHandler;
 import org.json.simple.parser.ParseException;
 
@@ -45,13 +45,20 @@ import org.json.simple.parser.ParseException;
  * 
  * @author jimp
  * 
- *         History: v1.0 revised from original due to refactoring of
- *         paradise-ubmc.u-strasbg.fr/webservices/annotate3d to
- *         http://arn-ibmc.in2p3.fr/api/compute/2d?tool=rnaview
+ * @version v1.0 revised from original due to refactoring of
+ *          paradise-ubmc.u-strasbg.fr/webservices/annotate3d to
+ *          http://arn-ibmc.in2p3.fr/api/compute/2d?tool=rnaview <br/>
+ *          See also testing URL from fjossinet:<br/>
+ *          http://charn2-ibmc.u-strasbg.fr:8080/api/compute/2d <br/>
+ *          If in doubt, check against the REST client at:
+ *          https://github.com/fjossinet/RNA-Science
+ *          -Toolbox/blob/master/pyrna/restclient.py
  */
 public class Annotate3D
 {
-  private static String twoDtoolsURL = "http://arn-ibmc.in2p3.fr/api/compute/2d";
+  // also test with
+  // "http://charn2-ibmc.u-strasbg.fr:8080/api/compute/2d";
+  private static String twoDtoolsURL = "http://arn-ibmc.in2p3.fr/api/compute/2d?tool=rnaview";
 
   private static ContentHandler createContentHandler()
   {
@@ -87,8 +94,8 @@ public class Annotate3D
       }
 
       @Override
-      public boolean startObjectEntry(String key) throws ParseException,
-              IOException
+      public boolean startObjectEntry(String key)
+              throws ParseException, IOException
       {
         // TODO Auto-generated method stub
         return false;
@@ -116,8 +123,8 @@ public class Annotate3D
       }
 
       @Override
-      public boolean primitive(Object value) throws ParseException,
-              IOException
+      public boolean primitive(Object value)
+              throws ParseException, IOException
       {
         // TODO Auto-generated method stub
         return false;
@@ -137,21 +144,26 @@ public class Annotate3D
     // return processJsonResponseFor(HttpClientUtils.doHttpUrlPost(twoDtoolsURL,
     // vals));
     ArrayList<Reader> readers = new ArrayList<Reader>();
-    final BufferedReader postResponse = HttpClientUtils.doHttpUrlPost(
-            twoDtoolsURL, vals, 0, 0);
+    final BufferedReader postResponse = HttpClientUtils
+            .doHttpUrlPost(twoDtoolsURL, vals, 0, 0);
     readers.add(postResponse);
     return readers.iterator();
 
   }
 
+  /**
+   * @param respons
+   * @return
+   * @throws Exception
+   */
   public static Iterator<Reader> processJsonResponseFor(Reader respons)
           throws Exception
   {
-    org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser();
+         // BH 2019 never called?
     try
     {
-      final JSONArray responses = (JSONArray) jp.parse(respons);
-      final Iterator rvals = responses.iterator();
+      @SuppressWarnings("unchecked")
+       final Iterator<Object> rvals = ((List<Object>) JSONUtils.parse(respons)).iterator();
       return new Iterator<Reader>()
       {
         @Override
@@ -160,10 +172,11 @@ public class Annotate3D
           return rvals.hasNext();
         }
 
-        @Override
+        @SuppressWarnings("unchecked")
+               @Override
         public Reader next()
         {
-          JSONObject val = (JSONObject) rvals.next();
+          Map<String, Object> val = (Map<String, Object>) rvals.next();
 
           Object sval = null;
           try
@@ -176,15 +189,13 @@ public class Annotate3D
           ;
           if (sval == null)
           {
-            System.err
-                    .println("DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :"
+            System.err.println(
+                    "DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :"
                             + val.toString());
 
             sval = "";
           }
-          return new StringReader(
-                  (sval instanceof JSONObject) ? ((JSONObject) sval)
-                          .toString() : sval.toString());
+          return new StringReader(sval.toString());
 
         }
 
@@ -221,9 +232,8 @@ public class Annotate3D
       };
     } catch (Exception foo)
     {
-      throw new Exception(
-              MessageManager
-                      .getString("exception.couldnt_parse_responde_from_annotated3d_server"),
+      throw new Exception(MessageManager.getString(
+              "exception.couldnt_parse_responde_from_annotated3d_server"),
               foo);
     }