JAL-3070 pull up hostURL and postURL to UIinfo and getter for action ext
[jalview.git] / src / jalview / ws / rest / RestServiceDescription.java
index 177ebb8..9268975 100644 (file)
@@ -22,6 +22,8 @@ package jalview.ws.rest;
 
 import jalview.datamodel.SequenceI;
 import jalview.io.packed.DataProvider.JvDataType;
+import jalview.util.StringUtils;
+import jalview.ws.api.UIinfo;
 import jalview.ws.rest.params.Alignment;
 import jalview.ws.rest.params.AnnotationFile;
 import jalview.ws.rest.params.SeqGroupIndexVector;
@@ -39,6 +41,9 @@ import java.util.regex.Pattern;
 
 public class RestServiceDescription
 {
+  private static final Pattern PARAM_ENCODED_URL_PATTERN = Pattern
+          .compile("([?&])([A-Za-z0-9_]+)=\\$([^$]+)\\$");
+
   /**
    * create a new rest service description ready to be configured
    */
@@ -62,10 +67,7 @@ public class RestServiceDescription
           boolean vseparable, char gapCharacter)
   {
     super();
-    this.details = new UIinfo();
-    details.Action = action == null ? "" : action;
-    details.description = description == null ? "" : description;
-    details.Name = name == null ? "" : name;
+    this.details = new UIinfo(action, action, name, description, postUrl);
     this.postUrl = postUrl == null ? "" : postUrl;
     this.urlSuffix = urlSuffix == null ? "" : urlSuffix;
     if (inputParams != null)
@@ -77,6 +79,7 @@ public class RestServiceDescription
     this.gapCharacter = gapCharacter;
   }
 
+  @Override
   public boolean equals(Object o)
   {
     if (o == null || !(o instanceof RestServiceDescription))
@@ -87,60 +90,17 @@ public class RestServiceDescription
     boolean diff = (gapCharacter != other.gapCharacter);
     diff |= vseparable != other.vseparable;
     diff |= hseparable != other.hseparable;
-    diff |= !(urlSuffix.equals(other.urlSuffix));
+    diff |= !(urlSuffix == null && other.urlSuffix == null
+            || (urlSuffix != null && other.urlSuffix != null
+                    && urlSuffix.equals(other.urlSuffix)));
     // TODO - robust diff that includes constants and reordering of URL
     // diff |= !(postUrl.equals(other.postUrl));
     // diff |= !inputParams.equals(other.inputParams);
-    diff |= !details.Name.equals(other.details.Name);
-    diff |= !details.Action.equals(other.details.Action);
-    diff |= !details.description.equals(other.details.description);
+    diff |= !details.equals(other.details);
     return !diff;
   }
 
