Skip to content

Commit a180fcc

Browse files
committed
enable streaming and ensure pass-through content-length header if available
1 parent 8a4440e commit a180fcc

File tree

2 files changed

+74
-25
lines changed

2 files changed

+74
-25
lines changed

maven-nodejs-proxy/src/main/java/io/wcm/devops/maven/nodejsproxy/resource/MavenProxyResource.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package io.wcm.devops.maven.nodejsproxy.resource;
2121

22+
import static javax.ws.rs.core.HttpHeaders.CONTENT_LENGTH;
2223
import io.wcm.devops.maven.nodejsproxy.MavenProxyConfiguration;
2324

2425
import java.io.IOException;
@@ -187,19 +188,15 @@ public Response getBinary(
187188
log.info("Proxy file: {}", url);
188189
HttpGet get = new HttpGet(url);
189190
HttpResponse response = httpClient.execute(get);
190-
try {
191-
if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
192-
byte[] data = EntityUtils.toByteArray(response.getEntity());
193-
return Response.ok(data)
194-
.type(MediaType.APPLICATION_OCTET_STREAM)
195-
.build();
196-
}
197-
else {
198-
return Response.status(Response.Status.NOT_FOUND).build();
199-
}
191+
if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
192+
return Response.ok(new SpoolStreamingOutput(response.getEntity()))
193+
.type(MediaType.APPLICATION_OCTET_STREAM)
194+
.header(CONTENT_LENGTH, response.containsHeader(CONTENT_LENGTH) ? response.getFirstHeader(CONTENT_LENGTH).getValue() : null)
195+
.build();
200196
}
201-
finally {
197+
else {
202198
EntityUtils.consumeQuietly(response.getEntity());
199+
return Response.status(Response.Status.NOT_FOUND).build();
203200
}
204201
}
205202
}
@@ -242,26 +239,24 @@ public Response getBinary(
242239
log.info("Proxy file: {}", url);
243240
HttpGet get = new HttpGet(url);
244241
HttpResponse response = httpClient.execute(get);
245-
try {
246-
if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
242+
if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
243+
if (getChecksum) {
247244
byte[] data = EntityUtils.toByteArray(response.getEntity());
248-
if (getChecksum) {
249-
return Response.ok(DigestUtils.sha1Hex(data))
250-
.type(MediaType.TEXT_PLAIN)
251-
.build();
252-
}
253-
else {
254-
return Response.ok(data)
255-
.type(MediaType.APPLICATION_OCTET_STREAM)
256-
.build();
257-
}
245+
EntityUtils.consumeQuietly(response.getEntity());
246+
return Response.ok(DigestUtils.sha1Hex(data))
247+
.type(MediaType.TEXT_PLAIN)
248+
.build();
258249
}
259250
else {
260-
return Response.status(Response.Status.NOT_FOUND).build();
251+
return Response.ok(new SpoolStreamingOutput(response.getEntity()))
252+
.type(MediaType.APPLICATION_OCTET_STREAM)
253+
.header(CONTENT_LENGTH, response.containsHeader(CONTENT_LENGTH) ? response.getFirstHeader(CONTENT_LENGTH).getValue() : null)
254+
.build();
261255
}
262256
}
263-
finally {
257+
else {
264258
EntityUtils.consumeQuietly(response.getEntity());
259+
return Response.status(Response.Status.NOT_FOUND).build();
265260
}
266261
}
267262

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2015 wcm.io
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package io.wcm.devops.maven.nodejsproxy.resource;
21+
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.OutputStream;
25+
26+
import javax.ws.rs.WebApplicationException;
27+
import javax.ws.rs.core.StreamingOutput;
28+
29+
import org.apache.commons.io.IOUtils;
30+
import org.apache.http.HttpEntity;
31+
import org.apache.http.util.EntityUtils;
32+
33+
/**
34+
* Spool binary data from HTTP response to JAX-RS output.
35+
*/
36+
class SpoolStreamingOutput implements StreamingOutput {
37+
38+
private final HttpEntity httpEntity;
39+
40+
public SpoolStreamingOutput(HttpEntity httpEntity) {
41+
this.httpEntity = httpEntity;
42+
}
43+
44+
@Override
45+
public void write(OutputStream os) throws IOException, WebApplicationException {
46+
try (InputStream is = httpEntity.getContent()) {
47+
IOUtils.copyLarge(is, os);
48+
}
49+
finally {
50+
EntityUtils.consumeQuietly(httpEntity);
51+
}
52+
}
53+
54+
}

0 commit comments

Comments
 (0)