Skip to content

Commit b19102d

Browse files
author
Stephane Landelle
committed
Change Protocol method to directly use a NettyResponseFuture
1 parent 5f53e1c commit b19102d

File tree

4 files changed

+13
-25
lines changed

4 files changed

+13
-25
lines changed

src/main/java/com/ning/http/client/providers/netty/handler/HttpProtocol.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,9 @@ public void handle(final Channel channel, final NettyResponseFuture<?> future, f
474474
}
475475
}
476476

477-
public void onError(Channel channel, Throwable e) {
477+
public void onError(NettyResponseFuture<?> future, Throwable e) {
478478
}
479479

480-
public void onClose(Channel channel) {
480+
public void onClose(NettyResponseFuture<?> future) {
481481
}
482482
}

src/main/java/com/ning/http/client/providers/netty/handler/Processor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws
133133
&& requestSender.applyIoExceptionFiltersAndReplayRequest(future, CHANNEL_CLOSED_EXCEPTION, channel))
134134
return;
135135

136-
protocol.onClose(channel);
136+
protocol.onClose(future);
137137

138138
if (future == null || future.isDone())
139139
channelManager.closeChannel(channel);
@@ -193,12 +193,11 @@ public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws
193193
try {
194194
LOGGER.debug("Was unable to recover Future: {}", future);
195195
requestSender.abort(future, cause);
196+
protocol.onError(future, e.getCause());
196197
} catch (Throwable t) {
197198
LOGGER.error(t.getMessage(), t);
198199
}
199200

200-
protocol.onError(channel, e.getCause());
201-
202201
channelManager.closeChannel(channel);
203202
ctx.sendUpstream(e);
204203
}

src/main/java/com/ning/http/client/providers/netty/handler/Protocol.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ public Protocol(ChannelManager channelManager, AsyncHttpClientConfig config, Net
8787

8888
public abstract void handle(Channel channel, NettyResponseFuture<?> future, Object message) throws Exception;
8989

90-
public abstract void onError(Channel channel, Throwable e);
90+
public abstract void onError(NettyResponseFuture<?> future, Throwable e);
9191

92-
public abstract void onClose(Channel channel);
92+
public abstract void onClose(NettyResponseFuture<?> future);
9393

9494
protected boolean exitAfterHandlingRedirect(//
9595
Channel channel,//

src/main/java/com/ning/http/client/providers/netty/handler/WebSocketProtocol.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.ning.http.client.HttpResponseHeaders;
3434
import com.ning.http.client.HttpResponseStatus;
3535
import com.ning.http.client.Request;
36-
import com.ning.http.client.providers.netty.DiscardEvent;
3736
import com.ning.http.client.providers.netty.NettyAsyncHttpProviderConfig;
3837
import com.ning.http.client.providers.netty.channel.ChannelManager;
3938
import com.ning.http.client.providers.netty.channel.Channels;
@@ -177,16 +176,11 @@ public void setContent(ChannelBuffer content) {
177176
}
178177

179178
@Override
180-
public void onError(Channel channel, Throwable e) {
181-
try {
182-
Object attribute = Channels.getAttribute(channel);
183-
logger.warn("onError {}", e);
184-
if (!(attribute instanceof NettyResponseFuture)) {
185-
return;
186-
}
179+
public void onError(NettyResponseFuture<?> future, Throwable e) {
180+
logger.warn("onError {}", e);
187181

188-
NettyResponseFuture<?> nettyResponse = (NettyResponseFuture<?>) attribute;
189-
WebSocketUpgradeHandler h = WebSocketUpgradeHandler.class.cast(nettyResponse.getAsyncHandler());
182+
try {
183+
WebSocketUpgradeHandler h = (WebSocketUpgradeHandler) future.getAsyncHandler();
190184

191185
NettyWebSocket webSocket = NettyWebSocket.class.cast(h.onCompleted());
192186
if (webSocket != null) {
@@ -199,20 +193,15 @@ public void onError(Channel channel, Throwable e) {
199193
}
200194

201195
@Override
202-
public void onClose(Channel channel) {
196+
public void onClose(NettyResponseFuture<?> future) {
203197
logger.trace("onClose {}");
204-
Object attribute = Channels.getAttribute(channel);
205-
if (!(attribute instanceof NettyResponseFuture))
206-
return;
207198

208199
try {
209-
NettyResponseFuture<?> nettyResponse = NettyResponseFuture.class.cast(attribute);
210-
WebSocketUpgradeHandler h = WebSocketUpgradeHandler.class.cast(nettyResponse.getAsyncHandler());
200+
WebSocketUpgradeHandler h = (WebSocketUpgradeHandler) future.getAsyncHandler();
211201
NettyWebSocket webSocket = NettyWebSocket.class.cast(h.onCompleted());
212202

213-
// FIXME How could this test not succeed, we just checked above that attribute is a NettyResponseFuture????
214203
logger.trace("Connection was closed abnormally (that is, with no close frame being sent).");
215-
if (attribute != DiscardEvent.INSTANCE && webSocket != null)
204+
if (webSocket != null)
216205
webSocket.close(1006, "Connection was closed abnormally (that is, with no close frame being sent).");
217206
} catch (Throwable t) {
218207
logger.error("onError", t);

0 commit comments

Comments
 (0)