Skip to content
Snippets Groups Projects
Commit 7608b5e2 authored by Harshika Dhoot's avatar Harshika Dhoot Committed by preeti singh[Microsoft]
Browse files

Adding retry for jersey client for Azure ITs in case of 502 and 503.

parent af389b85
No related branches found
No related tags found
1 merge request!316Adding retry for jersey client for Azure ITs in case of 502 and 503.
......@@ -18,6 +18,7 @@ package org.opengroup.osdu.notification.util;
import static org.junit.Assert.assertEquals;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
......@@ -29,6 +30,7 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.google.gson.Gson;
import com.sun.jersey.api.client.Client;
......@@ -78,58 +80,88 @@ public abstract class TestUtils {
Map<String, String> headers, boolean enforceHttp)
throws Exception {
ClientResponse response;
try {
Client client = getClient();
client.setConnectTimeout(300000);
client.setReadTimeout(50000);
client.setFollowRedirects(false);
String url = getApiPath(path + query, enforceHttp);
System.out.println(url);
System.out.println(httpMethod);
System.out.println(requestBody);
System.out.println(headers);
WebResource webResource = client.resource(url);
final WebResource.Builder builder = webResource.type(MediaType.APPLICATION_JSON)
.header("Authorization", token);
headers.forEach((k, v) -> builder.header(k, v));
Client client = getClient();
client.setConnectTimeout(300000);
client.setReadTimeout(300000);
client.setFollowRedirects(false);
String url = getApiPath(path + query, enforceHttp);
System.out.println(url);
System.out.println(httpMethod);
System.out.println(requestBody);
System.out.println(headers);
WebResource webResource = client.resource(url);
final WebResource.Builder builder = webResource.type(MediaType.APPLICATION_JSON)
.header("Authorization", token);
int retryCount = 2;
headers.forEach((k, v) -> builder.header(k, v));
try{
response = builder.method(httpMethod, ClientResponse.class, requestBody);
while (retryCount > 0) {
if (response.getStatusInfo().getFamily().equals(Response.Status.Family.valueOf("SERVER_ERROR"))) {
System.out.println("got resoponse : " + response.getStatusInfo());
Thread.sleep(5000);
System.out.println("Retrying.. ");
response = builder.method(httpMethod, ClientResponse.class, requestBody);
} else
break;
retryCount--;
}
System.out.println("sending response from TestUtils send method");
return response;
} catch (Exception e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.out.println("Retrying in case of socket timeout exception");
return builder.method(httpMethod, ClientResponse.class, requestBody);
}
e.printStackTrace();
throw new AssertionError("Error: Send request error", e);
}
logger.info("waiting on response");
return response;
}
public static ClientResponse send(String url, String path, String httpMethod, String token, String requestBody,
String query, Map<String, String> headers, boolean enforceHttp)
throws Exception {
ClientResponse response;
try {
Client client = getClient();
client.setConnectTimeout(300000);
client.setReadTimeout(50000);
client.setFollowRedirects(false);
String URL = getApiPath(url, path + query, enforceHttp);
System.out.println(url + path);
System.out.println(httpMethod);
System.out.println(requestBody);
System.out.println(headers);
WebResource webResource = client.resource(URL);
final WebResource.Builder builder = webResource.type(MediaType.APPLICATION_JSON);
if (!token.isEmpty()) {
logger.info("Token is not empty so adding to request header");
builder.header("Authorization", token);
}
headers.forEach((k, v) -> builder.header(k, v));
Client client = getClient();
client.setConnectTimeout(300000);
client.setReadTimeout(300000);
client.setFollowRedirects(false);
String URL = getApiPath(url, path + query, enforceHttp);
System.out.println(url + path);
System.out.println(httpMethod);
System.out.println(requestBody);
System.out.println(headers);
WebResource webResource = client.resource(URL);
final WebResource.Builder builder = webResource.type(MediaType.APPLICATION_JSON);
if (!token.isEmpty()) {
logger.info("Token is not empty so adding to request header");
builder.header("Authorization", token);
}
int retryCount = 2;
headers.forEach((k, v) -> builder.header(k, v));
try{
response = builder.method(httpMethod, ClientResponse.class, requestBody);
while (retryCount > 0) {
if (response.getStatusInfo().getFamily().equals(Response.Status.Family.valueOf("SERVER_ERROR"))) {
System.out.println("got resoponse : " + response.getStatusInfo());
Thread.sleep(5000);
System.out.println("Retrying.. ");
response = builder.method(httpMethod, ClientResponse.class, requestBody);
} else
break;
retryCount--;
}
System.out.println("sending response from TestUtils send method");
return response;
} catch (Exception e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.out.println("Retrying in case of socket timeout exception");
return builder.method(httpMethod, ClientResponse.class, requestBody);
}
e.printStackTrace();
throw new AssertionError("Error: Send request error", e);
}
logger.info("waiting on response");
return response;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment