2 * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.akraino.validation.ui.client.nexus;
18 import java.io.IOException;
19 import java.net.HttpURLConnection;
20 import java.net.InetSocketAddress;
21 import java.net.Proxy;
23 import java.security.KeyManagementException;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.cert.X509Certificate;
26 import java.util.ArrayList;
27 import java.util.List;
30 import javax.net.ssl.HostnameVerifier;
31 import javax.net.ssl.HttpsURLConnection;
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.SSLSession;
34 import javax.net.ssl.TrustManager;
35 import javax.net.ssl.X509TrustManager;
37 import org.akraino.validation.ui.client.nexus.resources.RobotTestResult;
38 import org.apache.commons.httpclient.HttpException;
39 import org.apache.log4j.Logger;
40 import org.json.JSONObject;
42 import org.jsoup.Jsoup;
43 import org.jsoup.nodes.Document;
44 import org.jsoup.nodes.Element;
46 import com.fasterxml.jackson.core.JsonParseException;
47 import com.fasterxml.jackson.databind.DeserializationFeature;
48 import com.fasterxml.jackson.databind.JsonMappingException;
49 import com.fasterxml.jackson.databind.ObjectMapper;
50 import com.sun.jersey.api.client.Client;
51 import com.sun.jersey.api.client.ClientHandlerException;
52 import com.sun.jersey.api.client.ClientResponse;
53 import com.sun.jersey.api.client.UniformInterfaceException;
54 import com.sun.jersey.api.client.WebResource;
55 import com.sun.jersey.api.client.config.ClientConfig;
56 import com.sun.jersey.api.client.config.DefaultClientConfig;
57 import com.sun.jersey.api.json.JSONConfiguration;
58 import com.sun.jersey.client.urlconnection.HTTPSProperties;
59 import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory;
60 import com.sun.jersey.client.urlconnection.URLConnectionClientHandler;
62 public final class NexusExecutorClient {
64 private static final Logger LOGGER = Logger.getLogger(NexusExecutorClient.class);
66 private final Client client;
67 private final String baseurl;
68 private final HostnameVerifier hostnameVerifier;
69 private final TrustManager[] trustAll;
71 public NexusExecutorClient(String newBaseurl) {
72 this.baseurl = newBaseurl;
73 ClientConfig clientConfig = new DefaultClientConfig();
74 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
75 client = new Client(new URLConnectionClientHandler(new HttpURLConnectionFactory() {
79 public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
81 String proxyIp = System.getenv("proxy_ip");
82 String proxyPort = System.getenv("proxy_port");
83 proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, Integer.parseInt(proxyPort)));
84 return (HttpURLConnection) url.openConnection(proxy);
85 } catch (NumberFormatException ex) {
86 return (HttpURLConnection) url.openConnection();
90 // Create all-trusting host name verifier
91 hostnameVerifier = new HostnameVerifier() {
93 public boolean verify(String hostname, SSLSession session) {
97 // Create a trust manager that does not validate certificate chains
98 trustAll = new TrustManager[] {new X509TrustManager() {
100 public X509Certificate[] getAcceptedIssuers() {
101 return null; // Not relevant.
105 public void checkClientTrusted(X509Certificate[] certs, String authType) {
106 // Do nothing. Just allow them all.
110 public void checkServerTrusted(X509Certificate[] certs, String authType) {
111 // Do nothing. Just allow them all.
116 public String getBaseUrl() {
120 public List<RobotTestResult> getRobotTestResults() throws ClientHandlerException, UniformInterfaceException,
121 JsonParseException, JsonMappingException, IOException, KeyManagementException, NoSuchAlgorithmException {
122 List<RobotTestResult> robotTestResults = new ArrayList<RobotTestResult>();
123 LOGGER.info("Trying to get Robot Test Results");
125 WebResource webResource = this.client.resource(this.baseurl + "/");
126 LOGGER.debug("Request URI of get: " + webResource.getURI().toString());
127 ClientResponse response = webResource.get(ClientResponse.class);
128 if (response.getStatus() != 200) {
129 throw new HttpException("Could not retrieve robot test results from Nexus. HTTP error code : "
130 + response.getStatus() + " and message: " + response.getEntity(String.class));
132 Document document = Jsoup.parse(response.getEntity(String.class));
133 List<Element> elements =
134 document.getElementsByTag("body").get(0).getElementsByTag("table").get(0).getElementsByTag("tr");
135 for (int i = 2; i < elements.size(); i++) {
136 String resultName = elements.get(i).getElementsByTag("td").get(0).getElementsByTag("a").get(0).text();
137 resultName = resultName.substring(0, resultName.length() - 1);
138 webResource = this.client.resource(this.baseurl + "/" + resultName + "/output.xml");
139 LOGGER.debug("Request URI of get: " + webResource.getURI().toString());
140 response = webResource.get(ClientResponse.class);
141 if (response.getStatus() != 200) {
142 throw new HttpException("Could not retrieve robot test result from Nexus. HTTP error code : "
143 + response.getStatus() + " and message: " + response.getEntity(String.class));
145 String result = response.getEntity(String.class);
146 JSONObject xmlJSONObj = XML.toJSONObject(result);
147 ObjectMapper mapper = new ObjectMapper();
148 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
149 RobotTestResult robotTestResult = mapper.readValue(xmlJSONObj.toString(), RobotTestResult.class);
150 robotTestResult.setName(resultName);
151 robotTestResults.add(robotTestResult);
153 return robotTestResults;
156 private void setProperties() throws NoSuchAlgorithmException, KeyManagementException {
157 SSLContext sslContext = SSLContext.getInstance("SSL");
158 sslContext.init(null, this.trustAll, new java.security.SecureRandom());
159 HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
160 // Install the all-trusting host verifier
161 HttpsURLConnection.setDefaultHostnameVerifier(this.hostnameVerifier);
162 DefaultClientConfig config = new DefaultClientConfig();
163 Map<String, Object> properties = config.getProperties();
164 HTTPSProperties httpsProperties = new HTTPSProperties((str, sslSession) -> true, sslContext);
165 properties.put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, httpsProperties);