/**
   * Creates a String representation of the given object.
   *
   * @param certificate to print
   * @return the String representation
   */
  private String toString(Object certificate) {
    final StringBuilder sb = new StringBuilder();
    sb.append("<html><body>\n");

    if (certificate instanceof X509Certificate) {
      renderX509(sb, (X509Certificate) certificate);
    } else {
      sb.append("<pre>\n");
      sb.append(certificate.toString());
      sb.append("</pre>\n");
    }

    sb.append("</body></html>");
    return sb.toString();
  }
Пример #2
0
 void handleRequest(InputStream in, OutputStream out) throws IOException {
   boolean newline = false;
   StringBuilder sb = new StringBuilder();
   while (true) {
     int ch = in.read();
     if (ch < 0) {
       throw new EOFException();
     }
     sb.append((char) ch);
     if (ch == '\r') {
       // empty
     } else if (ch == '\n') {
       if (newline) {
         // 2nd newline in a row, end of request
         break;
       }
       newline = true;
     } else {
       newline = false;
     }
   }
   String request = sb.toString();
   if (request.startsWith("GET / HTTP/1.") == false) {
     throw new IOException("Invalid request: " + request);
   }
   out.write("HTTP/1.0 200 OK\r\n\r\n".getBytes());
 }
  /**
   * Converts the byte array to hex string.
   *
   * @param raw the data.
   * @return the hex string.
   */
  private String getHex(byte[] raw) {
    if (raw == null) return null;

    StringBuilder hex = new StringBuilder(2 * raw.length);
    Formatter f = new Formatter(hex);
    try {
      for (byte b : raw) f.format("%02x", b);
    } finally {
      f.close();
    }
    return hex.toString();
  }
 public String getClasspath() {
   final StringBuilder sb = new StringBuilder();
   boolean firstPass = true;
   final Enumeration<File> componentEnum = this.pathComponents.elements();
   while (componentEnum.hasMoreElements()) {
     if (!firstPass) {
       sb.append(System.getProperty("path.separator"));
     } else {
       firstPass = false;
     }
     sb.append(componentEnum.nextElement().getAbsolutePath());
   }
   return sb.toString();
 }
Пример #5
0
 void sendRequest(InputStream in, OutputStream out) throws IOException {
   out.write("GET / HTTP/1.0\r\n\r\n".getBytes());
   out.flush();
   StringBuilder sb = new StringBuilder();
   while (true) {
     int ch = in.read();
     if (ch < 0) {
       break;
     }
     sb.append((char) ch);
   }
   String response = sb.toString();
   if (response.startsWith("HTTP/1.0 200 ") == false) {
     throw new IOException("Invalid response: " + response);
   }
 }
Пример #6
0
  private static String readFile(String fileName) throws IOException {
    String filePath = testDir + "/" + fileName;
    StringBuilder sb = new StringBuilder();

    try (FileReader fr = new FileReader(filePath);
        BufferedReader br = new BufferedReader(fr)) {
      String line;
      while ((line = br.readLine()) != null) {
        if (!line.trim().startsWith("#")) {
          sb.append(line).append("\n");
        }
      }
    }

    System.out.println("Successfully read " + fileName);
    return sb.toString();
  }
 /**
  * Calculates the hash of the certificate known as the "thumbprint" and returns it as a string
  * representation.
  *
  * @param cert The certificate to hash.
  * @param algorithm The hash algorithm to use.
  * @return The SHA-1 hash of the certificate.
  * @throws CertificateException
  */
 private static String getThumbprint(X509Certificate cert, String algorithm)
     throws CertificateException {
   MessageDigest digest;
   try {
     digest = MessageDigest.getInstance(algorithm);
   } catch (NoSuchAlgorithmException e) {
     throw new CertificateException(e);
   }
   byte[] encodedCert = cert.getEncoded();
   StringBuilder sb = new StringBuilder(encodedCert.length * 2);
   Formatter f = new Formatter(sb);
   try {
     for (byte b : digest.digest(encodedCert)) f.format("%02x", b);
   } finally {
     f.close();
   }
   return sb.toString();
 }