-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.java
More file actions
1142 lines (947 loc) · 39.7 KB
/
Request.java
File metadata and controls
1142 lines (947 loc) · 39.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package javaxt.http;
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.net.ssl.*;
//******************************************************************************
//** Http Request
//******************************************************************************
/**
* Used to set up a connection to an http server. This class is used in
* conjunction with the HTTP Response class. Example:
<pre>
javaxt.http.Response response = new javaxt.http.Request(url).getResponse();
</pre>
*
* A slightly more complex example might look like this:
<pre>
javaxt.http.Request request = new javaxt.http.Request(url);
request.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.10)");
request.setHeader("Accept-Encoding", "deflate"); //no gzip encoding
java.io.InputStream inputStream = request.getResponse().getInputStream();
new javaxt.io.File("/temp/image.jpg").write(inputStream);
inputStream.close();
</pre>
*
******************************************************************************/
public class Request {
private URLConnection conn = null;
private Proxy HttpProxy;
private java.net.URL url;
private java.net.URL orgURL;
private int connectionTimeout = 0;
protected int readTimeout = 0;
private boolean useCache = false;
private int maxRedirects = 5;
private String username;
private String password;
private String method;
private Map<String, List<String>> requestHeaders = null;
private HashMap<String, List<String>> RequestProperties = new HashMap<>();
//Http response properties
private Map<String, List<String>> headers = null;
private String protocol;
private String version;
private int responseCode;
private String message;
private static TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
private static final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
private boolean validateCertificates = true;
public Request clone(){
Request request = new Request(orgURL);
request.HttpProxy = HttpProxy;
request.connectionTimeout = connectionTimeout;
request.readTimeout = readTimeout;
request.useCache = useCache;
request.maxRedirects = maxRedirects;
request.username = username;
request.password = password;
request.requestHeaders = requestHeaders;
request.RequestProperties = RequestProperties;
request.method = method;
return request;
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** @param url URL endpoint
*/
public Request(String url){
this(url, null);
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** @param url URL endpoint
* @param httpProxy Proxy server
*/
public Request(String url, String httpProxy) {
this(getURL(url), httpProxy);
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** @param url URL endpoint
*/
public Request(java.net.URL url){
this(url, null);
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** @param url URL endpoint
* @param httpProxy Proxy server
*/
public Request(java.net.URL url, String httpProxy){
this.url = orgURL = url;
setProxy(httpProxy);
initHeaders();
}
private static java.net.URL getURL(String url){
try{
return new java.net.URL(url);
}
catch (Exception e) {
return null;
}
}
//**************************************************************************
//** initHeaders
//**************************************************************************
private void initHeaders(){
this.setUseCache(false);
this.setHeader("Accept", "*/*");
this.setHeader("Accept-Encoding", "gzip,deflate");
this.setHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
//this.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.10)"); //windows xp
//this.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0"); //windows 7
//this.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"); //windows 8
this.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/118.0"); //windows 10
}
public void setURL(java.net.URL url){
this.url = url;
this.orgURL = url;
}
//**************************************************************************
//** getURL
//**************************************************************************
/** Returns the URL used to make the request. Note that the URL may be
* different from the URL used to invoke this class if the request
* encounters a redirect. See setNumRedirects() for more information.
* Use the getInitialURL() to get the original URL used to instantiate this
* class.
*/
public java.net.URL getURL(){
return url;
}
//**************************************************************************
//** getInitialURL
//**************************************************************************
/** Returns the URL used to instantiate this class. This URL may be
* different from the URL used to execute the request. See getURL() for
* more information.
*/
public java.net.URL getInitialURL(){
return orgURL;
}
//**************************************************************************
//** setRequestMethod
//**************************************************************************
/** Used to specify the request method ("GET", "POST", "PUT", "DELETE", etc).
* By default, requests are made using "GET" when fetching data and "POST"
* when writing data to the server. This method is used to override these
* defaults (e.g. "PUT" and "DELETE" for REST services).
*/
public void setRequestMethod(String method){
if (method!=null){
method = method.trim();
if (method.equalsIgnoreCase("DELETE")) method = "DELETE";
else if (method.equalsIgnoreCase("PUT")) method = "PUT";
this.method = method;
}
}
//**************************************************************************
//** getResponse
//**************************************************************************
/** Used to return the response from the server.
*/
public Response getResponse(){
if (conn==null) conn = getConnection(false);
return new Response(this, conn);
}
//**************************************************************************
//** setUseCache
//**************************************************************************
/** Sets the header associated with cache-control. If true, the protocol is
* allowed to use caching whenever it can. If false, the protocol must
* always try to get a fresh copy of the object. By default, the useCache
* variable is set to false.
*/
public void setUseCache(boolean useCache){
this.useCache = useCache;
}
//**************************************************************************
//** validateSSLCertificates
//**************************************************************************
/** Used to enable/disable certificate validation for HTTPS Connections.
* By default, certificates are validated via a Certificate Authority (CA).
* However, there are times where you may not want to validate a site's
* certificate (e.g. connecting to a intranet site or a development server
* with self-signed certificate). In these cases, you can bypass the
* validation process by setting this method to false.
*/
public void validateSSLCertificates(boolean validateCertificates){
this.validateCertificates = validateCertificates;
}
//**************************************************************************
//** setMaxRedirects
//**************************************************************************
/** Sets the maximum number of redirects to follow. By default, this number
* is set to 5.
*/
public void setNumRedirects(int maxRedirects){
this.maxRedirects = maxRedirects;
}
//**************************************************************************
//** setCredentials
//**************************************************************************
/** Used to set the username and password used to authenticate the client.
* The credentials are used in the "Authorization" request header and
* encoded using "Basic" authentication. Note that credentials encoded
* using "Basic" authentication can be easily decoded. As a general rule,
* do not pass credentials to sites that are not secured using SSL.
*/
public void setCredentials(String username, String password){
this.username = username;
this.password = password;
}
//**************************************************************************
//** setUserName
//**************************************************************************
/** Used to set the username used to authenticate the client. See
* setCredentials() for more information.
*/
public void setUserName(String username){
this.username = username;
}
//**************************************************************************
//** setPassword
//**************************************************************************
/** Used to set the password used to authenticate the client. See
* setCredentials() for more information.
*/
public void setPassword(String password){
this.password = password;
}
//**************************************************************************
//** getCredentials
//**************************************************************************
/** Used to get a Base64 encoded string representing the username and
* password. The string is used for "Basic" authentication.
*/
private String getCredentials() throws Exception {
if (username==null || password==null) return null;
else return javaxt.utils.Base64.encode(
(username + ":" + password),
javaxt.utils.Base64.DONT_BREAK_LINES //important for long credentials
);
}
//**************************************************************************
//** setConnectTimeout
//**************************************************************************
/** Connection timeout in milliseconds
*/
public void setConnectTimeout(int timeout){
if (timeout>0) connectionTimeout = timeout;
}
//**************************************************************************
//** setReadTimeout
//**************************************************************************
/** Read timeout in milliseconds.
*/
public void setReadTimeout(int timeout){
if (readTimeout>0) readTimeout = timeout;
}
//**************************************************************************
//** ConnectTimeout
//**************************************************************************
/** Thread used to enforce the connectionTimeout property.
*/
private class ConnectTimeout implements Runnable {
HttpURLConnection con;
public ConnectTimeout(URLConnection con) {
this.con = (HttpURLConnection) con;
}
public void run() {
try {
Thread.sleep(connectionTimeout);
} catch (InterruptedException e) {
}
if (responseCode==-1){
con.disconnect();
//System.out.println("** Timer thread forcing to quit connection");
}
}
}
//**************************************************************************
//** write
//**************************************************************************
/** Used to open an HTTP connection to the URL and POST data to the server.
* @param payload InputStream containing the body of the HTTP request.
*/
public void write(InputStream payload) {
if (conn==null) conn = getConnection(true);
OutputStream output = null;
try{
output = conn.getOutputStream();
byte[] buf = new byte[8192]; //8KB
int i = 0;
while((i=payload.read(buf))!=-1) {
output.write(buf, 0, i);
}
}
catch (Exception e){}
finally {
try { if (output != null) output.close(); }
catch (Exception e){}
}
parseResponse(conn);
}
//**************************************************************************
//** write
//**************************************************************************
/** Used to open an HTTP connection and sent data to the server.
* @param payload String containing the body of the HTTP request.
*/
public void write(String payload) {
write(payload.getBytes());
}
//**************************************************************************
//** write
//**************************************************************************
/** Used to open an HTTP connection and sent data to the server.
* @param payload Byte array containing the body of the HTTP request.
*/
public void write(byte[] payload) {
setHeader("Content-Length", payload.length + "");
if (conn==null) conn = getConnection(true);
try{
conn.getOutputStream().write(payload);
}
catch(Exception e){
//e.printStackTrace();
}
parseResponse(conn);
}
//**************************************************************************
//** write
//**************************************************************************
/** Used to open an HTTP connection and sent a JSON object to the server.
* Note that the JSON object is encoded using UTF-8. In some applications,
* the encoding may need to be explicitly defined in the "Content-Type"
* request header. Example:
<pre>
request.setHeader("Content-Type", "application/json;charset=UTF-8");
</pre>
*/
public void write(javaxt.json.JSONObject json){
write(json.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8));
}
//**************************************************************************
//** write
//**************************************************************************
/** Used to open an HTTP connection and sent a JSON array to the server.
* Note that the JSON array is encoded using UTF-8. In some applications,
* the encoding may need to be explicitly defined in the "Content-Type"
* request header. Example:
<pre>
request.setHeader("Content-Type", "application/json;charset=UTF-8");
</pre>
*/
public void write(javaxt.json.JSONArray arr){
write(arr.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8));
}
//**************************************************************************
//** write
//**************************************************************************
/** Used to post an array of form inputs to a server. Form inputs can
* include text or binary data, including files. Payload is normally
* "multipart/form-data" encoded.
*/
public void write(javaxt.html.Input[] inputs){
//Generate boundary
String boundary = "---------------------------";
for (int i=0; i<14; i++) boundary += new Random().nextInt(10);
int boundarySize = boundary.length();
try{
//Compute payload size and generate content metadata for each input
long size = 0;
ArrayList<byte[]> metadata = new ArrayList<>();
for (int i=0; i<inputs.length; i++){
javaxt.html.Input input = inputs[i];
String contentType = "";
String fileName = "";
long inputLength = 0;
if (input.isFile()){
javaxt.io.File file = (javaxt.io.File) input.getValue();
fileName = "; filename=\"" + file.getName() + "\"";
contentType = "Content-Type: " + file.getContentType() + "\r\n";
inputLength = file.getSize();
}
else{
inputLength = input.getSize();
}
String contentDisposition = "Content-Disposition: form-data; name=\"" + input.getName() + "\"" + fileName;
byte[] md = (contentDisposition + "\r\n" + contentType + "\r\n").getBytes("UTF-8");
metadata.add(md);
size += ((i>0?2:0) + 2 + boundarySize + 2); //CRLF + 2 hyphens + boundary + CRLF
size += md.length + inputLength;
}
size+=(boundarySize+8); //CRLF + 2 hyphens + boundary + 2 hyphens + CRLF
//Set request headers
setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
setHeader("Content-Length", size+"");
//Open socket
if (conn==null) conn = getConnection(true);
//Write content
java.io.OutputStream outputStream = null;
try{
outputStream = conn.getOutputStream();
for (int i=0; i<inputs.length; i++){
//Write boundary and input metadata
byte[] bd = ((i>0?"\r\n":"") + "--" + boundary + "\r\n").getBytes("UTF-8");
byte[] md = metadata.get(i);
outputStream.write(bd);
outputStream.write(md);
//Write input value
if (inputs[i].isFile()){
javaxt.io.File file = (javaxt.io.File) inputs[i].getValue();
java.io.InputStream inputStream = file.getInputStream();
byte[] b = new byte[1024];
int x=0;
while ( (x = inputStream.read(b)) != -1) {
outputStream.write(b,0,x);
}
inputStream.close();
}
else{
outputStream.write(inputs[i].toByteArray());
}
}
//Write footer and close outputstream
byte[] bd = ("\r\n--" + boundary + "--\r\n").getBytes("UTF-8");
outputStream.write(bd);
outputStream.close();
}
catch(IOException e){
if (outputStream!=null){
try{outputStream.close();}
catch(Exception ex){}
}
}
}
catch(java.io.UnsupportedEncodingException e){} //should never happen!
parseResponse(conn);
}
public List<String> getHeader(String key){
Iterator<String> it = RequestProperties.keySet().iterator();
while (it.hasNext()){
String currKey = it.next();
if (key.equalsIgnoreCase(currKey)){
return RequestProperties.get(currKey);
}
}
return null;
}
//**************************************************************************
//** setHeader
//**************************************************************************
/** Used to set a Request Property in the HTTP header (e.g. "User-Agent").
*/
public void setHeader(String key, String value){
boolean foundProperty = false;
Iterator<String> it = RequestProperties.keySet().iterator();
while (it.hasNext()){
String currKey = it.next();
if (key.equalsIgnoreCase(currKey)){
foundProperty = true;
List<String> values = new ArrayList<>();
values.add(value);
RequestProperties.put(currKey, values);
break;
}
}
if (!foundProperty){
List<String> values = new ArrayList<>();
values.add(value);
RequestProperties.put(key, values);
}
}
//**************************************************************************
//** addHeader
//**************************************************************************
/** Used to add a Request Property to the HTTP header (e.g. "User-Agent").
*/
public void addHeader(String key, String value){
if (key.equalsIgnoreCase("If-None-Match") || key.equalsIgnoreCase("If-Modified-Since") && value!=null){
useCache = true;
}
boolean foundProperty = false;
Iterator<String> it = RequestProperties.keySet().iterator();
while (it.hasNext()){
String currKey = it.next();
if (key.equalsIgnoreCase(currKey)){
foundProperty = true;
List<String> values = RequestProperties.get(currKey);
if (values==null) values = new ArrayList<>();
values.add(value);
RequestProperties.put(currKey, values);
break;
}
}
if (!foundProperty){
List<String> values = new ArrayList<String>();
values.add(value);
RequestProperties.put(key, values);
}
}
//**************************************************************************
//** connect
//**************************************************************************
/** Used to create a URLConnection.
*/
private URLConnection connect(boolean doOutput){
try {
//Set flag used to indicate whether this is an SSL request
boolean ssl = url.getProtocol().equalsIgnoreCase("https");
//By default, Java 1.6 and earlier use the SSLv2 protocol to initiate
//an SSL handshake. For security reasons, most modern webservers will
//actively refuse to accept SSLv2 messages. As a workaround, we'll
//try to use to use SSLv3 or TLSv1.
if (ssl){
int javaVersion = javaxt.utils.Java.getVersion();
if (javaVersion<7){
String sslProtocols = System.getProperty("https.protocols");
if (sslProtocols==null){
System.setProperty("https.protocols", "TLSv1,SSLv3");
}
}
}
//Disable SSL certificate validation as needed (Part 1)
if (ssl && validateCertificates==false){
try {
//SSLContext sc = SSLContext.getInstance("SSL");
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}
}
//Encode whitespaces and other illegal chars using the javaxt URL class
url = new javaxt.utils.URL(url).toURL();
//Open connection
URLConnection conn;
if (HttpProxy==null || isLocalHost(url.getHost())){
conn = url.openConnection();
}
else{
conn = url.openConnection(HttpProxy);
}
//Set request method as needed
if (method!=null){
HttpURLConnection con;
if (ssl){
con = (HttpsURLConnection)conn;
}
else{
con = (HttpURLConnection)conn;
}
con.setRequestMethod(method);
}
//Set timeouts
if (connectionTimeout>0){
new Thread(new ConnectTimeout(conn)).start();
conn.setConnectTimeout(connectionTimeout);
}
if (readTimeout>0){
//new Thread(new ReadTimeout(conn)).start();
conn.setReadTimeout(readTimeout);
}
//Disable HTTP redirects and SSL certificate validation as needed
if (ssl){
HttpsURLConnection con = (HttpsURLConnection)conn;
con.setInstanceFollowRedirects(false);
if (validateCertificates==false){
con.setHostnameVerifier(DO_NOT_VERIFY);
}
}
else{
HttpURLConnection con = (HttpURLConnection)conn;
con.setInstanceFollowRedirects(false);
}
//Set request headers
conn.setUseCaches(useCache);
if (doOutput) conn.setDoOutput(true);
String credentials = getCredentials();
if (credentials!=null) conn.setRequestProperty ("Authorization", "Basic " + credentials);
Iterator<String> it = RequestProperties.keySet().iterator();
while (it.hasNext()){
String key = it.next();
List<String> values = RequestProperties.get(key);
if (values!=null){
if (values.size()==1){
conn.setRequestProperty(key, values.iterator().next());
}
else{
Iterator<String> value = values.iterator();
while (value.hasNext()){
conn.addRequestProperty(key, value.next());
}
}
}
}
return conn;
}
catch (Exception e) {
//e.printStackTrace();
return null;
}
}
//**************************************************************************
//** getConnection
//**************************************************************************
/** Used to open a connection to a url/host.
*/
private URLConnection getConnection(boolean doOutput){
//If we're writing data to the socket (e.g. "POST") and maxRedirects<0,
//simply open a connection and let the caller figure out what to do with
//the response.
if (doOutput && maxRedirects<1){
URLConnection conn = this.connect(true);
requestHeaders = conn.getRequestProperties();
return conn;
}
//If we're still here, perform a "GET" request and look for 3XX series
//response codes. Follow redirects as needed. Note that once the
//parseResponse() method is called, clients can no longer write to the
//socket. Therefore, for "POST" requests, we must open a writable socket
//after parsing the response from the original "GET" request. The net
//effect is that when posting data and maxRedirects>0, the HTTP client
//will make 2 requests to the server: 1 to test the URL, the second to
//post data.
conn = null;
URLConnection conn = this.connect(false);
if (conn!=null){
requestHeaders = conn.getRequestProperties();
parseResponse(conn);
if ((responseCode>=300 && responseCode<400) && maxRedirects>0){
int numRedirects = 0;
while (responseCode>=300 && responseCode<400){
if (useCache && responseCode==304) break;
//Parse location response header
String location = getResponseHeader("Location");
javaxt.utils.URL newUrl = new javaxt.utils.URL(location);
if (newUrl.getProtocol()==null){
javaxt.utils.URL url = new javaxt.utils.URL(this.url);
url.setPath(location);
this.url = url.toURL();
}
else{
this.url = newUrl.toURL();
}
//Connect to the new url
try{
conn = this.connect(false);
parseResponse(conn);
numRedirects++;
if (numRedirects>maxRedirects) break;
}
catch (Exception e) {
e.printStackTrace();
break;
}
}
}
}
//Open a writable socket as needed.
if (doOutput){
conn = connect(true);
requestHeaders = conn.getRequestProperties();
}
return conn;
}
//**************************************************************************
//** parseResponse
//**************************************************************************
/** Used to parse the first line from the http response. Note that once,
* this method is called, clients can no longer write to the socket!
*/
private void parseResponse(URLConnection conn){
protocol = "";
version = "";
responseCode = -1;
message = "";
//requestHeaders = conn.getRequestProperties();
headers = conn.getHeaderFields(); //<-- Once this is called, clients can no longer write to the socket!
if (!headers.isEmpty()){
List status = (List)headers.get(null);
if (status!=null){
StringTokenizer st = new StringTokenizer( (String)(status).get(0) );
if (st.hasMoreTokens()) protocol = st.nextToken().trim().toUpperCase();
if (protocol.contains("/")) {
String temp = protocol;
protocol = temp.substring(0,temp.indexOf("/"));
version = temp.substring(temp.indexOf("/")+1);
}
else{
protocol = "HTTP";
version = "1.1";
}
if (st.hasMoreTokens()) responseCode = Integer.valueOf(st.nextToken().trim());
if (st.hasMoreTokens()){
message = "";
while (st.hasMoreTokens()){
message += st.nextToken() + " ";
}
message = message.trim();
}
}
}
}
//**************************************************************************
//** getExpiration
//**************************************************************************
/** Returns the time when the document should be considered expired.
* The time will be zero if the document always needs to be revalidated.
* It will be <code>null</code> if no expiration time is specified.
*/
private Long getExpiration(URLConnection connection, long baseTime) {
DateFormat PATTERN_RFC1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz"); //, Locale.US
String cacheControl = connection.getHeaderField("Cache-Control");
if (cacheControl != null) {
StringTokenizer tok = new StringTokenizer(cacheControl, ",");
while(tok.hasMoreTokens()) {
String token = tok.nextToken().trim().toLowerCase();
if ("must-revalidate".equals(token)) {
return new Long(0);
}
else if (token.startsWith("max-age")) {
int eqIdx = token.indexOf('=');
if (eqIdx != -1) {
String value = token.substring(eqIdx+1).trim();
int seconds;
try {
seconds = Integer.parseInt(value);
return new Long(baseTime + seconds * 1000);
}
catch(NumberFormatException nfe) {
System.err.println("getExpiration(): Bad Cache-Control max-age value: " + value);
// ignore
}
}
}
}
}
String expires = connection.getHeaderField("Expires");
if (expires != null) {
try {
synchronized(PATTERN_RFC1123) {
java.util.Date expDate = PATTERN_RFC1123.parse(expires);
return new Long(expDate.getTime());
}
}
catch(java.text.ParseException pe) {
int seconds;
try {
seconds = Integer.parseInt(expires);
return new Long(baseTime + seconds * 1000);
}
catch(NumberFormatException nfe) {
System.err.println("getExpiration(): Bad Expires header value: " + expires);
}
}
}
return null;
}
//**************************************************************************
//** getResponseCode
//**************************************************************************
/** Returns the HTTP status code extracted from the first line in the
* response.
*/
protected int getResponseCode(){
return responseCode;
}
//**************************************************************************
//** getResponseMessage
//**************************************************************************
/** Returns the message extracted from the first line in the response.
*/
protected String getResponseMessage(){
return message;
}
protected Map<String, List<String>> getResponseHeaders(){
return headers;
}
public Map<String, List<String>> getRequestHeaders(){
if (requestHeaders!=null) return requestHeaders;
else{
return RequestProperties;
}
}
protected String[] getResponseHeaders(String headerName){
if (headers==null) return new String[0];
//Iterate through the headers and find the matching header
ArrayList<String> values = new ArrayList<>();
Iterator<String> it = headers.keySet().iterator();
while(it.hasNext()){
String key = it.next();
if (key!=null){
if (key.equalsIgnoreCase(headerName)){
List<String> list = headers.get(key);
Iterator<String> val = list.iterator();
while (val.hasNext()){
values.add(val.next());
}
}
}
}