Delete outdated unirest dependency (use Apache Http Client)

pull/772/head
pabloFuente 2022-11-29 23:56:41 +01:00
parent 9522e1ff63
commit 9ee5a75e74
7 changed files with 61 additions and 62 deletions

View File

@ -82,11 +82,6 @@
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>${version.gson}</version> <version>${version.gson}</version>
</dependency> </dependency>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>${version.unirest}</version>
</dependency>
<dependency> <dependency>
<groupId>org.jcodec</groupId> <groupId>org.jcodec</groupId>
<artifactId>jcodec-javase</artifactId> <artifactId>jcodec-javase</artifactId>

View File

@ -28,27 +28,32 @@ import java.util.Map.Entry;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import org.apache.http.client.HttpClient; 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.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy; 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.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder; 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 com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import com.mashape.unirest.http.HttpMethod;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.HttpRequest;
import com.mashape.unirest.request.HttpRequestWithBody;
public class CustomHttpClient { public class CustomHttpClient {
@ -56,6 +61,7 @@ public class CustomHttpClient {
private String openviduUrl; private String openviduUrl;
private String headerAuth; private String headerAuth;
private CloseableHttpClient client;
public CustomHttpClient(String url, String user, String pass) throws Exception { public CustomHttpClient(String url, String user, String pass) throws Exception {
this.openviduUrl = url.replaceFirst("/*$", ""); this.openviduUrl = url.replaceFirst("/*$", "");
@ -71,14 +77,13 @@ public class CustomHttpClient {
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
throw new Exception("Error building custom HttpClient: " + e.getMessage()); throw new Exception("Error building custom HttpClient: " + e.getMessage());
} }
HttpClient unsafeHttpClient = HttpClients.custom().setSSLContext(sslContext) client = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier())
.setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); .build();
Unirest.setHttpClient(unsafeHttpClient);
} }
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 Unirest.get(path).header("Authorization", credentials).asJson().getStatus(); return client.execute(new HttpGet(path)).getStatusLine().getStatusCode();
} }
public JsonObject rest(HttpMethod method, String path, int status) throws Exception { public JsonObject rest(HttpMethod method, String path, int status) throws Exception {
@ -230,80 +235,85 @@ public class CustomHttpClient {
} }
public void shutdown() throws IOException { public void shutdown() throws IOException {
Unirest.shutdown(); 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 {
HttpResponse<?> jsonResponse = null; HttpResponse jsonResponse = null;
JsonObject json = null; JsonObject json = null;
path = openviduUrl + (path.startsWith("/") ? path : ("/" + path)); path = openviduUrl + (path.startsWith("/") ? path : ("/" + path));
HttpRequest request = null; HttpRequestBase request = null;
if (body != null && !body.isEmpty()) { if (body != null && !body.isEmpty()) {
HttpEntityEnclosingRequestBase requestWithBody = (HttpEntityEnclosingRequestBase) request;
switch (method) { switch (method) {
case POST: case POST:
request = Unirest.post(path); requestWithBody = new HttpPost(path);
break; break;
case PUT: case PUT:
request = Unirest.put(path); requestWithBody = new HttpPut(path);
break; break;
case PATCH: case PATCH:
default: default:
request = Unirest.patch(path); requestWithBody = new HttpPatch(path);
break; break;
} }
((HttpRequestWithBody) request).header("Content-Type", "application/json").body(body.replaceAll("'", "\"")); requestWithBody.addHeader("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 = Unirest.get(path); request = new HttpGet(path);
request.header("Content-Type", "application/x-www-form-urlencoded"); request.addHeader("Content-Type", "application/x-www-form-urlencoded");
break; break;
case POST: case POST:
request = Unirest.post(path); request = new HttpPost(path);
break; break;
case DELETE: case DELETE:
request = Unirest.delete(path); request = new HttpDelete(path);
request.header("Content-Type", "application/x-www-form-urlencoded"); request.addHeader("Content-Type", "application/x-www-form-urlencoded");
break; break;
case PUT: case PUT:
request = Unirest.put(path); request = new HttpPut(path);
default: default:
break; break;
} }
} }
request = request.header("Authorization", this.headerAuth); request.addHeader("Authorization", this.headerAuth);
try { try {
jsonResponse = request.asJson(); jsonResponse = client.execute(request);
if (jsonResponse.getBody() != null) { } catch (Exception e) {
jsonResponse.getBody(); throw new Exception("Error sending request to " + path + ": " + e.getMessage());
json = JsonParser.parseString(((JsonNode) jsonResponse.getBody()).getObject().toString()) }
.getAsJsonObject(); if (jsonResponse.getEntity() != null) {
} String stringResponse = EntityUtils.toString(jsonResponse.getEntity(), "UTF-8");
} catch (UnirestException e) { JsonElement jsonElement = null;
try { try {
if (e.getCause().getCause().getCause() instanceof org.json.JSONException) { jsonElement = JsonParser.parseString(stringResponse);
try { } catch (JsonParseException e) {
jsonResponse = request.asString(); System.out.println("Response is not a JSON element: " + stringResponse);
} catch (UnirestException e1) { }
throw new Exception("Error sending request to " + path + ": " + e.getMessage()); if (jsonElement != null) {
} try {
} else { json = jsonElement.getAsJsonObject();
throw new Exception("Error sending request to " + path + ": " + e.getMessage()); } catch (IllegalStateException e) {
System.out.println("Response is not a JSON object: " + stringResponse);
} }
} catch (NullPointerException e2) {
throw new Exception("Error sending request to " + path + ": " + e.getMessage());
} }
} }
if (jsonResponse.getStatus() == 500) { if (jsonResponse.getStatusLine().getStatusCode() == 500) {
log.error("Internal Server Error: {}", jsonResponse.getBody().toString()); log.error("Internal Server Error: {}", EntityUtils.toString(jsonResponse.getEntity(), "UTF-8"));
} }
if (status != jsonResponse.getStatus()) { if (status != jsonResponse.getStatusLine().getStatusCode()) {
System.err.println(jsonResponse.getBody().toString()); String responseString = EntityUtils.toString(jsonResponse.getEntity(), "UTF-8");
throw new Exception(path + " expected to return status " + status + " but got " + jsonResponse.getStatus()); System.err.println(responseString);
throw new Exception(path + " expected to return status " + status + " but got "
+ jsonResponse.getStatusLine().getStatusCode());
} }
return json; return json;

View File

@ -115,11 +115,6 @@
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>${version.gson}</version> <version>${version.gson}</version>
</dependency> </dependency>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>${version.unirest}</version>
</dependency>
<dependency> <dependency>
<groupId>io.openvidu</groupId> <groupId>io.openvidu</groupId>
<artifactId>openvidu-test-browsers</artifactId> <artifactId>openvidu-test-browsers</artifactId>

View File

@ -31,6 +31,7 @@ import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedCondition;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitStrategy; import org.testcontainers.containers.wait.strategy.WaitStrategy;
@ -38,7 +39,6 @@ import org.testcontainers.utility.DockerImageName;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.mashape.unirest.http.HttpMethod;
import io.github.bonigarcia.wdm.WebDriverManager; import io.github.bonigarcia.wdm.WebDriverManager;
import io.openvidu.java.client.OpenVidu; import io.openvidu.java.client.OpenVidu;

View File

@ -32,13 +32,13 @@ import org.openqa.selenium.By;
import org.openqa.selenium.Keys; import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.ExpectedConditions;
import org.springframework.http.HttpMethod;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.mashape.unirest.http.HttpMethod;
import info.debatty.java.stringsimilarity.Cosine; import info.debatty.java.stringsimilarity.Cosine;
import io.openvidu.java.client.Connection; import io.openvidu.java.client.Connection;

View File

@ -51,6 +51,7 @@ import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.ExpectedConditions;
import org.springframework.http.HttpMethod;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
@ -58,7 +59,6 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonNull; import com.google.gson.JsonNull;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.mashape.unirest.http.HttpMethod;
import io.appium.java_client.AppiumDriver; import io.appium.java_client.AppiumDriver;
import io.openvidu.java.client.Connection; import io.openvidu.java.client.Connection;

View File

@ -52,7 +52,6 @@
<version.dockerjava>3.2.13</version.dockerjava> <version.dockerjava>3.2.13</version.dockerjava>
<version.slf4j>1.7.36</version.slf4j> <version.slf4j>1.7.36</version.slf4j>
<version.gson>2.10</version.gson> <version.gson>2.10</version.gson>
<version.unirest>1.4.9</version.unirest>
<version.jcodec>0.2.5</version.jcodec> <version.jcodec>0.2.5</version.jcodec>
<version.testcontainers>1.16.2</version.testcontainers> <version.testcontainers>1.16.2</version.testcontainers>
<version.appium>7.6.0</version.appium> <version.appium>7.6.0</version.appium>