Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/add timeout connection #2441

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1 +1 @@

- Add arcgis_connectionTimeout and arcgis_readTimeout sink options to allow set non infinite conection timeouts with arcgis (#2440)
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class NGSIArcgisFeatureTable extends ArcgisFeatureTable {
* @param timeoutSecs
*/
public NGSIArcgisFeatureTable(String featureServiceUrl, String username, String password,
String getTokenUrl, long timeoutSecs) {
super(featureServiceUrl, username, password, getTokenUrl, false);
String getTokenUrl, long timeoutSecs, int connectionTimeout, int readTimeout) {
super(featureServiceUrl, username, password, getTokenUrl, false, connectionTimeout, readTimeout);
this.timeoutSecs = timeoutSecs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class ArcgisFeatureTable {
private boolean connected;

private int batchSize;

private String errorDesc = "";
private int errorCode = 0;
private List<Feature> featureBatch = Collections.synchronizedList(new ArrayList<Feature>());
Expand Down Expand Up @@ -86,9 +87,8 @@ protected ArcgisFeatureTable() {
* @param readOnly
*/
public ArcgisFeatureTable(String url, String user, String password, String tokenGenUrl,
boolean readOnly) {
boolean readOnly, int connectionTimeout, int readTimeout) {
this();

LOGGER.debug("Arcgis constructor.. " + url);

LOGGER.debug("Arcgis url.. " + url);
Expand All @@ -97,13 +97,13 @@ public ArcgisFeatureTable(String url, String user, String password, String token

Credential credential = new UserCredential(user, password);
try {
arcGISFeatureTable = new RestFeatureTable(url, credential, tokenGenUrl);
arcGISFeatureTable = new RestFeatureTable(url, credential, tokenGenUrl, connectionTimeout, readTimeout);
LOGGER.debug("Recovering attribute info from feature table. ->" + url);
arcGISFeatureTable.getTableAttributesInfo();
LOGGER.debug("Table successfully connected.");
connected = true;
} catch (ArcgisException e) {
LOGGER.error("Argis error while connecting to Feature Table: (" + e.getMessage() + ")"
LOGGER.error("Arcgis error while connecting to Feature Table: (" + e.getMessage() + ")"
+ "\n\t URL: " + url
+ "\n\t tokenGenURL: " + tokenGenUrl);
connected = false;
Expand Down Expand Up @@ -570,7 +570,7 @@ private void commitFeatures(final List<Feature> featureList, int action) {
+ featureList.size());
}
} else {
LOGGER.error("WARN - Argis.commitFeatures called with " + sizeList + " entities and hasError() " + hasError());
LOGGER.error("WARN - Arcgis.commitFeatures called with " + sizeList + " entities and hasError() " + hasError());
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,23 @@ public abstract class CredentialRestApi extends RestApi {
protected Credential credential;
protected String referer;

protected int connectionTimeout = 0;
protected int readTimeout = 0;

/**
* @param tokenGenUrl
* @param credential
* @param referer
* @param expirationMins
*/
public CredentialRestApi(URL tokenGenUrl, Credential credential, String referer) {
public CredentialRestApi(URL tokenGenUrl, Credential credential, String referer,
int connectionTimeout, int readTimeout) {
super();
this.tokenGenUrl = tokenGenUrl;
this.credential = credential;
this.referer = referer;
this.connectionTimeout = connectionTimeout;
this.readTimeout = readTimeout;
}

/**
Expand All @@ -57,12 +63,15 @@ public CredentialRestApi(URL tokenGenUrl, Credential credential, String referer)
* @param expirationMins
* @throws ArcgisException
*/
public CredentialRestApi(String tokenGenUrl, Credential credential, String referer)
public CredentialRestApi(String tokenGenUrl, Credential credential, String referer,
int connectionTimeout, int readTimeout)
throws ArcgisException {
super();

this.credential = credential;
this.referer = referer;
this.connectionTimeout = connectionTimeout;
this.readTimeout = readTimeout;
try {
if (tokenGenUrl != null && !"".equals(tokenGenUrl.trim())) {
this.tokenGenUrl = new URL(tokenGenUrl);
Expand All @@ -88,7 +97,7 @@ public Credential getCredential() throws ArcgisException {
+ (credential != null ? credential.isExpired() : null));
if (tokenGenUrl != null && (credential == null || credential.isExpired())) {
LOGGER.debug("Creating/Refreshing token.");
credential = RestAuthentication.createToken(credential, tokenGenUrl, referer);
credential = RestAuthentication.createToken(credential, tokenGenUrl, referer, this.connectionTimeout, this.readTimeout);
}

return credential;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ protected static Map<String, String> checkParameters(Map<String, String> params)
* @throws Exception
*/
public static HttpResponse requestHTTP(String urlToRead, Map<String, String> params,
HttpMethod httpMethod, String body) {
HttpMethod httpMethod, String body,
int connectionTimeout, int readTimeout) {

HttpResponse httpResponse = new HttpResponse();
StringBuilder result = new StringBuilder("");
Expand Down Expand Up @@ -209,7 +210,9 @@ public boolean verify(String hostname, SSLSession session) {
URL url = new java.net.URL(strUrl);
conn = (java.net.HttpURLConnection) url.openConnection();
conn.setRequestMethod(httpMethod.toString());

conn.setConnectTimeout(connectionTimeout);
conn.setReadTimeout(readTimeout);

// Si es necesario ponemos el body
if (httpMethod != HttpMethod.GET) {
conn.setDoOutput(true);
Expand Down Expand Up @@ -283,8 +286,9 @@ public boolean verify(String hostname, SSLSession session) {
* @param params
* @return
*/
public static HttpResponse httpGet(String urlToRead, Map<String, String> params) {
return requestHTTP(urlToRead, params, HttpMethod.GET, "");
public static HttpResponse httpGet(String urlToRead, Map<String, String> params,
int connectionTimeout, int readTimeout) {
return requestHTTP(urlToRead, params, HttpMethod.GET, "", connectionTimeout, readTimeout);
}

/**
Expand All @@ -295,8 +299,9 @@ public static HttpResponse httpGet(String urlToRead, Map<String, String> params)
* @param body
* @return
*/
public static HttpResponse httpPost(String urlToRead, Map<String, String> params, String body) {
return requestHTTP(urlToRead, params, HttpMethod.POST, body);
public static HttpResponse httpPost(String urlToRead, Map<String, String> params, String body,
int connectionTimeout, int readTimeout) {
return requestHTTP(urlToRead, params, HttpMethod.POST, body, connectionTimeout, readTimeout );
}

/**
Expand All @@ -309,9 +314,10 @@ public static HttpResponse httpPost(String urlToRead, Map<String, String> params
* @throws UnsupportedEncodingException
*/
public static HttpResponse httpPost(String urlToRead, Map<String, String> params,
Map<String, String> bodyParams) {
Map<String, String> bodyParams,
int connectionTimeout, int readTimeout) {
bodyParams = checkParameters(bodyParams);
return requestHTTP(urlToRead, params, HttpMethod.POST, getPostParameters(bodyParams));
return requestHTTP(urlToRead, params, HttpMethod.POST, getPostParameters(bodyParams), connectionTimeout, readTimeout);
}

/**
Expand All @@ -322,8 +328,9 @@ public static HttpResponse httpPost(String urlToRead, Map<String, String> params
* @param body
* @return
*/
public static HttpResponse httpPost(String urlToRead, Map<String, String> bodyParams) {
return httpPost(urlToRead, null, bodyParams);
public static HttpResponse httpPost(String urlToRead, Map<String, String> bodyParams,
int connectionTimeout, int readTimeout) {
return httpPost(urlToRead, null, bodyParams, connectionTimeout, readTimeout);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public static LocalDateTime expirationFromJson(String tokenJson, String expirati
* @throws ArcGisException
*/
public static Credential createUserToken(String user, String password, URL tokenGenUrl,
String referer, Integer expirationMins) throws ArcgisException {
String referer, Integer expirationMins,
int connectionTimeout, int readTimeout) throws ArcgisException {
String tokenJSON = null;
try {
Map<String, String> bodyParams = new LinkedHashMap<String, String>();
Expand All @@ -163,7 +164,7 @@ public static Credential createUserToken(String user, String password, URL token
}
bodyParams.put(PARAM_REQUEST_FORMAT, REQUEST_FORMAT_PARAMETER);

HttpResponse response = httpPost(tokenGenUrl.toString(), bodyParams);
HttpResponse response = httpPost(tokenGenUrl.toString(), bodyParams, connectionTimeout, readTimeout);

if (response.getResponseCode() == 200) {
tokenJSON = response.getBody();
Expand Down Expand Up @@ -194,8 +195,8 @@ public static Credential createUserToken(String user, String password, URL token
* @throws ArcGisException
*/
public static Credential createUserToken(String user, String password, URL tokenGenUrl,
String referer) throws ArcgisException {
return createUserToken(user, password, tokenGenUrl, referer, null);
String referer, int connectionTimeout, int readTimeout) throws ArcgisException {
return createUserToken(user, password, tokenGenUrl, referer, null, connectionTimeout, readTimeout);
}

/**
Expand All @@ -208,13 +209,13 @@ public static Credential createUserToken(String user, String password, URL token
* @return
* @throws ArcgisException
*/
public static Credential createToken(Credential credential, URL tokenGenUrl, String referer)
public static Credential createToken(Credential credential, URL tokenGenUrl, String referer, int connectionTimeout, int readTimeout)
throws ArcgisException {

if (credential instanceof UserCredential) {
UserCredential userCredential = (UserCredential) credential;
credential = createUserToken(userCredential.getUser(), userCredential.getPassword(),
tokenGenUrl, referer);
tokenGenUrl, referer, connectionTimeout, readTimeout);

} else {
throw new ArcgisException("Invalid Credential type.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public class RestFeatureTable extends CredentialRestApi {

protected URL serviceUrl;

protected int connectionTimeout = 0;
protected int readTimeout = 0;

// Table info
private String uniqueIdField = "";
private Map<String, Field> tableAttributes = new HashMap<String, Field>();
Expand All @@ -90,9 +93,12 @@ public class RestFeatureTable extends CredentialRestApi {
* @param referer
* @param expirationMins
*/
private RestFeatureTable(URL serviceUrl, Credential credential) {
super((URL) null, credential, serviceUrl.toString());
private RestFeatureTable(URL serviceUrl, Credential credential,
int connectionTimeout, int readTimeout) {
super((URL) null, credential, serviceUrl.toString(), connectionTimeout, readTimeout);
this.serviceUrl = serviceUrl;
this.connectionTimeout = connectionTimeout;
this.readTimeout = readTimeout;
}


Expand All @@ -103,10 +109,12 @@ private RestFeatureTable(URL serviceUrl, Credential credential) {
* @param credential
* @throws MalformedURLException
*/
public RestFeatureTable(String url, Credential credential, String tokenGenUrl)
public RestFeatureTable(String url, Credential credential, String tokenGenUrl,
int connectionTimeout, int readTimeout)
throws ArcgisException {
super(tokenGenUrl, credential, url);

super(tokenGenUrl, credential, url, connectionTimeout, readTimeout);
this.connectionTimeout = connectionTimeout;
this.readTimeout = readTimeout;
try {
this.serviceUrl = new URL(url);
} catch (MalformedURLException e) {
Expand Down Expand Up @@ -212,7 +220,7 @@ public ResultPage<Feature> getFeatureList(String whereClause, int pageOffset, St
fullUrl += QUERY_RELATIVE_PATH;
}

HttpResponse response = httpGet(fullUrl, params);
HttpResponse response = httpGet(fullUrl, params, this.connectionTimeout, this.readTimeout);
LOGGER.debug("Response code: " + response.getResponseCode() + "\n\t" + response.getBody());

checkResponse(response);
Expand Down Expand Up @@ -265,7 +273,7 @@ public void sendFeatureList(List<Feature> featureList, String action) throws Arc
fullUrl += "/" + action;
}

HttpResponse response = httpPost(fullUrl, params, bodyParams);
HttpResponse response = httpPost(fullUrl, params, bodyParams, this.connectionTimeout, this.readTimeout);
LOGGER.debug("Response code: " + response.getResponseCode() + "\n\t" + response.getBody());

checkResponse(response);
Expand Down Expand Up @@ -350,7 +358,7 @@ public void deleteEntities(List<String> objectIdList) throws ArcgisException {
fullUrl += DELETE_FEATURES_RELATIVE_PATH;
}

HttpResponse response = httpPost(fullUrl, params, bodyParams);
HttpResponse response = httpPost(fullUrl, params, bodyParams, this.connectionTimeout, this.readTimeout);
LOGGER.debug("Response code: " + response.getResponseCode() + "\n\t" + response.getBody());

checkResponse(response);
Expand Down Expand Up @@ -397,7 +405,7 @@ public void getTableAttributesInfo() throws ArcgisException {
params.put(OUTPUT_FORMAT_PARAM, DEFAULT_OUTPUT_FORMAT);

LOGGER.debug("HttpGet " + fullUrl.toString() + " number of params: " + params.size());
HttpResponse response = httpGet(fullUrl, params);
HttpResponse response = httpGet(fullUrl, params, this.connectionTimeout, this.readTimeout);
LOGGER.debug("Response code: " + response.getResponseCode() + "\n\t" + response.getBody());

checkResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void setUp() throws Exception {

private ArcgisFeatureTable connectArcgis() {
return new ArcgisFeatureTable(ArcgisBaseTest.getFeatureUrl(), ArcgisBaseTest.getUser(),
ArcgisBaseTest.getPassword(), ArcgisBaseTest.getGenerateTokenUrl(), false);
ArcgisBaseTest.getPassword(), ArcgisBaseTest.getGenerateTokenUrl(), false, 0, 0);
}

/**
Expand Down Expand Up @@ -154,7 +154,7 @@ public void arcgisGetFeatures() {
if (!ArcgisBaseTest.connectionTestsSkipped()){
ArcgisFeatureTable arcgis = new ArcgisFeatureTable(
"https://sags1/arcgis/rest/services/Urbanismo/MobiliarioUrbano_ETRS89/FeatureServer/5",
"", "", "", false);
"", "", "", false, 0, 0);
List<Feature> resultList;
try {
resultList = arcgis.queryFeatures(arcgis.getUniqueIdField() + ">0");
Expand All @@ -178,7 +178,7 @@ public void arcgisGetSecuredFeatures() {
System.out.println("---------------- arcgisGetSecuredFeatures");
if (!ArcgisBaseTest.connectionTestsSkipped()){
ArcgisFeatureTable arcgis = new ArcgisFeatureTable(PORTAL_FEATURETABLE_URL, PORTAL_USER,
PORTAL_PASSWORD, PORTAL_GENERATE_TOKEN_URL, false);
PORTAL_PASSWORD, PORTAL_GENERATE_TOKEN_URL, false, 0, 0);
List<Feature> resultList;
try {
resultList = arcgis.queryFeatures(arcgis.getUniqueIdField() + ">0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class CredentialRestApiTest extends CredentialRestApi implements ArcgisBa
* @throws ArcgisException
*/
public CredentialRestApiTest() throws ArcgisException {
super(PORTAL_GENERATE_TOKEN_URL, null, PORTAL_FEATURETABLE_URL);
super(PORTAL_GENERATE_TOKEN_URL, null, PORTAL_FEATURETABLE_URL, 0, 0);
}

/**
Expand All @@ -55,7 +55,7 @@ public void testUserCredential() {
if (!ArcgisBaseTest.connectionTestsSkipped()){
try {
Credential credential = RestAuthentication.createUserToken(PORTAL_USER, PORTAL_PASSWORD,
new URL(PORTAL_GENERATE_TOKEN_URL), PORTAL_FEATURETABLE_URL, new Integer(1));
new URL(PORTAL_GENERATE_TOKEN_URL), PORTAL_FEATURETABLE_URL, new Integer(1), 0, 0);
System.out.println("ExpirationTime: " + credential.getExpirationTime());
this.setCredential(credential);
credential = getCredential();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void simpleGetTest() {
HashMap<String, String> params = new HashMap<String, String>();

try {
HttpResponse response = RestApi.httpGet(urlRequest, params);
HttpResponse response = RestApi.httpGet(urlRequest, params, 0, 0);
System.out.println(response.toString());
Assert.assertTrue(response.getResponseCode() == 200);
} catch (Exception e) {
Expand All @@ -79,7 +79,7 @@ public void parameterTest() {
params.put("f", "pjson");

try {
HttpResponse response = RestApi.httpGet(urlRequest, params);
HttpResponse response = RestApi.httpGet(urlRequest, params, 0, 0);
System.out.println(response.toString());
Assert.assertTrue(response.getResponseCode() == 200);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void getPortalTokenTest() {
System.out.println("------- TEST getPortalTokenTest()");
credential = RestAuthentication.createUserToken(PORTAL_USER, PORTAL_PASSWORD,
new URL(PORTAL_GENERATE_TOKEN_URL),
"https://sags1.int.ayto-santander.es/arcgis/rest/services/Policia");
"https://sags1.int.ayto-santander.es/arcgis/rest/services/Policia", 0, 0);
System.out.println("Recovered credential: " + credential);
assertTrue(!"".equals(credential.getToken()));
} catch (MalformedURLException e) {
Expand Down Expand Up @@ -88,7 +88,7 @@ public void getOnlineTokenTest() {
System.out.println("------- TEST getOnlineTokenTest()");

credential = RestAuthentication.createUserToken(ONLINE_USER, ONLINE_PASSWORD,
new URL(ONLINE_GENERATE_TOKEN_URL), "*");
new URL(ONLINE_GENERATE_TOKEN_URL), "*", 0, 0);
System.out.println("Recovered Token: " + credential);
assertTrue(!"".equals(credential.getToken()));
} catch (MalformedURLException e) {
Expand Down
Loading
Loading