openvidu-test-browsers: remove Apache HttpClient dependency

pull/789/head
pabloFuente 2023-01-27 16:32:37 +01:00
parent b78b127447
commit d9a5ce30af
3 changed files with 68 additions and 77 deletions

View File

@ -102,11 +102,6 @@
<artifactId>java-client</artifactId> <artifactId>java-client</artifactId>
<version>${version.appium}</version> <version>${version.appium}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>
</dependencies> </dependencies>
<profiles> <profiles>

View File

@ -17,32 +17,23 @@
package io.openvidu.test.browsers.utils; package io.openvidu.test.browsers.utils;
import java.io.IOException; import java.net.Socket;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpResponse;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.Base64; import java.util.Base64;
import java.util.Map.Entry; import java.util.Map.Entry;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509ExtendedTrustManager;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
@ -61,7 +52,7 @@ public class CustomHttpClient {
private String openviduUrl; private String openviduUrl;
private String headerAuth; private String headerAuth;
private CloseableHttpClient client; private HttpClient client;
public CustomHttpClient(String url) throws Exception { public CustomHttpClient(String url) throws Exception {
this(url, null, null); this(url, null, null);
@ -74,23 +65,48 @@ public class CustomHttpClient {
this.headerAuth = "Basic " + Base64.getEncoder().encodeToString((user + ":" + pass).getBytes()); this.headerAuth = "Basic " + Base64.getEncoder().encodeToString((user + ":" + pass).getBytes());
} }
SSLContext sslContext = null; SSLContext sslContext;
try { try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() { sslContext = SSLContext.getInstance("TLS");
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { sslContext.init(null, new TrustManager[] { new X509ExtendedTrustManager() {
return true; public X509Certificate[] getAcceptedIssuers() {
return null;
} }
}).build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { public void checkClientTrusted(final X509Certificate[] a_certificates, final String a_auth_type) {
throw new Exception("Error building custom HttpClient: " + e.getMessage()); }
public void checkServerTrusted(final X509Certificate[] a_certificates, final String a_auth_type) {
}
public void checkClientTrusted(final X509Certificate[] a_certificates, final String a_auth_type,
final Socket a_socket) {
}
public void checkServerTrusted(final X509Certificate[] a_certificates, final String a_auth_type,
final Socket a_socket) {
}
public void checkClientTrusted(final X509Certificate[] a_certificates, final String a_auth_type,
final SSLEngine a_engine) {
}
public void checkServerTrusted(final X509Certificate[] a_certificates, final String a_auth_type,
final SSLEngine a_engine) {
}
} }, null);
} catch (KeyManagementException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} }
client = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier())
.build(); this.client = HttpClient.newBuilder().sslContext(sslContext).build();
} }
public int getAndReturnStatus(String path, String credentials) throws Exception { public int getAndReturnStatus(String path, String credentials) throws Exception {
path = openviduUrl + (path.startsWith("/") ? path : ("/" + path)); path = openviduUrl + (path.startsWith("/") ? path : ("/" + path));
return client.execute(new HttpGet(path)).getStatusLine().getStatusCode(); return client
.send(HttpRequest.newBuilder().GET().uri(new URI(path)).build(), HttpResponse.BodyHandlers.ofString())
.statusCode();
} }
public JsonObject rest(HttpMethod method, String path, int status) throws Exception { public JsonObject rest(HttpMethod method, String path, int status) throws Exception {
@ -245,10 +261,6 @@ public class CustomHttpClient {
} }
} }
public void shutdown() throws IOException {
this.client.close();
}
private JsonObject commonRest(HttpMethod method, String path, String body, int status) throws Exception { private JsonObject commonRest(HttpMethod method, String path, String body, int status) throws Exception {
String stringResponse = this.commonRestString(method, path, body, status); String stringResponse = this.commonRestString(method, path, body, status);
if (stringResponse == null) { if (stringResponse == null) {
@ -272,76 +284,68 @@ public class CustomHttpClient {
} }
private String commonRestString(HttpMethod method, String path, String body, int status) throws Exception { private String commonRestString(HttpMethod method, String path, String body, int status) throws Exception {
HttpResponse jsonResponse = null;
path = openviduUrl + (path.startsWith("/") ? path : ("/" + path)); path = openviduUrl + (path.startsWith("/") ? path : ("/" + path));
HttpRequestBase request = null; HttpRequest.Builder builder = HttpRequest.newBuilder().uri(new URI(path));
if (body != null && !body.isEmpty()) { if (body != null && !body.isEmpty()) {
HttpEntityEnclosingRequestBase requestWithBody = (HttpEntityEnclosingRequestBase) request; body = body.replaceAll("'", "\"");
BodyPublisher bodyPublisher = HttpRequest.BodyPublishers.ofString(body);
switch (method) { switch (method) {
case POST: case POST:
requestWithBody = new HttpPost(path); builder = builder.POST(bodyPublisher);
break; break;
case PUT: case PUT:
requestWithBody = new HttpPut(path); builder = builder.PUT(bodyPublisher);
break; break;
case PATCH: case PATCH:
default: default:
requestWithBody = new HttpPatch(path); builder = builder.method("PATCH", bodyPublisher);
break; break;
} }
requestWithBody.addHeader("Content-Type", "application/json"); builder.setHeader("Content-Type", "application/json");
body = body.replaceAll("'", "\"");
requestWithBody.setEntity(new StringEntity(body));
request = requestWithBody;
} else { } else {
switch (method) { switch (method) {
case GET: case GET:
request = new HttpGet(path); builder = builder.GET();
request.addHeader("Content-Type", "application/x-www-form-urlencoded"); builder.setHeader("Content-Type", "application/x-www-form-urlencoded");
break; break;
case POST: case POST:
request = new HttpPost(path); builder = builder.POST(HttpRequest.BodyPublishers.noBody());
break; break;
case DELETE: case DELETE:
request = new HttpDelete(path); builder = builder.DELETE();
request.addHeader("Content-Type", "application/x-www-form-urlencoded"); builder.setHeader("Content-Type", "application/x-www-form-urlencoded");
break; break;
case PUT: case PUT:
request = new HttpPut(path); builder = builder.PUT(HttpRequest.BodyPublishers.noBody());
default: default:
break; break;
} }
} }
if (this.headerAuth != null) { if (this.headerAuth != null) {
request.addHeader("Authorization", this.headerAuth); builder.setHeader("Authorization", this.headerAuth);
} }
HttpResponse<String> response;
try { try {
jsonResponse = client.execute(request); response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString());
} catch (Exception e) { } catch (Exception e) {
throw new Exception("Error sending request to " + path + ": " + e.getMessage()); throw new Exception("Error sending request to " + path + ": " + e.getMessage());
} }
String stringResponse = null;
if (jsonResponse.getEntity() != null) { if (response.statusCode() == 500) {
stringResponse = EntityUtils.toString(jsonResponse.getEntity(), "UTF-8"); log.error("Internal Server Error: {}", response.body());
} }
if (jsonResponse.getStatusLine().getStatusCode() == 500) { if (status != response.statusCode()) {
log.error("Internal Server Error: {}", EntityUtils.toString(jsonResponse.getEntity(), "UTF-8"));
}
if (status != jsonResponse.getStatusLine().getStatusCode()) {
try { try {
String responseString = EntityUtils.toString(jsonResponse.getEntity(), "UTF-8"); System.err.println(response.body());
System.err.println(responseString);
} catch (Exception e) { } catch (Exception e) {
} }
throw new Exception(path + " expected to return status " + status + " but got " throw new Exception(path + " expected to return status " + status + " but got " + response.statusCode());
+ jsonResponse.getStatusLine().getStatusCode());
} }
return stringResponse; return response.body();
} }
} }

View File

@ -823,14 +823,6 @@ public class OpenViduTestE2e {
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage()); log.error(e.getMessage());
Assertions.fail("Error restarting OpenVidu Server"); Assertions.fail("Error restarting OpenVidu Server");
} finally {
if (restClient != null) {
try {
restClient.shutdown();
} catch (IOException e) {
log.error("Error shutting down HTTP client: {}", e.getMessage());
}
}
} }
return null; return null;
} }