Skip to content

Commit d78a94b

Browse files
author
Stephane Landelle
committed
Rename attachment into attribute
1 parent 028fef6 commit d78a94b

File tree

9 files changed

+59
-51
lines changed

9 files changed

+59
-51
lines changed

src/main/java/com/ning/http/client/providers/netty/channel/ChannelManager.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@ public void close() {
293293
openChannels.close();
294294

295295
for (Channel channel : openChannels) {
296-
Object attachment = Channels.getAttachment(channel);
297-
if (attachment instanceof NettyResponseFuture<?>) {
298-
NettyResponseFuture<?> future = (NettyResponseFuture<?>) attachment;
296+
Object attribute = Channels.getAttribute(channel);
297+
if (attribute instanceof NettyResponseFuture<?>) {
298+
NettyResponseFuture<?> future = (NettyResponseFuture<?>) attribute;
299299
future.cancelTimeouts();
300300
}
301301
}
@@ -351,19 +351,26 @@ public SslHandler createSslHandler(String peerHost, int peerPort) throws General
351351
: new SslHandler(sslEngine);
352352
}
353353

354+
public SslHandler getSslHandler(ChannelPipeline pipeline) {
355+
return (SslHandler) pipeline.get(SSL_HANDLER);
356+
}
357+
358+
private boolean isSslHandlerConfigured(ChannelPipeline pipeline) {
359+
return pipeline.get(SSL_HANDLER) != null;
360+
}
361+
354362
public void upgradeProtocol(ChannelPipeline pipeline, String scheme, String host, int port) throws IOException,
355363
GeneralSecurityException {
356364
if (pipeline.get(HTTP_HANDLER) != null)
357365
pipeline.remove(HTTP_HANDLER);
358366

359367
if (isSecure(scheme))
360-
if (pipeline.get(SSL_HANDLER) == null) {
368+
if (isSslHandlerConfigured(pipeline)){
369+
pipeline.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec());
370+
} else {
361371
pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec());
362372
pipeline.addFirst(SSL_HANDLER, createSslHandler(host, port));
363-
} else {
364-
pipeline.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec());
365373
}
366-
367374
else
368375
pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec());
369376

