11package org .togetherjava .tjbot .features .utils ;
22
3- import java .net .URI ;
4- import java .net .http .HttpClient ;
5- import java .net .http .HttpRequest ;
6- import java .net .http .HttpResponse ;
7- import java .util .concurrent .CompletableFuture ;
8-
93import com .linkedin .urls .Url ;
104import com .linkedin .urls .detection .UrlDetector ;
115import com .linkedin .urls .detection .UrlDetectorOptions ;
126
7+ import java .net .URI ;
8+ import java .net .http .HttpClient ;
9+ import java .net .http .HttpRequest ;
10+ import java .net .http .HttpResponse ;
1311import java .util .List ;
1412import java .util .Optional ;
1513import java .util .Set ;
14+ import java .util .concurrent .CompletableFuture ;
1615
1716/**
1817 * Utility class to detect links.
@@ -63,25 +62,21 @@ public static List<String> extractLinks(String content, Set<LinkFilter> filter)
6362 public static boolean containsLink (String content ) {
6463 return !(new UrlDetector (content , UrlDetectorOptions .BRACKET_MATCH ).detect ().isEmpty ());
6564 }
65+
6666 public static CompletableFuture <Boolean > isLinkBroken (String url ) {
6767 HttpClient client = HttpClient .newHttpClient ();
6868
6969 HttpRequest request = HttpRequest .newBuilder (URI .create (url ))
70- .method ("HEAD" , HttpRequest .BodyPublishers .noBody ())
71- .build ();
70+ .method ("HEAD" , HttpRequest .BodyPublishers .noBody ())
71+ .build ();
7272
7373 return client .sendAsync (request , HttpResponse .BodyHandlers .discarding ())
74- .thenApply (response -> response .statusCode () >= 400 )
75- .exceptionally (ignored -> true );
74+ .thenApply (response -> response .statusCode () >= 400 )
75+ .exceptionally (ignored -> true );
7676 }
77- public static CompletableFuture <String > replaceDeadLinks (
78- String text ,
79- String replacement
80- ) {
81- Set <LinkFilter > filters = Set .of (
82- LinkFilter .SUPPRESSED ,
83- LinkFilter .NON_HTTP_SCHEME
84- );
77+
78+ public static CompletableFuture <String > replaceDeadLinks (String text , String replacement ) {
79+ Set <LinkFilter > filters = Set .of (LinkFilter .SUPPRESSED , LinkFilter .NON_HTTP_SCHEME );
8580
8681 List <String > links = extractLinks (text , filters );
8782
@@ -91,25 +86,18 @@ public static CompletableFuture<String> replaceDeadLinks(
9186
9287 StringBuilder result = new StringBuilder (text );
9388
94- List <CompletableFuture <Void >> checks = links .stream ()
95- .map (link ->
96- isLinkBroken (link ).thenAccept (isDead -> {
97- if (isDead ) {
98- int index = result .indexOf (link );
99- if (index != -1 ) {
100- result .replace (
101- index ,
102- index + link .length (),
103- replacement
104- );
105- }
106- }
107- })
108- )
109- .toList ();
89+ List <CompletableFuture <Void >> checks =
90+ links .stream ().map (link -> isLinkBroken (link ).thenAccept (isDead -> {
91+ if (isDead ) {
92+ int index = result .indexOf (link );
93+ if (index != -1 ) {
94+ result .replace (index , index + link .length (), replacement );
95+ }
96+ }
97+ })).toList ();
11098
11199 return CompletableFuture .allOf (checks .toArray (new CompletableFuture [0 ]))
112- .thenApply (v -> result .toString ());
100+ .thenApply (v -> result .toString ());
113101 }
114102
115103 private static Optional <String > toLink (Url url , Set <LinkFilter > filter ) {
0 commit comments