-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from elisherer/feature/retry_with_https
mimic follow redirects of http to https in case content received empty
- Loading branch information
Showing
3 changed files
with
29 additions
and
9 deletions.
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
24 changes: 24 additions & 0 deletions
24
library/src/main/java/net/jimblackler/jsonschemafriend/UrlUtils.java
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package net.jimblackler.jsonschemafriend; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
|
||
import static net.jimblackler.jsonschemafriend.StreamUtils.streamToString; | ||
|
||
public class UrlUtils { | ||
static String readFromStream(URL url) throws IOException { | ||
String result; | ||
try (InputStream stream = url.openStream()) { | ||
result = streamToString(stream); | ||
} | ||
if (result.isEmpty() && "http".equals(url.getProtocol())) { | ||
// in case tried http and received empty content, try to connect to same url with https | ||
URL secureUrl = new URL(url.toString().replaceFirst("http", "https")); | ||
try (InputStream stream = secureUrl.openStream()) { | ||
result = streamToString(stream); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
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