Move HttpRequestSensor to o.a.b.core.sensor.http Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/1dde632f Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/1dde632f Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/1dde632f
Branch: refs/heads/master Commit: 1dde632fe624e1efec91573715c9470e12fd18e9 Parents: 080d2de Author: Aled Sage <[email protected]> Authored: Thu Aug 20 11:27:20 2015 +0100 Committer: Aled Sage <[email protected]> Committed: Thu Aug 20 11:27:20 2015 +0100 ---------------------------------------------------------------------- .../brooklyn/core/sensor/HttpRequestSensor.java | 96 ------------------- .../core/sensor/http/HttpRequestSensor.java | 97 ++++++++++++++++++++ .../core/sensor/ssh/SshCommandSensor.java | 2 +- .../core/sensor/HttpRequestSensorTest.java | 85 ----------------- .../core/sensor/http/HttpRequestSensorTest.java | 85 +++++++++++++++++ .../entity/java/JmxAttributeSensor.java | 2 +- 6 files changed, 184 insertions(+), 183 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/1dde632f/core/src/main/java/org/apache/brooklyn/core/sensor/HttpRequestSensor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/sensor/HttpRequestSensor.java b/core/src/main/java/org/apache/brooklyn/core/sensor/HttpRequestSensor.java deleted file mode 100644 index 542fc01..0000000 --- a/core/src/main/java/org/apache/brooklyn/core/sensor/HttpRequestSensor.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.core.sensor; - -import java.net.URI; - -import net.minidev.json.JSONObject; - -import org.apache.brooklyn.api.entity.EntityLocal; -import org.apache.brooklyn.config.ConfigKey; -import org.apache.brooklyn.core.config.ConfigKeys; -import org.apache.brooklyn.core.effector.AddSensor; -import org.apache.brooklyn.feed.http.HttpFeed; -import org.apache.brooklyn.feed.http.HttpPollConfig; -import org.apache.brooklyn.feed.http.HttpValueFunctions; -import org.apache.brooklyn.util.core.config.ConfigBag; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.annotations.Beta; -import com.google.common.base.Functions; -import com.google.common.base.Supplier; - -/** - * Configurable {@link org.apache.brooklyn.api.entity.EntityInitializer} which adds an HTTP sensor feed to retrieve the - * {@link JSONObject} from a JSON response in order to populate the sensor with the data at the {@code jsonPath}. - * - * @see SshCommandSensor - * @see JmxAttributeSensor - */ -@Beta -public final class HttpRequestSensor<T> extends AddSensor<T> { - - private static final Logger LOG = LoggerFactory.getLogger(HttpRequestSensor.class); - - public static final ConfigKey<String> SENSOR_URI = ConfigKeys.newStringConfigKey("uri", "HTTP URI to poll for JSON"); - public static final ConfigKey<String> JSON_PATH = ConfigKeys.newStringConfigKey("jsonPath", "JSON path to select in HTTP response; default $", "$"); - public static final ConfigKey<String> USERNAME = ConfigKeys.newStringConfigKey("username", "Username for HTTP request, if required"); - public static final ConfigKey<String> PASSWORD = ConfigKeys.newStringConfigKey("password", "Password for HTTP request, if required"); - - protected final Supplier<URI> uri; - protected final String jsonPath; - protected final String username; - protected final String password; - - public HttpRequestSensor(final ConfigBag params) { - super(params); - - uri = new Supplier<URI>() { - @Override - public URI get() { - return URI.create(params.get(SENSOR_URI)); - } - }; - jsonPath = params.get(JSON_PATH); - username = params.get(USERNAME); - password = params.get(PASSWORD); - } - - @Override - public void apply(final EntityLocal entity) { - super.apply(entity); - - if (LOG.isDebugEnabled()) { - LOG.debug("Adding HTTP JSON sensor {} to {}", name, entity); - } - - HttpPollConfig<T> pollConfig = new HttpPollConfig<T>(sensor) - .checkSuccess(HttpValueFunctions.responseCodeEquals(200)) - .onFailureOrException(Functions.constant((T) null)) - .onSuccess(HttpValueFunctions.<T>jsonContentsFromPath(jsonPath)) - .period(period); - - HttpFeed.builder().entity(entity) - .baseUri(uri) - .credentialsIfNotNull(username, password) - .poll(pollConfig) - .build(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/1dde632f/core/src/main/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensor.java b/core/src/main/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensor.java new file mode 100644 index 0000000..8541fd1 --- /dev/null +++ b/core/src/main/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensor.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.core.sensor.http; + +import java.net.URI; + +import net.minidev.json.JSONObject; + +import org.apache.brooklyn.api.entity.EntityLocal; +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.effector.AddSensor; +import org.apache.brooklyn.core.sensor.ssh.SshCommandSensor; +import org.apache.brooklyn.feed.http.HttpFeed; +import org.apache.brooklyn.feed.http.HttpPollConfig; +import org.apache.brooklyn.feed.http.HttpValueFunctions; +import org.apache.brooklyn.util.core.config.ConfigBag; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.annotations.Beta; +import com.google.common.base.Functions; +import com.google.common.base.Supplier; + +/** + * Configurable {@link org.apache.brooklyn.api.entity.EntityInitializer} which adds an HTTP sensor feed to retrieve the + * {@link JSONObject} from a JSON response in order to populate the sensor with the data at the {@code jsonPath}. + * + * @see SshCommandSensor + * @see JmxAttributeSensor + */ +@Beta +public final class HttpRequestSensor<T> extends AddSensor<T> { + + private static final Logger LOG = LoggerFactory.getLogger(HttpRequestSensor.class); + + public static final ConfigKey<String> SENSOR_URI = ConfigKeys.newStringConfigKey("uri", "HTTP URI to poll for JSON"); + public static final ConfigKey<String> JSON_PATH = ConfigKeys.newStringConfigKey("jsonPath", "JSON path to select in HTTP response; default $", "$"); + public static final ConfigKey<String> USERNAME = ConfigKeys.newStringConfigKey("username", "Username for HTTP request, if required"); + public static final ConfigKey<String> PASSWORD = ConfigKeys.newStringConfigKey("password", "Password for HTTP request, if required"); + + protected final Supplier<URI> uri; + protected final String jsonPath; + protected final String username; + protected final String password; + + public HttpRequestSensor(final ConfigBag params) { + super(params); + + uri = new Supplier<URI>() { + @Override + public URI get() { + return URI.create(params.get(SENSOR_URI)); + } + }; + jsonPath = params.get(JSON_PATH); + username = params.get(USERNAME); + password = params.get(PASSWORD); + } + + @Override + public void apply(final EntityLocal entity) { + super.apply(entity); + + if (LOG.isDebugEnabled()) { + LOG.debug("Adding HTTP JSON sensor {} to {}", name, entity); + } + + HttpPollConfig<T> pollConfig = new HttpPollConfig<T>(sensor) + .checkSuccess(HttpValueFunctions.responseCodeEquals(200)) + .onFailureOrException(Functions.constant((T) null)) + .onSuccess(HttpValueFunctions.<T>jsonContentsFromPath(jsonPath)) + .period(period); + + HttpFeed.builder().entity(entity) + .baseUri(uri) + .credentialsIfNotNull(username, password) + .poll(pollConfig) + .build(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/1dde632f/core/src/main/java/org/apache/brooklyn/core/sensor/ssh/SshCommandSensor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/sensor/ssh/SshCommandSensor.java b/core/src/main/java/org/apache/brooklyn/core/sensor/ssh/SshCommandSensor.java index c418f93..0218a99 100644 --- a/core/src/main/java/org/apache/brooklyn/core/sensor/ssh/SshCommandSensor.java +++ b/core/src/main/java/org/apache/brooklyn/core/sensor/ssh/SshCommandSensor.java @@ -26,7 +26,7 @@ import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.core.config.ConfigKeys; import org.apache.brooklyn.core.effector.AddSensor; import org.apache.brooklyn.core.entity.BrooklynConfigKeys; -import org.apache.brooklyn.core.sensor.HttpRequestSensor; +import org.apache.brooklyn.core.sensor.http.HttpRequestSensor; import org.apache.brooklyn.feed.ssh.SshFeed; import org.apache.brooklyn.feed.ssh.SshPollConfig; import org.apache.brooklyn.feed.ssh.SshValueFunctions; http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/1dde632f/core/src/test/java/org/apache/brooklyn/core/sensor/HttpRequestSensorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/sensor/HttpRequestSensorTest.java b/core/src/test/java/org/apache/brooklyn/core/sensor/HttpRequestSensorTest.java deleted file mode 100644 index 4715594..0000000 --- a/core/src/test/java/org/apache/brooklyn/core/sensor/HttpRequestSensorTest.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.core.sensor; - -import org.apache.brooklyn.api.entity.EntityLocal; -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.api.location.Location; -import org.apache.brooklyn.api.sensor.AttributeSensor; -import org.apache.brooklyn.core.entity.Attributes; -import org.apache.brooklyn.core.entity.Entities; -import org.apache.brooklyn.core.sensor.HttpRequestSensor; -import org.apache.brooklyn.core.sensor.Sensors; -import org.apache.brooklyn.core.test.TestHttpRequestHandler; -import org.apache.brooklyn.core.test.TestHttpServer; -import org.apache.brooklyn.core.test.entity.TestApplication; -import org.apache.brooklyn.core.test.entity.TestEntity; -import org.apache.brooklyn.test.EntityTestUtils; -import org.apache.brooklyn.util.core.config.ConfigBag; -import org.apache.brooklyn.util.time.Duration; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableList; - -public class HttpRequestSensorTest { - final static AttributeSensor<String> SENSOR_STRING = Sensors.newStringSensor("aString"); - final static String TARGET_TYPE = "java.lang.String"; - - private TestApplication app; - private EntityLocal entity; - - private TestHttpServer server; - private String serverUrl; - - @BeforeClass(alwaysRun=true) - public void setUp() throws Exception { - server = new TestHttpServer() - .handler("/myKey/myValue", new TestHttpRequestHandler().header("Content-Type", "application/json").response("{\"myKey\":\"myValue\"}")) - .start(); - serverUrl = server.getUrl(); - - app = TestApplication.Factory.newManagedInstanceForTests(); - entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) - .location(app.newLocalhostProvisioningLocation().obtain())); - app.start(ImmutableList.<Location>of()); - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - if (app != null) Entities.destroyAll(app.getManagementContext()); - server.stop(); - } - - @Test - 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, TARGET_TYPE) - .configure(HttpRequestSensor.JSON_PATH, "$.myKey") - .configure(HttpRequestSensor.SENSOR_URI, serverUrl + "/myKey/myValue")); - sensor.apply(entity); - entity.setAttribute(Attributes.SERVICE_UP, true); - - EntityTestUtils.assertAttributeEqualsEventually(entity, SENSOR_STRING, "myValue"); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/1dde632f/core/src/test/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensorTest.java b/core/src/test/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensorTest.java new file mode 100644 index 0000000..2fda742 --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/core/sensor/http/HttpRequestSensorTest.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.core.sensor.http; + +import org.apache.brooklyn.api.entity.EntityLocal; +import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.location.Location; +import org.apache.brooklyn.api.sensor.AttributeSensor; +import org.apache.brooklyn.core.entity.Attributes; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.sensor.Sensors; +import org.apache.brooklyn.core.sensor.http.HttpRequestSensor; +import org.apache.brooklyn.core.test.TestHttpRequestHandler; +import org.apache.brooklyn.core.test.TestHttpServer; +import org.apache.brooklyn.core.test.entity.TestApplication; +import org.apache.brooklyn.core.test.entity.TestEntity; +import org.apache.brooklyn.test.EntityTestUtils; +import org.apache.brooklyn.util.core.config.ConfigBag; +import org.apache.brooklyn.util.time.Duration; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; + +public class HttpRequestSensorTest { + final static AttributeSensor<String> SENSOR_STRING = Sensors.newStringSensor("aString"); + final static String TARGET_TYPE = "java.lang.String"; + + private TestApplication app; + private EntityLocal entity; + + private TestHttpServer server; + private String serverUrl; + + @BeforeClass(alwaysRun=true) + public void setUp() throws Exception { + server = new TestHttpServer() + .handler("/myKey/myValue", new TestHttpRequestHandler().header("Content-Type", "application/json").response("{\"myKey\":\"myValue\"}")) + .start(); + serverUrl = server.getUrl(); + + app = TestApplication.Factory.newManagedInstanceForTests(); + entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) + .location(app.newLocalhostProvisioningLocation().obtain())); + app.start(ImmutableList.<Location>of()); + } + + @AfterMethod(alwaysRun=true) + public void tearDown() throws Exception { + if (app != null) Entities.destroyAll(app.getManagementContext()); + server.stop(); + } + + @Test + 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, TARGET_TYPE) + .configure(HttpRequestSensor.JSON_PATH, "$.myKey") + .configure(HttpRequestSensor.SENSOR_URI, serverUrl + "/myKey/myValue")); + sensor.apply(entity); + entity.setAttribute(Attributes.SERVICE_UP, true); + + EntityTestUtils.assertAttributeEqualsEventually(entity, SENSOR_STRING, "myValue"); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/1dde632f/software/base/src/main/java/org/apache/brooklyn/entity/java/JmxAttributeSensor.java ---------------------------------------------------------------------- diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/java/JmxAttributeSensor.java b/software/base/src/main/java/org/apache/brooklyn/entity/java/JmxAttributeSensor.java index 595547e..a629779 100644 --- a/software/base/src/main/java/org/apache/brooklyn/entity/java/JmxAttributeSensor.java +++ b/software/base/src/main/java/org/apache/brooklyn/entity/java/JmxAttributeSensor.java @@ -29,7 +29,7 @@ import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.core.config.ConfigKeys; import org.apache.brooklyn.core.effector.AddSensor; import org.apache.brooklyn.core.sensor.DependentConfiguration; -import org.apache.brooklyn.core.sensor.HttpRequestSensor; +import org.apache.brooklyn.core.sensor.http.HttpRequestSensor; import org.apache.brooklyn.core.sensor.ssh.SshCommandSensor; import org.apache.brooklyn.feed.jmx.JmxAttributePollConfig; import org.apache.brooklyn.feed.jmx.JmxFeed;
