Mercurial > hg > thermostat-ng > web-gateway
changeset 218:61021e0a81d2
Fix NPE when querying /jvms/0.0.1/systems/foo/jvms/bar
This patch fixes an issue where the /jvms web service could no longer
handle .../systems/foo/jvms/bar after the last update.
It also adds a test for this usage, and changes some of the internal
APIs to pass around ints instead of Integer (thus avoiding potential NPEs)
Reviewed-by: sgehwolf
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-July/024322.html
author | Simon Tooke <stooke@redhat.com> |
---|---|
date | Wed, 26 Jul 2017 14:31:56 -0400 |
parents | 8864f8d6e0a2 |
children | 7a5d4d4f9cae |
files | common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/executor/MongoExecutor.java common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/servlet/MongoHttpHandlerHelper.java services/jvms/src/main/java/com/redhat/thermostat/gateway/service/jvms/http/JvmsHttpHandler.java tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvms/JvmsServiceIntegrationTest.java |
diffstat | 4 files changed, 40 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/executor/MongoExecutor.java Wed Jul 26 09:49:28 2017 -0400 +++ b/common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/executor/MongoExecutor.java Wed Jul 26 14:31:56 2017 -0400 @@ -61,13 +61,13 @@ import com.redhat.thermostat.gateway.common.mongodb.keycloak.KeycloakFields; public class MongoExecutor { - public MongoDataResultContainer execGetRequest(MongoCollection<Document> collection, Integer limit, Integer offset, + public MongoDataResultContainer execGetRequest(MongoCollection<Document> collection, int limit, int offset, String sort, String queries, String includes, String excludes, Set<String> realms) throws IOException { return execGetRequest(collection, limit, offset, sort, buildClientQueries(queries), includes, excludes, realms); } - public MongoDataResultContainer execGetRequest(MongoCollection<Document> collection, Integer limit, Integer offset, + public MongoDataResultContainer execGetRequest(MongoCollection<Document> collection, int limit, int offset, String sort, List<String> queries, String includes, String excludes, Set<String> realms) { FindIterable<Document> documents = collection.find(); @@ -179,7 +179,7 @@ } } - public static FindIterable<Document> buildProjection(FindIterable<Document> documents, String includes, String excludes) { + private static FindIterable<Document> buildProjection(FindIterable<Document> documents, String includes, String excludes) { if (excludes != null) { List<String> excludesList = Arrays.asList(excludes.split(",")); documents = documents.projection(fields(exclude(excludesList), excludeId())); @@ -207,7 +207,7 @@ } } - private final boolean isNullOrEmpty(final String s) { + private boolean isNullOrEmpty(final String s) { return s == null || s.isEmpty(); } } \ No newline at end of file
--- a/common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/servlet/MongoHttpHandlerHelper.java Wed Jul 26 09:49:28 2017 -0400 +++ b/common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/servlet/MongoHttpHandlerHelper.java Wed Jul 26 14:31:56 2017 -0400 @@ -81,15 +81,15 @@ * HTTP GET handling */ - public Response handleGetWithSystemID(HttpServletRequest httpServletRequest, ServletContext context, String systemId, Integer limit, Integer offset, String sort, String queries, String includes, String excludes, String returnMetadata) { + public Response handleGetWithSystemID(HttpServletRequest httpServletRequest, ServletContext context, String systemId, int limit, int offset, String sort, String queries, String includes, String excludes, String returnMetadata) { return handleGet(httpServletRequest, context, limit, offset, sort, andSystemIdQuery(queries, systemId), includes, excludes, returnMetadata); } - public Response handleGetWithJvmID(HttpServletRequest httpServletRequest, ServletContext context, String systemId, String jvmId, Integer limit, Integer offset, String sort, String queries, String includes, String excludes, String returnMetadata) { + public Response handleGetWithJvmID(HttpServletRequest httpServletRequest, ServletContext context, String systemId, String jvmId, int limit, int offset, String sort, String queries, String includes, String excludes, String returnMetadata) { return handleGet(httpServletRequest, context, limit, offset, sort, andSystemIdJvmIdQuery(queries, systemId, jvmId), includes, excludes, returnMetadata); } - public Response handleGet(HttpServletRequest httpServletRequest, ServletContext context, Integer limit, Integer offset, String sort, String queries, String includes, String excludes, String returnMetadata) { + public Response handleGet(HttpServletRequest httpServletRequest, ServletContext context, int limit, int offset, String sort, String queries, String includes, String excludes, String returnMetadata) { try { boolean metadata = Boolean.valueOf(returnMetadata); RealmAuthorizer realmAuthorizer = (RealmAuthorizer) httpServletRequest.getAttribute(RealmAuthorizer.class.getName());
--- a/services/jvms/src/main/java/com/redhat/thermostat/gateway/service/jvms/http/JvmsHttpHandler.java Wed Jul 26 09:49:28 2017 -0400 +++ b/services/jvms/src/main/java/com/redhat/thermostat/gateway/service/jvms/http/JvmsHttpHandler.java Wed Jul 26 14:31:56 2017 -0400 @@ -63,6 +63,9 @@ private final JvmInfoMongoStorageHandler mongoStorageHandler = new JvmInfoMongoStorageHandler(); private final MongoHttpHandlerHelper serviceHelper = new MongoHttpHandlerHelper( collectionName ); + private static final int LIMIT_1 = 1; + private static final int OFFSET_ZERO = 0; + @GET @Path("/systems/{" + RequestParameters.SYSTEM_ID +"}") @Consumes({ "application/json" }) @@ -117,7 +120,7 @@ @Context ServletContext context, @Context HttpServletRequest httpServletRequest ) { - return serviceHelper.handleGetWithJvmID(httpServletRequest, context, systemId, jvmId, null, null, null, null, includes, excludes, metadata); + return serviceHelper.handleGetWithJvmID(httpServletRequest, context, systemId, jvmId, LIMIT_1, OFFSET_ZERO, null, null, includes, excludes, metadata); } @PUT
--- a/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvms/JvmsServiceIntegrationTest.java Wed Jul 26 09:49:28 2017 -0400 +++ b/tests/integration-tests/src/test/java/com/redhat/thermostat/gateway/service/jvms/JvmsServiceIntegrationTest.java Wed Jul 26 14:31:56 2017 -0400 @@ -266,6 +266,35 @@ } @Test + public void testGetSysJvm() throws InterruptedException, ExecutionException, TimeoutException { + String url = jvmsUrl + "/systems/1"; + + ContentResponse postResponse = client.newRequest(url).method(HttpMethod.POST) + .content(new StringContentProvider(postData), "application/json").send(); + assertEquals(200, postResponse.getStatus()); + + String query = "/jvms/jid2"; + ContentResponse getResponse = client.newRequest(url + query).method(HttpMethod.GET).send(); + assertEquals(200, getResponse.getStatus()); + String expected = "{\"response\":[{\"agentId\":\"aid\",\"jvmId\":\"jid2\",\"jvmPid\":2," + + "\"startTime\":1495727607481,\"stopTime\":1495727607482,\"javaVersion\":\"1.8.0_131\"," + + "\"javaHome\":\"/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre\"," + + "\"mainClass\":\"mc\",\"javaCommandLine\":\"j cl\",\"jvmName\":\"vm\",\"vmArguments\":" + + "\"-Djline.log.jul\\u003dtrue\",\"jvmInfo\":\"mixed mode\",\"lastUpdated\":333,\"jvmVersion\":" + + "\"25.131-b12\",\"environment\":[{\"key\":\"PATH\",\"value\":\"/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin\"}," + + "{\"key\":\"XAUTHORITY\",\"value\":\"/run/user/1000/gdm/Xauthority\"},{\"key\":\"GDMSESSION\",\"value\":\"i3\"}," + + "{\"key\":\"fish_greeting\",\"value\":\"\"},{\"key\":\"TERM\",\"value\":\"xterm-256color\"},{\"key\":\"DARWIN_MODE\"," + + "\"value\":\"0\"},{\"key\":\"LANG\",\"value\":\"en_US.UTF-8\"},{\"key\":\"DBUS_SESSION_BUS_ADDRESS\"," + + "\"value\":\"unix:path\\u003d/run/user/1000/bus\"},{\"key\":\"XDG_SESSION_ID\",\"value\":\"2\"}," + + "{\"key\":\"XDG_SESSION_TYPE\",\"value\":\"x11\"},{\"key\":\"XDG_CURRENT_DESKTOP\",\"value\":\"i3\"}," + + "{\"key\":\"DISPLAY\",\"value\":\":0\"},{\"key\":\"CYGWIN_MODE\",\"value\":\"0\"},{\"key\":\"COLORTERM\"," + + "\"value\":\"truecolor\"},{\"key\":\"_\",\"value\":\"/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc24.x86_64/jre/../bin/java\"}]," + + "\"uid\":1000,\"username\":\"user\",\"systemId\":\"1\"}]}"; + + assertEquals(expected, getResponse.getContentAsString()); + } + + @Test public void testGetInclude() throws InterruptedException, ExecutionException, TimeoutException { String url = jvmsUrl + "/systems/1";