@Test public void testLookupPostWithLimit() throws Exception { String content = "{\n" + " \"metric\": \"sys.cpu.user\",\n" + " \"limit\": 3000\n" + "}"; decoder = new TestHttpQueryDecoder(config); DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/api/search/lookup"); request.content().writeBytes(content.getBytes()); addCookie(request); decoder.decode(null, request, results); Assert.assertEquals(1, results.size()); Assert.assertEquals(SearchLookupRequest.class, results.iterator().next().getClass()); SearchLookupRequest lookup = (SearchLookupRequest) results.iterator().next(); Assert.assertEquals("sys.cpu.user", lookup.getQuery()); Assert.assertEquals(3000, lookup.getLimit()); Assert.assertEquals(0, lookup.getTags().size()); }
@Override protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception { LOG.trace(LOG_RECEIVED_REQUEST, msg); final String uri = msg.uri(); final QueryStringDecoder decoder = new QueryStringDecoder(uri); if (decoder.path().equals(nonSecureRedirectAddress)) { out.add(new StrictTransportResponse()); return; } final String sessionId = getSessionId(msg); LOG.trace("SessionID: " + sessionId); HttpRequest request; try { if (msg.method().equals(HttpMethod.GET)) { HttpGetRequest get = AnnotationResolver.getClassForHttpGet(decoder.path()); request = get.parseQueryParameters(decoder); } else if (msg.method().equals(HttpMethod.POST)) { HttpPostRequest post = AnnotationResolver.getClassForHttpPost(decoder.path()); String content = ""; ByteBuf body = msg.content(); if (null != body) { content = body.toString(StandardCharsets.UTF_8); } request = post.parseBody(content); } else { TimelyException e = new TimelyException(HttpResponseStatus.METHOD_NOT_ALLOWED.code(), "unhandled method type", ""); e.addResponseHeader(HttpHeaderNames.ALLOW.toString(), HttpMethod.GET.name() + "," + HttpMethod.POST.name()); LOG.warn("Unhandled HTTP request type {}", msg.method()); throw e; } if (request instanceof AuthenticatedRequest) { Multimap<String, String> headers = HttpHeaderUtils.toMultimap(msg.headers()); ((AuthenticatedRequest) request).addHeaders(headers); X509Certificate clientCert = AuthenticationService.getClientCertificate(ctx); 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); } request.setHttpRequest(msg.copy()); LOG.trace(LOG_PARSED_REQUEST, request); request.validate(); out.add(request); } catch (UnsupportedOperationException | NullPointerException e) { msg.retain(); out.add(msg); return; } try { if (request instanceof AuthenticatedRequest) { try { AuthenticationService.enforceAccess((AuthenticatedRequest) request); } catch (TimelyException e) { if (!security.isAllowAnonymousHttpAccess()) { throw e; } } } } catch (Exception e) { out.clear(); out.add(e); } }
HttpRequestDecoder extends MessageToMessageDecoder<FullHttpRequest> implements TimelyHttpHandler { @Override protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception { LOG.trace(LOG_RECEIVED_REQUEST, msg); final String uri = msg.uri(); final QueryStringDecoder decoder = new QueryStringDecoder(uri); if (decoder.path().equals(nonSecureRedirectAddress)) { out.add(new StrictTransportResponse()); return; } final String sessionId = getSessionId(msg); LOG.trace("SessionID: " + sessionId); HttpRequest request; try { if (msg.method().equals(HttpMethod.GET)) { HttpGetRequest get = AnnotationResolver.getClassForHttpGet(decoder.path()); request = get.parseQueryParameters(decoder); } else if (msg.method().equals(HttpMethod.POST)) { HttpPostRequest post = AnnotationResolver.getClassForHttpPost(decoder.path()); String content = ""; ByteBuf body = msg.content(); if (null != body) { content = body.toString(StandardCharsets.UTF_8); } request = post.parseBody(content); } else { TimelyException e = new TimelyException(HttpResponseStatus.METHOD_NOT_ALLOWED.code(), "unhandled method type", ""); e.addResponseHeader(HttpHeaderNames.ALLOW.toString(), HttpMethod.GET.name() + "," + HttpMethod.POST.name()); LOG.warn("Unhandled HTTP request type {}", msg.method()); throw e; } if (request instanceof AuthenticatedRequest) { Multimap<String, String> headers = HttpHeaderUtils.toMultimap(msg.headers()); ((AuthenticatedRequest) request).addHeaders(headers); X509Certificate clientCert = AuthenticationService.getClientCertificate(ctx); 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); } request.setHttpRequest(msg.copy()); LOG.trace(LOG_PARSED_REQUEST, request); request.validate(); out.add(request); } catch (UnsupportedOperationException | NullPointerException e) { msg.retain(); out.add(msg); return; } try { if (request instanceof AuthenticatedRequest) { try { AuthenticationService.enforceAccess((AuthenticatedRequest) request); } catch (TimelyException e) { if (!security.isAllowAnonymousHttpAccess()) { throw e; } } } } catch (Exception e) { out.clear(); out.add(e); } } }
HttpRequestDecoder extends MessageToMessageDecoder<FullHttpRequest> implements TimelyHttpHandler { @Override protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception { LOG.trace(LOG_RECEIVED_REQUEST, msg); final String uri = msg.uri(); final QueryStringDecoder decoder = new QueryStringDecoder(uri); if (decoder.path().equals(nonSecureRedirectAddress)) { out.add(new StrictTransportResponse()); return; } final String sessionId = getSessionId(msg); LOG.trace("SessionID: " + sessionId); HttpRequest request; try { if (msg.method().equals(HttpMethod.GET)) { HttpGetRequest get = AnnotationResolver.getClassForHttpGet(decoder.path()); request = get.parseQueryParameters(decoder); } else if (msg.method().equals(HttpMethod.POST)) { HttpPostRequest post = AnnotationResolver.getClassForHttpPost(decoder.path()); String content = ""; ByteBuf body = msg.content(); if (null != body) { content = body.toString(StandardCharsets.UTF_8); } request = post.parseBody(content); } else { TimelyException e = new TimelyException(HttpResponseStatus.METHOD_NOT_ALLOWED.code(), "unhandled method type", ""); e.addResponseHeader(HttpHeaderNames.ALLOW.toString(), HttpMethod.GET.name() + "," + HttpMethod.POST.name()); LOG.warn("Unhandled HTTP request type {}", msg.method()); throw e; } if (request instanceof AuthenticatedRequest) { Multimap<String, String> headers = HttpHeaderUtils.toMultimap(msg.headers()); ((AuthenticatedRequest) request).addHeaders(headers); X509Certificate clientCert = AuthenticationService.getClientCertificate(ctx); 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); } request.setHttpRequest(msg.copy()); LOG.trace(LOG_PARSED_REQUEST, request); request.validate(); out.add(request); } catch (UnsupportedOperationException | NullPointerException e) { msg.retain(); out.add(msg); return; } try { if (request instanceof AuthenticatedRequest) { try { AuthenticationService.enforceAccess((AuthenticatedRequest) request); } catch (TimelyException e) { if (!security.isAllowAnonymousHttpAccess()) { throw e; } } } } catch (Exception e) { out.clear(); out.add(e); } } HttpRequestDecoder(Security security, Http http); }
HttpRequestDecoder extends MessageToMessageDecoder<FullHttpRequest> implements TimelyHttpHandler { @Override protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception { LOG.trace(LOG_RECEIVED_REQUEST, msg); final String uri = msg.uri(); final QueryStringDecoder decoder = new QueryStringDecoder(uri); if (decoder.path().equals(nonSecureRedirectAddress)) { out.add(new StrictTransportResponse()); return; } final String sessionId = getSessionId(msg); LOG.trace("SessionID: " + sessionId); HttpRequest request; try { if (msg.method().equals(HttpMethod.GET)) { HttpGetRequest get = AnnotationResolver.getClassForHttpGet(decoder.path()); request = get.parseQueryParameters(decoder); } else if (msg.method().equals(HttpMethod.POST)) { HttpPostRequest post = AnnotationResolver.getClassForHttpPost(decoder.path()); String content = ""; ByteBuf body = msg.content(); if (null != body) { content = body.toString(StandardCharsets.UTF_8); } request = post.parseBody(content); } else { TimelyException e = new TimelyException(HttpResponseStatus.METHOD_NOT_ALLOWED.code(), "unhandled method type", ""); e.addResponseHeader(HttpHeaderNames.ALLOW.toString(), HttpMethod.GET.name() + "," + HttpMethod.POST.name()); LOG.warn("Unhandled HTTP request type {}", msg.method()); throw e; } if (request instanceof AuthenticatedRequest) { Multimap<String, String> headers = HttpHeaderUtils.toMultimap(msg.headers()); ((AuthenticatedRequest) request).addHeaders(headers); X509Certificate clientCert = AuthenticationService.getClientCertificate(ctx); 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); } request.setHttpRequest(msg.copy()); LOG.trace(LOG_PARSED_REQUEST, request); request.validate(); out.add(request); } catch (UnsupportedOperationException | NullPointerException e) { msg.retain(); out.add(msg); return; } try { if (request instanceof AuthenticatedRequest) { try { AuthenticationService.enforceAccess((AuthenticatedRequest) request); } catch (TimelyException e) { if (!security.isAllowAnonymousHttpAccess()) { throw e; } } } } catch (Exception e) { out.clear(); out.add(e); } } HttpRequestDecoder(Security security, Http http); static String getSessionId(FullHttpRequest msg); }
HttpRequestDecoder extends MessageToMessageDecoder<FullHttpRequest> implements TimelyHttpHandler { @Override protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception { LOG.trace(LOG_RECEIVED_REQUEST, msg); final String uri = msg.uri(); final QueryStringDecoder decoder = new QueryStringDecoder(uri); if (decoder.path().equals(nonSecureRedirectAddress)) { out.add(new StrictTransportResponse()); return; } final String sessionId = getSessionId(msg); LOG.trace("SessionID: " + sessionId); HttpRequest request; try { if (msg.method().equals(HttpMethod.GET)) { HttpGetRequest get = AnnotationResolver.getClassForHttpGet(decoder.path()); request = get.parseQueryParameters(decoder); } else if (msg.method().equals(HttpMethod.POST)) { HttpPostRequest post = AnnotationResolver.getClassForHttpPost(decoder.path()); String content = ""; ByteBuf body = msg.content(); if (null != body) { content = body.toString(StandardCharsets.UTF_8); } request = post.parseBody(content); } else { TimelyException e = new TimelyException(HttpResponseStatus.METHOD_NOT_ALLOWED.code(), "unhandled method type", ""); e.addResponseHeader(HttpHeaderNames.ALLOW.toString(), HttpMethod.GET.name() + "," + HttpMethod.POST.name()); LOG.warn("Unhandled HTTP request type {}", msg.method()); throw e; } if (request instanceof AuthenticatedRequest) { Multimap<String, String> headers = HttpHeaderUtils.toMultimap(msg.headers()); ((AuthenticatedRequest) request).addHeaders(headers); X509Certificate clientCert = AuthenticationService.getClientCertificate(ctx); 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); } request.setHttpRequest(msg.copy()); LOG.trace(LOG_PARSED_REQUEST, request); request.validate(); out.add(request); } catch (UnsupportedOperationException | NullPointerException e) { msg.retain(); out.add(msg); return; } try { if (request instanceof AuthenticatedRequest) { try { AuthenticationService.enforceAccess((AuthenticatedRequest) request); } catch (TimelyException e) { if (!security.isAllowAnonymousHttpAccess()) { throw e; } } } } catch (Exception e) { out.clear(); out.add(e); } } HttpRequestDecoder(Security security, Http http); static String getSessionId(FullHttpRequest msg);  }