/**
   * Process connection response data.
   *
   * @param conn connection to read response data from.
   * @return assembled response as string.
   * @throws IOException on I/O error
   */
  private StringBuilder readResponse(URLConnection conn) throws IOException {
    StringBuilder retval = new StringBuilder();
    HttpURLConnection http = (HttpURLConnection) conn;
    int resp = 200;
    try {
      resp = http.getResponseCode();
    } catch (Throwable ex) {
    }

    if (resp >= 200 && resp < 300) {
      BufferedReader input = null;
      try {
        input = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      } catch (Throwable ex) {
        retval.append(ex.toString());
        return retval;
      }

      String line = null;
      while ((line = input.readLine()) != null) {
        retval.append(line);
        retval.append("\n");
      }
      input.close();
    } else {
      retval.append(http.getResponseMessage());
    }

    LogContext.getLogger().finer(String.format("<-- HTTP Response: %d: %s", resp, retval));

    return retval;
  }
  private void testConnection() throws IOException {
    // Test database connection.
    URLConnection conn = makeTestConnection();
    if (conn != null) {
      conn.connect();
      StringBuilder buf = readResponse(conn);

      buf.insert(0, "Experiment confirmation: ");
      LogContext.getLogger().fine(buf.toString());
    }
  }
  /**
   * {@inheritDoc}
   *
   * @see edu.mcmaster.maplelab.common.datamodel.TrialLogger#saveSessionConfig()
   */
  public void saveSessionConfig() throws IOException {
    // Save session configuration data.
    URLConnection conn = makeSaveConfigConnection();
    if (conn == null) return;

    conn.setDoOutput(true);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    Map<String, String> fields = new HashMap<String, String>();

    fields.put("config", getSession().toPropertiesString());

    String row = encode(fields);

    LogContext.getLogger().finer("-> config log: " + row);

    if (getSession().getExperimentDBKey() >= 0) {
      OutputStreamWriter out = null;
      try {
        out = new OutputStreamWriter(conn.getOutputStream());
        out.write(row);
        out.flush();
      } finally {
        if (out != null) out.close();
      }
      conn.connect();
      readResponse(conn);
    }
  }
 /**
  * Attempt to make errors generated when 3D not available a little nicer. {@inheritDoc}
  *
  * @see java.awt.Component#addNotify()
  */
 @Override
 public void addNotify() {
   try {
     super.addNotify();
   } catch (UnsatisfiedLinkError ex) {
     LogContext.getLogger()
         .warning("Sorry, 3D model rendering has not been enabled " + "for this platform.");
     ex.printStackTrace();
   }
 }
  /**
   * Setup a connection to the server
   *
   * @param path format encoded path string (relative to base URL)
   * @param pathArgs arguments to String.format() for path
   * @return opened connection
   * @throws IOException on setup error
   */
  protected URLConnection initConnection(String path, Object... pathArgs) throws IOException {

    URL url = null;
    try {
      url = new URL(_base + String.format(path, pathArgs));

      LogContext.getLogger().finer("--> Initializing HTTP connection: " + url);
    } catch (MalformedURLException ex) {
      throw (IOException) new IOException("Couldn't create test URL: " + url).initCause(ex);
    }

    URLConnection retval = url.openConnection();
    retval.setUseCaches(false);
    retval.setDoInput(true);

    return retval;
  }
  /**
   * Convenience function for converting a map of key-value pairs into a URL query string.
   *
   * @param fields key-value pairs
   * @return query string.
   */
  protected static String encode(Map<String, String> fields) {
    StringBuilder buf = new StringBuilder();
    try {
      boolean prev = false;
      for (String key : fields.keySet()) {
        if (prev) {
          buf.append("&");
        } else {
          prev = true;
        }

        buf.append(URLEncoder.encode(key, "UTF-8"));
        buf.append("=");
        buf.append(URLEncoder.encode(fields.get(key), "UTF-8"));
      }
    } catch (Exception e) {
      LogContext.getLogger().log(Level.WARNING, "Problem encoding map.", e);
    }
    return buf.toString();
  }