@@ -377,13 +384,14 @@ public String getPoolKey(NettyResponseFuture<?> future) {
377384

378385
public void verifyChannelPipeline(ChannelPipeline pipeline, String scheme) throws IOException, GeneralSecurityException {
379386

380-
boolean isSecure = isSecure(scheme);
381-
if (pipeline.get(SSL_HANDLER) != null) {
382-
if (!isSecure)
383-
pipeline.remove(SSL_HANDLER);
387+
boolean sslHandlerConfigured = isSslHandlerConfigured(pipeline);
388+
389+
if (isSecure(scheme)) {
390+
if (!sslHandlerConfigured)
391+
pipeline.addFirst(SSL_HANDLER, new SslInitializer(this));
384392

385-
} else if (isSecure)
386-
pipeline.addFirst(SSL_HANDLER, new SslInitializer(this));
393+
} else if (sslHandlerConfigured)
394+
pipeline.remove(SSL_HANDLER);
387395
}
388396

389397
public ClientBootstrap getBootstrap(String scheme, boolean useProxy, boolean useSSl) {
@@ -408,6 +416,6 @@ public void call() throws Exception {
408416
}
409417

410418
public void drainChannel(final Channel channel, final NettyResponseFuture<?> future) {
411-
Channels.setAttachment(channel, newDrainCallback(future, channel, future.isKeepAlive(), getPoolKey(future)));
419+
Channels.setAttribute(channel, newDrainCallback(future, channel, future.isKeepAlive(), getPoolKey(future)));
412420
}
413421
}

src/main/java/com/ning/http/client/providers/netty/channel/Channels.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ public final class Channels {
2222
private Channels() {
2323
}
2424

25-
public static void setAttachment(Channel channel, Object attachment) {
26-
channel.setAttachment(attachment);
25+
public static void setAttribute(Channel channel, Object attribute) {
26+
channel.setAttachment(attribute);
2727
}
2828

29-
public static Object getAttachment(Channel channel) {
29+
public static Object getAttribute(Channel channel) {
3030
return channel.getAttachment();
3131
}
3232

3333
public static void setDiscard(Channel channel) {
34-
setAttachment(channel, DiscardEvent.INSTANCE);
34+
setAttribute(channel, DiscardEvent.INSTANCE);
3535
}
3636

3737
public static boolean isChannelValid(Channel channel) {

src/main/java/com/ning/http/client/providers/netty/channel/pool/DefaultChannelPool.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ private List<IdleChannel> expiredChannels(ConcurrentLinkedQueue<IdleChannel> poo
146146
}
147147

148148
private boolean isChannelCloseable(Channel channel) {
149-
Object attachment = Channels.getAttachment(channel);
150-
if (attachment instanceof NettyResponseFuture) {
151-
NettyResponseFuture<?> future = (NettyResponseFuture<?>) attachment;
149+
Object attribute = Channels.getAttribute(channel);
150+
if (attribute instanceof NettyResponseFuture) {
151+
NettyResponseFuture<?> future = (NettyResponseFuture<?>) attribute;
152152
if (!future.isDone())
153153
LOGGER.error("Future not in appropriate state %s, not closing", future);
154154
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public void call() throws Exception {
246246
if (future.isKeepAlive() && HttpHeaders.isTransferEncodingChunked(response))
247247
// We must make sure there is no bytes left
248248
// before executing the next request.
249-
Channels.setAttachment(channel, callback);
249+
Channels.setAttribute(channel, callback);
250250
else
251251
callback.call();
252252

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ public void messageReceived(final ChannelHandlerContext ctx, MessageEvent e) thr
6969
super.messageReceived(ctx, e);
7070

7171
Channel channel = ctx.getChannel();
72-
Object attachment = Channels.getAttachment(channel);
72+
Object attribute = Channels.getAttribute(channel);
7373

74-
if (attachment == null)
75-
LOGGER.debug("ChannelHandlerContext doesn't have any attachment");
74+
if (attribute == null)
75+
LOGGER.debug("ChannelHandlerContext doesn't have any attribute");
7676

77-
if (attachment instanceof Callback) {
77+
if (attribute instanceof Callback) {
7878
Object message = e.getMessage();
79-
Callback ac = (Callback) attachment;
79+
Callback ac = (Callback) attribute;
8080
if (message instanceof HttpChunk) {
8181
// the AsyncCallable is to be processed on the last chunk
8282
if (HttpChunk.class.cast(message).isLast())
@@ -88,11 +88,11 @@ public void messageReceived(final ChannelHandlerContext ctx, MessageEvent e) thr
8888
Channels.setDiscard(channel);
8989
}
9090

91-
} else if (attachment instanceof NettyResponseFuture<?>) {
92-
NettyResponseFuture<?> future = (NettyResponseFuture<?>) attachment;
91+
} else if (attribute instanceof NettyResponseFuture<?>) {
92+
NettyResponseFuture<?> future = (NettyResponseFuture<?>) attribute;
9393
protocol.handle(channel, future, e.getMessage());
9494

95-
} else if (attachment != DiscardEvent.INSTANCE) {
95+
} else if (attribute != DiscardEvent.INSTANCE) {
9696
// unhandled message
9797
try {
9898
ctx.getChannel().close();
@@ -117,16 +117,16 @@ public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws
117117
LOGGER.trace("super.channelClosed", ex);
118118
}
119119

120-
Object attachment = Channels.getAttachment(channel);
121-
LOGGER.debug("Channel Closed: {} with attachment {}", channel, attachment);
120+
Object attribute = Channels.getAttribute(channel);
121+
LOGGER.debug("Channel Closed: {} with attribute {}", channel, attribute);
122122

123-
if (attachment instanceof Callback) {
124-
Callback callback = (Callback) attachment;
125-
Channels.setAttachment(channel, callback.future());
123+
if (attribute instanceof Callback) {
124+
Callback callback = (Callback) attribute;
125+
Channels.setAttribute(channel, callback.future());
126126
callback.call();
127127

128-
} else if (attachment instanceof NettyResponseFuture<?>) {
129-
NettyResponseFuture<?> future = (NettyResponseFuture<?>) attachment;
128+
} else if (attribute instanceof NettyResponseFuture<?>) {
129+
NettyResponseFuture<?> future = (NettyResponseFuture<?>) attribute;
130130
future.touch();
131131

132132
if (!config.getIOExceptionFilters().isEmpty()
@@ -155,9 +155,9 @@ public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws
155155
LOGGER.debug("Unexpected I/O exception on channel {}", channel, cause);
156156

157157
try {
158-
Object attachment = Channels.getAttachment(channel);
159-
if (attachment instanceof NettyResponseFuture<?>) {
160-
future = (NettyResponseFuture<?>) attachment;
158+
Object attribute = Channels.getAttribute(channel);
159+
if (attribute instanceof NettyResponseFuture<?>) {
160+
future = (NettyResponseFuture<?>) attribute;
161161
future.attachChannel(null, false);
162162
future.touch();
163163

@@ -182,8 +182,8 @@ public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws
182182
LOGGER.debug("Trying to recover from dead Channel: {}", channel);
183183
return;
184184
}
185-
} else if (attachment instanceof Callback) {
186-
future = ((Callback) attachment).future();
185+
} else if (attribute instanceof Callback) {
186+
future = ((Callback) attribute).future();
187187
}
188188
} catch (Throwable t) {
189189
cause = t;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ protected boolean exitAfterHandlingRedirect(//
145145
// We must make sure there is no bytes left before
146146
// executing the next request.
147147
// FIXME investigate this
148-
Channels.setAttachment(channel, callback);
148+
Channels.setAttribute(channel, callback);
149149
} else {
150150
// FIXME don't understand: this offers the connection to the pool, or even closes it, while the
151151
// request has not been sent, right?

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public void setContent(ChannelBuffer content) {
177177
@Override
178178
public void onError(Channel channel, Throwable e) {
179179
try {
180-
Object attribute = Channels.getAttachment(channel);
180+
Object attribute = Channels.getAttribute(channel);
181181
logger.warn("onError {}", e);
182182
if (!(attribute instanceof NettyResponseFuture)) {
183183
return;
@@ -199,7 +199,7 @@ public void onError(Channel channel, Throwable e) {
199199
@Override
200200
public void onClose(Channel channel) {
201201
logger.trace("onClose {}");
202-
Object attribute = Channels.getAttachment(channel);
202+
Object attribute = Channels.getAttribute(channel);
203203
if (!(attribute instanceof NettyResponseFuture))
204204
return;
205205

src/main/java/com/ning/http/client/providers/netty/request/NettyRequestSender.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ private <T> ListenableFuture<T> sendRequestWithCachedChannel(Request request, Ur
202202
future.attachChannel(channel, false);
203203

204204
LOGGER.debug("\nUsing cached Channel {}\n for request \n{}\n", channel, future.getNettyRequest().getHttpRequest());
205-
Channels.setAttachment(channel, future);
205+
Channels.setAttribute(channel, future);
206206

207207
try {
208208
writeRequest(future, channel);
@@ -416,9 +416,9 @@ public boolean retry(NettyResponseFuture<?> future, Channel channel) {
416416
// channelManager.removeAll(channel);
417417

418418
if (future == null) {
419-
Object attachment = Channels.getAttachment(channel);
420-
if (attachment instanceof NettyResponseFuture)
421-
future = (NettyResponseFuture<?>) attachment;
419+
Object attribute = Channels.getAttribute(channel);
420+
if (attribute instanceof NettyResponseFuture)
421+
future = (NettyResponseFuture<?>) attribute;
422422
}
423423

424424
if (future != null && future.canBeReplayed()) {

src/main/java/com/ning/http/client/providers/netty/request/body/NettyConnectListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ private void writeRequest(Channel channel, String poolKey) {
9090
public final void operationComplete(ChannelFuture f) throws Exception {
9191
Channel channel = f.getChannel();
9292
if (f.isSuccess()) {
93-
Channels.setAttachment(channel, future);
94-
final SslHandler sslHandler = (SslHandler) channel.getPipeline().get(ChannelManager.SSL_HANDLER);
93+
Channels.setAttribute(channel, future);
94+
final SslHandler sslHandler = channelManager.getSslHandler(channel.getPipeline());
9595

9696
final HostnameVerifier hostnameVerifier = config.getHostnameVerifier();
9797
if (hostnameVerifier != null && sslHandler != null) {

0 commit comments

Comments
 (0)