1 package com.jsql.model.accessible;
2
3 import com.jsql.model.InjectionModel;
4 import com.jsql.view.subscriber.Seal;
5 import com.jsql.util.ConnectionUtil;
6 import com.jsql.util.LogLevelUtil;
7 import org.apache.commons.lang3.StringUtils;
8 import org.apache.logging.log4j.LogManager;
9 import org.apache.logging.log4j.Logger;
10
11 import java.net.URI;
12 import java.net.http.HttpRequest;
13 import java.net.http.HttpRequest.BodyPublishers;
14 import java.net.http.HttpResponse;
15 import java.net.http.HttpResponse.BodyHandlers;
16 import java.time.Duration;
17 import java.util.AbstractMap.SimpleEntry;
18 import java.util.Objects;
19 import java.util.Optional;
20 import java.util.concurrent.Callable;
21 import java.util.stream.Stream;
22
23
24
25
26
27 public class CallableHttpHead implements Callable<CallableHttpHead> {
28
29 private static final Logger LOGGER = LogManager.getRootLogger();
30
31
32
33
34 private final String urlAdminPage;
35
36
37
38
39 private String responseCodeHttp = StringUtils.EMPTY;
40
41 private final InjectionModel injectionModel;
42
43 private final String metadataInjectionProcess;
44
45
46
47
48
49 public CallableHttpHead(String urlAdminPage, InjectionModel injectionModel, String metadataInjectionProcess) {
50 this.urlAdminPage = urlAdminPage;
51 this.injectionModel= injectionModel;
52 this.metadataInjectionProcess= metadataInjectionProcess;
53 }
54
55
56
57
58 @Override
59 public CallableHttpHead call() {
60 if (this.injectionModel.getResourceAccess().isSearchAdminStopped()) {
61 return this;
62 }
63
64 try {
65 var builderHttpRequest = HttpRequest.newBuilder()
66 .uri(URI.create(this.urlAdminPage))
67 .method("HEAD", BodyPublishers.noBody())
68 .timeout(Duration.ofSeconds(4));
69
70 Stream.of(
71 this.injectionModel.getMediatorUtils().parameterUtil().getHeaderFromEntries().split("\\\\r\\\\n")
72 )
73 .map(e -> {
74 if (e.split(":").length == 2) {
75 return new SimpleEntry<>(
76 e.split(":")[0],
77 e.split(":")[1]
78 );
79 } else {
80 return null;
81 }
82 })
83 .filter(Objects::nonNull)
84 .forEach(e -> builderHttpRequest.header(e.getKey(), e.getValue()));
85
86 var httpRequest = builderHttpRequest.build();
87 var httpClient = this.injectionModel.getMediatorUtils().connectionUtil().getHttpClient()
88 .connectTimeout(Duration.ofSeconds(4))
89 .build();
90 HttpResponse<Void> response = httpClient.send(httpRequest, BodyHandlers.discarding());
91
92 this.responseCodeHttp = String.valueOf(response.statusCode());
93
94 this.injectionModel.sendToViews(new Seal.MessageHeader(
95 this.urlAdminPage,
96 null,
97 ConnectionUtil.getHeadersMap(httpRequest.headers()),
98 ConnectionUtil.getHeadersMap(response),
99 null,
100 null,
101 null,
102 this.metadataInjectionProcess,
103 null
104 ));
105 } catch (InterruptedException e) {
106 LOGGER.log(LogLevelUtil.IGNORE, e, e);
107 Thread.currentThread().interrupt();
108 } catch (Exception e) {
109 var eMessageImplicit = String.format(
110 "Problem connecting to %s (implicit reason): %s",
111 this.urlAdminPage,
112 InjectionModel.getImplicitReason(e)
113 );
114 String eMessage = Optional.ofNullable(e.getMessage()).orElse(eMessageImplicit);
115 LOGGER.log(LogLevelUtil.CONSOLE_ERROR, eMessage);
116 }
117
118 return this;
119 }
120
121
122
123
124
125
126 public boolean isHttpResponseOk() {
127 return this.responseCodeHttp.matches("[23]\\d\\d");
128 }
129
130
131
132
133 public String getUrl() {
134 return this.urlAdminPage;
135 }
136 }