|
25 | 25 | import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; |
26 | 26 |
|
27 | 27 | import java.io.IOException; |
| 28 | +import java.io.OutputStream; |
28 | 29 | import java.net.URI; |
29 | 30 | import java.net.http.HttpClient; |
30 | 31 | import java.net.http.HttpRequest; |
@@ -95,41 +96,54 @@ private StreamingResponseBody processStreamResponse(HttpRequest.Builder requestB |
95 | 96 | return outputStream -> { |
96 | 97 | try { |
97 | 98 | HttpResponse<Stream<String>> response = httpClient.send( |
98 | | - requestBuilder.build(), HttpResponse.BodyHandlers.ofLines()); |
99 | | - try (Stream<String> lines = response.body()) { |
100 | | - lines.filter(line -> !line.isEmpty()) |
101 | | - .forEach(line -> { |
102 | | - try { |
103 | | - if (!line.startsWith("data:")) { |
104 | | - line = "data: " + line; |
105 | | - } |
106 | | - if (!line.endsWith("\n\n")) { |
107 | | - line = line + "\n\n"; |
108 | | - } |
109 | | - outputStream.write(line.getBytes(StandardCharsets.UTF_8)); |
110 | | - outputStream.flush(); |
111 | | - } catch (IOException e) { |
112 | | - throw new ServiceException(ExceptionEnum.CM326.getResultCode(), |
113 | | - ExceptionEnum.CM326.getResultMsg()); |
114 | | - } |
115 | | - }); |
116 | | - } |
| 99 | + requestBuilder.build(), HttpResponse.BodyHandlers.ofLines()); |
| 100 | + processLines(response.body(), outputStream); |
117 | 101 | } catch (Exception e) { |
118 | | - try { |
119 | | - String errorEvent = "data: " + |
120 | | - JsonUtils.encode(Map.of("error", e.getMessage())) + "\n\n"; |
121 | | - outputStream.write(errorEvent.getBytes(StandardCharsets.UTF_8)); |
122 | | - outputStream.flush(); |
123 | | - } catch (IOException ioException) { |
124 | | - throw new ServiceException(ExceptionEnum.CM326.getResultCode(), ExceptionEnum.CM326.getResultMsg()); |
125 | | - } |
| 102 | + handleError(e, outputStream); |
126 | 103 | } finally { |
127 | | - try { |
128 | | - outputStream.close(); |
129 | | - } catch (IOException e) { |
130 | | - // 忽略关闭异常 |
131 | | - } |
| 104 | + closeStream(outputStream); |
132 | 105 | } |
133 | 106 | }; |
134 | 107 | } |
| 108 | + |
| 109 | + private void processLines(Stream<String> lines, OutputStream outputStream) { |
| 110 | + try (Stream<String> filteredLines = lines.filter(line -> !line.isEmpty())) { |
| 111 | + filteredLines.forEach(line -> writeLine(line, outputStream)); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private void writeLine(String line, OutputStream outputStream) { |
| 116 | + try { |
| 117 | + if (!line.startsWith("data:")) { |
| 118 | + line = "data: " + line; |
| 119 | + } |
| 120 | + if (!line.endsWith("\n\n")) { |
| 121 | + line = line + "\n\n"; |
| 122 | + } |
| 123 | + outputStream.write(line.getBytes(StandardCharsets.UTF_8)); |
| 124 | + outputStream.flush(); |
| 125 | + } catch (IOException e) { |
| 126 | + throw new ServiceException(ExceptionEnum.CM326.getResultCode(), |
| 127 | + ExceptionEnum.CM326.getResultMsg()); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private void handleError(Exception e, OutputStream outputStream) { |
| 132 | + try { |
| 133 | + String errorEvent = "data: " + JsonUtils.encode(Map.of("error", e.getMessage())) + "\n\n"; |
| 134 | + outputStream.write(errorEvent.getBytes(StandardCharsets.UTF_8)); |
| 135 | + outputStream.flush(); |
| 136 | + } catch (IOException ioException) { |
| 137 | + throw new ServiceException(ExceptionEnum.CM326.getResultCode(), ExceptionEnum.CM326.getResultMsg()); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private void closeStream(OutputStream outputStream) { |
| 142 | + try { |
| 143 | + outputStream.close(); |
| 144 | + } catch (IOException e) { |
| 145 | + // 忽略关闭异常 |
| 146 | + } |
| 147 | + } |
| 148 | + |
135 | 149 | } |
0 commit comments