-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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
Provide native Java 11 HTTP client versions of FormValidation#URLCheck
methods
#7508
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,14 +44,19 @@ | |
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.lang.reflect.Method; | ||
import java.net.HttpURLConnection; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.Base64; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.stream.Stream; | ||
import javax.servlet.ServletException; | ||
import jenkins.model.Jenkins; | ||
import jenkins.util.SystemProperties; | ||
|
@@ -461,10 +466,46 @@ public static FormValidation validateBase64(String value, boolean allowWhitespac | |
* This allows the check method to call various utility methods in a concise syntax. | ||
*/ | ||
public abstract static class URLCheck { | ||
/** | ||
* Open the given URI and read text content from it. This method honors the Content-type | ||
* header. | ||
* | ||
* @throws IOException if the URI scheme is not supported, the connection was interrupted, | ||
* or the response was an error | ||
* @since TODO | ||
*/ | ||
protected Stream<String> open(URI uri) throws IOException { | ||
HttpClient httpClient = ProxyConfiguration.newHttpClient(); | ||
HttpRequest httpRequest; | ||
try { | ||
httpRequest = ProxyConfiguration.newHttpRequestBuilder(uri).GET().build(); | ||
} catch (IllegalArgumentException e) { | ||
throw new IOException(e); | ||
} | ||
java.net.http.HttpResponse<Stream<String>> httpResponse; | ||
try { | ||
httpResponse = | ||
httpClient.send(httpRequest, java.net.http.HttpResponse.BodyHandlers.ofLines()); | ||
} catch (InterruptedException e) { | ||
throw new IOException(e); | ||
} | ||
if (httpResponse.statusCode() != HttpURLConnection.HTTP_OK) { | ||
throw new IOException( | ||
"Server returned HTTP response code " | ||
+ httpResponse.statusCode() | ||
+ " for URI " | ||
+ uri); | ||
} | ||
return httpResponse.body(); | ||
} | ||
|
||
/** | ||
* Opens the given URL and reads text content from it. | ||
* This method honors Content-type header. | ||
* | ||
* @deprecated use {@link #open(URI)} | ||
*/ | ||
@Deprecated | ||
protected BufferedReader open(URL url) throws IOException { | ||
// use HTTP content type to find out the charset. | ||
URLConnection con = ProxyConfiguration.open(url); | ||
|
@@ -475,11 +516,25 @@ protected BufferedReader open(URL url) throws IOException { | |
new InputStreamReader(con.getInputStream(), getCharset(con))); | ||
} | ||
|
||
/** | ||
* Find the string literal from the given stream of lines. | ||
* | ||
* @return true if found, false otherwise | ||
* @since TODO | ||
*/ | ||
protected boolean findText(Stream<String> in, String literal) { | ||
try (in) { | ||
return in.anyMatch(line -> line.contains(literal)); | ||
} | ||
} | ||
|
||
/** | ||
* Finds the string literal from the given reader. | ||
* @return | ||
* true if found, false otherwise. | ||
* @deprecated use {@link #findText(Stream, String)} | ||
*/ | ||
@Deprecated | ||
protected boolean findText(BufferedReader in, String literal) throws IOException { | ||
String line; | ||
while ((line = in.readLine()) != null) | ||
|
@@ -499,9 +554,9 @@ protected FormValidation handleIOException(String url, IOException e) throws IOE | |
// any invalid URL comes here | ||
if (e.getMessage().equals(url)) | ||
// Sun JRE (and probably others too) often return just the URL in the error. | ||
return error("Unable to connect " + url); | ||
return error("Unable to connect " + url, e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor unrelated change while I was here: append the stack trace for debuggability. We love stack traces. |
||
else | ||
return error(e.getMessage()); | ||
return error(e.getMessage(), e); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated change while I was here: while testing with URLs like https://app.assembla.com/login that have a redirect, I noticed that the old
HttpURLConnection
API automatically followed redirects but the new Java 11 HTTP client API was not. To ease migration I figured it would be best to follow redirects by default. I could not think of a case where you would not want to follow them, and it matches the behavior of the old API. Of course anyone who wants to opt out can always do so by creating a custom builder without this option (something that was not possible with the old API).