@@ -659,7 +659,7 @@ public static void encode(String s, Appendable dest) throws IOException {
659659 // special html characters
660660 dest .append ("&#" ).append ("" + (int ) c ).append (";" );
661661 } else if (c == ' ' ) {
662- // non breaking space
662+ // non- breaking space
663663 dest .append (" " );
664664 } else if (c == '\t' ) {
665665 dest .append (" " );
@@ -672,22 +672,6 @@ public static void encode(String s, Appendable dest) throws IOException {
672672 }
673673 }
674674
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-
691675 /**
692676 * Write out line information wrt. to the given annotation in the format:
693677 * {@code Linenumber Blame Author} incl. appropriate links.
@@ -951,8 +935,7 @@ public static void uriEncode(String str, Appendable dest) throws IOException {
951935 *
952936 * @param buf where to append the query string
953937 * @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.
956939 * @throws NullPointerException if the given buffer is {@code null}.
957940 * @see #uriEncode(String)
958941 */
@@ -1454,16 +1437,16 @@ private static String generatePageLink(int page, int offset, int limit, long siz
14541437
14551438
14561439 /**
1457- * Check if the string is a HTTP URL.
1440+ * Check if the string is an HTTP URL.
14581441 *
14591442 * @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
14611444 */
14621445 public static boolean isHttpUri (String string ) {
14631446 URL url ;
14641447 try {
1465- url = new URL (string );
1466- } catch (MalformedURLException ex ) {
1448+ url = new URI (string ). toURL ( );
1449+ } catch (MalformedURLException | URISyntaxException ex ) {
14671450 return false ;
14681451 }
14691452 return url .getProtocol ().equals ("http" ) || url .getProtocol ().equals ("https" );
@@ -1474,26 +1457,25 @@ public static boolean isHttpUri(String string) {
14741457 /**
14751458 * If given path is a URL, return the string representation with the user-info part filtered out.
14761459 * @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
14781462 */
14791463 public static String redactUrl (String path ) {
14801464 URL url ;
14811465 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 ) {
14851468 return path ;
14861469 }
14871470 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 );
14901472 } else {
14911473 return path ;
14921474 }
14931475 }
14941476
14951477 /**
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
14971479 * then it is returned as it was received. This has the same effect as
14981480 * invoking <code>linkify(url, true)</code>.
14991481 *
@@ -1507,7 +1489,7 @@ public static String linkify(String url) {
15071489 }
15081490
15091491 /**
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
15111493 * then it is returned as it was received.
15121494 *
15131495 * @param url the HTTP URL
@@ -1554,7 +1536,7 @@ public static String buildLink(String name, Map<String, String> attrs)
15541536 buffer .append ("=\" " );
15551537 String value = attr .getValue ();
15561538 if (attr .getKey ().equals ("href" )) {
1557- value = Util . encodeURL (value );
1539+ value = new URI (value ). toString ( );
15581540 }
15591541 buffer .append (value );
15601542 buffer .append ("\" " );
@@ -1609,11 +1591,12 @@ public static String buildLink(String name, String url, boolean newTab)
16091591 }
16101592
16111593 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 );
16131596 try {
16141597 StringBuilder stringBuilder = new StringBuilder ();
16151598 stringBuilder .append (text .substring (result .start (0 ), result .start (1 )));
1616- stringBuilder .append (buildLink (appendedUrl , appendedUrl , true ));
1599+ stringBuilder .append (buildLink (group1 , appendedUrl , true ));
16171600 stringBuilder .append (text .substring (result .end (1 ), result .end (0 )));
16181601 return stringBuilder .toString ();
16191602 } catch (URISyntaxException |MalformedURLException e ) {
@@ -1670,7 +1653,7 @@ public static String completeUrl(String url, HttpServletRequest req) {
16701653 }
16711654 return url ;
16721655 } catch (URISyntaxException ex ) {
1673- LOGGER .log (Level .INFO ,
1656+ LOGGER .log (Level .WARNING ,
16741657 String .format ("Unable to convert given URL part '%s' to complete URL" , url ),
16751658 ex );
16761659 return url ;
0 commit comments