|
22 | 22 | import org.springframework.http.HttpMethod; |
23 | 23 | import org.springframework.http.MediaType; |
24 | 24 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| 25 | +import org.springframework.web.client.RequestCallback; |
| 26 | +import org.springframework.web.client.ResponseExtractor; |
25 | 27 | import org.springframework.web.client.RestTemplate; |
26 | 28 |
|
27 | 29 | import javax.naming.InvalidNameException; |
|
40 | 42 | import java.security.cert.Certificate; |
41 | 43 | import java.security.cert.CertificateParsingException; |
42 | 44 | import java.security.cert.X509Certificate; |
43 | | -import java.util.ArrayList; |
44 | | -import java.util.Collection; |
45 | | -import java.util.List; |
46 | | -import java.util.Map; |
| 45 | +import java.util.*; |
47 | 46 |
|
48 | 47 | public class CollectorImpl implements Collector { |
49 | 48 | private DatabaseClient client = null; |
@@ -118,18 +117,22 @@ public DiskQueue<String> run(String jobId, String entity, String flow, int threa |
118 | 117 | uriString += "&options=" + URLEncoder.encode(objectMapper.writeValueAsString(options), "UTF-8"); |
119 | 118 | } |
120 | 119 | URI uri = new URI(uriString); |
121 | | - HttpHeaders headers = new HttpHeaders(); |
122 | | - headers.set("Accept", MediaType.TEXT_PLAIN_VALUE); |
123 | | - Resource responseBody = template.exchange(uri, HttpMethod.GET, new HttpEntity<>(headers), Resource.class).getBody(); |
124 | | - if(responseBody != null) { |
125 | | - InputStream inputStream = responseBody.getInputStream(); |
126 | | - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); |
| 120 | + RequestCallback requestCallback = request -> request.getHeaders() |
| 121 | + .setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL)); |
| 122 | + |
| 123 | + // Streams the response instead of loading it all in memory |
| 124 | + ResponseExtractor<Void> responseExtractor = response -> { |
| 125 | + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody())); |
127 | 126 | String line; |
128 | 127 | while((line = bufferedReader.readLine()) != null) { |
129 | 128 | results.add(line); |
130 | 129 | } |
131 | | - inputStream.close(); |
132 | | - } |
| 130 | + bufferedReader.close(); |
| 131 | + return null; |
| 132 | + }; |
| 133 | + |
| 134 | + template.execute(uri, HttpMethod.GET, requestCallback, responseExtractor); |
| 135 | + |
133 | 136 | return results; |
134 | 137 | } |
135 | 138 | catch(Exception e) { |
|
0 commit comments