Skip to content

Commit 30f9f6c

Browse files
Merge pull request #58 from privacyidea/http_timeouts
http timeouts, v1.2.2
2 parents 02fd264 + e7a2f59 commit 30f9f6c

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>org.privacyidea</groupId>
77
<artifactId>privacyidea-java-client</artifactId>
8-
<version>1.2.1</version>
8+
<version>1.2.2</version>
99
<packaging>jar</packaging>
1010
<properties>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/org/privacyidea/Endpoint.java

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers()
7373
this.piconfig = privacyIDEA.configuration();
7474

7575
OkHttpClient.Builder builder = new OkHttpClient.Builder();
76+
builder.connectTimeout(piconfig.httpTimeoutMs, TimeUnit.MILLISECONDS)
77+
.writeTimeout(piconfig.httpTimeoutMs, TimeUnit.MILLISECONDS)
78+
.readTimeout(piconfig.httpTimeoutMs, TimeUnit.MILLISECONDS);
79+
7680
if (!this.piconfig.doSSLVerify)
7781
{
7882
// Trust all certs and verify every host
@@ -108,7 +112,7 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
108112
if (httpUrl == null)
109113
{
110114
privacyIDEA.error("Server url could not be parsed: " + (piconfig.serverURL + endpoint));
111-
// Invoke the callback to terminate the thread that called this method.
115+
// Invoke the callback to terminate the thread that called this function.
112116
callback.onFailure(null, new IOException("Request could not be created because the url could not be parsed"));
113117
return;
114118
}
@@ -118,12 +122,7 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
118122
{
119123
if (k.equals("pass") || k.equals("password"))
120124
{
121-
StringBuilder tmp = new StringBuilder();
122-
for (int i = 0; i < v.length(); i++)
123-
{
124-
tmp.append("*");
125-
}
126-
v = tmp.toString();
125+
v = "*".repeat(v.length());
127126
}
128127

129128
privacyIDEA.log(k + "=" + v);
@@ -133,15 +132,8 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
133132
{
134133
params.forEach((key, value) ->
135134
{
136-
try
137-
{
138-
String encValue = URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
139-
urlBuilder.addQueryParameter(key, encValue);
140-
}
141-
catch (UnsupportedEncodingException e)
142-
{
143-
e.printStackTrace();
144-
}
135+
String encValue = URLEncoder.encode(value, StandardCharsets.UTF_8);
136+
urlBuilder.addQueryParameter(key, encValue);
145137
});
146138
}
147139

@@ -168,14 +160,7 @@ void sendRequestAsync(String endpoint, Map<String, String> params, Map<String, S
168160
// they are already in the correct encoding for the server
169161
if (!WEBAUTHN_PARAMETERS.contains(key))
170162
{
171-
try
172-
{
173-
encValue = URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
174-
}
175-
catch (UnsupportedEncodingException e)
176-
{
177-
privacyIDEA.error(e);
178-
}
163+
encValue = URLEncoder.encode(value, StandardCharsets.UTF_8);
179164
}
180165
formBodyBuilder.add(key, encValue);
181166
}

src/main/java/org/privacyidea/PIConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class PIConfig
2626
String serviceAccountRealm = "";
2727
boolean disableLog = false;
2828
String userAgent;
29+
int httpTimeoutMs = 30000;
2930

3031
public PIConfig(String serverURL, String userAgent)
3132
{

src/main/java/org/privacyidea/PrivacyIDEA.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ public static class Builder
567567
private IPILogger logger = null;
568568
private boolean disableLog = false;
569569
private IPISimpleLogger simpleLogBridge = null;
570+
private int httpTimeoutMs = 30000;
570571

571572
/**
572573
* @param serverURL the server URL is mandatory to communicate with privacyIDEA.
@@ -666,6 +667,17 @@ public Builder disableLog()
666667
return this;
667668
}
668669

670+
/**
671+
* Set the timeout for http requests in milliseconds.
672+
* @param httpTimeoutMs timeout in milliseconds
673+
* @return Builder
674+
*/
675+
public Builder httpTimeoutMs(int httpTimeoutMs)
676+
{
677+
this.httpTimeoutMs = httpTimeoutMs;
678+
return this;
679+
}
680+
669681
public PrivacyIDEA build()
670682
{
671683
PIConfig configuration = new PIConfig(serverURL, userAgent);
@@ -675,6 +687,7 @@ public PrivacyIDEA build()
675687
configuration.serviceAccountPass = serviceAccountPass;
676688
configuration.serviceAccountRealm = serviceAccountRealm;
677689
configuration.disableLog = disableLog;
690+
configuration.httpTimeoutMs = httpTimeoutMs;
678691
return new PrivacyIDEA(configuration, logger, simpleLogBridge);
679692
}
680693
}

src/test/java/org/privacyidea/TestGetTokenInfo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void setup()
4949
.serviceAccount(serviceAccount, servicePassword)
5050
.serviceRealm(serviceRealm)
5151
.disableLog()
52+
.httpTimeoutMs(15000)
5253
.sslVerify(false)
5354
.logger(new PILogImplementation())
5455
.build();

0 commit comments

Comments
 (0)