-
Notifications
You must be signed in to change notification settings - Fork 161
/
DashboardRendererServiceImpl.java
68 lines (58 loc) · 2.51 KB
/
DashboardRendererServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package org.jbpm.dashboard.renderer.service.impl;
import org.apache.commons.lang.StringUtils;
import org.jboss.errai.bus.server.annotations.Service;
import org.jbpm.dashboard.renderer.service.DashboardRendererService;
import org.jbpm.dashboard.renderer.service.ConnectionStatus;
import javax.enterprise.context.ApplicationScoped;
import java.net.HttpURLConnection;
import java.net.URL;
@Service
@ApplicationScoped
public class DashboardRendererServiceImpl implements DashboardRendererService {
/**
* Maximum amount of time (in milliseconds) a ping alive request to the dashbuilder remote URL may last.
* <p>The system property <i>dashbuilder.bind.timeout</i> can be used to set a custom value.</p>
*/
public static int PING_TIMEOUT = 1000;
@Override
public ConnectionStatus getAppStatus(String theUrl) {
ConnectionStatus connectionStatus = new ConnectionStatus();
try {
// Check whether the service is available
String targetUrl = resolveUrl(theUrl);
int status = pingUrl(targetUrl);
connectionStatus.setStatus(status);
} catch (Exception e) {
e.printStackTrace();
connectionStatus.setStatus(-1);
}
return connectionStatus;
}
protected int pingUrl(String anUrl) throws Exception {
int timeout = PING_TIMEOUT;
String bindTimeout = System.getProperty("dashbuilder.bind.timeout");
if (!StringUtils.isBlank(bindTimeout)) {
timeout = Integer.parseInt(bindTimeout);
}
URL url = new URL(anUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(timeout);
return urlConnection.getResponseCode();
}
protected String resolveUrl(String anUrl) throws Exception {
// Get the bind address/port.
String bindAddress = System.getProperty("dashbuilder.bind.address");
String bindPort = System.getProperty("dashbuilder.bind.port");
URL url = new URL(anUrl);
String host = url.getHost();
int port = url.getPort();
String targetUrl = anUrl;
if (!StringUtils.isBlank(bindAddress) && !host.equals(bindAddress)) {
targetUrl = targetUrl.replace(host, bindAddress);
}
if (!StringUtils.isBlank(bindPort) && port != Integer.parseInt(bindPort)) {
targetUrl = targetUrl.replace(Integer.toString(port), bindPort);
}
return !anUrl.equals(targetUrl) ? targetUrl :anUrl;
}
}