forked from apache/dubbo
/
URL.java
1488 lines (1332 loc) · 48.1 KB
/
URL.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.common;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.common.utils.StringUtils;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* URL - Uniform Resource Locator (Immutable, ThreadSafe)
* <p>
* url example:
* <ul>
* <li>http://www.facebook.com/friends?param1=value1&param2=value2
* <li>http://username:password@10.20.130.230:8080/list?version=1.0.0
* <li>ftp://username:password@192.168.1.7:21/1/read.txt
* <li>registry://192.168.1.7:9090/org.apache.dubbo.service1?param1=value1&param2=value2
* </ul>
* <p>
* Some strange example below:
* <ul>
* <li>192.168.1.3:20880<br>
* for this case, url protocol = null, url host = 192.168.1.3, port = 20880, url path = null
* <li>file:///home/user1/router.js?type=script<br>
* for this case, url protocol = file, url host = null, url path = home/user1/router.js
* <li>file://home/user1/router.js?type=script<br>
* for this case, url protocol = file, url host = home, url path = user1/router.js
* <li>file:///D:/1/router.js?type=script<br>
* for this case, url protocol = file, url host = null, url path = D:/1/router.js
* <li>file:/D:/1/router.js?type=script<br>
* same as above file:///D:/1/router.js?type=script
* <li>/home/user1/router.js?type=script <br>
* for this case, url protocol = null, url host = null, url path = home/user1/router.js
* <li>home/user1/router.js?type=script <br>
* for this case, url protocol = null, url host = home, url path = user1/router.js
* </ul>
*
* @see java.net.URL
* @see java.net.URI
*/
/**
* @description :所有配置最终都将转换为 URL 表示,并由服务提供方生成,经注册中心传递给消费方,各属性对应 URL 的参数,参见配置项一览表中的 "对应URL参数" 列。
* @result: protocol://username:password@host:port/path?key=value&key=value
*/
public /**final**/ class URL implements Serializable {
private static final long serialVersionUID = -1985165475234910535L;
/**
* 协议名
*/
private final String protocol;
/**
* 用户名
*/
private final String username;
/**
* 密码
*/
private final String password;
// by default, host to registry
/**
* by default, host to registry
* 地址(默认为注册地址)
*/
private final String host;
// by default, port to registry
/**
* by default, port to registry
* 端口(默认为注册端口)
*/
private final int port;
/**
* 路径(服务名)
*/
private final String path;
/**
* 参数集合
*/
private final Map<String, String> parameters;
// ==== cache ====
/**
* transient: 不会被序列化到
*/
private volatile transient Map<String, Number> numbers;
private volatile transient Map<String, URL> urls;
private volatile transient String ip;
private volatile transient String full;
private volatile transient String identity;
private volatile transient String parameter;
private volatile transient String string;
protected URL() {
this.protocol = null;
this.username = null;
this.password = null;
this.host = null;
this.port = 0;
this.path = null;
this.parameters = null;
}
public URL(String protocol, String host, int port) {
this(protocol, null, null, host, port, null, (Map<String, String>) null);
}
public URL(String protocol, String host, int port, String[] pairs) { // varargs ... confilict with the following path argument, use array instead.
this(protocol, null, null, host, port, null, CollectionUtils.toStringMap(pairs));
}
public URL(String protocol, String host, int port, Map<String, String> parameters) {
this(protocol, null, null, host, port, null, parameters);
}
public URL(String protocol, String host, int port, String path) {
this(protocol, null, null, host, port, path, (Map<String, String>) null);
}
public URL(String protocol, String host, int port, String path, String... pairs) {
this(protocol, null, null, host, port, path, CollectionUtils.toStringMap(pairs));
}
public URL(String protocol, String host, int port, String path, Map<String, String> parameters) {
this(protocol, null, null, host, port, path, parameters);
}
public URL(String protocol, String username, String password, String host, int port, String path) {
this(protocol, username, password, host, port, path, (Map<String, String>) null);
}
public URL(String protocol, String username, String password, String host, int port, String path, String... pairs) {
this(protocol, username, password, host, port, path, CollectionUtils.toStringMap(pairs));
}
public URL(String protocol, String username, String password, String host, int port, String path, Map<String, String> parameters) {
if ((username == null || username.length() == 0)
&& password != null && password.length() > 0) {
throw new IllegalArgumentException("Invalid url, password without username!");
}
this.protocol = protocol;
this.username = username;
this.password = password;
this.host = host;
this.port = (port < 0 ? 0 : port);
// trim the beginning "/"
while (path != null && path.startsWith("/")) {
path = path.substring(1);
}
this.path = path;
if (parameters == null) {
parameters = new HashMap<String, String>();
} else {
parameters = new HashMap<String, String>(parameters);
}
this.parameters = Collections.unmodifiableMap(parameters);
}
/**
* Parse url string
*
* @param url URL string
* @return URL instance
* @see URL
*/
public static URL valueOf(String url) {
if (url == null || (url = url.trim()).length() == 0) {
throw new IllegalArgumentException("url == null");
}
String protocol = null;
String username = null;
String password = null;
String host = null;
int port = 0;
String path = null;
Map<String, String> parameters = null;
int i = url.indexOf("?"); // seperator between body and parameters
if (i >= 0) {
String[] parts = url.substring(i + 1).split("\\&");
parameters = new HashMap<String, String>();
for (String part : parts) {
part = part.trim();
if (part.length() > 0) {
int j = part.indexOf('=');
if (j >= 0) {
parameters.put(part.substring(0, j), part.substring(j + 1));
} else {
parameters.put(part, part);
}
}
}
url = url.substring(0, i);
}
i = url.indexOf("://");
if (i >= 0) {
if (i == 0) {
throw new IllegalStateException("url missing protocol: \"" + url + "\"");
}
protocol = url.substring(0, i);
url = url.substring(i + 3);
} else {
// case: file:/path/to/file.txt
i = url.indexOf(":/");
if (i >= 0) {
if (i == 0) {
throw new IllegalStateException("url missing protocol: \"" + url + "\"");
}
protocol = url.substring(0, i);
url = url.substring(i + 1);
}
}
i = url.indexOf("/");
if (i >= 0) {
path = url.substring(i + 1);
url = url.substring(0, i);
}
i = url.lastIndexOf("@");
if (i >= 0) {
username = url.substring(0, i);
int j = username.indexOf(":");
if (j >= 0) {
password = username.substring(j + 1);
username = username.substring(0, j);
}
url = url.substring(i + 1);
}
i = url.lastIndexOf(":");
if (i >= 0 && i < url.length() - 1) {
if (url.lastIndexOf("%") > i) {
// ipv6 address with scope id
// e.g. fe80:0:0:0:894:aeec:f37d:23e1%en0
// see https://howdoesinternetwork.com/2013/ipv6-zone-id
// ignore
} else {
port = Integer.parseInt(url.substring(i+1));
url = url.substring(0, i);
}
}
if (url.length() > 0) {
host = url;
}
return new URL(protocol, username, password, host, port, path, parameters);
}
public static String encode(String value) {
if (value == null || value.length() == 0) {
return "";
}
try {
return URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public static String decode(String value) {
if (value == null || value.length() == 0) {
return "";
}
try {
return URLDecoder.decode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
public String getProtocol() {
return protocol;
}
public URL setProtocol(String protocol) {
return new URL(protocol, username, password, host, port, path, getParameters());
}
public String getUsername() {
return username;
}
public URL setUsername(String username) {
return new URL(protocol, username, password, host, port, path, getParameters());
}
public String getPassword() {
return password;
}
public URL setPassword(String password) {
return new URL(protocol, username, password, host, port, path, getParameters());
}
public String getAuthority() {
if ((username == null || username.length() == 0)
&& (password == null || password.length() == 0)) {
return null;
}
return (username == null ? "" : username)
+ ":" + (password == null ? "" : password);
}
public String getHost() {
return host;
}
public URL setHost(String host) {
return new URL(protocol, username, password, host, port, path, getParameters());
}
/**
* Fetch IP address for this URL.
*
* Pls. note that IP should be used instead of Host when to compare with socket's address or to search in a map
* which use address as its key.
*
* @return ip in string format
*/
public String getIp() {
if (ip == null) {
ip = NetUtils.getIpByHost(host);
}
return ip;
}
public int getPort() {
return port;
}
public URL setPort(int port) {
return new URL(protocol, username, password, host, port, path, getParameters());
}
public int getPort(int defaultPort) {
return port <= 0 ? defaultPort : port;
}
public String getAddress() {
return port <= 0 ? host : host + ":" + port;
}
public URL setAddress(String address) {
int i = address.lastIndexOf(':');
String host;
int port = this.port;
if (i >= 0) {
host = address.substring(0, i);
port = Integer.parseInt(address.substring(i + 1));
} else {
host = address;
}
return new URL(protocol, username, password, host, port, path, getParameters());
}
public String getBackupAddress() {
return getBackupAddress(0);
}
public String getBackupAddress(int defaultPort) {
StringBuilder address = new StringBuilder(appendDefaultPort(getAddress(), defaultPort));
String[] backups = getParameter(Constants.BACKUP_KEY, new String[0]);
if (backups != null && backups.length > 0) {
for (String backup : backups) {
address.append(",");
address.append(appendDefaultPort(backup, defaultPort));
}
}
return address.toString();
}
public List<URL> getBackupUrls() {
List<URL> urls = new ArrayList<URL>();
urls.add(this);
String[] backups = getParameter(Constants.BACKUP_KEY, new String[0]);
if (backups != null && backups.length > 0) {
for (String backup : backups) {
urls.add(this.setAddress(backup));
}
}
return urls;
}
private String appendDefaultPort(String address, int defaultPort) {
if (address != null && address.length() > 0
&& defaultPort > 0) {
int i = address.indexOf(':');
if (i < 0) {
return address + ":" + defaultPort;
} else if (Integer.parseInt(address.substring(i + 1)) == 0) {
return address.substring(0, i + 1) + defaultPort;
}
}
return address;
}
public String getPath() {
return path;
}
public URL setPath(String path) {
return new URL(protocol, username, password, host, port, path, getParameters());
}
public String getAbsolutePath() {
if (path != null && !path.startsWith("/")) {
return "/" + path;
}
return path;
}
public Map<String, String> getParameters() {
return parameters;
}
public String getParameterAndDecoded(String key) {
return getParameterAndDecoded(key, null);
}
public String getParameterAndDecoded(String key, String defaultValue) {
return decode(getParameter(key, defaultValue));
}
public String getParameter(String key) {
String value = parameters.get(key);
if (value == null || value.length() == 0) {
value = parameters.get(Constants.DEFAULT_KEY_PREFIX + key);
}
return value;
}
public String getParameter(String key, String defaultValue) {
String value = getParameter(key);
if (value == null || value.length() == 0) {
return defaultValue;
}
return value;
}
public String[] getParameter(String key, String[] defaultValue) {
String value = getParameter(key);
if (value == null || value.length() == 0) {
return defaultValue;
}
return Constants.COMMA_SPLIT_PATTERN.split(value);
}
private Map<String, Number> getNumbers() {
if (numbers == null) { // concurrent initialization is tolerant
numbers = new ConcurrentHashMap<String, Number>();
}
return numbers;
}
private Map<String, URL> getUrls() {
if (urls == null) { // concurrent initialization is tolerant
urls = new ConcurrentHashMap<String, URL>();
}
return urls;
}
public URL getUrlParameter(String key) {
URL u = getUrls().get(key);
if (u != null) {
return u;
}
String value = getParameterAndDecoded(key);
if (value == null || value.length() == 0) {
return null;
}
u = URL.valueOf(value);
getUrls().put(key, u);
return u;
}
public double getParameter(String key, double defaultValue) {
Number n = getNumbers().get(key);
if (n != null) {
return n.doubleValue();
}
String value = getParameter(key);
if (value == null || value.length() == 0) {
return defaultValue;
}
double d = Double.parseDouble(value);
getNumbers().put(key, d);
return d;
}
public float getParameter(String key, float defaultValue) {
Number n = getNumbers().get(key);
if (n != null) {
return n.floatValue();
}
String value = getParameter(key);
if (value == null || value.length() == 0) {
return defaultValue;
}
float f = Float.parseFloat(value);
getNumbers().put(key, f);
return f;
}
public long getParameter(String key, long defaultValue) {
Number n = getNumbers().get(key);
if (n != null) {
return n.longValue();
}
String value = getParameter(key);
if (value == null || value.length() == 0) {
return defaultValue;
}
long l = Long.parseLong(value);
getNumbers().put(key, l);
return l;
}
public int getParameter(String key, int defaultValue) {
Number n = getNumbers().get(key);
if (n != null) {
return n.intValue();
}
String value = getParameter(key);
if (value == null || value.length() == 0) {
return defaultValue;
}
int i = Integer.parseInt(value);
getNumbers().put(key, i);
return i;
}
public short getParameter(String key, short defaultValue) {
Number n = getNumbers().get(key);
if (n != null) {
return n.shortValue();
}
String value = getParameter(key);
if (value == null || value.length() == 0) {
return defaultValue;
}
short s = Short.parseShort(value);
getNumbers().put(key, s);
return s;
}
public byte getParameter(String key, byte defaultValue) {
Number n = getNumbers().get(key);
if (n != null) {
return n.byteValue();
}
String value = getParameter(key);
if (value == null || value.length() == 0) {
return defaultValue;
}
byte b = Byte.parseByte(value);
getNumbers().put(key, b);
return b;
}
public float getPositiveParameter(String key, float defaultValue) {
if (defaultValue <= 0) {
throw new IllegalArgumentException("defaultValue <= 0");
}
float value = getParameter(key, defaultValue);
if (value <= 0) {
return defaultValue;
}
return value;
}
public double getPositiveParameter(String key, double defaultValue) {
if (defaultValue <= 0) {
throw new IllegalArgumentException("defaultValue <= 0");
}
double value = getParameter(key, defaultValue);
if (value <= 0) {
return defaultValue;
}
return value;
}
public long getPositiveParameter(String key, long defaultValue) {
if (defaultValue <= 0) {
throw new IllegalArgumentException("defaultValue <= 0");
}
long value = getParameter(key, defaultValue);
if (value <= 0) {
return defaultValue;
}
return value;
}
public int getPositiveParameter(String key, int defaultValue) {
if (defaultValue <= 0) {
throw new IllegalArgumentException("defaultValue <= 0");
}
int value = getParameter(key, defaultValue);
if (value <= 0) {
return defaultValue;
}
return value;
}
public short getPositiveParameter(String key, short defaultValue) {
if (defaultValue <= 0) {
throw new IllegalArgumentException("defaultValue <= 0");
}
short value = getParameter(key, defaultValue);
if (value <= 0) {
return defaultValue;
}
return value;
}
public byte getPositiveParameter(String key, byte defaultValue) {
if (defaultValue <= 0) {
throw new IllegalArgumentException("defaultValue <= 0");
}
byte value = getParameter(key, defaultValue);
if (value <= 0) {
return defaultValue;
}
return value;
}
public char getParameter(String key, char defaultValue) {
String value = getParameter(key);
if (value == null || value.length() == 0) {
return defaultValue;
}
return value.charAt(0);
}
public boolean getParameter(String key, boolean defaultValue) {
String value = getParameter(key);
if (value == null || value.length() == 0) {
return defaultValue;
}
return Boolean.parseBoolean(value);
}
public boolean hasParameter(String key) {
String value = getParameter(key);
return value != null && value.length() > 0;
}
public String getMethodParameterAndDecoded(String method, String key) {
return URL.decode(getMethodParameter(method, key));
}
public String getMethodParameterAndDecoded(String method, String key, String defaultValue) {
return URL.decode(getMethodParameter(method, key, defaultValue));
}
public String getMethodParameter(String method, String key) {
String value = parameters.get(method + "." + key);
if (value == null || value.length() == 0) {
return getParameter(key);
}
return value;
}
public String getMethodParameter(String method, String key, String defaultValue) {
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
return defaultValue;
}
return value;
}
public double getMethodParameter(String method, String key, double defaultValue) {
String methodKey = method + "." + key;
Number n = getNumbers().get(methodKey);
if (n != null) {
return n.intValue();
}
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
return defaultValue;
}
double d = Double.parseDouble(value);
getNumbers().put(methodKey, d);
return d;
}
public float getMethodParameter(String method, String key, float defaultValue) {
String methodKey = method + "." + key;
Number n = getNumbers().get(methodKey);
if (n != null) {
return n.intValue();
}
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
return defaultValue;
}
float f = Float.parseFloat(value);
getNumbers().put(methodKey, f);
return f;
}
public long getMethodParameter(String method, String key, long defaultValue) {
String methodKey = method + "." + key;
Number n = getNumbers().get(methodKey);
if (n != null) {
return n.intValue();
}
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
return defaultValue;
}
long l = Long.parseLong(value);
getNumbers().put(methodKey, l);
return l;
}
public int getMethodParameter(String method, String key, int defaultValue) {
String methodKey = method + "." + key;
Number n = getNumbers().get(methodKey);
if (n != null) {
return n.intValue();
}
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
return defaultValue;
}
int i = Integer.parseInt(value);
getNumbers().put(methodKey, i);
return i;
}
public short getMethodParameter(String method, String key, short defaultValue) {
String methodKey = method + "." + key;
Number n = getNumbers().get(methodKey);
if (n != null) {
return n.shortValue();
}
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
return defaultValue;
}
short s = Short.parseShort(value);
getNumbers().put(methodKey, s);
return s;
}
public byte getMethodParameter(String method, String key, byte defaultValue) {
String methodKey = method + "." + key;
Number n = getNumbers().get(methodKey);
if (n != null) {
return n.byteValue();
}
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
return defaultValue;
}
byte b = Byte.parseByte(value);
getNumbers().put(methodKey, b);
return b;
}
public double getMethodPositiveParameter(String method, String key, double defaultValue) {
if (defaultValue <= 0) {
throw new IllegalArgumentException("defaultValue <= 0");
}
double value = getMethodParameter(method, key, defaultValue);
if (value <= 0) {
return defaultValue;
}
return value;
}
public float getMethodPositiveParameter(String method, String key, float defaultValue) {
if (defaultValue <= 0) {
throw new IllegalArgumentException("defaultValue <= 0");
}
float value = getMethodParameter(method, key, defaultValue);
if (value <= 0) {
return defaultValue;
}
return value;
}
public long getMethodPositiveParameter(String method, String key, long defaultValue) {
if (defaultValue <= 0) {
throw new IllegalArgumentException("defaultValue <= 0");
}
long value = getMethodParameter(method, key, defaultValue);
if (value <= 0) {
return defaultValue;
}
return value;
}
public int getMethodPositiveParameter(String method, String key, int defaultValue) {
if (defaultValue <= 0) {
throw new IllegalArgumentException("defaultValue <= 0");
}
int value = getMethodParameter(method, key, defaultValue);
if (value <= 0) {
return defaultValue;
}
return value;
}
public short getMethodPositiveParameter(String method, String key, short defaultValue) {
if (defaultValue <= 0) {
throw new IllegalArgumentException("defaultValue <= 0");
}
short value = getMethodParameter(method, key, defaultValue);
if (value <= 0) {
return defaultValue;
}
return value;
}
public byte getMethodPositiveParameter(String method, String key, byte defaultValue) {
if (defaultValue <= 0) {
throw new IllegalArgumentException("defaultValue <= 0");
}
byte value = getMethodParameter(method, key, defaultValue);
if (value <= 0) {
return defaultValue;
}
return value;
}
public char getMethodParameter(String method, String key, char defaultValue) {
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
return defaultValue;
}
return value.charAt(0);
}
public boolean getMethodParameter(String method, String key, boolean defaultValue) {
String value = getMethodParameter(method, key);
if (value == null || value.length() == 0) {
return defaultValue;
}
return Boolean.parseBoolean(value);
}
public boolean hasMethodParameter(String method, String key) {
if (method == null) {
String suffix = "." + key;
for (String fullKey : parameters.keySet()) {
if (fullKey.endsWith(suffix)) {
return true;
}
}
return false;
}
if (key == null) {
String prefix = method + ".";
for (String fullKey : parameters.keySet()) {
if (fullKey.startsWith(prefix)) {
return true;
}
}
return false;
}
String value = getMethodParameter(method, key);
return value != null && value.length() > 0;
}
public boolean isLocalHost() {
return NetUtils.isLocalHost(host) || getParameter(Constants.LOCALHOST_KEY, false);
}
public boolean isAnyHost() {
return Constants.ANYHOST_VALUE.equals(host) || getParameter(Constants.ANYHOST_KEY, false);
}
public URL addParameterAndEncoded(String key, String value) {
if (value == null || value.length() == 0) {
return this;
}
return addParameter(key, encode(value));
}
public URL addParameter(String key, boolean value) {
return addParameter(key, String.valueOf(value));
}
public URL addParameter(String key, char value) {
return addParameter(key, String.valueOf(value));
}
public URL addParameter(String key, byte value) {
return addParameter(key, String.valueOf(value));
}
public URL addParameter(String key, short value) {
return addParameter(key, String.valueOf(value));
}
public URL addParameter(String key, int value) {
return addParameter(key, String.valueOf(value));
}
public URL addParameter(String key, long value) {
return addParameter(key, String.valueOf(value));
}
public URL addParameter(String key, float value) {
return addParameter(key, String.valueOf(value));
}
public URL addParameter(String key, double value) {
return addParameter(key, String.valueOf(value));
}
public URL addParameter(String key, Enum<?> value) {
if (value == null) {
return this;
}
return addParameter(key, String.valueOf(value));
}
public URL addParameter(String key, Number value) {
if (value == null) {
return this;
}
return addParameter(key, String.valueOf(value));
}
public URL addParameter(String key, CharSequence value) {
if (value == null || value.length() == 0) {
return this;
}
return addParameter(key, String.valueOf(value));
}
public URL addParameter(String key, String value) {
if (key == null || key.length() == 0
|| value == null || value.length() == 0) {
return this;
}
// if value doesn't change, return immediately
if (value.equals(getParameters().get(key))) { // value != null
return this;
}
Map<String, String> map = new HashMap<String, String>(getParameters());
map.put(key, value);
return new URL(protocol, username, password, host, port, path, map);
}
public URL addParameterIfAbsent(String key, String value) {
if (key == null || key.length() == 0
|| value == null || value.length() == 0) {
return this;
}
if (hasParameter(key)) {
return this;
}
Map<String, String> map = new HashMap<String, String>(getParameters());
map.put(key, value);
return new URL(protocol, username, password, host, port, path, map);
}
/**
* Add parameters to a new url.
*
* @param parameters parameters in key-value pairs
* @return A new URL
*/
public URL addParameters(Map<String, String> parameters) {
if (parameters == null || parameters.size() == 0) {
return this;
}
boolean hasAndEqual = true;