Skip to content

Commit

Permalink
Introduce timeout for driver HTTP connection
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Aug 28, 2024
1 parent f90e3b7 commit dbc159e
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions driver-http/src/main/java/uniregistrar/driver/http/HttpDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
Expand All @@ -31,23 +32,16 @@ public class HttpDriver implements Driver {

private static final Logger log = LoggerFactory.getLogger(HttpDriver.class);

public static final HttpClient DEFAULT_HTTP_CLIENT = HttpClients.createDefault();
public static final URI DEFAULT_CREATE_URI = null;
public static final URI DEFAULT_UPDATE_URI = null;
public static final URI DEFAULT_DEACTIVATE_URI = null;
public static final URI DEFAULT_EXECUTE_URI = null;
public static final URI DEFAULT_PROPERTIES_URI = null;
public static final Boolean DEFAULT_INCLUDE_METHOD_PARAMETER = false;

private HttpClient httpClient = DEFAULT_HTTP_CLIENT;
private URI createUri = DEFAULT_CREATE_URI;
private URI updateUri = DEFAULT_UPDATE_URI;
private URI deactivateUri = DEFAULT_DEACTIVATE_URI;
private URI executeUri = DEFAULT_EXECUTE_URI;
private URI propertiesUri = DEFAULT_PROPERTIES_URI;
private Boolean includeMethodParameter = DEFAULT_INCLUDE_METHOD_PARAMETER;
private static final int HTTP_CLIENT_TIMEOUT = 60;

private HttpClient httpClient;
private String method;
private URI createUri = null;
private URI updateUri = null;
private URI deactivateUri = null;
private URI executeUri = null;
private URI propertiesUri = null;
private Boolean includeMethodParameter = false;

private Consumer<Map<String, Object>> beforeWriteCreateConsumer;
private Consumer<Map<String, Object>> beforeReadCreateConsumer;
Expand All @@ -59,7 +53,16 @@ public class HttpDriver implements Driver {
private Consumer<Map<String, Object>> beforeReadExecuteConsumer;

public HttpDriver() {
this.httpClient = buildDefaultHttpClient();
}

private static HttpClient buildDefaultHttpClient() {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(HTTP_CLIENT_TIMEOUT * 1000)
.setConnectionRequestTimeout(HTTP_CLIENT_TIMEOUT * 1000)
.setSocketTimeout(HTTP_CLIENT_TIMEOUT * 1000)
.build();
return HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
}

@Override
Expand Down Expand Up @@ -489,14 +492,6 @@ public static boolean isStateHttpContent(Map<String, Object> httpContentJson) {
* Getters and setters
*/

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}

public HttpClient getHttpClient() {
return this.httpClient;
}
Expand All @@ -505,6 +500,14 @@ public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}

public URI getCreateUri() {
return this.createUri;
}
Expand All @@ -526,7 +529,6 @@ public void setUpdateUri(URI updateUri) {
}

public void setUpdateUri(String updateUri) {

this.updateUri = URI.create(updateUri);
}

Expand All @@ -551,7 +553,6 @@ public void setExecuteUri(URI executeUri) {
}

public void setExecuteUri(String executeUri) {

this.executeUri = URI.create(executeUri);
}

Expand Down

0 comments on commit dbc159e

Please sign in to comment.