+ /*
+ * set a maximum wait time before trying anyway
+ * to avoid hanging indefinitely
+ */
+ int waited = 0;
+ int pause = 25;
+ while (busy && waited < 1001)
+ {
+ try
+ {
+ Thread.sleep(pause);
+ waited += pause;
+ } catch (InterruptedException q)
+ {
+ }
+ }
+ busy = true;
+ long startTime = System.currentTimeMillis();
+ try
+ {
+ return sendRestCommand(command);
+ } finally
+ {
+ /*
+ * Make sure busy flag is reset come what may!
+ */
+ busy = false;
+ if (debug)
+ {
+ System.out.println("Chimera command took "
+ + (System.currentTimeMillis() - startTime) + "ms: "
+ + command);
+ }
+ }
+ }
+
+ /**
+ * Sends the command to Chimera's REST API, and returns any response lines.
+ *
+ * @param command
+ * @return
+ */
+ protected List<String> sendRestCommand(String command)
+ {
+ String restUrl = "http://127.0.0.1:" + this.chimeraRestPort + "/run";
+ List<NameValuePair> commands = new ArrayList<>(1);
+ String method = getHttpRequestMethod();
+ if ("GET".equals(method))
+ {
+ try
+ {
+ command = URLEncoder.encode(command, StandardCharsets.UTF_8.name());
+ } catch (UnsupportedEncodingException e)
+ {
+ command = command.replace(" ", "+").replace("#", "%23")
+ .replace("|", "%7C").replace(";", "%3B")
+ .replace(":", "%3A");
+ }
+ }
+ commands.add(new BasicNameValuePair("command", command));
+
+ List<String> reply = new ArrayList<>();
+ BufferedReader response = null;
+ try
+ {
+ response = "GET".equals(method)
+ ? HttpClientUtils.doHttpGet(restUrl, commands,
+ CONNECTION_TIMEOUT_MS, REST_REPLY_TIMEOUT_MS)
+ : HttpClientUtils.doHttpUrlPost(restUrl, commands,
+ CONNECTION_TIMEOUT_MS, REST_REPLY_TIMEOUT_MS);
+ String line = "";
+ while ((line = response.readLine()) != null)
+ {
+ reply.add(line);
+ }
+ } catch (Exception e)
+ {
+ logger.error("REST call '" + command + "' failed: " + e.getMessage());
+ } finally