Replace use of java.net.HttpURLConnection with jav.net.http.HttpClient, make the outboud url connection asynchronous
This commit is contained in:
1 parent
a48665bf60
commit
21e97e480c
3 files changed
+28
-47
No files matched your search
+1
-1
@@ -195,7 +195,7 @@ public class OpenIDWellKnown {
|
|||||||
attempts++;
|
attempts++;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Map<String,Object> json = JsonUrlReader.fetch("GET", getWellKnownEndpoint().toURL(), "");
|
Map<String,Object> json = JsonUrlReader.fetch("GET", getWellKnownEndpoint(), "");
|
||||||
issuer = (String) json.get("issuer");
|
issuer = (String) json.get("issuer");
|
||||||
authorization_endpoint = UriBuilder.fromUri((String) json.get("authorization_endpoint")).build();
|
authorization_endpoint = UriBuilder.fromUri((String) json.get("authorization_endpoint")).build();
|
||||||
token_endpoint = UriBuilder.fromUri((String) json.get("token_endpoint")).build();
|
token_endpoint = UriBuilder.fromUri((String) json.get("token_endpoint")).build();
|
||||||
|
|||||||
+1
-1
@@ -205,7 +205,7 @@ public class TokenValidationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Map<String,Object> json =
|
Map<String,Object> json =
|
||||||
JsonUrlReader.fetch("POST", confService.getTokenEndpoint().toURL(),
|
JsonUrlReader.fetch("POST", confService.getTokenEndpoint(),
|
||||||
bodyBuilder.toString());
|
bodyBuilder.toString());
|
||||||
|
|
||||||
return (String) json.get("id_token");
|
return (String) json.get("id_token");
|
||||||
|
|||||||
+26
-45
@@ -19,18 +19,19 @@
|
|||||||
|
|
||||||
package org.apache.guacamole.auth.openid.util;
|
package org.apache.guacamole.auth.openid.util;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.net.URI;
|
||||||
import java.io.InputStreamReader;
|
import java.net.http.HttpClient;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.URL;
|
import java.net.http.HttpResponse;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
import org.jose4j.json.JsonUtil;
|
import org.jose4j.json.JsonUtil;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Utility class to open a http connection to a URL, send a body
|
* Utility class to open a http connection to a URL, send a body
|
||||||
* and receive a response in the form of a parsed JSON
|
* and receive a response in the form of a parsed JSON
|
||||||
@@ -54,8 +55,8 @@ public final class JsonUrlReader {
|
|||||||
* @param String method
|
* @param String method
|
||||||
* The htpp method to use. Should be "GET", "POST" or "PATCH".
|
* The htpp method to use. Should be "GET", "POST" or "PATCH".
|
||||||
*
|
*
|
||||||
* @param URL url
|
* @param URI uri
|
||||||
* A URL value giving the address where to recover the JSON
|
* A URI value giving the address where to recover the JSON
|
||||||
*
|
*
|
||||||
* @param String body
|
* @param String body
|
||||||
* A pre-encoded body string to be sent to the address if the method is
|
* A pre-encoded body string to be sent to the address if the method is
|
||||||
@@ -64,48 +65,28 @@ public final class JsonUrlReader {
|
|||||||
* @return
|
* @return
|
||||||
* A Map<String,Object> containing the decoded json values.
|
* A Map<String,Object> containing the decoded json values.
|
||||||
*/
|
*/
|
||||||
public static Map<String,Object> fetch(String method, URL url, String body) throws IOException {
|
public static Map<String, Object> fetch(String method, URI uri, String body) throws IOException {
|
||||||
if (url == null || url.toString() == "") {
|
if (uri == null || uri.toString().isEmpty()) {
|
||||||
throw new IOException("JsonUrlReader : Missing URL");
|
throw new IOException("JsonUrlReader: Missing URL");
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Open connection, using HttpURLConnection
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(uri)
|
||||||
|
.header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
||||||
|
.method(method,
|
||||||
|
HttpRequest.BodyPublishers.ofString(body == null ? "" : body, StandardCharsets.UTF_8))
|
||||||
|
.build();
|
||||||
|
|
||||||
conn.setRequestMethod(method);
|
// Asynchronous, non-blocking send, so that java servlet not blocked by outbound connection
|
||||||
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
CompletableFuture<HttpResponse<String>> future =
|
||||||
|
client.sendAsync(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
if (! method.equals("GET")) {
|
HttpResponse<String> response = future.join();
|
||||||
conn.setDoOutput(true);
|
int status = response.statusCode();
|
||||||
try (OutputStream out = conn.getOutputStream()) {
|
logger.debug("Response body: {}", response.body());
|
||||||
byte [] bodyBytes = body.getBytes(StandardCharsets.UTF_8);
|
Map<String, Object> json = JsonUtil.parseJson(response.body());
|
||||||
out.write(bodyBytes, 0, bodyBytes.length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read response
|
|
||||||
int status = conn.getResponseCode();
|
|
||||||
|
|
||||||
BufferedReader reader = new BufferedReader(
|
|
||||||
new InputStreamReader(
|
|
||||||
status >= 200 && status < 300
|
|
||||||
? conn.getInputStream()
|
|
||||||
: conn.getErrorStream(),
|
|
||||||
StandardCharsets.UTF_8
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
StringBuilder responseBody = new StringBuilder();
|
|
||||||
String line;
|
|
||||||
while ((line = reader.readLine()) != null) {
|
|
||||||
responseBody.append(line);
|
|
||||||
}
|
|
||||||
reader.close();
|
|
||||||
|
|
||||||
logger.debug("Response body : {}", responseBody.toString());
|
|
||||||
|
|
||||||
Map<String,Object> json = JsonUtil.parseJson(responseBody.toString());
|
|
||||||
|
|
||||||
if (status < 200 || status >= 300) {
|
if (status < 200 || status >= 300) {
|
||||||
throw new IOException("(status: " + status + "): " + json.toString());
|
throw new IOException("(status: " + status + "): " + json.toString());
|
||||||
@@ -114,7 +95,7 @@ public final class JsonUrlReader {
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
throw new IOException("JsonUrlreader error : " + e.getMessage());
|
throw new IOException("JsonUrlReader error: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user