List of usage examples for javax.net.ssl HttpsURLConnection getConnectTimeout
public int getConnectTimeout()
From source file:org.comixwall.pffw.GraphsBase.java
/** * Run the controller task./* w ww . j a v a 2 s. c o m*/ * We fetch the graphs using secure http, or fall back to plain http if secure connection fails. * <p> * Note that the PFFW uses a self-signed server certificate. So the code should trust that certificate * and not reject the hostname. * * @return True on success, false on failure. */ @Override public boolean executeTask() { Boolean retval = true; try { String output = controller.execute("symon", "RenderLayout", mLayout, mGraphWidth, mGraphHeight); JSONArray jsonArray = new JSONArray(output); mGraphsJsonObject = new JSONObject(jsonArray.get(0).toString()); Iterator<String> it = mGraphsJsonObject.keys(); while (it.hasNext()) { String title = it.next(); String file = mGraphsJsonObject.getString(title); try { InputStream stream = null; try { String outputGraph = controller.execute("symon", "GetGraph", file); String base64Graph = new JSONArray(outputGraph).get(0).toString(); stream = new ByteArrayInputStream(Base64.decode(base64Graph, Base64.DEFAULT)); } catch (Exception e) { e.printStackTrace(); logger.warning("SSH graph connection exception: " + e.toString()); } // Try secure http if ssh fails if (stream == null) { // 1540861800_404e00f4044d07242a77f802e457f774 String hash = file.substring(file.indexOf('_') + 1); try { // Using https here gives: CertPathValidatorException: Trust anchor for certification path not found. // So we should trust the PFFW server crt and hostname URL secureUrl = new URL("https://" + controller.getHost() + "/symon/graph.php?" + hash); HttpsURLConnection secureUrlConn = (HttpsURLConnection) secureUrl.openConnection(); // Tell the URLConnection to use a SocketFactory from our SSLContext secureUrlConn.setSSLSocketFactory(sslContext.getSocketFactory()); // Install the PFFW host verifier secureUrlConn.setHostnameVerifier(hostnameVerifier); logger.finest("Using secure http: " + secureUrl.toString()); // ATTENTION: Setting a timeout value enables SocketTimeoutException, set both timeouts secureUrlConn.setConnectTimeout(5000); secureUrlConn.setReadTimeout(5000); logger.finest("Secure URL connection timeout values: " + secureUrlConn.getConnectTimeout() + ", " + secureUrlConn.getReadTimeout()); stream = secureUrlConn.getInputStream(); } catch (Exception e) { e.printStackTrace(); logger.warning("Secure URL connection exception: " + e.toString()); } // Try plain http if secure http fails if (stream == null) { // ATTENTION: Don't use try-catch here, catch in the outer exception handling URL plainUrl = new URL("http://" + controller.getHost() + "/symon/graph.php?" + hash); HttpURLConnection plainUrlConn = (HttpURLConnection) plainUrl.openConnection(); logger.finest("Using plain http: " + plainUrlConn.toString()); // ATTENTION: Setting a timeout value enables SocketTimeoutException, set both timeouts plainUrlConn.setConnectTimeout(5000); plainUrlConn.setReadTimeout(5000); logger.finest("Plain URL connection timeout values: " + plainUrlConn.getConnectTimeout() + ", " + plainUrlConn.getReadTimeout()); stream = plainUrlConn.getInputStream(); } } Bitmap bmp = BitmapFactory.decodeStream(stream); setBitmap(title, bmp); } catch (Exception e) { // We are especially interested in SocketTimeoutException, but catch all e.printStackTrace(); logger.info("GraphsBase doInBackground exception: " + e.toString()); // We should break out of while loop on exception, because all conn attempts have failed break; } } output = controller.execute("pf", "GetReloadRate"); int timeout = Integer.parseInt(new JSONArray(output).get(0).toString()); mRefreshTimeout = timeout < 10 ? 10 : timeout; } catch (Exception e) { e.printStackTrace(); logger.warning("doInBackground exception: " + e.toString()); retval = false; } return retval; }