55
66import com .google .gson .Gson ;
77import com .google .gson .GsonBuilder ;
8- import com .google .gson .JsonArray ;
98import com .google .gson .JsonElement ;
109import com .google .gson .JsonObject ;
1110
1615import java .io .InputStreamReader ;
1716import java .io .UnsupportedEncodingException ;
1817import java .lang .reflect .Field ;
19- import java .lang .reflect .Modifier ;
2018import java .lang .reflect .Type ;
19+ import java .net .CookieHandler ;
20+ import java .net .CookieManager ;
2121import java .net .HttpURLConnection ;
2222import java .net .URL ;
2323import java .net .URLEncoder ;
2424import java .nio .charset .Charset ;
25- import java .util .ArrayList ;
2625import java .util .Date ;
27- import java .util .Iterator ;
2826import java .util .Map ;
2927import java .util .UUID ;
3028
@@ -50,6 +48,11 @@ public class JsonServiceClient implements ServiceClient {
5048 public JsonServiceClient (String baseUrl ) {
5149 this .baseUrl = baseUrl .endsWith ("/" ) ? baseUrl : baseUrl + "/" ;
5250 this .replyUrl = this .baseUrl + "json/reply/" ;
51+
52+ //Automatically populate response cookies
53+ if (CookieHandler .getDefault () == null ){
54+ CookieHandler .setDefault (new CookieManager ());
55+ }
5356 }
5457
5558 public void setTimeout (int timeoutMs ) {
@@ -176,11 +179,13 @@ public HttpURLConnection createRequest(String requestUrl, String httpMethod, byt
176179 private static void addBasicAuth (HttpURLConnection req , String userName , String password ) {
177180 req .setRequestProperty (HttpHeaders .Authorization ,
178181 "Basic " + Utils .toBase64String (userName + ":" + password ));
182+ req .setRequestProperty ("X-Auth" , "Basic" ); // HttpURLConnection doesn't allow re-reading Authorization Header
179183 }
180184
181185 private static boolean shouldAuthenticate (HttpURLConnection req , String userName , String password ){
182186 try {
183187 return req .getResponseCode () == 401
188+ && req .getRequestProperty ("X-Auth" ) == null //only auth if auth never attempted
184189 && userName != null
185190 && password != null ;
186191 } catch (IOException e ) {
@@ -252,6 +257,11 @@ public static boolean hasRequestBody(String httpMethod)
252257 return true ;
253258 }
254259
260+ @ Override
261+ public boolean getAlwaysSendBasicAuthHeaders () {
262+ return this .alwaysSendBasicAuthHeaders ;
263+ }
264+
255265 @ Override
256266 public void setAlwaysSendBasicAuthHeaders (boolean value ) {
257267 this .alwaysSendBasicAuthHeaders = value ;
@@ -368,8 +378,8 @@ public <TResponse> TResponse send(String requestUrl, String httpMethod, byte[] r
368378 else {
369379 BufferedReader reader = new BufferedReader (new InputStreamReader (is ));
370380 TResponse response = resClass != null
371- ? (TResponse ) getGson ().fromJson (reader , resClass )
372- : (TResponse ) getGson ().fromJson (reader , resType );
381+ ? (TResponse ) getGson ().fromJson (reader , resClass )
382+ : (TResponse ) getGson ().fromJson (reader , resType );
373383
374384 reader .close ();
375385 return response ;
0 commit comments