下面列出了io.netty.channel.ChannelHandlerContext#attr ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
Attribute<ObjectOutputStream> oosAttr = ctx.attr(OOS);
ObjectOutputStream oos = oosAttr.get();
if (oos == null) {
oos = newObjectOutputStream(new ByteBufOutputStream(out));
ObjectOutputStream newOos = oosAttr.setIfAbsent(oos);
if (newOos != null) {
oos = newOos;
}
}
synchronized (oos) {
if (resetInterval != 0) {
// Resetting will prevent OOM on the receiving side.
writtenObjects ++;
if (writtenObjects % resetInterval == 0) {
oos.reset();
}
}
oos.writeObject(msg);
oos.flush();
}
}
public static QueryStringDecoder getOrCreateQueryStringDecoder(HttpServerRequest<?> request,
ChannelHandlerContext channelHandlerContext) {
if (null == request) {
throw new NullPointerException("Request can not be null.");
}
String uri = request.getUri();
if (null == uri) {
return null;
}
Attribute<QueryStringDecoder> queryDecoderAttr = channelHandlerContext.attr(queryDecoderKey);
QueryStringDecoder _queryStringDecoder = queryDecoderAttr.get();
if (null == _queryStringDecoder) {
_queryStringDecoder = new QueryStringDecoder(uri);
queryDecoderAttr.setIfAbsent(_queryStringDecoder);
}
return _queryStringDecoder;
}
public void put(UUID recId, ChannelHandlerContext ctx) {
if(ctx == null) {
return;
}
Attribute<UUID> recIdAttr = ctx.attr(REC_KEY);
if(!recIdAttr.compareAndSet(null, recId)) {
throw new IllegalStateException("attempt to set existing non-null ctx recId [" + recIdAttr.get() + "] to [" + recId + "]");
}
sessions.put(recIdAttr.get(), ctx);
}
public ChannelHandlerContext remove(ChannelHandlerContext ctx) {
if(ctx == null) {
return null;
}
Attribute<UUID> recIdAttr = ctx.attr(REC_KEY);
if(recIdAttr != null && recIdAttr.get() != null) {
return remove(recIdAttr.get());
}
return null;
}
public HttpsOriginalHostCaptureFilter(HttpRequest originalRequest, ChannelHandlerContext ctx) {
super(originalRequest, ctx);
// if this is an HTTP CONNECT, set the isHttps attribute on the ChannelHandlerConect and capture the hostname from the original request.
// capturing the original host (and the remapped/modified host in clientToProxyRequest() below) guarantees that we will
// have the "true" host, rather than relying on the Host header in subsequent requests (which may be absent or spoofed by malicious clients).
if (ProxyUtils.isCONNECT(originalRequest)) {
Attribute<String> originalHostAttr = ctx.attr(AttributeKey.<String>valueOf(HttpsAwareFiltersAdapter.ORIGINAL_HOST_ATTRIBUTE_NAME));
String hostAndPort = originalRequest.getUri();
originalHostAttr.set(hostAndPort);
Attribute<Boolean> isHttpsAttr = ctx.attr(AttributeKey.<Boolean>valueOf(HttpsAwareFiltersAdapter.IS_HTTPS_ATTRIBUTE_NAME));
isHttpsAttr.set(true);
}
}
public HttpsOriginalHostCaptureFilter(HttpRequest originalRequest, ChannelHandlerContext ctx) {
super(originalRequest, ctx);
// if this is an HTTP CONNECT, set the isHttps attribute on the ChannelHandlerConect and capture the hostname from the original request.
// capturing the original host (and the remapped/modified host in clientToProxyRequest() below) guarantees that we will
// have the "true" host, rather than relying on the Host header in subsequent requests (which may be absent or spoofed by malicious clients).
if (ProxyUtils.isCONNECT(originalRequest)) {
Attribute<String> originalHostAttr = ctx.attr(AttributeKey.<String>valueOf(HttpsAwareFiltersAdapter.ORIGINAL_HOST_ATTRIBUTE_NAME));
String hostAndPort = originalRequest.getUri();
originalHostAttr.set(hostAndPort);
Attribute<Boolean> isHttpsAttr = ctx.attr(AttributeKey.<Boolean>valueOf(HttpsAwareFiltersAdapter.IS_HTTPS_ATTRIBUTE_NAME));
isHttpsAttr.set(true);
}
}
public HttpsOriginalHostCaptureFilter(HttpRequest originalRequest, ChannelHandlerContext ctx) {
super(originalRequest, ctx);
// if this is an HTTP CONNECT, set the isHttps attribute on the ChannelHandlerConect and capture the hostname from the original request.
// capturing the original host (and the remapped/modified host in clientToProxyRequest() below) guarantees that we will
// have the "true" host, rather than relying on the Host header in subsequent requests (which may be absent or spoofed by malicious clients).
if (ProxyUtils.isCONNECT(originalRequest)) {
Attribute<String> originalHostAttr = ctx.attr(AttributeKey.<String>valueOf(HttpsAwareFiltersAdapter.ORIGINAL_HOST_ATTRIBUTE_NAME));
String hostAndPort = originalRequest.getUri();
originalHostAttr.set(hostAndPort);
Attribute<Boolean> isHttpsAttr = ctx.attr(AttributeKey.<Boolean>valueOf(HttpsAwareFiltersAdapter.IS_HTTPS_ATTRIBUTE_NAME));
isHttpsAttr.set(true);
}
}
@Override
public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception {
Attribute<Unmarshaller> attr = ctx.attr(UNMARSHALLER);
Unmarshaller unmarshaller = attr.get();
if (unmarshaller == null) {
unmarshaller = super.getUnmarshaller(ctx);
attr.set(unmarshaller);
}
return unmarshaller;
}
static Attribute<Connection> getSessionAttribute(ChannelHandlerContext ctx) {
return ctx.attr(AttributeKey.<Connection>valueOf(SESSION_ATTRIBUTE_KEY));
}