+ 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
+ {
+ if (response != null)
+ {
+ try
+ {
+ response.close();
+ } catch (IOException e)
+ {
+ }
+ }
+ }
+ return reply;
+ }
+
+ /**
+ * Returns "POST" as the HTTP request method to use for REST service calls to
+ * Chimera
+ *
+ * @return
+ */
+ protected String getHttpRequestMethod()
+ {
+ return "POST";
+ }
+
+ /**
+ * Send a command to stdin of Chimera process, and optionally read any
+ * responses.
+ *
+ * @param command
+ * @param readReply
+ * @return
+ */
+ protected List<String> sendStdinCommand(String command, boolean readReply)
+ {
+ chimeraListenerThread.clearResponse(command);