/**
  * Private constructor is called by singleton creator.
  *
  * @throws DefaultHttpClientException
  */
 private MyExperimentRestClient() throws DefaultHttpClientException {
   config = new MyExperimentConfig();
   host = config.getProp("myexperiment.host");
   user = config.getProp("myexperiment.username");
   password = config.getProp("myexperiment.password");
   myExpRestClient = new DefaultHttpAuthRestClient("http", host, 80, "");
   myExpRestClient.setUser(user);
   myExpRestClient.setPassword(password);
 }
 /**
  * Get workflow xml description from myExperiment
  *
  * @return Workflow object
  */
 public Workflow getMyExperimentWorkflow(int wfId) throws DefaultHttpClientException {
   Workflow wf = new Workflow();
   wf.setWfid(wfId);
   String fileName = wfId + ".t2flow";
   wf.setFilename(fileName);
   // get workflow metadata
   if (myExperimentSession != null) {
     HttpResponse response =
         myExpRestClient.executeGet(
             "/workflow.xml?id=" + wfId, "application/xml", myExperimentSession);
     int statusCode = response.getStatusLine().getStatusCode();
     if (statusCode == 404) {
       throw new DefaultHttpClientException(
           "Workflow " + wfId + " is not available on myExperiment.");
     }
     XmlResponseParser responseParser = new XmlResponseParser(response);
     if (responseParser == null) {
       throw new DefaultHttpClientException("Error while initialising response parser");
     }
     responseParser.parseResponse();
     if (responseParser.isIsParsed()) {
       XPathEvaluator xPathEval = new XPathEvaluator(responseParser);
       wf.setTitle(getXmlDescrWorkflowProperty(xPathEval, "/workflow/title", false));
       wf.setDescription(getXmlDescrWorkflowProperty(xPathEval, "/workflow/description", true));
       wf.setMyExperimentContentUri(
           getXmlDescrWorkflowProperty(xPathEval, "/workflow/content-uri", false));
       wf.setMyExperimentPreviewUri(
           getXmlDescrWorkflowProperty(xPathEval, "/workflow/preview", false));
     }
   } else {
     throw new DefaultHttpClientException("MyExperiment-Session is not initialised!");
   }
   return wf;
 }
 /**
  * Get workflow t2flow input stream from content-uri
  *
  * @param wf Workflow
  * @return t2flow input stream
  */
 public InputStream getMyExperimentWorkflowContentStream(Workflow wf)
     throws DefaultHttpClientException {
   try {
     if (myExperimentSession != null) {
       if (wf != null) {
         String contentUri = wf.getMyExperimentContentUri();
         if (contentUri != null) {
           URL url = new URL(wf.getMyExperimentContentUri());
           HttpResponse wfResponse =
               myExpRestClient.executeGet(
                   url,
                   "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                   myExperimentSession);
           return wfResponse.getEntity().getContent();
         } else {
           throw new DefaultHttpClientException("Invalid workflow or content uri is empty");
         }
       } else {
         throw new DefaultHttpClientException("Invalid workflow object!");
       }
     } else {
       throw new DefaultHttpClientException("MyExperiment-Session is not initialised!");
     }
   } catch (Exception ex) {
     throw new DefaultHttpClientException(ex.getMessage());
   }
 }
  /**
   * Intialise myExperiment session
   *
   * @return Session handler string
   */
  public void createNewMyExperimentSession() throws DefaultHttpClientException {
    // get myExperiment session
    String myExperimentSessionStr = null;
    try {
      String sessRequest =
          "<session><username>"
              + user
              + "</username><password>"
              + password
              + "</password></session>";
      HttpResponse sessionResponse =
          myExpRestClient.executeStringPost("/session", sessRequest, ContentType.APPLICATION_XML);
      InputStream is = sessionResponse.getEntity().getContent();
      Header[] headers = sessionResponse.getAllHeaders();

      for (int i = 0; i < headers.length; i++) {
        Header h = headers[i];
        if (h.getName().equals("Set-Cookie")) {
          String val = h.getValue();
          int end = val.indexOf(";");
          myExperimentSessionStr = val.substring(0, end);
        }
      }
      EntityUtils.consume(sessionResponse.getEntity());
      myExperimentSession = myExperimentSessionStr;
    } catch (Exception ex) {
      throw new DefaultHttpClientException(ex.getMessage());
    }
  }