curator-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Randgalt <...@git.apache.org>
Subject [GitHub] curator pull request: initial BasicAuthExhibitorRestClient
Date Sun, 08 Jun 2014 04:51:08 GMT
Github user Randgalt commented on a diff in the pull request:

    https://github.com/apache/curator/pull/10#discussion_r13522676
  
    --- Diff: curator-client/src/main/java/org/apache/curator/ensemble/exhibitor/BasicAuthExhibitorRestClient.java
---
    @@ -0,0 +1,72 @@
    +package org.apache.curator.ensemble.exhibitor;
    +
    +import org.apache.curator.utils.CloseableUtils;
    +import sun.misc.BASE64Encoder;
    +
    +import javax.net.ssl.*;
    +import java.io.BufferedInputStream;
    +import java.io.InputStream;
    +import java.net.HttpURLConnection;
    +import java.net.URI;
    +import java.security.cert.CertificateException;
    +import java.security.cert.X509Certificate;
    +
    +public class BasicAuthExhibitorRestClient  implements ExhibitorRestClient
    +{
    +    private final boolean useSsl;
    +    private final boolean validateSsl;
    +    private final String userInfo;
    +
    +    public BasicAuthExhibitorRestClient(boolean useSsl, boolean validateSsl, String userInfo)
    +    {
    +        this.useSsl = useSsl;
    +        this.validateSsl = validateSsl;
    +        this.userInfo = userInfo;
    +    }
    +
    +    @Override
    +    public String getRaw(String hostname, int port, String uriPath, String mimeType)
throws Exception
    +    {
    +        URI uri = new URI(useSsl ? "https" : "http", null, hostname, port, uriPath, null,
null);
    +        HttpURLConnection connection = (HttpURLConnection)uri.toURL().openConnection();
    +        if (useSsl && !validateSsl) {
    +            X509TrustManager trustAllCert = new X509TrustManager() {
    +                public void checkClientTrusted(X509Certificate[] x509Certificates, String
s) throws CertificateException {}
    +
    +                public void checkServerTrusted(X509Certificate[] x509Certificates, String
s) throws CertificateException {}
    +
    +                public X509Certificate[] getAcceptedIssuers() { return null; }
    +            };
    +            SSLContext sc = SSLContext.getInstance("SSL");
    +            sc.init(null, new TrustManager[]{trustAllCert}, new java.security.SecureRandom());
    +            ((HttpsURLConnection)connection).setSSLSocketFactory(sc.getSocketFactory());
    +            ((HttpsURLConnection)connection).setHostnameVerifier(new HostnameVerifier(){
    +                public boolean verify(String host,  SSLSession session){
    +                    return true;
    +                }
    +            });
    +        }
    +        connection.addRequestProperty("Accept", mimeType);
    +        connection.addRequestProperty("Authorization", "Basic " + new BASE64Encoder().encode(userInfo.getBytes()));
    +
    +        StringBuilder       str = new StringBuilder();
    --- End diff --
    
    You can use Guava's CharStreams here. It would be cleaner:
    
    ```java
    Reader in = new InputStreamReader(new BufferedInputStream(connection.getInputStream()));
    try
    {
        return CharStreams.toString(in);
    }
    finally
    {
        CloseableUtils.closeQuietly(in);
    }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message