List of usage examples for org.apache.http.conn HttpRoutedConnection isSecure
boolean isSecure();
From source file:com.msopentech.thali.utilities.universal.ThaliClientToDeviceHubUtilities.java
/** * This is a horrible hack used by clients to get the server key for the local Thali Device Hub. Eventually we'll * introduce something actually reasonably secure for this purposes. * @param httpClient//from w w w . j av a 2 s . co m * @return * @throws java.io.IOException * @throws UnrecoverableKeyException * @throws NoSuchAlgorithmException * @throws KeyStoreException * @throws KeyManagementException */ public static PublicKey getServersRootPublicKey(org.apache.http.client.HttpClient httpClient) throws IOException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { // Taken from http://stackoverflow.com/questions/13273305/apache-httpclient-get-server-certificate // And yes we should do this with a request interceptor since it would work in all cases where we get a SSL // connection even if the HTTP request fails and I'm too lazy to rewrite it. ((AbstractHttpClient) httpClient).addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(org.apache.http.HttpResponse response, HttpContext context) throws HttpException, IOException { Object unTypedHttpConnection = context.getAttribute(ExecutionContext.HTTP_CONNECTION); // Android doesn't return an object that supports the HttpRoutedConnection interface which makes no sense! // What it does return is BasicPooledConnAdapter but that is supposed to support HttpRoutedConnection! But doesn't. if (unTypedHttpConnection instanceof BasicPooledConnAdapter) { BasicPooledConnAdapter basicPooledConnAdapter = (BasicPooledConnAdapter) unTypedHttpConnection; if (basicPooledConnAdapter.isSecure()) { java.security.cert.Certificate[] certificates = basicPooledConnAdapter.getSSLSession() .getPeerCertificates(); context.setAttribute(PEER_CERT_ATTRIBUTE, certificates); } return; } if (unTypedHttpConnection instanceof HttpRoutedConnection) { HttpRoutedConnection httpRoutedConnection = (HttpRoutedConnection) unTypedHttpConnection; if (httpRoutedConnection.isSecure()) { java.security.cert.Certificate[] certificates = httpRoutedConnection.getSSLSession() .getPeerCertificates(); context.setAttribute(PEER_CERT_ATTRIBUTE, certificates); } return; } throw new RuntimeException("Unexpected HTTP_CONNECTION: " + unTypedHttpConnection.toString()); } }); HttpContext httpContext = new BasicHttpContext(); HttpUriRequest httpUriRequest = new HttpGet("/"); org.apache.http.HttpResponse apacheHttpResponse = httpClient.execute(httpUriRequest, httpContext); java.security.cert.Certificate[] certificates = (java.security.cert.Certificate[]) httpContext .getAttribute(PEER_CERT_ATTRIBUTE); // TODO: Where is it written that the last cert is the server's root cert? Are certs guaranteed to be returned in order from leaf to root? return certificates[certificates.length - 1].getPublicKey(); }