Skip to content

Commit 0e0a2a4

Browse files
committed
Minor refactor, ensure some NPE can't happen while handling headers.
1 parent 1592189 commit 0e0a2a4

File tree

5 files changed

+317
-361
lines changed

5 files changed

+317
-361
lines changed

modules/org.restlet/src/org/restlet/Message.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
package org.restlet;
2626

27+
import static org.restlet.engine.header.HeaderConstants.ATTRIBUTE_HEADERS;
28+
import static org.restlet.representation.Representation.UNKNOWN_SIZE;
29+
2730
import java.io.IOException;
2831
import java.util.Date;
2932
import java.util.List;
@@ -37,7 +40,6 @@
3740
import org.restlet.data.MediaType;
3841
import org.restlet.data.RecipientInfo;
3942
import org.restlet.data.Warning;
40-
import org.restlet.engine.header.HeaderConstants;
4143
import org.restlet.representation.Representation;
4244
import org.restlet.representation.StringRepresentation;
4345
import org.restlet.resource.ClientResource;
@@ -105,8 +107,7 @@ public Message(Representation entity) {
105107
// [ifndef gwt] method
106108
/**
107109
* If the entity is transient or its size unknown in advance but available,
108-
* then the entity is wrapped with a
109-
* {@link org.restlet.representation.BufferingRepresentation}.<br>
110+
* then the entity is wrapped with a {@link org.restlet.representation.BufferingRepresentation}.<br>
110111
* <br>
111112
* Be careful as this method could create potentially very large byte
112113
* buffers in memory that could impact your application performance.
@@ -117,10 +118,9 @@ public Message(Representation entity) {
117118
*/
118119
public void bufferEntity() {
119120
if ((getEntity() != null)
120-
&& (getEntity().isTransient() || (getEntity().getSize() == Representation.UNKNOWN_SIZE))
121+
&& (getEntity().isTransient() || getEntity().getSize() == UNKNOWN_SIZE)
121122
&& getEntity().isAvailable()) {
122-
setEntity(new org.restlet.representation.BufferingRepresentation(
123-
getEntity()));
123+
setEntity(new org.restlet.representation.BufferingRepresentation(getEntity()));
124124
}
125125
}
126126

@@ -153,17 +153,15 @@ public void flushBuffers() throws IOException {
153153
* <tr>
154154
* <td>org.restlet.http.headers</td>
155155
* <td>org.restlet.util.Series&lt;org.restlet.engine.header.Header&gt;</td>
156-
* <td>Server HTTP connectors must provide all request headers and client
157-
* HTTP connectors must provide all response headers, exactly as they were
158-
* received. In addition, developers can also use this attribute to specify
159-
* <b>non-standard</b> headers that should be added to the request or to the
160-
* response.</td>
156+
* <td>Server HTTP connectors must provide all request headers and client HTTP connectors must provide all response
157+
* headers, exactly as they were received. In addition, developers can also use this attribute to specify
158+
* <b>non-standard</b> headers that should be added to the request or to the response.</td>
161159
* </tr>
162160
* <tr>
163161
* <td>org.restlet.https.clientCertificates</td>
164162
* <td>List<java.security.cert.Certificate></td>
165-
* <td>For requests received via a secure connector, indicates the ordered
166-
* list of client certificates, if they are available and accessible.</td>
163+
* <td>For requests received via a secure connector, indicates the ordered list of client certificates, if they are
164+
* available and accessible.</td>
167165
* </tr>
168166
* </table>
169167
* <br>
@@ -242,8 +240,7 @@ public Representation getEntity() {
242240
public String getEntityAsText() {
243241
if (this.entityText == null) {
244242
try {
245-
this.entityText = (getEntity() == null) ? null : getEntity()
246-
.getText();
243+
this.entityText = (getEntity() == null) ? null : getEntity().getText();
247244
} catch (java.io.IOException e) {
248245
Context.getCurrentLogger().log(java.util.logging.Level.FINE,
249246
"Unable to get the entity text.", e);
@@ -263,14 +260,13 @@ public String getEntityAsText() {
263260
*/
264261
@SuppressWarnings("unchecked")
265262
public Series<Header> getHeaders() {
266-
Series<Header> headers = (Series<Header>) getAttributes().get(
267-
HeaderConstants.ATTRIBUTE_HEADERS);
263+
Series<Header> headers = (Series<Header>) getAttributes().get(ATTRIBUTE_HEADERS);
268264
if (headers == null) {
269265
// [ifndef gwt] instruction
270266
headers = new Series<Header>(Header.class);
271267
// [ifdef gwt] instruction uncomment
272268
// headers = new org.restlet.engine.util.HeaderSeries();
273-
getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, headers);
269+
getAttributes().put(ATTRIBUTE_HEADERS, headers);
274270
}
275271
return headers;
276272
}

modules/org.restlet/src/org/restlet/engine/adapter/ClientAdapter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ protected void readResponseHeaders(ClientCall httpCall, Response response) {
125125
Series<Header> responseHeaders = httpCall.getResponseHeaders();
126126

127127
// Put the response headers in the call's attributes map
128-
response.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS,
129-
responseHeaders);
128+
response.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, responseHeaders);
130129

131130
HeaderUtils.copyResponseTransportHeaders(responseHeaders, response);
132131
} catch (Exception e) {

modules/org.restlet/src/org/restlet/engine/adapter/ClientCall.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424

2525
package org.restlet.engine.adapter;
2626

27+
import static org.restlet.data.Encoding.IDENTITY;
28+
import static org.restlet.data.Status.CONNECTOR_ERROR_COMMUNICATION;
29+
import static org.restlet.data.Status.REDIRECTION_NOT_MODIFIED;
30+
import static org.restlet.data.Status.SUCCESS_NO_CONTENT;
31+
import static org.restlet.data.Status.SUCCESS_RESET_CONTENT;
32+
import static org.restlet.representation.Representation.UNKNOWN_SIZE;
33+
2734
import java.io.IOException;
2835
import java.io.InputStream;
2936
import java.io.OutputStream;
@@ -32,7 +39,6 @@
3239
import org.restlet.Context;
3340
import org.restlet.Request;
3441
import org.restlet.Response;
35-
import org.restlet.data.Encoding;
3642
import org.restlet.data.Header;
3743
import org.restlet.data.Method;
3844
import org.restlet.data.Status;
@@ -87,8 +93,7 @@ public ClientCall(HttpClientHelper helper, String method, String requestUri) {
8793
}
8894

8995
/**
90-
* Returns the content length of the request entity if know,
91-
* {@link Representation#UNKNOWN_SIZE} otherwise.
96+
* Returns the content length of the request entity if know, {@link Representation#UNKNOWN_SIZE} otherwise.
9297
*
9398
* @return The request content length.
9499
*/
@@ -148,26 +153,24 @@ public HttpClientHelper getHelper() {
148153
public Representation getResponseEntity(Response response) {
149154
Representation result = null;
150155
// boolean available = false;
151-
long size = Representation.UNKNOWN_SIZE;
156+
long size = UNKNOWN_SIZE;
152157

153158
// Compute the content length
154159
Series<Header> responseHeaders = getResponseHeaders();
155160
String transferEncoding = responseHeaders.getFirstValue(
156161
HeaderConstants.HEADER_TRANSFER_ENCODING, true);
157162
if ((transferEncoding != null)
158-
&& !Encoding.IDENTITY.getName().equalsIgnoreCase(
159-
transferEncoding)) {
160-
size = Representation.UNKNOWN_SIZE;
163+
&& !IDENTITY.getName().equalsIgnoreCase(transferEncoding)) {
164+
size = UNKNOWN_SIZE;
161165
} else {
162166
size = getContentLength();
163167
}
164168

165169
if (!getMethod().equals(Method.HEAD.getName())
166170
&& !response.getStatus().isInformational()
167-
&& !response.getStatus()
168-
.equals(Status.REDIRECTION_NOT_MODIFIED)
169-
&& !response.getStatus().equals(Status.SUCCESS_NO_CONTENT)
170-
&& !response.getStatus().equals(Status.SUCCESS_RESET_CONTENT)) {
171+
&& !response.getStatus().equals(REDIRECTION_NOT_MODIFIED)
172+
&& !response.getStatus().equals(SUCCESS_NO_CONTENT)
173+
&& !response.getStatus().equals(SUCCESS_RESET_CONTENT)) {
171174
// Make sure that an InputRepresentation will not be instantiated
172175
// while the stream is closed.
173176
InputStream stream = getUnClosedResponseEntityStream(getResponseEntityStream(size));
@@ -187,7 +190,7 @@ public Representation getResponseEntity(Response response) {
187190
result.setSize(size);
188191

189192
// Informs that the size has not been specified in the header.
190-
if (size == Representation.UNKNOWN_SIZE) {
193+
if (size == UNKNOWN_SIZE) {
191194
getLogger()
192195
.fine("The length of the message body is unknown. The entity must be handled carefully and consumed entirely in order to surely release the connection.");
193196
}
@@ -205,8 +208,7 @@ public Representation getResponseEntity(Response response) {
205208
* The expected entity size or -1 if unknown.
206209
* @return The response channel if it exists.
207210
*/
208-
public abstract java.nio.channels.ReadableByteChannel getResponseEntityChannel(
209-
long size);
211+
public abstract java.nio.channels.ReadableByteChannel getResponseEntityChannel(long size);
210212

211213
/**
212214
* Returns the response entity stream if it exists.
@@ -276,8 +278,7 @@ protected boolean isServerKeepAlive() {
276278
*/
277279
public Status sendRequest(Request request) {
278280
Status result = null;
279-
Representation entity = request.isEntityAvailable() ? request
280-
.getEntity() : null;
281+
Representation entity = request.isEntityAvailable() ? request.getEntity() : null;
281282

282283
// Get the connector service to callback
283284
org.restlet.service.ConnectorService connectorService = ConnectorHelper
@@ -317,7 +318,7 @@ public Status sendRequest(Request request) {
317318
.log(Level.FINE,
318319
"An error occurred during the communication with the remote HTTP server.",
319320
ioe);
320-
result = new Status(Status.CONNECTOR_ERROR_COMMUNICATION, ioe);
321+
result = new Status(CONNECTOR_ERROR_COMMUNICATION, ioe);
321322
} finally {
322323
if (entity != null) {
323324
entity.release();
@@ -343,10 +344,8 @@ public Status sendRequest(Request request) {
343344
* @param callback
344345
* The callback invoked upon request completion.
345346
*/
346-
public void sendRequest(Request request, Response response,
347-
org.restlet.Uniform callback) throws Exception {
348-
Context.getCurrentLogger().warning(
349-
"Currently callbacks are not available for this connector.");
347+
public void sendRequest(Request request, Response response, org.restlet.Uniform callback) throws Exception {
348+
Context.getCurrentLogger().warning("Currently callbacks are not available for this connector.");
350349
}
351350

352351
/**
@@ -355,7 +354,8 @@ public void sendRequest(Request request, Response response,
355354
* @return True if the request should be chunked
356355
*/
357356
protected boolean shouldRequestBeChunked(Request request) {
358-
return request.isEntityAvailable() && (request.getEntity() != null)
357+
return request.isEntityAvailable()
358+
&& (request.getEntity() != null)
359359
&& !request.getEntity().hasKnownSize();
360360
}
361361
}

modules/org.restlet/src/org/restlet/engine/adapter/ServerAdapter.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424

2525
package org.restlet.engine.adapter;
2626

27+
import static org.restlet.engine.header.HeaderConstants.ATTRIBUTE_HEADERS;
28+
import static org.restlet.engine.header.HeaderConstants.ATTRIBUTE_HTTPS_KEY_SIZE;
29+
import static org.restlet.engine.header.HeaderConstants.ATTRIBUTE_HTTPS_SSL_SESSION_ID;
30+
import static org.restlet.engine.header.HeaderConstants.ATTRIBUTE_VERSION;
31+
2732
import java.io.IOException;
2833
import java.security.cert.Certificate;
2934
import java.util.List;
@@ -33,7 +38,6 @@
3338
import org.restlet.data.Header;
3439
import org.restlet.data.Method;
3540
import org.restlet.data.Status;
36-
import org.restlet.engine.header.HeaderConstants;
3741
import org.restlet.engine.header.HeaderUtils;
3842
import org.restlet.representation.Representation;
3943
import org.restlet.util.Series;
@@ -189,7 +193,8 @@ public void commit(HttpResponse response) {
189193
// [ifndef gae]
190194
if (response.getHttpCall().isConnectionBroken(t)) {
191195
// output a single log line for this common case to avoid filling servers logs
192-
getLogger().log(Level.INFO, "The connection was broken. It was probably closed by the client. Reason: " + t.getMessage());
196+
getLogger().log(Level.INFO,
197+
"The connection was broken. It was probably closed by the client. Reason: " + t.getMessage());
193198
} else
194199
// [enddef]
195200
{
@@ -226,12 +231,10 @@ public void commit(HttpResponse response) {
226231
*/
227232
public HttpRequest toRequest(ServerCall httpCall) {
228233
HttpRequest result = new HttpRequest(getContext(), httpCall);
229-
result.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS,
230-
httpCall.getRequestHeaders());
234+
result.getAttributes().put(ATTRIBUTE_HEADERS, httpCall.getRequestHeaders());
231235

232236
if (httpCall.getVersion() != null) {
233-
result.getAttributes().put(HeaderConstants.ATTRIBUTE_VERSION,
234-
httpCall.getVersion());
237+
result.getAttributes().put(ATTRIBUTE_VERSION, httpCall.getVersion());
235238
}
236239

237240
if (httpCall.isConfidential()) {
@@ -250,16 +253,13 @@ public HttpRequest toRequest(ServerCall httpCall) {
250253
Integer keySize = httpCall.getSslKeySize();
251254

252255
if (keySize != null) {
253-
result.getAttributes().put(
254-
HeaderConstants.ATTRIBUTE_HTTPS_KEY_SIZE, keySize);
256+
result.getAttributes().put(ATTRIBUTE_HTTPS_KEY_SIZE, keySize);
255257
}
256258

257259
String sslSessionId = httpCall.getSslSessionId();
258260

259261
if (sslSessionId != null) {
260-
result.getAttributes().put(
261-
HeaderConstants.ATTRIBUTE_HTTPS_SSL_SESSION_ID,
262-
sslSessionId);
262+
result.getAttributes().put(ATTRIBUTE_HTTPS_SSL_SESSION_ID, sslSessionId);
263263
}
264264
}
265265

0 commit comments

Comments
 (0)