-  /**
-   * Service UI Info { Action, Specific Name of Service, Brief Description }
-   */
-
-  public class UIinfo
-  {
-    public String getAction()
-    {
-      return Action;
-    }
-
-    public void setAction(String action)
-    {
-      Action = action;
-    }
-
-    public String getName()
-    {
-      return Name;
-    }
-
-    public void setName(String name)
-    {
-      Name = name;
-    }
-
-    public String getDescription()
-    {
-      return description;
-    }
-
-    public void setDescription(String description)
-    {
-      this.description = description;
-    }
-
-    String Action;
-
-    String Name;
-
-    String description;
-  }
-
-  public UIinfo details = new UIinfo();
+  public UIinfo details;
 
   public String getAction()
   {
@@ -231,7 +191,7 @@ public class RestServiceDescription
   /**
    * input info given as key/value pairs - mapped to post arguments
    */
-  Map<String, InputType> inputParams = new HashMap<String, InputType>();
+  Map<String, InputType> inputParams = new HashMap<>();
 
   /**
    * assigns the given inputType it to its corresponding input parameter token
@@ -356,131 +316,6 @@ public class RestServiceDescription
     return invalidMessage == null;
   }
 
-  private static boolean debug = false;
-
-  /**
-   * parse the string into a list
-   * 
-   * @param list
-   * @param separator
-   * @return elements separated by separator
-   */
-  public static String[] separatorListToArray(String list, String separator)
-  {
-    int seplen = separator.length();
-    if (list == null || list.equals("") || list.equals(separator))
-      return null;
-    java.util.ArrayList<String> jv = new ArrayList<String>();
-    int cp = 0, pos, escape;
-    boolean wasescaped = false, wasquoted = false;
-    String lstitem = null;
-    while ((pos = list.indexOf(separator, cp)) >= cp)
-    {
-
-      escape = (pos > 0 && list.charAt(pos - 1) == '\\') ? -1 : 0;
-      if (wasescaped || wasquoted)
-      {
-        // append to previous pos
-        jv.set(jv.size() - 1,
-                lstitem = lstitem + separator
-                        + list.substring(cp, pos + escape));
-
-      }
-      else
-      {
-        jv.add(lstitem = list.substring(cp, pos + escape));
-      }
-      cp = pos + seplen;
-      wasescaped = escape == -1;
-      if (!wasescaped)
-      {
-        // last separator may be in an unmatched quote
-        if (java.util.regex.Pattern.matches("('[^']*')*[^']*'", lstitem))
-        {
-          wasquoted = true;
-        }
-      }
-
-    }
-    if (cp < list.length())
-    {
-      String c = list.substring(cp);
-      if (wasescaped || wasquoted)
-      {
-        // append final separator
-        jv.set(jv.size() - 1, lstitem + separator + c);
-      }
-      else
-      {
-        if (!c.equals(separator))
-        {
-          jv.add(c);
-        }
-      }
-    }
-    if (jv.size() > 0)
-    {
-      String[] v = jv.toArray(new String[jv.size()]);
-      jv.clear();
-      if (debug)
-      {
-        System.err.println("Array from '" + separator
-                + "' separated List:\n" + v.length);
-        for (int i = 0; i < v.length; i++)
-        {
-          System.err.println("item " + i + " '" + v[i] + "'");
-        }
-      }
-      return v;
-    }
-    if (debug)
-    {
-      System.err.println("Empty Array from '" + separator
-              + "' separated List");
-    }
-    return null;
-  }
-
-  /**
-   * concatenate the list with separator
-   * 
-   * @param list
-   * @param separator
-   * @return concatenated string
-   */
-  public static String arrayToSeparatorList(String[] list, String separator)
-  {
-    StringBuffer v = new StringBuffer();
-    if (list != null && list.length > 0)
-    {
-      for (int i = 0, iSize = list.length; i < iSize; i++)
-      {
-        if (list[i] != null)
-        {
-          if (v.length() > 0)
-          {
-            v.append(separator);
-          }
-          // TODO - escape any separator values in list[i]
-          v.append(list[i]);
-        }
-      }
-      if (debug)
-      {
-        System.err.println("Returning '" + separator
-                + "' separated List:\n");
-        System.err.println(v);
-      }
-      return v.toString();
-    }
-    if (debug)
-    {
-      System.err.println("Returning empty '" + separator
-              + "' separated List\n");
-    }
-    return "" + separator;
-  }
-
   /**
    * parse a string containing a list of service properties and configure the
    * service description
@@ -492,7 +327,7 @@ public class RestServiceDescription
   private boolean configureFromServiceInputProperties(String propList,
           StringBuffer warnings)
   {
-    String[] props = separatorListToArray(propList, ",");
+    String[] props = StringUtils.separatorListToArray(propList, ",");
     if (props == null)
     {
       return true;
@@ -573,7 +408,7 @@ public class RestServiceDescription
     }
     StringTokenizer st = new StringTokenizer(outstring, ";");
     String tok = "";
-    resultData = new ArrayList<JvDataType>();
+    resultData = new ArrayList<>();
     while (st.hasMoreTokens())
     {
       try
@@ -581,8 +416,8 @@ public class RestServiceDescription
         resultData.add(JvDataType.valueOf(tok = st.nextToken()));
       } catch (NoSuchElementException x)
       {
-        warnings.append("Invalid result type: '" + tok
-                + "' (must be one of: ");
+        warnings.append(
+                "Invalid result type: '" + tok + "' (must be one of: ");
         String sep = "";
         for (JvDataType vl : JvDataType.values())
         {
@@ -597,7 +432,7 @@ public class RestServiceDescription
 
   private String getServiceIOProperties()
   {
-    ArrayList<String> vls = new ArrayList<String>();
+    ArrayList<String> vls = new ArrayList<>();
     if (isHseparable())
     {
       vls.add("hseparable");
@@ -610,20 +445,22 @@ public class RestServiceDescription
     ;
     vls.add(new String("gapCharacter='" + gapCharacter + "'"));
     vls.add(new String("returns='" + _genOutputFormatString() + "'"));
-    return arrayToSeparatorList(vls.toArray(new String[0]), ",");
+    return StringUtils.arrayToSeparatorList(vls.toArray(new String[0]),
+            ",");
   }
 
+  @Override
   public String toString()
   {
     StringBuffer result = new StringBuffer();
     result.append("|");
-    result.append(details.Name);
+    result.append(details.getName());
     result.append('|');
-    result.append(details.Action);
+    result.append(details.getAction());
     result.append('|');
-    if (details.description != null)
+    if (details.getDescription() != null)
     {
-      result.append(details.description);
+      result.append(details.getDescription());
     }
     ;
     // list job input flags
@@ -653,7 +490,7 @@ public class RestServiceDescription
   public boolean configureFromEncodedString(String encoding,
           StringBuffer warnings)
   {
-    String[] list = separatorListToArray(encoding, "|");
+    String[] list = StringUtils.separatorListToArray(encoding, "|");
 
     int nextpos = parseServiceList(list, warnings, 0);
     if (nextpos > 0)
@@ -675,7 +512,8 @@ public class RestServiceDescription
    * @param p
    * @return
    */
-  protected int parseServiceList(String[] list, StringBuffer warnings, int p)
+  protected int parseServiceList(String[] list, StringBuffer warnings,
+          int p)
   {
     boolean invalid = false;
     // look for the first non-empty position - expect it to be service name
@@ -683,9 +521,8 @@ public class RestServiceDescription
     {
       p++;
     }
-    details.Name = list[p];
-    details.Action = list[p + 1];
-    details.description = list[p + 2];
+    details = new UIinfo(list[p + 1], list[p + 1], list[p], list[p + 2],
+            postUrl);
     invalid |= !configureFromServiceInputProperties(list[p + 3], warnings);
     if (list.length - p > 5 && list[p + 5] != null
             && list[p + 5].trim().length() > 5)
@@ -700,7 +537,8 @@ public class RestServiceDescription
               && list[p + 4].trim().length() > 5)
       {
         urlSuffix = null;
-        invalid |= !configureFromInputParamEncodedUrl(list[p + 4], warnings);
+        invalid |= !configureFromInputParamEncodedUrl(list[p + 4],
+                warnings);
         p += 5;
       }
     }
