HttpRequestSensor extends AbstractAddSensorFeed<T> { @Override public void apply(final EntityLocal entity) { AttributeSensor<T> sensor = addSensor(entity); String name = initParam(SENSOR_NAME); if (LOG.isDebugEnabled()) { LOG.debug("Adding HTTP JSON sensor {} to {}", name, entity); } final ConfigBag allConfig = ConfigBag.newInstanceCopying(initParams()); new Supplier<URI>() { @Override public URI get() { return URI.create(EntityInitializers.resolve(allConfig, SENSOR_URI)); } }; final Supplier<URI> uri = new UriSupplier(allConfig); final String jsonPath = EntityInitializers.resolve(allConfig, JSON_PATH); final String username = EntityInitializers.resolve(allConfig, USERNAME); final String password = EntityInitializers.resolve(allConfig, PASSWORD); final Map<String, String> headers = EntityInitializers.resolve(allConfig, HEADERS); final Boolean preemptiveBasicAuth = EntityInitializers.resolve(allConfig, PREEMPTIVE_BASIC_AUTH); final Boolean suppressDuplicates = EntityInitializers.resolve(allConfig, SUPPRESS_DUPLICATES); final Duration logWarningGraceTimeOnStartup = EntityInitializers.resolve(allConfig, LOG_WARNING_GRACE_TIME_ON_STARTUP); final Duration logWarningGraceTime = EntityInitializers.resolve(allConfig, LOG_WARNING_GRACE_TIME); Function<? super HttpToolResponse, T> successFunction; if (Strings.isBlank(jsonPath)) { successFunction = (Function) HttpValueFunctions.stringContentsFunction(); } else { successFunction = HttpValueFunctions.<T>jsonContentsFromPath(jsonPath); } HttpPollConfig<T> pollConfig = new HttpPollConfig<T>(sensor) .checkSuccess(HttpValueFunctions.responseCodeEquals(200)) .onFailureOrException(Functions.constant((T) null)) .onSuccess(successFunction) .suppressDuplicates(Boolean.TRUE.equals(suppressDuplicates)) .logWarningGraceTimeOnStartup(logWarningGraceTimeOnStartup) .logWarningGraceTime(logWarningGraceTime) .period(initParam(SENSOR_PERIOD)); HttpFeed.Builder httpRequestBuilder = HttpFeed.builder().entity(entity) .baseUri(uri) .credentialsIfNotNull(username, password) .preemptiveBasicAuth(Boolean.TRUE.equals(preemptiveBasicAuth)) .poll(pollConfig); if (headers != null) { httpRequestBuilder.headers(headers); } HttpFeed feed = httpRequestBuilder.build(); entity.addFeed(feed); } protected  HttpRequestSensor();  HttpRequestSensor(final ConfigBag params); @Override void apply(final EntityLocal entity); static final ConfigKey<String> SENSOR_URI; static final ConfigKey<String> JSON_PATH; static final ConfigKey<String> USERNAME; static final ConfigKey<String> PASSWORD; static final ConfigKey<Map<String, String>> HEADERS; static final ConfigKey<Boolean> PREEMPTIVE_BASIC_AUTH; }
@Test @SuppressWarnings("deprecation") public void testHttpSensor() throws Exception { HttpRequestSensor<Integer> sensor = new HttpRequestSensor<Integer>(ConfigBag.newInstance() .configure(HttpRequestSensor.SENSOR_PERIOD, Duration.millis(100)) .configure(HttpRequestSensor.SENSOR_NAME, SENSOR_STRING.getName()) .configure(HttpRequestSensor.SENSOR_TYPE, STRING_TARGET_TYPE) .configure(HttpRequestSensor.JSON_PATH, "$.myKey") .configure(HttpRequestSensor.SENSOR_URI, serverUrl + "/myKey/myValue")); sensor.apply((org.apache.brooklyn.api.entity.EntityLocal)entity); entity.sensors().set(Attributes.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_STRING, "myValue"); }
@Test(groups="Integration") @SuppressWarnings("deprecation") public void testHttpSensorSuppressingDuplicates() throws Exception { RecordingSensorEventListener<String> listener = new RecordingSensorEventListener<>(); entity.subscriptions().subscribe(entity, SENSOR_STRING, listener); HttpRequestSensor<Integer> sensor = new HttpRequestSensor<Integer>(ConfigBag.newInstance() .configure(HttpRequestSensor.SUPPRESS_DUPLICATES, true) .configure(HttpRequestSensor.SENSOR_PERIOD, Duration.millis(1)) .configure(HttpRequestSensor.SENSOR_NAME, SENSOR_STRING.getName()) .configure(HttpRequestSensor.SENSOR_TYPE, STRING_TARGET_TYPE) .configure(HttpRequestSensor.JSON_PATH, "$.myKey") .configure(HttpRequestSensor.SENSOR_URI, serverUrl + "/myKey/myValue")); sensor.apply((org.apache.brooklyn.api.entity.EntityLocal)entity); entity.sensors().set(Attributes.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_STRING, "myValue"); listener.assertHasEventEventually(Predicates.alwaysTrue()); Asserts.succeedsContinually(new Runnable() { @Override public void run() { listener.assertEventCount(1); }}); }
@Test(groups="Broken") @SuppressWarnings("deprecation") public void testDefaultJsonPath() throws Exception { HttpRequestSensor<Integer> sensor = new HttpRequestSensor<Integer>(ConfigBag.newInstance() .configure(HttpRequestSensor.SENSOR_PERIOD, Duration.millis(100)) .configure(HttpRequestSensor.SENSOR_NAME, SENSOR_STRING.getName()) .configure(HttpRequestSensor.SENSOR_TYPE, STRING_TARGET_TYPE) .configure(HttpRequestSensor.SENSOR_URI, serverUrl + "/jsonstring")); sensor.apply((org.apache.brooklyn.api.entity.EntityLocal)entity); entity.sensors().set(Attributes.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_STRING, "myValue"); }
@Test @SuppressWarnings("deprecation") public void testPreemptiveBasicAuth() throws Exception { HttpRequestSensor<Integer> sensor = new HttpRequestSensor<Integer>(ConfigBag.newInstance() .configure(HttpRequestSensor.PREEMPTIVE_BASIC_AUTH, true) .configure(HttpRequestSensor.USERNAME, "myuser") .configure(HttpRequestSensor.PASSWORD, "mypass") .configure(HttpRequestSensor.SENSOR_PERIOD, Duration.minutes(1)) .configure(HttpRequestSensor.SENSOR_NAME, SENSOR_STRING.getName()) .configure(HttpRequestSensor.SENSOR_TYPE, STRING_TARGET_TYPE) .configure(HttpRequestSensor.JSON_PATH, "$.myKey") .configure(HttpRequestSensor.SENSOR_URI, serverUrl + "/myKey/myValue")); sensor.apply((org.apache.brooklyn.api.entity.EntityLocal)entity); entity.sensors().set(Attributes.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_STRING, "myValue"); HttpRequest req = Iterables.getFirst(recordingHandler.getRequests(), null); String headerVal = req.getFirstHeader("Authorization").getValue(); String expectedVal = HttpFeedTest.getBasicAuthHeaderVal("myuser", "mypass"); assertEquals(headerVal, expectedVal); }
@Test @SuppressWarnings("deprecation") public void testDoNotParseJson() throws Exception { HttpRequestSensor<Integer> sensor = new HttpRequestSensor<Integer>(ConfigBag.newInstance() .configure(HttpRequestSensor.SENSOR_PERIOD, Duration.minutes(1)) .configure(HttpRequestSensor.SENSOR_NAME, SENSOR_STRING.getName()) .configure(HttpRequestSensor.SENSOR_TYPE, STRING_TARGET_TYPE) .configure(HttpRequestSensor.JSON_PATH, "") .configure(HttpRequestSensor.SENSOR_URI, serverUrl + "/myKey/myValue")); sensor.apply((org.apache.brooklyn.api.entity.EntityLocal)entity); entity.sensors().set(Attributes.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_STRING, "{\"myKey\":\"myValue\"}"); }
@Test @SuppressWarnings("deprecation") public void testNonJson() throws Exception { HttpRequestSensor<Integer> sensor = new HttpRequestSensor<Integer>(ConfigBag.newInstance() .configure(HttpRequestSensor.SENSOR_PERIOD, Duration.minutes(1)) .configure(HttpRequestSensor.SENSOR_NAME, SENSOR_STRING.getName()) .configure(HttpRequestSensor.SENSOR_TYPE, STRING_TARGET_TYPE) .configure(HttpRequestSensor.JSON_PATH, "") .configure(HttpRequestSensor.SENSOR_URI, serverUrl + "/nonjson")); sensor.apply((org.apache.brooklyn.api.entity.EntityLocal)entity); entity.sensors().set(Attributes.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_STRING, "myresponse"); }