1 |
|
package net.technearts.rip; |
2 |
|
|
3 |
|
import static java.nio.charset.StandardCharsets.UTF_8; |
4 |
|
import static java.util.Arrays.asList; |
5 |
|
import static java.util.Optional.ofNullable; |
6 |
|
import static net.technearts.rip.OP.AND; |
7 |
|
import static net.technearts.rip.OP.OR; |
8 |
|
import static org.eclipse.jetty.http.HttpStatus.NOT_FOUND_404; |
9 |
|
import static org.eclipse.jetty.http.HttpStatus.OK_200; |
10 |
|
|
11 |
|
import java.io.IOException; |
12 |
|
import java.nio.file.Files; |
13 |
|
import java.nio.file.Path; |
14 |
|
import java.util.Arrays; |
15 |
|
import java.util.HashMap; |
16 |
|
import java.util.LinkedHashMap; |
17 |
|
import java.util.Map; |
18 |
|
import java.util.Optional; |
19 |
|
import java.util.function.BiFunction; |
20 |
|
import java.util.function.Consumer; |
21 |
|
import java.util.function.Function; |
22 |
|
import java.util.function.Predicate; |
23 |
|
|
24 |
|
import org.slf4j.Logger; |
25 |
|
import org.slf4j.LoggerFactory; |
26 |
|
|
27 |
|
import net.sf.jmimemagic.Magic; |
28 |
|
import net.sf.jmimemagic.MagicException; |
29 |
|
import net.sf.jmimemagic.MagicMatch; |
30 |
|
import net.sf.jmimemagic.MagicMatchNotFoundException; |
31 |
|
import net.sf.jmimemagic.MagicParseException; |
32 |
|
import spark.ModelAndView; |
33 |
|
import spark.Request; |
34 |
|
import spark.Response; |
35 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
36 |
|
enum OP { |
37 |
|
AND, OR; |
38 |
|
} |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
|
|
| 71.9% |
Uncovered Elements: 43 (153) |
Complexity: 40 |
Complexity Density: 0.37 |
|
43 |
|
public class RipResponseBuilder { |
44 |
|
|
45 |
|
private static final Map<RipRoute, Map<Predicate<Request>, RipResponse>> CONDITIONS = new LinkedHashMap<>(); |
46 |
|
private static final Map<RipRoute, BiFunction<Request, Response, String>> LOGS = new LinkedHashMap<>(); |
47 |
|
private static final Logger LOG = LoggerFactory |
48 |
|
.getLogger(RipResponseBuilder.class); |
49 |
|
|
50 |
|
private RipRoute route; |
51 |
|
private Predicate<Request> condition; |
52 |
|
private OP op = AND; |
53 |
|
|
|
|
| 76.5% |
Uncovered Elements: 12 (51) |
Complexity: 6 |
Complexity Density: 0.15 |
|
54 |
9 |
RipResponseBuilder(final RipRoute route) {... |
55 |
9 |
LOG.info("Criando RipResponseBuilder para requisição {} {}", |
56 |
|
route.getMethod(), route.getPath()); |
57 |
9 |
this.route = route; |
58 |
9 |
if (!CONDITIONS.containsKey(route)) { |
59 |
5 |
CONDITIONS.put(route, |
60 |
|
new LinkedHashMap<Predicate<Request>, RipResponse>()); |
61 |
|
} |
62 |
|
|
63 |
9 |
route.route = (req, res) -> { |
64 |
3871 |
final Optional<Map.Entry<Predicate<Request>, RipResponse>> optional = CONDITIONS |
65 |
|
.get(route).entrySet().stream() |
66 |
|
.filter(entry -> entry.getKey().test(req)).findFirst(); |
67 |
3969 |
RipResponse response; |
68 |
3985 |
String result; |
69 |
3902 |
if (optional.isPresent()) { |
70 |
3943 |
response = optional.get().getValue(); |
71 |
3930 |
LOG.debug("Requisição para {}:\n{}", req.pathInfo(), req.body()); |
72 |
3946 |
LOG.debug("Respondendo com \n{}", response.getContent()); |
73 |
3915 |
res.status(response.getStatus()); |
74 |
3932 |
result = response.getContent(); |
75 |
3917 |
if (response.getContentType() == null) { |
76 |
3950 |
res.header("Content-Type", contentType(result.getBytes(UTF_8))); |
77 |
|
} else { |
78 |
0 |
res.header("Content-Type", response.getContentType()); |
79 |
|
} |
80 |
|
} else { |
81 |
0 |
res.status(NOT_FOUND_404); |
82 |
0 |
LOG.warn("Resposta para {} {} não encontrada", route.getMethod(), |
83 |
|
route.getPath()); |
84 |
0 |
result = ""; |
85 |
|
} |
86 |
3937 |
ofNullable(LOGS.get(route)).ifPresent(f -> f.apply(req, res)); |
87 |
3907 |
return result; |
88 |
|
}; |
89 |
9 |
route.templateRoute = (req, res) -> { |
90 |
1 |
final Optional<Map.Entry<Predicate<Request>, RipResponse>> optional = CONDITIONS |
91 |
|
.get(route).entrySet().stream() |
92 |
|
.filter(entry -> entry.getKey().test(req)).findFirst(); |
93 |
1 |
RipResponse response; |
94 |
1 |
ModelAndView result; |
95 |
1 |
if (optional.isPresent()) { |
96 |
1 |
response = optional.get().getValue(); |
97 |
1 |
LOG.debug("Respondendo com \n{}", response.getContent()); |
98 |
1 |
res.status(response.getStatus()); |
99 |
1 |
final Map<String, Object> attributes = new HashMap<>(); |
100 |
1 |
for (final Map.Entry<String, Function<Request, String>> f : response |
101 |
|
.getAttributes().entrySet()) { |
102 |
2 |
attributes.put(f.getKey(), f.getValue().apply(req)); |
103 |
|
} |
104 |
1 |
if (response.getContentType() != null) { |
105 |
0 |
res.header("Content-Type", response.getContentType()); |
106 |
|
} else { |
107 |
1 |
res.header("Content-Type", "text/plain"); |
108 |
|
} |
109 |
1 |
result = new ModelAndView(attributes, response.getContent()); |
110 |
|
} else { |
111 |
0 |
res.status(NOT_FOUND_404); |
112 |
0 |
LOG.debug("Resposta para {} {} não encontrada", route.getMethod(), |
113 |
|
route.getPath()); |
114 |
0 |
result = null; |
115 |
|
} |
116 |
1 |
return result; |
117 |
|
}; |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
@return |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
125 |
1 |
public RipResponseBuilder and() {... |
126 |
1 |
op = AND; |
127 |
1 |
return this; |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
@param |
137 |
|
@param |
138 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
139 |
0 |
@SafeVarargs... |
140 |
|
public final void buildResponse(final String template, |
141 |
|
final Consumer<Map<String, Function<Request, String>>>... consumers) { |
142 |
0 |
buildResponse(template, OK_200, consumers); |
143 |
|
} |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
145 |
1 |
@SafeVarargs... |
146 |
|
public final void buildResponse(final String template, final int status, |
147 |
|
final Consumer<Map<String, Function<Request, String>>>... consumers) { |
148 |
1 |
buildResponse(template, status, null, consumers); |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
@param |
158 |
|
@param |
159 |
|
@param |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
161 |
1 |
@SafeVarargs... |
162 |
|
public final void buildResponse(final String template, final int status, |
163 |
|
final String contentType, |
164 |
|
final Consumer<Map<String, Function<Request, String>>>... consumers) { |
165 |
1 |
final Map<String, Function<Request, String>> attributes = new HashMap<>(); |
166 |
1 |
for (final Consumer<Map<String, Function<Request, String>>> consumer : consumers) { |
167 |
2 |
consumer.accept(attributes); |
168 |
|
} |
169 |
1 |
buildResponse(template, status, contentType, attributes); |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
@param |
177 |
|
@param |
178 |
|
@param |
179 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
180 |
1 |
public final void buildResponse(final String template, final int status,... |
181 |
|
final String contentType, |
182 |
|
final Map<String, Function<Request, String>> attributes) { |
183 |
1 |
if (condition == null) { |
184 |
1 |
condition = s -> true; |
185 |
|
} |
186 |
1 |
final RipResponse res = new RipResponse(attributes, template, status, |
187 |
|
contentType); |
188 |
1 |
CONDITIONS.get(route).put(condition, res); |
189 |
1 |
route.createTemplateMethod(); |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
@param |
197 |
|
@param |
198 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
199 |
0 |
public final void buildResponse(final String template,... |
200 |
|
final Map<String, Function<Request, String>> attributes) { |
201 |
0 |
buildResponse(template, OK_200, null, attributes); |
202 |
|
} |
203 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
204 |
0 |
@SafeVarargs... |
205 |
|
public final void buildResponse(final String template, |
206 |
|
final String contentType, |
207 |
|
final Consumer<Map<String, Function<Request, String>>>... consumers) { |
208 |
0 |
buildResponse(template, OK_200, contentType, consumers); |
209 |
|
} |
210 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
0 |
public final void buildResponse(final String template,... |
212 |
|
final String contentType, |
213 |
|
final Map<String, Function<Request, String>> attributes) { |
214 |
0 |
buildResponse(template, OK_200, contentType, attributes); |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
@param |
221 |
|
@return |
222 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
223 |
5 |
public RipResponseBuilder contains(final String content) {... |
224 |
5 |
final Predicate<Request> newCondition = req -> req.body().contains(content); |
225 |
5 |
updateConditions(newCondition); |
226 |
5 |
return this; |
227 |
|
} |
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
@param |
233 |
|
@return |
234 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
235 |
1 |
public RipResponseBuilder containsAll(final String... contents) {... |
236 |
1 |
final Predicate<Request> newCondition = req -> Arrays.asList(contents) |
237 |
|
.stream().allMatch(req.body()::contains); |
238 |
1 |
updateConditions(newCondition); |
239 |
1 |
return this; |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
@param |
247 |
|
@return |
248 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
249 |
1 |
public RipResponseBuilder containsAny(final String... contents) {... |
250 |
1 |
final Predicate<Request> newCondition = req -> Arrays.asList(contents) |
251 |
|
.stream().anyMatch(req.body()::contains); |
252 |
1 |
updateConditions(newCondition); |
253 |
1 |
return this; |
254 |
|
} |
255 |
|
|
|
|
| 66.7% |
Uncovered Elements: 3 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
256 |
3935 |
private String contentType(final byte[] stream) {... |
257 |
3929 |
MagicMatch match = null; |
258 |
3955 |
try { |
259 |
3975 |
match = Magic.getMagicMatch(stream, false); |
260 |
3937 |
if (match.getSubMatches().size() > 0) { |
261 |
0 |
return match.getSubMatches().toArray()[0].toString(); |
262 |
|
} |
263 |
|
} catch (MagicParseException | MagicMatchNotFoundException | MagicException |
264 |
|
| NullPointerException e) { |
265 |
0 |
return "text/html;charset=utf-8"; |
266 |
|
} |
267 |
3973 |
return match.getMimeType(); |
268 |
|
} |
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
@param |
275 |
|
@return |
276 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
277 |
0 |
public RipResponseBuilder log(final BiFunction<Request, Response, String> f) {... |
278 |
0 |
LOGS.put(route, f); |
279 |
0 |
return this; |
280 |
|
} |
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
@param |
286 |
|
@return |
287 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
288 |
0 |
public RipResponseBuilder matches(final Predicate<Request> condition) {... |
289 |
0 |
updateConditions(condition); |
290 |
0 |
return this; |
291 |
|
} |
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
@param |
297 |
|
@return |
298 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
299 |
0 |
public RipResponseBuilder matchesAll(... |
300 |
|
@SuppressWarnings("unchecked") final Predicate<Request>... conditions) { |
301 |
0 |
final Predicate<Request> newCondition = asList(conditions).stream() |
302 |
|
.reduce(req -> true, Predicate::and); |
303 |
0 |
updateConditions(newCondition); |
304 |
0 |
return this; |
305 |
|
} |
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
@param |
312 |
|
@return |
313 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
314 |
0 |
public RipResponseBuilder matchesAny(... |
315 |
|
@SuppressWarnings("unchecked") final Predicate<Request>... conditions) { |
316 |
0 |
final Predicate<Request> newCondition = asList(conditions).stream() |
317 |
|
.reduce(req -> false, Predicate::or); |
318 |
0 |
updateConditions(newCondition); |
319 |
0 |
return this; |
320 |
|
} |
321 |
|
|
322 |
|
|
323 |
|
|
324 |
|
|
325 |
|
@return |
326 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
327 |
1 |
public RipResponseBuilder or() {... |
328 |
1 |
op = OR; |
329 |
1 |
return this; |
330 |
|
} |
331 |
|
|
332 |
|
|
333 |
|
|
334 |
|
|
335 |
|
|
336 |
|
@param |
337 |
|
|
338 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
339 |
2 |
public void respond(final Path withFile) {... |
340 |
2 |
respond(withFile, OK_200); |
341 |
|
} |
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
|
346 |
|
|
347 |
|
@param |
348 |
|
|
349 |
|
@param |
350 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
351 |
2 |
public void respond(final Path withFile, final int status) {... |
352 |
2 |
respond(withFile, status, null); |
353 |
|
} |
354 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
355 |
2 |
public void respond(final Path withFile, final int status,... |
356 |
|
final String contentType) { |
357 |
2 |
try { |
358 |
2 |
respond(new String(Files.readAllBytes(withFile)), status, contentType); |
359 |
|
} catch (final IOException e) { |
360 |
0 |
respond("Arquivo não encontrado.", NOT_FOUND_404); |
361 |
|
} |
362 |
|
} |
363 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
364 |
0 |
public void respond(final Path withFile, final String contentType) {... |
365 |
0 |
respond(withFile, OK_200, contentType); |
366 |
|
} |
367 |
|
|
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
@param |
373 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
374 |
4 |
public void respond(final String response) {... |
375 |
4 |
respond(response, OK_200); |
376 |
|
} |
377 |
|
|
378 |
|
|
379 |
|
|
380 |
|
|
381 |
|
|
382 |
|
@param |
383 |
|
@param |
384 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
385 |
6 |
public void respond(final String response, final int status) {... |
386 |
6 |
respond(response, status, null); |
387 |
|
} |
388 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
389 |
8 |
public void respond(final String response, final int status,... |
390 |
|
final String contentType) { |
391 |
8 |
if (condition == null) { |
392 |
3 |
condition = s -> true; |
393 |
|
} |
394 |
8 |
final RipResponse res = new RipResponse(response, status, contentType); |
395 |
8 |
CONDITIONS.get(route).put(condition, res); |
396 |
8 |
route.createMethod(); |
397 |
|
} |
398 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
399 |
0 |
public void respond(final String response, final String contentType) {... |
400 |
0 |
respond(response, OK_200, contentType); |
401 |
|
} |
402 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 4 |
Complexity Density: 0.44 |
|
403 |
7 |
private void updateConditions(final Predicate<Request> newCondition) {... |
404 |
7 |
if (condition == null) { |
405 |
5 |
condition = newCondition; |
406 |
|
} else { |
407 |
2 |
switch (op) { |
408 |
1 |
case OR: |
409 |
1 |
condition = condition.or(newCondition); |
410 |
1 |
break; |
411 |
1 |
case AND: |
412 |
1 |
condition = condition.and(newCondition); |
413 |
1 |
break; |
414 |
|
} |
415 |
|
} |
416 |
|
} |
417 |
|
|
418 |
|
} |