public static VisitReport contactEndpoint()

in taverna-workflowmodel-api/src/main/java/org/apache/taverna/workflowmodel/health/RemoteHealthChecker.java [105:225]


	public static VisitReport contactEndpoint(Activity<?> activity,
			String endpoint) {
		WeakReference<VisitReport> cachedReportRef = visitReportsByEndpoint
				.get(endpoint);
		VisitReport cachedReport = null;
		if (cachedReportRef != null)
			cachedReport = cachedReportRef.get();
		if (cachedReport != null) {
			long now = currentTimeMillis();
			long age = now - cachedReport.getCheckTime();
			if (age < getEndpointExpiryInMilliseconds()) {
				VisitReport newReport;
				try {
					// Make a copy
					newReport = cachedReport.clone();
					// But changed the subject
					newReport.setSubject(activity);
					logger.info("Returning cached report for endpoint "
							+ endpoint + ": " + newReport);
					return newReport;
				} catch (CloneNotSupportedException e) {
					logger.warn("Could not clone VisitReport " + cachedReport,
							e);
				}
			}
		}
		
		Status status = Status.OK;
		String message = "Responded OK";
		int resultId = NO_PROBLEM;
		URLConnection connection = null;
		int responseCode = HTTP_OK;
		Exception ex = null;
		try {
			URL url = new URL(endpoint);
			connection = url.openConnection();
			connection.setReadTimeout(timeout);
			connection.setConnectTimeout(timeout);
			if (connection instanceof HttpURLConnection) {
				HttpURLConnection httpConnection = (HttpURLConnection) connection;
				httpConnection.setRequestMethod("HEAD");
				httpConnection.connect();
				responseCode = httpConnection.getResponseCode();
				if (responseCode != HTTP_OK) {
					try {
						if ((connection != null)
								&& (connection.getInputStream() != null))
							connection.getInputStream().close();
					} catch (IOException e) {
						logger.info(
								"Unable to close connection to " + endpoint, e);
					}
					connection = url.openConnection();
					connection.setReadTimeout(timeout);
					connection.setConnectTimeout(timeout);
					httpConnection = (HttpURLConnection) connection;
					httpConnection.setRequestMethod("GET");
					httpConnection.connect();
					responseCode = httpConnection.getResponseCode();
				}
				if (responseCode != HTTP_OK) {
					if ((responseCode > HTTP_INTERNAL_ERROR)) {
						status = WARNING;
						message = "Unexpected response";
						resultId = CONNECTION_PROBLEM;
					} else if ((responseCode == HTTP_NOT_FOUND)
							|| (responseCode == HTTP_GONE)) {
						status = WARNING;
						message = "Bad response";
						resultId = CONNECTION_PROBLEM;
					}
				}
			} else {
			    connection.connect();
				status = WARNING;
				message = "Not HTTP";
				resultId = NOT_HTTP;
			}
		} catch (MalformedURLException e) {
			status = SEVERE;
			message = "Invalid URL";
			resultId = INVALID_URL;
			ex = e;
		} catch (SocketTimeoutException e) {
			status = SEVERE;
			message = "Timed out";
			resultId = TIME_OUT;
			ex = e;
		} catch (SSLException e){
			// Some kind of error when trying to establish an HTTPS connection to the endpoint
			status = SEVERE;
			message = "HTTPS connection problem";
			resultId = IO_PROBLEM; // SSLException is an IOException
			ex = e;
		} catch (IOException e) {
			status = SEVERE;
			message = "Read problem";
			resultId = IO_PROBLEM;
			ex = e;
		} finally {
			try {
				if ((connection != null)
						&& (connection.getInputStream() != null))
					connection.getInputStream().close();
			} catch (IOException e) {
				logger.info("Unable to close connection to " + endpoint, e);
			}
		}
		
		VisitReport vr = new VisitReport(HealthCheck.getInstance(), activity, message,
				resultId, status);
		vr.setProperty("endpoint", endpoint);
		if (ex != null)
		    vr.setProperty("exception", ex);
		if (responseCode != HTTP_OK)
			vr.setProperty("responseCode", Integer.toString(responseCode));
		if (resultId == TIME_OUT)
			vr.setProperty("timeOut", Integer.toString(timeout));
		visitReportsByEndpoint.put(endpoint, new WeakReference<>(vr));
		return vr;
	}