public String getString(String relativePath) throws IOException {
    URI uri = this.baseUri.resolve(relativePath);
    System.out.println("GET (String): " + uri.toASCIIString());

    InputStream in = null;
    InputStreamReader reader = null;
    HttpURLConnection connection = null;

    try {
      connection = (HttpURLConnection) uri.toURL().openConnection();
      connection.connect();
      if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
        String body = getPotentialBody(connection);
        String err =
            String.format(
                "GET request failed (%d %s) %s%n%s",
                connection.getResponseCode(),
                connection.getResponseMessage(),
                uri.toASCIIString(),
                body);
        throw new IOException(err);
      }
      in = connection.getInputStream();
      reader = new InputStreamReader(in);
      StringWriter writer = new StringWriter();
      IO.copy(reader, writer);
      return writer.toString();
    } finally {
      IO.close(reader);
      IO.close(in);
    }
  }
  public Properties getProperties(String relativePath) throws IOException {
    URI uri = this.baseUri.resolve(relativePath);
    System.out.println("GET (Properties): " + uri.toASCIIString());

    InputStream in = null;
    HttpURLConnection connection = null;

    try {
      connection = (HttpURLConnection) uri.toURL().openConnection();
      connection.connect();
      if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
        String body = getPotentialBody(connection);
        String err =
            String.format(
                "GET request failed (%d %s) %s%n%s",
                connection.getResponseCode(),
                connection.getResponseMessage(),
                uri.toASCIIString(),
                body);
        throw new IOException(err);
      }
      in = connection.getInputStream();
      Properties props = new Properties();
      props.load(in);
      return props;
    } finally {
      IO.close(in);
    }
  }
 /**
  * Attempt to obtain the body text if available. Do not throw an exception if body is unable to be
  * fetched.
  *
  * @param connection the connection to fetch the body content from.
  * @return the body content, if present.
  */
 private String getPotentialBody(HttpURLConnection connection) {
   InputStream in = null;
   InputStreamReader reader = null;
   try {
     in = connection.getInputStream();
     reader = new InputStreamReader(in);
     StringWriter writer = new StringWriter();
     IO.copy(reader, writer);
     return writer.toString();
   } catch (IOException e) {
     return "<no body:" + e.getMessage() + ">";
   } finally {
     IO.close(reader);
     IO.close(in);
   }
 }
Example #4
0
  public static void main(String[] args) throws IOException {
    IO io = new IO(System.in);
    int sum;

    while (true) {

      int nV = io.nextInt();
      int nE = io.nextInt();
      if (nV == 0) {
        break;
      }
      V[] vertices = new V[nV];
      E[] edges = new E[nE];

      for (int j = 0; j < nV; j++) {
        vertices[j] = new V(j);
      }

      for (int j = 0; j < nE; j++) {
        int start = io.nextInt();
        int end = io.nextInt();
        double w = io.nextDouble();
        if (end > start) {
          edges[j] = new E(vertices[start], vertices[end], w);
        } else {
          edges[j] = new E(vertices[end], vertices[start], w);
        }
      }

      ArrayList<E> result = compute(nV, edges);
      if (result.size() > 0) {
        sum = 0;
        for (int i = 0; i < result.size(); i++) {
          sum += result.get(i).w;
        }
        io.println(sum);
        String[] resultStrings = new String[result.size()];
        for (int i = 0; i < result.size(); i++) {
          resultStrings[i] = "" + result.get(i).start.name + " " + result.get(i).end.name;
        }
        Arrays.sort(resultStrings);
        for (int i = 0; i < result.size(); i++) {
          io.printf("%s\n", resultStrings[i]);
        }
      } else {
        io.println("Impossible");
      }
    }

    io.close();
  }
Example #5
0
 public static String decode(byte[] bytes) {
   try {
     char[] chars = new char[bytes.length];
     InputStream in = new ByteArrayInputStream(bytes);
     Reader r = new InStreamUTF8(in);
     int len;
     len = r.read(chars);
     IO.close(r);
     return new String(chars, 0, len);
   } catch (IOException ex) {
     IO.exception(ex);
     return null;
   }
 }
Example #6
0
  public void signJar(Jar jar) {
    if (digestNames == null || digestNames.length == 0)
      error("Need at least one digest algorithm name, none are specified");

    if (keystoreFile == null || !keystoreFile.getAbsoluteFile().exists()) {
      error("No such keystore file: " + keystoreFile);
      return;
    }

    if (alias == null) {
      error("Private key alias not set for signing");
      return;
    }

    MessageDigest digestAlgorithms[] = new MessageDigest[digestNames.length];

    getAlgorithms(digestNames, digestAlgorithms);

    try {
      Manifest manifest = jar.getManifest();
      manifest.getMainAttributes().putValue("Signed-By", "Bnd");

      // Create a new manifest that contains the
      // Name parts with the specified digests

      ByteArrayOutputStream o = new ByteArrayOutputStream();
      manifest.write(o);
      doManifest(jar, digestNames, digestAlgorithms, o);
      o.flush();
      byte newManifestBytes[] = o.toByteArray();
      jar.putResource("META-INF/MANIFEST.MF", new EmbeddedResource(newManifestBytes, 0));

      // Use the bytes from the new manifest to create
      // a signature file

      byte[] signatureFileBytes = doSignatureFile(digestNames, digestAlgorithms, newManifestBytes);
      jar.putResource("META-INF/BND.SF", new EmbeddedResource(signatureFileBytes, 0));

      // Now we must create an RSA signature
      // this requires the private key from the keystore

      KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());

      KeyStore.PrivateKeyEntry privateKeyEntry = null;

      java.io.FileInputStream keystoreInputStream = null;
      try {
        keystoreInputStream = new java.io.FileInputStream(keystoreFile);
        char[] pw = password == null ? new char[0] : password.toCharArray();

        keystore.load(keystoreInputStream, pw);
        keystoreInputStream.close();
        privateKeyEntry =
            (PrivateKeyEntry) keystore.getEntry(alias, new KeyStore.PasswordProtection(pw));
      } catch (Exception e) {
        error(
            "No able to load the private key from the give keystore("
                + keystoreFile.getAbsolutePath()
                + ") with alias "
                + alias
                + " : "
                + e);
        return;
      } finally {
        IO.close(keystoreInputStream);
      }
      PrivateKey privateKey = privateKeyEntry.getPrivateKey();

      Signature signature = Signature.getInstance("MD5withRSA");
      signature.initSign(privateKey);

      signature.update(signatureFileBytes);

      signature.sign();

      // TODO, place the SF in a PCKS#7 structure ...
      // no standard class for this? The following
      // is an idea but we will to have do ASN.1 BER
      // encoding ...

      ByteArrayOutputStream tmpStream = new ByteArrayOutputStream();
      jar.putResource("META-INF/BND.RSA", new EmbeddedResource(tmpStream.toByteArray(), 0));
    } catch (Exception e) {
      error("During signing: " + e);
    }
  }
Example #7
0
 @Override
 public void closeStream() {
   IO.close(input);
 }