ConfigurationResourceManager implements LoggableResource { public boolean handleConfigurationResource(final HttpServerRequest request) { final Logger requestLog = RequestLoggerFactory.getLogger(ConfigurationResourceManager.class, request); if(null == getRegisteredResources().get(request.uri())){ return false; } String resourceUri = request.uri(); String resourceSchema = getRegisteredResources().get(request.uri()); if(HttpMethod.PUT == request.method()) { requestLog.info("Refresh resource " + resourceUri); request.bodyHandler(buffer -> configurationResourceValidator.validateConfigurationResource(buffer, resourceSchema, event -> { if (event.failed() || (event.succeeded() && !event.result().isSuccess())) { requestLog.error("Could not parse configuration resource for uri '" + resourceUri + "' message: " + event.result().getMessage()); request.response().setStatusCode(StatusCode.BAD_REQUEST.getStatusCode()); request.response().setStatusMessage(StatusCode.BAD_REQUEST.getStatusMessage() + " " + event.result().getMessage()); ResponseStatusCodeLogUtil.info(request, StatusCode.BAD_REQUEST, ConfigurationResourceManager.class); if (event.result().getValidationDetails() != null) { request.response().headers().add("content-type", "application/json"); request.response().end(event.result().getValidationDetails().encode()); } else { request.response().end(event.result().getMessage()); } } else { storage.put(resourceUri, buffer, status -> { if (status == StatusCode.OK.getStatusCode()) { if(logConfigurationResourceChanges){ RequestLogger.logRequest(vertx.eventBus(), request, status, buffer); } JsonObject object = new JsonObject(); object.put("requestUri", resourceUri); object.put("type", ConfigurationResourceChangeType.CHANGE); vertx.eventBus().publish(CONFIG_RESOURCE_CHANGED_ADDRESS, object); } else { request.response().setStatusCode(status); } ResponseStatusCodeLogUtil.info(request, StatusCode.fromCode(status), ConfigurationResourceManager.class); request.response().end(); }); } })); return true; } if(HttpMethod.DELETE == request.method()) { requestLog.info("Remove resource " + resourceUri); storage.delete(resourceUri, status -> { if (status == StatusCode.OK.getStatusCode()) { JsonObject object = new JsonObject(); object.put("requestUri", resourceUri); object.put("type", ConfigurationResourceChangeType.REMOVE); vertx.eventBus().publish(CONFIG_RESOURCE_CHANGED_ADDRESS, object); } else { request.response().setStatusCode(status); } request.response().end(); }); return true; } return false; } ConfigurationResourceManager(Vertx vertx, final ResourceStorage storage); Future<Optional<Buffer>> getRegisteredResource(String resourceUri); void registerResource(String resourceUri, String resourceSchema); void registerObserver(ConfigurationResourceObserver observer, String resourceUri); @Override void enableResourceLogging(boolean resourceLoggingEnabled); boolean handleConfigurationResource(final HttpServerRequest request); Map<String, List<ConfigurationResourceObserver>> getObservers(); static final String CONFIG_RESOURCE_CHANGED_ADDRESS; }
@Test public void testRequestWithoutUri(TestContext context) { Async async = context.async(); MockResourceStorage storage = new MockResourceStorage(); configurationResourceManager = new ConfigurationResourceManager(vertx, storage); String resourceURI = "/gateleen/resources/person"; PersonResourceRequest request = new PersonResourceRequest(HttpMethod.PUT, null, null, null, new DummyHttpServerResponse()); context.assertFalse(storage.getMockData().containsKey(resourceURI)); boolean handled = configurationResourceManager.handleConfigurationResource(request); context.assertFalse(handled, "PUT Request without uri should not have been handled"); await().atMost(3, SECONDS).until( () -> storage.getMockData().get(resourceURI), nullValue()); async.complete(); }