@@ -754,8 +592,8 @@ public class RestServiceDescription
             url.append("$");
             url.append(param.getValue().getURLtokenPrefix());
             url.append(":");
-            url.append(arrayToSeparatorList(vals.toArray(new String[0]),
-                    ","));
+            url.append(StringUtils.arrayToSeparatorList(
+                    vals.toArray(new String[0]), ","));
             url.append("$");
           }
         }
@@ -781,9 +619,8 @@ public class RestServiceDescription
     boolean valid = true;
     int lastp = 0;
     String url = new String();
-    Matcher prms = Pattern.compile("([?&])([A-Za-z0-9_]+)=\\$([^$]+)\\$")
-            .matcher(ipurl);
-    Map<String, InputType> iparams = new Hashtable<String, InputType>();
+    Matcher prms = PARAM_ENCODED_URL_PATTERN.matcher(ipurl);
+    Map<String, InputType> iparams = new Hashtable<>();
     InputType jinput;
     while (prms.find())
     {
@@ -802,8 +639,8 @@ public class RestServiceDescription
         iprmparams = iprm.substring(colon + 1);
         iprm = iprm.substring(0, colon);
       }
-      valid = parseTypeString(prms.group(0), tok, iprm, iprmparams,
-              iparams, warnings);
+      valid = parseTypeString(prms.group(0), tok, iprm, iprmparams, iparams,
+              warnings);
     }
     if (valid)
     {
@@ -824,11 +661,9 @@ public class RestServiceDescription
   public static Class[] getInputTypes()
   {
     // TODO - find a better way of maintaining this classlist
-    return new Class[]
-    { jalview.ws.rest.params.Alignment.class,
+    return new Class[] { jalview.ws.rest.params.Alignment.class,
         jalview.ws.rest.params.AnnotationFile.class,
-        SeqGroupIndexVector.class,
-        jalview.ws.rest.params.SeqIdVector.class,
+        SeqGroupIndexVector.class, jalview.ws.rest.params.SeqIdVector.class,
         jalview.ws.rest.params.SeqVector.class,
         jalview.ws.rest.params.Tree.class };
   }
@@ -843,11 +678,12 @@ public class RestServiceDescription
     {
       try
       {
-        jinput = (InputType) (type.getConstructor().newInstance(null));
+        jinput = (InputType) (type.getConstructor().newInstance());
         if (iprm.equalsIgnoreCase(jinput.getURLtokenPrefix()))
         {
-          ArrayList<String> al = new ArrayList<String>();
-          for (String prprm : separatorListToArray(iprmparams, ","))
+          ArrayList<String> al = new ArrayList<>();
+          for (String prprm : StringUtils.separatorListToArray(iprmparams,
+                  ","))
           {
             // hack to ensure that strings like "sep=','" containing unescaped
             // commas as values are concatenated
@@ -920,7 +756,8 @@ public class RestServiceDescription
   /**
    * search the input types for an instance of the given class
    * 
-   * @param <validInput.inputType> class1
+   * @param <validInput.inputType>
+   *          class1
    * @return
    */
   public boolean inputInvolves(Class<?> class1)
@@ -954,7 +791,7 @@ public class RestServiceDescription
     return jobId + urlSuffix;
   }
 
-  private List<JvDataType> resultData = new ArrayList<JvDataType>();
+  private List<JvDataType> resultData = new ArrayList<>();
 
   /**
    * 
@@ -968,7 +805,7 @@ public class RestServiceDescription
   {
     if (resultData == null)
     {
-      resultData = new ArrayList<JvDataType>();
+      resultData = new ArrayList<>();
     }
     resultData.add(dt);
   }
@@ -998,8 +835,8 @@ public class RestServiceDescription
   public static List<RestServiceDescription> parseDescriptions(
           String services) throws Exception
   {
-    String[] list = separatorListToArray(services, "|");
-    List<RestServiceDescription> svcparsed = new ArrayList<RestServiceDescription>();
+    String[] list = StringUtils.separatorListToArray(services, "|");
+    List<RestServiceDescription> svcparsed = new ArrayList<>();
     int p = 0, lastp = 0;
     StringBuffer warnings = new StringBuffer();
     do