1414import java .io .BufferedReader ;
1515import java .io .InputStream ;
1616import java .io .InputStreamReader ;
17- import java .net .*;
17+ import java .net .HttpURLConnection ;
18+ import java .net .URI ;
19+ import java .net .URL ;
20+ import java .net .URLConnection ;
21+ import java .net .URLEncoder ;
1822import java .nio .charset .Charset ;
1923import java .nio .charset .StandardCharsets ;
2024import java .security .KeyManagementException ;
2327import java .util .ArrayList ;
2428import java .util .Collection ;
2529import java .util .List ;
30+ import java .util .Objects ;
2631
2732
2833public class RemoteUrl {
@@ -35,8 +40,10 @@ public class RemoteUrl {
3540 private static final Charset UTF_8 = StandardCharsets .UTF_8 ;
3641 private static final String ACCEPT_HEADER_VALUE = "application/json, application/yaml, */*" ;
3742 private static final String USER_AGENT_HEADER_VALUE = "Apache-HttpClient/Swagger" ;
43+ static final int CONNECTION_TIMEOUT = 30000 ;
44+ static final int READ_TIMEOUT = 60000 ;
3845
39- private static ConnectionConfigurator createConnectionConfigurator () {
46+ static ConnectionConfigurator createConnectionConfigurator () {
4047 if (Boolean .parseBoolean (System .getProperty (TRUST_ALL ))) {
4148 try {
4249 // Create a trust manager that does not validate certificate chains
@@ -73,6 +80,8 @@ public void process(URLConnection connection) {
7380 final HttpsURLConnection httpsConnection = (HttpsURLConnection ) connection ;
7481 httpsConnection .setSSLSocketFactory (sf );
7582 httpsConnection .setHostnameVerifier (trustAllNames );
83+ httpsConnection .setConnectTimeout (CONNECTION_TIMEOUT );
84+ httpsConnection .setReadTimeout (READ_TIMEOUT );
7685 }
7786 }
7887 };
@@ -86,7 +95,8 @@ public void process(URLConnection connection) {
8695
8796 @ Override
8897 public void process (URLConnection connection ) {
89- // Do nothing
98+ connection .setConnectTimeout (CONNECTION_TIMEOUT );
99+ connection .setReadTimeout (READ_TIMEOUT );
90100 }
91101 };
92102 }
@@ -104,6 +114,10 @@ public static String cleanUrl(String url) {
104114 }
105115
106116 public static String urlToString (String url , List <AuthorizationValue > auths ) throws Exception {
117+ return urlToString (url , auths , null , null );
118+ }
119+
120+ public static String urlToString (String url , List <AuthorizationValue > auths , final Integer connectionTimeout , final Integer readTimeout ) throws Exception {
107121 InputStream is = null ;
108122 BufferedReader br = null ;
109123
@@ -141,6 +155,7 @@ public static String urlToString(String url, List<AuthorizationValue> auths) thr
141155 } else {
142156 conn = inUrl .openConnection ();
143157 }
158+ setConnectionTimeouts (connectionTimeout , readTimeout , conn );
144159 CONNECTION_CONFIGURATOR .process (conn );
145160 for (AuthorizationValue item : header ) {
146161 conn .setRequestProperty (item .getKeyName (), item .getValue ());
@@ -189,6 +204,15 @@ public static String urlToString(String url, List<AuthorizationValue> auths) thr
189204 }
190205 }
191206
207+ private static void setConnectionTimeouts (final Integer connectionTimeout , final Integer readTimeout , final URLConnection conn ) {
208+ if (Objects .nonNull (connectionTimeout )) {
209+ conn .setConnectTimeout (connectionTimeout );
210+ }
211+ if (Objects .nonNull (readTimeout )) {
212+ conn .setReadTimeout (readTimeout );
213+ }
214+ }
215+
192216 private static void appendValue (URL url , AuthorizationValue value , Collection <AuthorizationValue > to ) {
193217 if (value instanceof ManagedValue ) {
194218 if (!((ManagedValue ) value ).process (url )) {
@@ -198,7 +222,7 @@ private static void appendValue(URL url, AuthorizationValue value, Collection<Au
198222 to .add (value );
199223 }
200224
201- private interface ConnectionConfigurator {
225+ interface ConnectionConfigurator {
202226
203227 void process (URLConnection connection );
204228 }
0 commit comments