Java Code Examples for io.netty.channel.ChannelHandlerContext#attr()

The following examples show how to use io.netty.channel.ChannelHandlerContext#attr() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
@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();
    }
}
 
Example 2
Source Project: karyon   File: HttpKeyEvaluationContext.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
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);
}
 
Example 4
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;
}
 
Example 5
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);
    }
}
 
Example 6
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);
    }
}
 
Example 7
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);
    }
}
 
Example 8
@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;
}
 
Example 9
Source Project: p2p   File: PeerChannelHandler.java    License: Apache License 2.0 4 votes vote down vote up
static Attribute<Connection> getSessionAttribute(ChannelHandlerContext ctx) {
    return ctx.attr(AttributeKey.<Connection>valueOf(SESSION_ATTRIBUTE_KEY));
}