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>
<version>${version.gson}</version>
</dependency>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>${version.unirest}</version>
</dependency>
<dependency>
<groupId>org.jcodec</groupId>
<artifactId>jcodec-javase</artifactId>

View File

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

View File

@ -115,11 +115,6 @@
<artifactId>gson</artifactId>
<version>${version.gson}</version>
</dependency>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>${version.unirest}</version>
</dependency>
<dependency>
<groupId>io.openvidu</groupId>
<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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
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.JsonParser;
import com.mashape.unirest.http.HttpMethod;
import io.github.bonigarcia.wdm.WebDriverManager;
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.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.springframework.http.HttpMethod;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import com.mashape.unirest.http.HttpMethod;
import info.debatty.java.stringsimilarity.Cosine;
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.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.springframework.http.HttpMethod;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.google.gson.JsonArray;
@ -58,7 +59,6 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mashape.unirest.http.HttpMethod;
import io.appium.java_client.AppiumDriver;
import io.openvidu.java.client.Connection;

View File

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