Skip to content

Commit ec43978

Browse files
authored
Merge pull request #2162 from dmatej/fixedNpe
Fixed NPE in toString
2 parents 53e4350 + d7a96f1 commit ec43978

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

modules/grizzly/src/main/java/org/glassfish/grizzly/filterchain/FilterChainContext.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2022 Contributors to the Eclipse Foundation
23
* Copyright (c) 2008, 2020 Oracle and/or its affiliates. All rights reserved.
34
*
45
* This program and the accompanying materials are made available under the
@@ -254,7 +255,7 @@ public void fork(final NextAction nextAction) {
254255

255256
/**
256257
* Get the current processing task state.
257-
*
258+
*
258259
* @return the current processing task state.
259260
*/
260261
public State state() {
@@ -433,7 +434,7 @@ public TransportContext getTransportContext() {
433434

434435
/**
435436
* Get the general Grizzly {@link Context} this filter context wraps.
436-
*
437+
*
437438
* @return the general Grizzly {@link Context} this filter context wraps.
438439
*/
439440
public final Context getInternalContext() {
@@ -926,7 +927,8 @@ public String toString() {
926927
sb.append("connection=").append(getConnection());
927928
sb.append(", closeable=").append(getCloseable());
928929
sb.append(", operation=").append(getOperation());
929-
sb.append(", message=").append(String.valueOf(getMessage()));
930+
// message can be null, then valueOf(char[]) would be executed -> NPE
931+
sb.append(", message=").append(String.valueOf(Object.class.cast(getMessage())));
930932
sb.append(", address=").append(getAddress());
931933
sb.append(']');
932934

@@ -950,7 +952,6 @@ static Operation ioEvent2Operation(final IOEvent ioEvent) {
950952
}
951953
}
952954

953-
@SuppressWarnings("deprecation")
954955
public static final class TransportContext {
955956
private boolean isBlocking;
956957
CompletionHandler completionHandler;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2022 Eclipse Foundation and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.grizzly.filterchain;
18+
19+
import java.nio.channels.ServerSocketChannel;
20+
21+
import org.glassfish.grizzly.Buffer;
22+
import org.glassfish.grizzly.filterchain.FilterChainContext.Operation;
23+
import org.glassfish.grizzly.memory.Buffers;
24+
import org.glassfish.grizzly.memory.MemoryManager;
25+
import org.glassfish.grizzly.nio.transport.TCPNIOConnection;
26+
import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
27+
import org.junit.Test;
28+
29+
import static org.junit.Assert.assertEquals;
30+
31+
/**
32+
* @author David Matejcek
33+
*/
34+
public class FilterChainContextTest {
35+
36+
@Test
37+
public void testToString_null() throws Exception {
38+
assertEquals("FilterChainContext [connection=null, closeable=null, operation=NONE, message=null, address=null]",
39+
new FilterChainContext().toString());
40+
}
41+
42+
@Test
43+
public void testToString_usual() throws Exception {
44+
try (ServerSocketChannel channel = ServerSocketChannel.open()) {
45+
final TCPNIOConnection connection = new TCPNIOConnection(new TCPNIOTransport(), channel);
46+
final FilterChainContext context = FilterChainContext.create(connection);
47+
final Buffer buffer = Buffers.wrap(MemoryManager.DEFAULT_MEMORY_MANAGER, "Ororok orebuh");
48+
context.setAddress("localhost");
49+
context.setMessage(buffer);
50+
context.setOperation(Operation.CONNECT);
51+
assertEquals("FilterChainContext ["
52+
+ "connection=TCPNIOConnection{localSocketAddress=null, peerSocketAddress=null}, "
53+
+ "closeable=TCPNIOConnection{localSocketAddress=null, peerSocketAddress=null}, "
54+
+ "operation=CONNECT, "
55+
+ "message=" + buffer + ", " // contains hashcode
56+
+ "address=localhost"
57+
+ "]",
58+
context.toString());
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)