-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathGridInfoExtractor.java
More file actions
59 lines (51 loc) · 2.14 KB
/
GridInfoExtractor.java
File metadata and controls
59 lines (51 loc) · 2.14 KB
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
package com.seleniumgrid2api.api;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.openqa.selenium.remote.SessionId;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public final class GridInfoExtractor {
private GridInfoExtractor() {
}
public static GridInfo getHostNameAndPort(String hubHost, int hubPort, SessionId session) {
GridInfo retVal = null;
try {
HttpHost host = new HttpHost(hubHost, hubPort);
DefaultHttpClient client = new DefaultHttpClient();
URL sessionURL = new URL("http://" + hubHost + ":" + hubPort + "/grid/api/testsession?session=" + session);
BasicHttpEntityEnclosingRequest basicHttpEntityEnclosingRequest = new BasicHttpEntityEnclosingRequest("POST", sessionURL.toExternalForm());
HttpResponse response = client.execute(host, basicHttpEntityEnclosingRequest);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
JsonObject object = extractObject(response);
retVal = new GridInfo(object);
} else {
System.out.println("Problem connecting to Grid Server");
}
} catch (IOException e) {
throw new RuntimeException("Failed to acquire remote webdriver node and port info", e);
}
return retVal;
}
private static JsonObject extractObject(HttpResponse resp) throws IOException {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()))) {
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
bufferedReader.close();
JsonParser parser = new JsonParser();
return (JsonObject) parser.parse(stringBuilder.toString());
} catch (Exception e) {
System.out.println("error" + e.toString());
}
return null;
}
}