Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 39fcdac

Browse files
committed
JERSEY-3189: Using HttpUrlConnection#setFixedLengthStreamingMode(long) when available.
Change-Id: I879f9b8fb9886e2fb0bae431f8c30e7c6f58d5c9
1 parent 33d07ca commit 39fcdac

File tree

5 files changed

+76
-19
lines changed

5 files changed

+76
-19
lines changed

connectors/netty-connector/src/main/java/org/glassfish/jersey/netty/connector/NettyConnector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ public void operationComplete(io.netty.util.concurrent.Future<? super Void> futu
249249
nettyRequest.headers().add(HttpHeaderNames.HOST, jerseyRequest.getUri().getHost());
250250

251251
if (jerseyRequest.hasEntity()) {
252-
if (jerseyRequest.getLength() == -1) {
252+
if (jerseyRequest.getLengthLong() == -1) {
253253
HttpUtil.setTransferEncodingChunked(nettyRequest, true);
254254
} else {
255-
nettyRequest.headers().add(HttpHeaderNames.CONTENT_LENGTH, jerseyRequest.getLength());
255+
nettyRequest.headers().add(HttpHeaderNames.CONTENT_LENGTH, jerseyRequest.getLengthLong());
256256
}
257257
}
258258

core-client/src/main/java/org/glassfish/jersey/client/internal/HttpUrlConnector.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2011-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2011-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -361,9 +361,14 @@ private ClientResponse _apply(final ClientRequest request) throws IOException {
361361
ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.class);
362362

363363
if (entityProcessing == null || entityProcessing != RequestEntityProcessing.BUFFERED) {
364-
final int length = request.getLength();
364+
final long length = request.getLengthLong();
365365
if (fixLengthStreaming && length > 0) {
366-
uc.setFixedLengthStreamingMode(length);
366+
// uc.setFixedLengthStreamingMode(long) was introduced in JDK 1.7 and Jersey client supports 1.6+
367+
if ("1.6".equals(Runtime.class.getPackage().getSpecificationVersion())) {
368+
uc.setFixedLengthStreamingMode(request.getLength());
369+
} else {
370+
uc.setFixedLengthStreamingMode(length);
371+
}
367372
} else if (entityProcessing == RequestEntityProcessing.CHUNKED) {
368373
uc.setChunkedStreamingMode(chunkSize);
369374
}

core-client/src/test/java/org/glassfish/jersey/client/HttpUrlConnectorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -130,7 +130,7 @@ public HttpURLConnection getConnection(URL endpointUrl) throws IOException {
130130

131131

132132
res.getHeaders().putSingle(HttpHeaders.LINK, Link.fromPath("action").rel("test").build().toString());
133-
assertEquals(URI.create("http://redirected.org:8080/action"), res.getLink("test").getUri());
133+
assertEquals(URI.create("http://redirected.org:8080/action"), res.getLink("test").getUri());
134134
}
135135

136136
private HttpURLConnection wrapRedirectedHttp(final HttpURLConnection connection) {

core-common/src/main/java/org/glassfish/jersey/message/internal/OutboundMessageContext.java

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2012-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2012-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -113,7 +113,6 @@ public static interface StreamProvider {
113113
* which will cause ignoring the written entity (in that case the entity will
114114
* still be written by {@link javax.ws.rs.ext.MessageBodyWriter message body writers}
115115
* but the output will be ignored).
116-
*
117116
* @throws java.io.IOException in case of an IO error.
118117
*/
119118
public OutputStream getOutputStream(int contentLength) throws IOException;
@@ -169,7 +168,7 @@ public MultivaluedMap<String, String> getStringHeaders() {
169168

170169
/**
171170
* Get a message header as a single string value.
172-
*
171+
* <p>
173172
* Each single header value is converted to String using a
174173
* {@link javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate} if one is available
175174
* via {@link javax.ws.rs.ext.RuntimeDelegate#createHeaderDelegate(java.lang.Class)}
@@ -424,16 +423,52 @@ public Set<String> getAllowedMethods() {
424423

425424
/**
426425
* Get Content-Length value.
426+
* <p>
427+
* <B>Note</B>: {@link #getLengthLong() getLengthLong()}
428+
* should be preferred over this method, since it returns a {@code long}
429+
* instead and is therefore more portable.</P>
427430
*
428-
* @return Content-Length as integer if present and valid number. In other
431+
* @return Content-Length as a postive integer if present and valid number. In other
429432
* cases returns -1.
430433
*/
431434
public int getLength() {
432435
return singleHeader(HttpHeaders.CONTENT_LENGTH, Integer.class, new Function<String, Integer>() {
433436
@Override
434437
public Integer apply(String input) {
435438
try {
436-
return (input != null && !input.isEmpty()) ? Integer.parseInt(input) : -1;
439+
if (input != null && !input.isEmpty()) {
440+
int i = Integer.parseInt(input);
441+
if (i >= 0) {
442+
return i;
443+
}
444+
}
445+
return -1;
446+
447+
} catch (NumberFormatException ex) {
448+
throw new ProcessingException(ex);
449+
}
450+
}
451+
}, true);
452+
}
453+
454+
/**
455+
* Get Content-Length value.
456+
*
457+
* @return Content-Length as a positive long if present and valid number. In other
458+
* cases returns -1.
459+
*/
460+
public long getLengthLong() {
461+
return singleHeader(HttpHeaders.CONTENT_LENGTH, Long.class, new Function<String, Long>() {
462+
@Override
463+
public Long apply(String input) {
464+
try {
465+
if (input != null && !input.isEmpty()) {
466+
long l = Long.parseLong(input);
467+
if (l >= 0) {
468+
return l;
469+
}
470+
}
471+
return -1L;
437472
} catch (NumberFormatException ex) {
438473
throw new ProcessingException(ex);
439474
}
@@ -611,7 +646,7 @@ public Link.Builder getLinkBuilder(String relation) {
611646

612647
/**
613648
* Check if there is an entity available in the message.
614-
*
649+
* <p>
615650
* The method returns {@code true} if the entity is present, returns
616651
* {@code false} otherwise.
617652
*
@@ -624,7 +659,7 @@ public boolean hasEntity() {
624659

625660
/**
626661
* Get the message entity Java instance.
627-
*
662+
* <p>
628663
* Returns {@code null} if the message does not contain an entity.
629664
*
630665
* @return the message entity or {@code null} if message does not contain an
@@ -728,7 +763,7 @@ public Type getEntityType() {
728763

729764
/**
730765
* Set the message entity type information.
731-
*
766+
* <p>
732767
* This method overrides any computed or previously set entity type information.
733768
*
734769
* @param type overriding message entity type.

core-common/src/test/java/org/glassfish/jersey/message/internal/OutboundMessageContextTest.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2012-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2012-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -58,13 +58,12 @@
5858
import org.junit.Test;
5959
import static org.hamcrest.Matchers.contains;
6060
import static org.hamcrest.Matchers.equalTo;
61+
import static org.junit.Assert.assertEquals;
6162
import static org.junit.Assert.assertFalse;
63+
import static org.junit.Assert.assertNull;
6264
import static org.junit.Assert.assertThat;
6365
import static org.junit.Assert.assertTrue;
6466

65-
import static org.junit.Assert.assertEquals;
66-
import static org.junit.Assert.assertNull;
67-
6867
/**
6968
* {@link OutboundMessageContext} test.
7069
*
@@ -218,4 +217,22 @@ public void testGetLink() {
218217
assertTrue(r.getLink("self").equals(link1));
219218
assertTrue(r.getLink("update").equals(link2) || r.getLink("update").equals(link3));
220219
}
220+
221+
@Test
222+
public void testGetLength() {
223+
OutboundMessageContext r = new OutboundMessageContext();
224+
r.getHeaders().add("Content-Length", 50);
225+
assertEquals(50, r.getLengthLong());
226+
}
227+
228+
@Test
229+
public void testGetLength_tooLongForInt() {
230+
OutboundMessageContext r = new OutboundMessageContext();
231+
long length = Integer.MAX_VALUE + 5;
232+
r.getHeaders().add("Content-Length", length);
233+
234+
// value is not a valid integer -> returning -1 (see the javadoc).
235+
assertEquals(-1, r.getLength());
236+
assertEquals(length, r.getLengthLong());
237+
}
221238
}

0 commit comments

Comments
 (0)