@@ -42,6 +42,7 @@ public URL(String url){
4242 url = url .trim ();
4343 parameters = new LinkedHashMap <>();
4444 extendedParameters = new LinkedHashMap <>();
45+ protocol = "" ;
4546
4647
4748 if (url .contains ("://" )){
@@ -50,8 +51,11 @@ public URL(String url){
5051 }
5152 else {
5253 if (url .startsWith ("jdbc" )){
53- protocol = url .substring (0 , url .indexOf (";" ));
54- url = url .substring (url .indexOf (";" )+1 );
54+ int idx = url .indexOf (";" );
55+ if (idx >-1 ){
56+ protocol = url .substring (0 , idx );
57+ url = url .substring (idx +1 );
58+ }
5559 }
5660 }
5761
@@ -409,7 +413,13 @@ public String removeParameter(String key){
409413 }
410414 }
411415
412-
416+
417+ //**************************************************************************
418+ //** getParameter
419+ //**************************************************************************
420+ /** Returns values associated with a given key and parameter list. Performs
421+ * a case insensitive search for the given key.
422+ */
413423 public static List <String > getParameter (String key , HashMap <String , List <String >> parameters ){
414424 List <String > values = parameters .get (key );
415425 if (values ==null ){
@@ -422,18 +432,34 @@ public static List<String> getParameter(String key, HashMap<String, List<String>
422432 return values ;
423433 };
424434
435+
436+ //**************************************************************************
437+ //** setParameter
438+ //**************************************************************************
439+ /** Used to set or update a parameter value in a parameter list. Performs a
440+ * case insensitive search for the given key. When updating a value,
441+ * preserves the case of the matched key (e.g. given "foo", matches to
442+ * "Foo", parameters retains "Foo" not "foo").
443+ */
425444 public static void setParameter (String key , List <String > values , HashMap <String , List <String >> parameters ){
426445 Iterator <String > it = parameters .keySet ().iterator ();
427446 while (it .hasNext ()){
428447 String s = it .next ();
429448 if (s .equalsIgnoreCase (key )){
430- parameters .put (key , values );
449+ parameters .put (s , values );
431450 return ;
432451 }
433452 }
434453 parameters .put (key , values );
435454 }
436455
456+
457+ //**************************************************************************
458+ //** removeParameter
459+ //**************************************************************************
460+ /** Used to remove a parameter from a parameter list. Performs a case
461+ * insensitive search for the given key.
462+ */
437463 public static List <String > removeParameter (String key , HashMap <String , List <String >> parameters ){
438464 List <String > values = parameters .remove (key );
439465 if (values ==null ){
@@ -463,9 +489,11 @@ public String getHost(){
463489 /** Used to update the host name or IP address found in the URL.
464490 */
465491 public void setHost (String host ){
466- if (host .contains (":" )){
467- port = Integer .valueOf (host .substring (host .indexOf (":" )+1 ));
468- host = host .substring (0 , host .indexOf (":" ));
492+ int idx = host .indexOf (":" );
493+ if (idx >-1 ){
494+ try { port = Integer .valueOf (host .substring (idx +1 )); }
495+ catch (Exception e ){}
496+ host = host .substring (0 , idx );
469497 }
470498 this .host = host ;
471499 }
@@ -519,8 +547,7 @@ public String getProtocol(){
519547 public String getQueryString (){
520548
521549 StringBuilder str = new StringBuilder ();
522- HashSet <String > keys = getKeys ();
523- Iterator <String > it = keys .iterator ();
550+ Iterator <String > it = parameters .keySet ().iterator ();
524551 while (it .hasNext ()){
525552 String key = it .next ();
526553 List <String > values = getParameter (key , parameters );
0 commit comments