AttributeComponent(String attributeName, Function<Object, String> stringifer, boolean addQuote, @Nullable Function<HttpHeaders, Boolean> condition) { super(condition, addQuote); key = AttributeKey.valueOf(requireNonNull(attributeName, "attributeName")); this.stringifer = requireNonNull(stringifer, "stringifer"); }
/** * Returns true if this is an HTTPS request. * * @return true if https, false if http */ public boolean isHttps() { Attribute<Boolean> isHttpsAttr = ctx.attr(AttributeKey.<Boolean>valueOf(IS_HTTPS_ATTRIBUTE_NAME)); Boolean isHttps = isHttpsAttr.get(); if (isHttps == null) { return false; } else { return isHttps; } }
public static final AttributeKey<Connection> CONNECTION_ATTRIBUTE_KEY = AttributeKey.valueOf("rx-netty-conn-attr");
/** * Returns the host and port of this HTTPS request, including any modifications by other filters. * * @return host and port of this HTTPS request * @throws IllegalStateException if this is not an HTTPS request */ private String getHttpsRequestHostAndPort() throws IllegalStateException { if (!isHttps()) { throw new IllegalStateException("Request is not HTTPS. Cannot get host and port on non-HTTPS request using this method."); } Attribute<String> hostnameAttr = ctx.attr(AttributeKey.<String>valueOf(HOST_ATTRIBUTE_NAME)); return hostnameAttr.get(); }
/** * Returns the original host and port of this HTTPS request, as sent by the client. Does not reflect any modifications * by other filters. * TODO: evaluate this (unused) method and its capture mechanism in HttpsOriginalHostCaptureFilter; remove if not useful. * * @return host and port of this HTTPS request * @throws IllegalStateException if this is not an HTTPS request */ private String getHttpsOriginalRequestHostAndPort() throws IllegalStateException { if (!isHttps()) { throw new IllegalStateException("Request is not HTTPS. Cannot get original host and port on non-HTTPS request using this method."); } Attribute<String> hostnameAttr = ctx.attr(AttributeKey.<String>valueOf(ORIGINAL_HOST_ATTRIBUTE_NAME)); return hostnameAttr.get(); } }
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 HttpResponse clientToProxyRequest(HttpObject httpObject) { if (httpObject instanceof HttpRequest) { HttpRequest httpRequest = (HttpRequest) httpObject; if (ProxyUtils.isCONNECT(httpRequest)) { Attribute<String> hostname = ctx.attr(AttributeKey.<String>valueOf(HttpsAwareFiltersAdapter.HOST_ATTRIBUTE_NAME)); String hostAndPort = httpRequest.getUri(); // CONNECT requests contain the port, even when using the default port. a sensible default is to remove the // default port, since in most cases it is not explicitly specified and its presence (in a HAR file, for example) // would be unexpected. String hostNoDefaultPort = BrowserMobHttpUtil.removeMatchingPort(hostAndPort, 443); hostname.set(hostNoDefaultPort); } } return null; } }
@Test public void testXmlConfig() throws Exception { try { final JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(context); context.reset(); configurator.doConfigure(getClass().getResource("testXmlConfig.xml")); final RequestContextExportingAppender rcea = (RequestContextExportingAppender) logger.getAppender("RCEA"); assertThat(rcea).isNotNull(); assertThat(rcea.getBuiltIns()).containsExactly(BuiltInProperty.REMOTE_HOST); assertThat(rcea.getHttpRequestHeaders()).containsExactly(HttpHeaderNames.USER_AGENT); assertThat(rcea.getHttpResponseHeaders()).containsExactly(HttpHeaderNames.SET_COOKIE); final AttributeKey<Object> fooAttr = AttributeKey.valueOf("com.example.AttrKeys#FOO"); final AttributeKey<Object> barAttr = AttributeKey.valueOf("com.example.AttrKeys#BAR"); assertThat(rcea.getAttributes()).containsOnly(new SimpleEntry<>("foo", fooAttr), new SimpleEntry<>("bar", barAttr)); } finally { // Revert to the original configuration. final JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(context); context.reset(); configurator.doConfigure(getClass().getResource("/logback-test.xml")); } }
@Test public void testMutabilityAndImmutability() { final AttributeKey<Object> someAttr = AttributeKey.valueOf(RequestContextExportingAppenderTest.class, "SOME_ATTR"); final RequestContextExportingAppender a = new RequestContextExportingAppender(); // Ensure mutability before start. a.addBuiltIn(BuiltInProperty.ELAPSED_NANOS); assertThat(a.getBuiltIns()).containsExactly(BuiltInProperty.ELAPSED_NANOS); a.addAttribute("some-attr", someAttr); assertThat(a.getAttributes()).containsOnlyKeys("some-attr") .containsValue(someAttr); a.addHttpRequestHeader(HttpHeaderNames.USER_AGENT); assertThat(a.getHttpRequestHeaders()).containsExactly(HttpHeaderNames.USER_AGENT); a.addHttpResponseHeader(HttpHeaderNames.SET_COOKIE); assertThat(a.getHttpResponseHeaders()).containsExactly(HttpHeaderNames.SET_COOKIE); final ListAppender<ILoggingEvent> la = new ListAppender<>(); a.addAppender(la); a.start(); la.start(); // Ensure immutability after start. assertThatThrownBy(() -> a.addBuiltIn(BuiltInProperty.REQ_PATH)) .isExactlyInstanceOf(IllegalStateException.class); assertThatThrownBy(() -> a.addAttribute("my-attr", MY_ATTR)) .isExactlyInstanceOf(IllegalStateException.class); assertThatThrownBy(() -> a.addHttpRequestHeader(HttpHeaderNames.ACCEPT)) .isExactlyInstanceOf(IllegalStateException.class); assertThatThrownBy(() -> a.addHttpResponseHeader(HttpHeaderNames.DATE)) .isExactlyInstanceOf(IllegalStateException.class); }
@SuppressWarnings("unchecked") @Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) { AttributeKey<Session> sessionKey = AttributeKey.valueOf(RpcConstant.SESSION_KEY); receiver.receive(ctx.channel().attr(sessionKey).get(), (T) msg); }
/** * 判断当前channel 是否登录过 * @return */ public boolean isLogin(){ return Optional.ofNullable(this.channel).map(channel1 -> { AttributeKey<Boolean> _login = AttributeKey.valueOf("login"); return channel1.isActive() && channel1.hasAttr(_login); }).orElse(false); }
public void processPubAck(Channel channel, MqttMessageIdVariableHeader variableHeader) { int messageId = variableHeader.messageId(); LOGGER.debug("PUBACK - clientId: {}, messageId: {}", (String) channel.attr(AttributeKey.valueOf("clientId")).get(), messageId); dupPublishMessageStoreService.remove((String) channel.attr(AttributeKey.valueOf("clientId")).get(), messageId); }
/** * 设置链接参数信息 * * @param channel * @param key */ static public void removeSessionAttr(Channel channel, String key) { if (channel != null) { channel.attr(AttributeKey.valueOf(key)).remove(); } }
public void processPingReq(Channel channel, MqttMessage msg) { MqttMessage pingRespMessage = MqttMessageFactory.newMessage( new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false, 0), null, null); LOGGER.debug("PINGREQ - clientId: {}", (String) channel.attr(AttributeKey.valueOf("clientId")).get()); channel.writeAndFlush(pingRespMessage); }
public void processPubRec(Channel channel, MqttMessageIdVariableHeader variableHeader) { MqttMessage pubRelMessage = MqttMessageFactory.newMessage( new MqttFixedHeader(MqttMessageType.PUBREL, false, MqttQoS.AT_MOST_ONCE, false, 0), MqttMessageIdVariableHeader.from(variableHeader.messageId()), null); LOGGER.debug("PUBREC - clientId: {}, messageId: {}", (String) channel.attr(AttributeKey.valueOf("clientId")).get(), variableHeader.messageId()); dupPublishMessageStoreService.remove((String) channel.attr(AttributeKey.valueOf("clientId")).get(), variableHeader.messageId()); DupPubRelMessageStore dupPubRelMessageStore = new DupPubRelMessageStore().setClientId((String) channel.attr(AttributeKey.valueOf("clientId")).get()) .setMessageId(variableHeader.messageId()); dupPubRelMessageStoreService.put((String) channel.attr(AttributeKey.valueOf("clientId")).get(), dupPubRelMessageStore); channel.writeAndFlush(pubRelMessage); }
public void processPubRel(Channel channel, MqttMessageIdVariableHeader variableHeader) { MqttMessage pubCompMessage = MqttMessageFactory.newMessage( new MqttFixedHeader(MqttMessageType.PUBCOMP, false, MqttQoS.AT_MOST_ONCE, false, 0), MqttMessageIdVariableHeader.from(variableHeader.messageId()), null); LOGGER.debug("PUBREL - clientId: {}, messageId: {}", (String) channel.attr(AttributeKey.valueOf("clientId")).get(), variableHeader.messageId()); channel.writeAndFlush(pubCompMessage); }
private void cacheNode(Node node, SocketChannel channel) { String name = "node-" + node.getId(); boolean exists = AttributeKey.exists(name); AttributeKey attributeKey; if (exists) { attributeKey = AttributeKey.valueOf(name); } else { attributeKey = AttributeKey.newInstance(name); } Attribute<Node> attribute = channel.attr(attributeKey); attribute.set(node); }
private boolean handleRequest(HttpRequest httpRequest, Channel channel, ChannelHandlerContext ctx) throws HandlerException { httpMethodInfoBuilder = httpMethodHandler.getDestinationMethod( httpRequest, new BasicHttpResponder(channel, HttpHeaders.isKeepAlive(httpRequest))); ctx.attr(AttributeKey.valueOf(METHOD_INFO_BUILDER)).set(httpMethodInfoBuilder); return httpMethodInfoBuilder != null; }
@Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { super.channelUnregistered(ctx); SocketChannel channel = (SocketChannel) ctx.channel(); String nodeId = IpUtil.getNodeId(channel.remoteAddress()); Attribute<Node> nodeAttribute = channel.attr(AttributeKey.valueOf("node-" + nodeId)); Node node = nodeAttribute.get(); if (node != null && node.getDisconnectListener() != null) { node.getDisconnectListener().action(); } //channelUnregistered之前,channel就已经close了,可以调用channel.isOpen()查看状态 //channel.close(); }