public static PublicKey getPublicKey(String publicKeyFilepath,
      String algorithm) throws SamlException {
    try {
      InputStream pubKey = null;
      try {
        URL url = new URL(publicKeyFilepath);
        pubKey = url.openStream();
      } catch (MalformedURLException e) {
        pubKey = new FileInputStream(publicKeyFilepath);
      }
      byte[] bytes = new byte[pubKey.available()];
      pubKey.read(bytes);
      pubKey.close();
      X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(bytes);
      KeyFactory factory = KeyFactory.getInstance(algorithm);
      return factory.generatePublic(pubSpec);
    } catch (FileNotFoundException e) {
      throw new SamlException("ERROR: Public key file not found - "
        + publicKeyFilepath);
    } catch (IOException e) {
      throw new SamlException("ERROR: Invalid public key file - "
        + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
      throw new SamlException("ERROR: Invalid public key algorithm - "
        + e.getMessage());
    } catch (InvalidKeySpecException e) {
      throw new SamlException("ERROR: Invalid public key spec - "
        + e.getMessage());
    }
  }