Package: ResponseEntityIo
ResponseEntityIo
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ResponseEntityIo() |
|
|
|
|
|
||||||||||||||||||||
lambda$store$0(List, Map.Entry) |
|
|
|
|
|
||||||||||||||||||||
lambda$store$1(PrintWriter, Map.Entry) |
|
|
|
|
|
||||||||||||||||||||
load(Path) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
store(Path, ResponseEntity, List) |
|
|
|
|
|
||||||||||||||||||||
store(Path, ResponseEntity, List, Function) |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.rest;
28:
29: import javax.annotation.Nonnull;
30: import java.util.List;
31: import java.util.Map;
32: import java.util.Optional;
33: import java.util.function.Function;
34: import java.io.ByteArrayOutputStream;
35: import java.io.IOException;
36: import java.io.OutputStreamWriter;
37: import java.io.PrintWriter;
38: import java.nio.file.Files;
39: import java.nio.file.Path;
40: import org.springframework.http.HttpStatus;
41: import org.springframework.http.ResponseEntity;
42: import org.springframework.util.LinkedMultiValueMap;
43: import org.springframework.util.MultiValueMap;
44: import lombok.extern.slf4j.Slf4j;
45: import static java.util.Comparator.*;
46: import static java.util.stream.Collectors.*;
47: import static java.nio.charset.StandardCharsets.UTF_8;
48:
49: /***********************************************************************************************************************
50: *
51: * @author Fabrizio Giudici
52: *
53: **********************************************************************************************************************/
54: @Slf4j
55: public class ResponseEntityIo
56: {
57: /*******************************************************************************************************************
58: *
59: *
60: ******************************************************************************************************************/
61: public static void store (@Nonnull final Path path,
62: @Nonnull final ResponseEntity<?> response,
63: @Nonnull final List<String> ignoredHeaders)
64: {
65: store(path, response, ignoredHeaders, Function.identity());
66: }
67:
68: /*******************************************************************************************************************
69: *
70: *
71: ******************************************************************************************************************/
72: public static void store (@Nonnull final Path path,
73: @Nonnull final ResponseEntity<?> response,
74: @Nonnull final List<String> ignoredHeaders,
75: @Nonnull final Function<String, String> postProcessor)
76: {
77: try
78: {
79: log.trace("store({}, ..., ...)", path);
80:
81: Files.createDirectories(path.getParent());
82: final ByteArrayOutputStream baos = new ByteArrayOutputStream();
83:
84: try (final PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, UTF_8)))
85: {
86: pw.printf("HTTP/1.1 %d %s%n", response.getStatusCode().value(), response.getStatusCode().name());
87: response.getHeaders().entrySet().stream()
88:• .filter(e -> !ignoredHeaders.contains(e.getKey()))
89: .sorted(comparing(Map.Entry::getKey))
90: .forEach(e -> pw.printf("%s: %s%n", e.getKey(), e.getValue().get(0)));
91: pw.println();
92: pw.flush();
93: final Object body = response.getBody();
94: log.info(">>>> TYPE {}", body.getClass());
95:
96:• if (body instanceof String)
97: {
98: pw.print(postProcessor.apply((String)body));
99: }
100: else
101: {
102: baos.write((byte[])body);
103: }
104: }
105:
106: Files.write(path, baos.toByteArray());
107: }
108: catch (IOException e)
109: {
110: log.error("Coundln't store a cache item {}", path);
111: }
112: }
113:
114: /*******************************************************************************************************************
115: *
116: *
117: ******************************************************************************************************************/
118: @Nonnull
119: /* package */ static Optional<ResponseEntity<String>> load (@Nonnull final Path path)
120: throws IOException
121: {
122: log.trace("load({})", path);
123:
124:• if (!Files.exists(path))
125: {
126: return Optional.empty();
127: }
128:
129: final List<String> lines = Files.readAllLines(path, UTF_8);
130: final HttpStatus status = HttpStatus.valueOf(Integer.parseInt(lines.get(0).split(" ")[1]));
131: final MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
132:
133: int i = 1;
134:
135:• for (; (i < lines.size()) && !lines.get(i).equals(""); i++)
136: {
137: final String[] split = lines.get(i).split(":");
138: headers.add(split[0], split[1].trim());
139: }
140:
141: final String body = lines.stream().skip(i + 1).collect(joining("\n"));
142: final ResponseEntity<String> response = new ResponseEntity<>(body, headers, status);
143: // log.trace(">>>> returning {}", response);
144:
145: return Optional.of(response);
146: }
147: }