@Test public void testPutMetric() throws Exception { decoder = new WebSocketRequestDecoder(anonConfig.getSecurity()); String request = "{" + "\"operation\" : \"put\",\n" + "\"name\" : \"sys.cpu.user\",\n" + "\"timestamp\":" + TEST_TIME + ",\n" + "\"measure\":1.0,\n" + "\"tags\":[ {\n" + "\"key\":\"tag1\",\n" + "\"value\":\"value1\"\n" + "},{\n" + "\"key\":\"tag2\",\n" + "\"value\":\"value2\"\n" + "}]\n" + "}"; TextWebSocketFrame frame = new TextWebSocketFrame(); frame.content().writeBytes(request.getBytes(StandardCharsets.UTF_8)); decoder.decode(ctx, frame, results); Assert.assertEquals(1, results.size()); Assert.assertEquals(MetricRequest.class, results.get(0).getClass()); Metric metric = ((MetricRequest) results.iterator().next()).getMetric(); Assert.assertEquals("sys.cpu.user", metric.getName()); Assert.assertEquals(TEST_TIME, metric.getValue().getTimestamp()); Assert.assertEquals(1.0D, metric.getValue().getMeasure(), 0.0D); Assert.assertEquals(2, metric.getTags().size()); }
@Override protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception { if (msg instanceof TextWebSocketFrame) { TextWebSocketFrame frame = (TextWebSocketFrame) msg; String content = frame.text(); WebSocketRequest request = JsonUtil.getObjectMapper().readValue(content, WebSocketRequest.class); LOG.trace("Received WS request {}", content); String sessionId = ctx.channel().attr(SubscriptionRegistry.SESSION_ID_ATTR).get(); Multimap<String, String> headers = ctx.channel().attr(HTTP_HEADERS_ATTR).get(); X509Certificate clientCert = ctx.channel().attr(CLIENT_CERT_ATTR).get(); if (request instanceof AuthenticatedRequest) { if (headers != null) { ((AuthenticatedRequest) request).addHeaders(headers); } TimelyAuthenticationToken token; String authHeader = HttpHeaderUtils.getSingleHeader(headers, AUTH_HEADER, true); if (authHeader != null) { token = AuthenticationService.getAuthenticationToken(headers); } else if (StringUtils.isNotBlank(sessionId)) { TimelyPrincipal principal; ((AuthenticatedRequest) request).setSessionId(sessionId); if (AuthCache.containsKey(sessionId)) { principal = AuthCache.get(sessionId); } else { principal = TimelyPrincipal.anonymousPrincipal(); } SubjectIssuerDNPair dn = principal.getPrimaryUser().getDn(); token = AuthenticationService.getAuthenticationToken(null, dn.subjectDN(), dn.issuerDN()); } else if (clientCert != null) { token = AuthenticationService.getAuthenticationToken(clientCert, headers); } else { SubjectIssuerDNPair dn = TimelyPrincipal.anonymousPrincipal().getPrimaryUser().getDn(); token = AuthenticationService.getAuthenticationToken(null, dn.subjectDN(), dn.issuerDN()); } ((AuthenticatedRequest) request).setToken(token); } try { request.validate(); } catch (IllegalArgumentException e) { LOG.error("Error validating web socket request: " + e.getMessage()); ctx.writeAndFlush(new CloseWebSocketFrame(1008, e.getMessage())); return; } try { if (request instanceof AuthenticatedRequest) { try { AuthenticationService.enforceAccess((AuthenticatedRequest) request); } catch (TimelyException e) { if (!security.isAllowAnonymousWsAccess()) { throw e; } } } } catch (Exception e) { out.clear(); LOG.error("Error during access enforcment: " + e.getMessage()); ctx.writeAndFlush(new CloseWebSocketFrame(1008, e.getMessage())); return; } out.add(request); } else { LOG.error("Unhandled web socket frame type"); ctx.writeAndFlush(new CloseWebSocketFrame(1003, "Unhandled web socket frame type, only TextWebSocketFrame is supported")); } }
WebSocketRequestDecoder extends MessageToMessageDecoder<WebSocketFrame> { @Override protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception { if (msg instanceof TextWebSocketFrame) { TextWebSocketFrame frame = (TextWebSocketFrame) msg; String content = frame.text(); WebSocketRequest request = JsonUtil.getObjectMapper().readValue(content, WebSocketRequest.class); LOG.trace("Received WS request {}", content); String sessionId = ctx.channel().attr(SubscriptionRegistry.SESSION_ID_ATTR).get(); Multimap<String, String> headers = ctx.channel().attr(HTTP_HEADERS_ATTR).get(); X509Certificate clientCert = ctx.channel().attr(CLIENT_CERT_ATTR).get(); if (request instanceof AuthenticatedRequest) { if (headers != null) { ((AuthenticatedRequest) request).addHeaders(headers); } TimelyAuthenticationToken token; String authHeader = HttpHeaderUtils.getSingleHeader(headers, AUTH_HEADER, true); if (authHeader != null) { token = AuthenticationService.getAuthenticationToken(headers); } else if (StringUtils.isNotBlank(sessionId)) { TimelyPrincipal principal; ((AuthenticatedRequest) request).setSessionId(sessionId); if (AuthCache.containsKey(sessionId)) { principal = AuthCache.get(sessionId); } else { principal = TimelyPrincipal.anonymousPrincipal(); } SubjectIssuerDNPair dn = principal.getPrimaryUser().getDn(); token = AuthenticationService.getAuthenticationToken(null, dn.subjectDN(), dn.issuerDN()); } else if (clientCert != null) { token = AuthenticationService.getAuthenticationToken(clientCert, headers); } else { SubjectIssuerDNPair dn = TimelyPrincipal.anonymousPrincipal().getPrimaryUser().getDn(); token = AuthenticationService.getAuthenticationToken(null, dn.subjectDN(), dn.issuerDN()); } ((AuthenticatedRequest) request).setToken(token); } try { request.validate(); } catch (IllegalArgumentException e) { LOG.error("Error validating web socket request: " + e.getMessage()); ctx.writeAndFlush(new CloseWebSocketFrame(1008, e.getMessage())); return; } try { if (request instanceof AuthenticatedRequest) { try { AuthenticationService.enforceAccess((AuthenticatedRequest) request); } catch (TimelyException e) { if (!security.isAllowAnonymousWsAccess()) { throw e; } } } } catch (Exception e) { out.clear(); LOG.error("Error during access enforcment: " + e.getMessage()); ctx.writeAndFlush(new CloseWebSocketFrame(1008, e.getMessage())); return; } out.add(request); } else { LOG.error("Unhandled web socket frame type"); ctx.writeAndFlush(new CloseWebSocketFrame(1003, "Unhandled web socket frame type, only TextWebSocketFrame is supported")); } } }
WebSocketRequestDecoder extends MessageToMessageDecoder<WebSocketFrame> { @Override protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception { if (msg instanceof TextWebSocketFrame) { TextWebSocketFrame frame = (TextWebSocketFrame) msg; String content = frame.text(); WebSocketRequest request = JsonUtil.getObjectMapper().readValue(content, WebSocketRequest.class); LOG.trace("Received WS request {}", content); String sessionId = ctx.channel().attr(SubscriptionRegistry.SESSION_ID_ATTR).get(); Multimap<String, String> headers = ctx.channel().attr(HTTP_HEADERS_ATTR).get(); X509Certificate clientCert = ctx.channel().attr(CLIENT_CERT_ATTR).get(); if (request instanceof AuthenticatedRequest) { if (headers != null) { ((AuthenticatedRequest) request).addHeaders(headers); } TimelyAuthenticationToken token; String authHeader = HttpHeaderUtils.getSingleHeader(headers, AUTH_HEADER, true); if (authHeader != null) { token = AuthenticationService.getAuthenticationToken(headers); } else if (StringUtils.isNotBlank(sessionId)) { TimelyPrincipal principal; ((AuthenticatedRequest) request).setSessionId(sessionId); if (AuthCache.containsKey(sessionId)) { principal = AuthCache.get(sessionId); } else { principal = TimelyPrincipal.anonymousPrincipal(); } SubjectIssuerDNPair dn = principal.getPrimaryUser().getDn(); token = AuthenticationService.getAuthenticationToken(null, dn.subjectDN(), dn.issuerDN()); } else if (clientCert != null) { token = AuthenticationService.getAuthenticationToken(clientCert, headers); } else { SubjectIssuerDNPair dn = TimelyPrincipal.anonymousPrincipal().getPrimaryUser().getDn(); token = AuthenticationService.getAuthenticationToken(null, dn.subjectDN(), dn.issuerDN()); } ((AuthenticatedRequest) request).setToken(token); } try { request.validate(); } catch (IllegalArgumentException e) { LOG.error("Error validating web socket request: " + e.getMessage()); ctx.writeAndFlush(new CloseWebSocketFrame(1008, e.getMessage())); return; } try { if (request instanceof AuthenticatedRequest) { try { AuthenticationService.enforceAccess((AuthenticatedRequest) request); } catch (TimelyException e) { if (!security.isAllowAnonymousWsAccess()) { throw e; } } } } catch (Exception e) { out.clear(); LOG.error("Error during access enforcment: " + e.getMessage()); ctx.writeAndFlush(new CloseWebSocketFrame(1008, e.getMessage())); return; } out.add(request); } else { LOG.error("Unhandled web socket frame type"); ctx.writeAndFlush(new CloseWebSocketFrame(1003, "Unhandled web socket frame type, only TextWebSocketFrame is supported")); } } WebSocketRequestDecoder(Security security); }
WebSocketRequestDecoder extends MessageToMessageDecoder<WebSocketFrame> { @Override protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception { if (msg instanceof TextWebSocketFrame) { TextWebSocketFrame frame = (TextWebSocketFrame) msg; String content = frame.text(); WebSocketRequest request = JsonUtil.getObjectMapper().readValue(content, WebSocketRequest.class); LOG.trace("Received WS request {}", content); String sessionId = ctx.channel().attr(SubscriptionRegistry.SESSION_ID_ATTR).get(); Multimap<String, String> headers = ctx.channel().attr(HTTP_HEADERS_ATTR).get(); X509Certificate clientCert = ctx.channel().attr(CLIENT_CERT_ATTR).get(); if (request instanceof AuthenticatedRequest) { if (headers != null) { ((AuthenticatedRequest) request).addHeaders(headers); } TimelyAuthenticationToken token; String authHeader = HttpHeaderUtils.getSingleHeader(headers, AUTH_HEADER, true); if (authHeader != null) { token = AuthenticationService.getAuthenticationToken(headers); } else if (StringUtils.isNotBlank(sessionId)) { TimelyPrincipal principal; ((AuthenticatedRequest) request).setSessionId(sessionId); if (AuthCache.containsKey(sessionId)) { principal = AuthCache.get(sessionId); } else { principal = TimelyPrincipal.anonymousPrincipal(); } SubjectIssuerDNPair dn = principal.getPrimaryUser().getDn(); token = AuthenticationService.getAuthenticationToken(null, dn.subjectDN(), dn.issuerDN()); } else if (clientCert != null) { token = AuthenticationService.getAuthenticationToken(clientCert, headers); } else { SubjectIssuerDNPair dn = TimelyPrincipal.anonymousPrincipal().getPrimaryUser().getDn(); token = AuthenticationService.getAuthenticationToken(null, dn.subjectDN(), dn.issuerDN()); } ((AuthenticatedRequest) request).setToken(token); } try { request.validate(); } catch (IllegalArgumentException e) { LOG.error("Error validating web socket request: " + e.getMessage()); ctx.writeAndFlush(new CloseWebSocketFrame(1008, e.getMessage())); return; } try { if (request instanceof AuthenticatedRequest) { try { AuthenticationService.enforceAccess((AuthenticatedRequest) request); } catch (TimelyException e) { if (!security.isAllowAnonymousWsAccess()) { throw e; } } } } catch (Exception e) { out.clear(); LOG.error("Error during access enforcment: " + e.getMessage()); ctx.writeAndFlush(new CloseWebSocketFrame(1008, e.getMessage())); return; } out.add(request); } else { LOG.error("Unhandled web socket frame type"); ctx.writeAndFlush(new CloseWebSocketFrame(1003, "Unhandled web socket frame type, only TextWebSocketFrame is supported")); } } WebSocketRequestDecoder(Security security); static void close(); @Override void exceptionCaught(ChannelHandlerContext ctx, Throwable cause); @Override void userEventTriggered(ChannelHandlerContext ctx, Object evt); }
WebSocketRequestDecoder extends MessageToMessageDecoder<WebSocketFrame> { @Override protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception { if (msg instanceof TextWebSocketFrame) { TextWebSocketFrame frame = (TextWebSocketFrame) msg; String content = frame.text(); WebSocketRequest request = JsonUtil.getObjectMapper().readValue(content, WebSocketRequest.class); LOG.trace("Received WS request {}", content); String sessionId = ctx.channel().attr(SubscriptionRegistry.SESSION_ID_ATTR).get(); Multimap<String, String> headers = ctx.channel().attr(HTTP_HEADERS_ATTR).get(); X509Certificate clientCert = ctx.channel().attr(CLIENT_CERT_ATTR).get(); if (request instanceof AuthenticatedRequest) { if (headers != null) { ((AuthenticatedRequest) request).addHeaders(headers); } TimelyAuthenticationToken token; String authHeader = HttpHeaderUtils.getSingleHeader(headers, AUTH_HEADER, true); if (authHeader != null) { token = AuthenticationService.getAuthenticationToken(headers); } else if (StringUtils.isNotBlank(sessionId)) { TimelyPrincipal principal; ((AuthenticatedRequest) request).setSessionId(sessionId); if (AuthCache.containsKey(sessionId)) { principal = AuthCache.get(sessionId); } else { principal = TimelyPrincipal.anonymousPrincipal(); } SubjectIssuerDNPair dn = principal.getPrimaryUser().getDn(); token = AuthenticationService.getAuthenticationToken(null, dn.subjectDN(), dn.issuerDN()); } else if (clientCert != null) { token = AuthenticationService.getAuthenticationToken(clientCert, headers); } else { SubjectIssuerDNPair dn = TimelyPrincipal.anonymousPrincipal().getPrimaryUser().getDn(); token = AuthenticationService.getAuthenticationToken(null, dn.subjectDN(), dn.issuerDN()); } ((AuthenticatedRequest) request).setToken(token); } try { request.validate(); } catch (IllegalArgumentException e) { LOG.error("Error validating web socket request: " + e.getMessage()); ctx.writeAndFlush(new CloseWebSocketFrame(1008, e.getMessage())); return; } try { if (request instanceof AuthenticatedRequest) { try { AuthenticationService.enforceAccess((AuthenticatedRequest) request); } catch (TimelyException e) { if (!security.isAllowAnonymousWsAccess()) { throw e; } } } } catch (Exception e) { out.clear(); LOG.error("Error during access enforcment: " + e.getMessage()); ctx.writeAndFlush(new CloseWebSocketFrame(1008, e.getMessage())); return; } out.add(request); } else { LOG.error("Unhandled web socket frame type"); ctx.writeAndFlush(new CloseWebSocketFrame(1003, "Unhandled web socket frame type, only TextWebSocketFrame is supported")); } } WebSocketRequestDecoder(Security security); static void close(); @Override void exceptionCaught(ChannelHandlerContext ctx, Throwable cause); @Override void userEventTriggered(ChannelHandlerContext ctx, Object evt); static final AttributeKey<Multimap<String, String>> HTTP_HEADERS_ATTR; static final AttributeKey<X509Certificate> CLIENT_CERT_ATTR; }