@@ -659,7 +659,7 @@ public static void encode(String s, Appendable dest) throws IOException {
659
659
// special html characters
660
660
dest .append ("&#" ).append ("" + (int ) c ).append (";" );
661
661
} else if (c == ' ' ) {
662
- // non breaking space
662
+ // non- breaking space
663
663
dest .append (" " );
664
664
} else if (c == '\t' ) {
665
665
dest .append (" " );
@@ -672,22 +672,6 @@ public static void encode(String s, Appendable dest) throws IOException {
672
672
}
673
673
}
674
674
675
- /**
676
- * Encode URL.
677
- *
678
- * @param urlStr string URL
679
- * @return the encoded URL
680
- * @throws URISyntaxException URI syntax
681
- * @throws MalformedURLException URL malformed
682
- */
683
- public static String encodeURL (String urlStr ) throws URISyntaxException , MalformedURLException {
684
- URL url = new URL (urlStr );
685
- URI constructed = new URI (url .getProtocol (), url .getUserInfo (),
686
- url .getHost (), url .getPort (),
687
- url .getPath (), url .getQuery (), url .getRef ());
688
- return constructed .toString ();
689
- }
690
-
691
675
/**
692
676
* Write out line information wrt. to the given annotation in the format:
693
677
* {@code Linenumber Blame Author} incl. appropriate links.
@@ -951,8 +935,7 @@ public static void uriEncode(String str, Appendable dest) throws IOException {
951
935
*
952
936
* @param buf where to append the query string
953
937
* @param key the name of the parameter to add. Append as is!
954
- * @param value the value for the given parameter. Gets automatically UTF-8
955
- * URL encoded.
938
+ * @param value the value for the given parameter. Gets automatically UTF-8 URL encoded.
956
939
* @throws NullPointerException if the given buffer is {@code null}.
957
940
* @see #uriEncode(String)
958
941
*/
@@ -1454,16 +1437,16 @@ private static String generatePageLink(int page, int offset, int limit, long siz
1454
1437
1455
1438
1456
1439
/**
1457
- * Check if the string is a HTTP URL.
1440
+ * Check if the string is an HTTP URL.
1458
1441
*
1459
1442
* @param string the string to check
1460
- * @return true if it is http URL, false otherwise
1443
+ * @return true if it is HTTP URL, false otherwise
1461
1444
*/
1462
1445
public static boolean isHttpUri (String string ) {
1463
1446
URL url ;
1464
1447
try {
1465
- url = new URL (string );
1466
- } catch (MalformedURLException ex ) {
1448
+ url = new URI (string ). toURL ( );
1449
+ } catch (MalformedURLException | URISyntaxException ex ) {
1467
1450
return false ;
1468
1451
}
1469
1452
return url .getProtocol ().equals ("http" ) || url .getProtocol ().equals ("https" );
@@ -1474,26 +1457,25 @@ public static boolean isHttpUri(String string) {
1474
1457
/**
1475
1458
* If given path is a URL, return the string representation with the user-info part filtered out.
1476
1459
* @param path path to object
1477
- * @return either the original string or string representation of URL with the user-info part removed
1460
+ * @return either the original string (if the URL is not valid)
1461
+ * or string representation of the URL with the user-info part removed
1478
1462
*/
1479
1463
public static String redactUrl (String path ) {
1480
1464
URL url ;
1481
1465
try {
1482
- url = new URL (path );
1483
- } catch (MalformedURLException e ) {
1484
- // not an URL
1466
+ url = new URI (path ).toURL ();
1467
+ } catch (MalformedURLException | URISyntaxException e ) {
1485
1468
return path ;
1486
1469
}
1487
1470
if (url .getUserInfo () != null ) {
1488
- return url .toString ().replace (url .getUserInfo (),
1489
- REDACTED_USER_INFO );
1471
+ return url .toString ().replace (url .getUserInfo (), REDACTED_USER_INFO );
1490
1472
} else {
1491
1473
return path ;
1492
1474
}
1493
1475
}
1494
1476
1495
1477
/**
1496
- * Build a HTML link to the given HTTP URL. If the URL is not an http URL
1478
+ * Build an HTML link to the given HTTP URL. If the URL is not a HTTP URL
1497
1479
* then it is returned as it was received. This has the same effect as
1498
1480
* invoking <code>linkify(url, true)</code>.
1499
1481
*
@@ -1507,7 +1489,7 @@ public static String linkify(String url) {
1507
1489
}
1508
1490
1509
1491
/**
1510
- * Build a html link to the given http URL. If the URL is not an http URL
1492
+ * Build an HTML link to the given http URL. If the URL is not a HTTP URL
1511
1493
* then it is returned as it was received.
1512
1494
*
1513
1495
* @param url the HTTP URL
@@ -1554,7 +1536,7 @@ public static String buildLink(String name, Map<String, String> attrs)
1554
1536
buffer .append ("=\" " );
1555
1537
String value = attr .getValue ();
1556
1538
if (attr .getKey ().equals ("href" )) {
1557
- value = Util . encodeURL (value );
1539
+ value = new URI (value ). toString ( );
1558
1540
}
1559
1541
buffer .append (value );
1560
1542
buffer .append ("\" " );
@@ -1609,11 +1591,12 @@ public static String buildLink(String name, String url, boolean newTab)
1609
1591
}
1610
1592
1611
1593
private static String buildLinkReplacer (MatchResult result , String text , String url ) {
1612
- final String appendedUrl = url + result .group (1 );
1594
+ final String group1 = result .group (1 );
1595
+ final String appendedUrl = url + uriEncode (group1 );
1613
1596
try {
1614
1597
StringBuilder stringBuilder = new StringBuilder ();
1615
1598
stringBuilder .append (text .substring (result .start (0 ), result .start (1 )));
1616
- stringBuilder .append (buildLink (appendedUrl , appendedUrl , true ));
1599
+ stringBuilder .append (buildLink (group1 , appendedUrl , true ));
1617
1600
stringBuilder .append (text .substring (result .end (1 ), result .end (0 )));
1618
1601
return stringBuilder .toString ();
1619
1602
} catch (URISyntaxException |MalformedURLException e ) {
@@ -1670,7 +1653,7 @@ public static String completeUrl(String url, HttpServletRequest req) {
1670
1653
}
1671
1654
return url ;
1672
1655
} catch (URISyntaxException ex ) {
1673
- LOGGER .log (Level .INFO ,
1656
+ LOGGER .log (Level .WARNING ,
1674
1657
String .format ("Unable to convert given URL part '%s' to complete URL" , url ),
1675
1658
ex );
1676
1659
return url ;
0 commit comments