private static char[] readResponseData(InputStream stream, String encoding) {
    BufferedReader in = null;
    char[] data = null;
    try {
      StringBuffer buf = new StringBuffer();
      data = new char[1024];

      in = new BufferedReader(new InputStreamReader(stream, encoding));
      int charsRead;
      while ((charsRead = in.read(data)) != -1) {
        buf.append(data, 0, charsRead);
      }
      data = new char[buf.length()];
      buf.getChars(0, data.length, data, 0);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        in.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return data != null ? data : null;
  }
Example #2
0
  public String loadJSONFromFile(
      String buildID,
      int buildNumber,
      String workPlacePath,
      String vInputFile,
      BuildListener listener,
      boolean deleteInputFile)
      throws Exception {
    String output = null;
    StringBuffer listOfNames = new StringBuffer();
    BufferedReader reader = null;
    String fileName = null;
    boolean notInTestMode = true;
    if (listener == null) {
      notInTestMode = false;
    }

    // Set the right File name.
    if ("".equals(vInputFile) || vInputFile == null) {
      fileName = workPlacePath + File.separator + buildNumber + "." + buildID + "." + "vapi.input";
    } else {
      fileName = vInputFile;
    }

    try {

      reader =
          this.loadFileFromWorkSpace(
              buildID,
              buildNumber,
              workPlacePath,
              vInputFile,
              listener,
              deleteInputFile,
              "vapi.input");
      String line = null;
      while ((line = reader.readLine()) != null) {
        listOfNames.append(line);
      }

    } catch (Exception e) {

      if (notInTestMode) {
        listener
            .getLogger()
            .print(
                "Failed to read json input file for the vAPI input.  Failed to load file '"
                    + fileName
                    + "'\n");
      } else {

        System.out.println(
            "Failed to open the read file for the vAPI input.  Failed to load file '"
                + fileName
                + "'");
      }

      throw e;
    } finally {
      reader.close();
    }

    output = listOfNames.toString();

    if (notInTestMode) {
      listener.getLogger().print("Input jSON for vAPI is:\n");
      listener.getLogger().print(output + "\n");
    }

    if (deleteInputFile) {
      if (notInTestMode) {
        listener
            .getLogger()
            .print("Job set to delete the input file.  Deleting " + fileName + "\n");
      }
      try {
        File fileToDelete = new File(fileName);
        fileToDelete.renameTo(new File(fileToDelete + ".delete"));
      } catch (Exception e) {
        if (notInTestMode) {
          listener
              .getLogger()
              .print(
                  "Failed to delete input file from workspace.  Failed to delete file '"
                      + fileName
                      + "'\n");

        } else {

          System.out.println(
              "Failed to delete the input file from the workspace.  Failed to delete file '"
                  + fileName
                  + "'");
        }
        throw e;
      }
    }

    return output;
  }
Example #3
0
  /**
   * @param context if null, use the default format (Mozilla/5.0 (Linux; U; Android %s)
   *     AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 %sSafari/534.30).
   * @return
   */
  public static String getUserAgent(Context context) {
    String webUserAgent = null;
    if (context != null) {
      try {
        Class sysResCls = Class.forName("com.android.internal.R$string");
        Field webUserAgentField = sysResCls.getDeclaredField("web_user_agent");
        Integer resId = (Integer) webUserAgentField.get(null);
        webUserAgent = context.getString(resId);
      } catch (Throwable ignored) {
      }
    }
    if (TextUtils.isEmpty(webUserAgent)) {
      webUserAgent =
          "Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 %sSafari/533.1";
    }

    Locale locale = Locale.getDefault();
    StringBuffer buffer = new StringBuffer();
    // Add version
    final String version = Build.VERSION.RELEASE;
    if (version.length() > 0) {
      buffer.append(version);
    } else {
      // default to "1.0"
      buffer.append("1.0");
    }
    buffer.append("; ");
    final String language = locale.getLanguage();
    if (language != null) {
      buffer.append(language.toLowerCase());
      final String country = locale.getCountry();
      if (country != null) {
        buffer.append("-");
        buffer.append(country.toLowerCase());
      }
    } else {
      // default to "en"
      buffer.append("en");
    }
    // add the model for the release build
    if ("REL".equals(Build.VERSION.CODENAME)) {
      final String model = Build.MODEL;
      if (model.length() > 0) {
        buffer.append("; ");
        buffer.append(model);
      }
    }
    final String id = Build.ID;
    if (id.length() > 0) {
      buffer.append(" Build/");
      buffer.append(id);
    }
    return String.format(webUserAgent, buffer, "Mobile ");
  }
Example #4
0
  public String loadJSONEnvInput(
      String buildID,
      int buildNumber,
      String workPlacePath,
      String envInputFile,
      BuildListener listener)
      throws Exception {
    String output = null;
    StringBuffer listOfEnvs = new StringBuffer();
    BufferedReader reader = null;
    String fileName = null;
    boolean notInTestMode = true;
    if (listener == null) {
      notInTestMode = false;
    }

    // Set the right File name.
    if ("".equals(envInputFile) || envInputFile == null) {
      fileName =
          workPlacePath + File.separator + buildNumber + "." + buildID + "." + "environment.input";
    } else {
      fileName = envInputFile;
    }

    try {

      reader =
          this.loadFileFromWorkSpace(
              buildID,
              buildNumber,
              workPlacePath,
              envInputFile,
              listener,
              false,
              "environment.input");
      String line = null;
      while ((line = reader.readLine()) != null) {
        listOfEnvs.append(line);
      }

      output = listOfEnvs.toString();

      output = "\"environment\":{  " + output + "}";

    } catch (Exception e) {

      if (notInTestMode) {
        listener
            .getLogger()
            .print(
                "Failed to read input file for the environment varibles.  Failed to load file '"
                    + fileName
                    + "'\n");
      } else {

        System.out.println(
            "Failed to open the read file for the environment varibles.  Failed to load file '"
                + fileName
                + "'");
      }

      throw e;
    } finally {
      reader.close();
    }

    return output;
  }