10
10
import java .util .Objects ;
11
11
12
12
import static com .bobocode .HTTPGet .parseImageUrls ;
13
+ import static java .lang .Long .parseLong ;
13
14
import static java .net .http .HttpRequest .BodyPublishers .noBody ;
14
15
import static java .util .Comparator .comparingLong ;
16
+ import static java .util .Objects .nonNull ;
17
+ import static java .util .Objects .requireNonNull ;
15
18
16
19
public class HM12 {
17
20
@@ -22,33 +25,29 @@ public static void main(String[] args) {
22
25
parseImageUrls (get (link ).body ())
23
26
.parallelStream ()
24
27
.map (HM12 ::get )
25
- .filter (res -> Objects . nonNull (getHeader (res , "Location" )))
28
+ .filter (res -> nonNull (getHeader (res , "Location" )))
26
29
.map (res -> getHeader (res , "Location" ))
27
30
.map (HM12 ::head )
28
- .filter (res -> Objects .nonNull (getHeader (res , "Content-Length" )))
29
- .max (comparingLong (res -> Long .parseLong (getHeader (res , "Content-Length" ))))
31
+ .max (comparingLong (res -> parseLong (requireNonNull (getHeader (res , "Content-Length" )))))
30
32
.ifPresent (res -> System .out .printf ("%s - length: %s" , res .uri (), getHeader (res , "Content-Length" )));
31
33
}
32
34
33
35
@ SneakyThrows
34
- public static HttpResponse <String > get (String link ) {
35
- return exchange (link , "GET" , noBody ());
36
- }
37
-
38
- @ SneakyThrows
39
- public static HttpResponse <String > head (String link ) {
40
- return exchange (link , "HEAD" , noBody ());
41
- }
42
-
43
- @ SneakyThrows
44
- public static HttpResponse <String > exchange (String link , String method , HttpRequest .BodyPublisher bodyPublisher ) {
36
+ private static HttpResponse <String > exchange (String link , String method , HttpRequest .BodyPublisher bodyPublisher ) {
45
37
var request = HttpRequest .newBuilder (new URI (link )).method (method , bodyPublisher ).build ();
46
38
return httpClient .send (request , HttpResponse .BodyHandlers .ofString ());
47
39
}
48
40
49
41
private static String getHeader (HttpResponse <String > response , String header ) {
50
- Objects .requireNonNull (response );
51
42
List <String > values = response .headers ().map ().get (header );
52
43
return values == null || values .isEmpty () ? null : values .get (0 );
53
44
}
45
+
46
+ private static HttpResponse <String > get (String link ) {
47
+ return exchange (link , "GET" , noBody ());
48
+ }
49
+
50
+ private static HttpResponse <String > head (String link ) {
51
+ return exchange (link , "HEAD" , noBody ());
52
+ }
54
53
}
0 commit comments