From 53234a6d048e6f7420f8d13b12c170488e2e2dff Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 7 Sep 2016 11:17:34 -0700 Subject: [PATCH] SGF-514 - Configure HttpService with annotations. (cherry picked from commit f39912a44ca8eb588bddc11cb7606b8d409f220c) Signed-off-by: John Blum --- .../config/annotation/EnableHttpService.java | 92 +++++++++++++++++++ .../annotation/HttpServiceConfiguration.java | 68 ++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 src/main/java/org/springframework/data/gemfire/config/annotation/EnableHttpService.java create mode 100644 src/main/java/org/springframework/data/gemfire/config/annotation/HttpServiceConfiguration.java diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/EnableHttpService.java b/src/main/java/org/springframework/data/gemfire/config/annotation/EnableHttpService.java new file mode 100644 index 00000000..25e10f01 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/EnableHttpService.java @@ -0,0 +1,92 @@ +/* + * Copyright 2012 the original author or authors. + * + * Licensed 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.springframework.data.gemfire.config.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.context.annotation.Import; + +/** + * The EnableHttpService annotation marks a Spring {@link org.springframework.context.annotation.Configuration @Configuration} + * annotated class to configure and enable GemFire/Geode's embedded HTTP service. + * + * By using this annotation, this allows GemFire's embedded HTTP services, like Pulse, the Management REST API + * and the Developer REST API to be enabled. + * + * @author John Blum + * @see org.springframework.data.gemfire.config.annotation.HttpServiceConfiguration + * @see Developing REST Applications for Apache Geode + * @since 1.9.0 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +@Documented +@Import(HttpServiceConfiguration.class) +@SuppressWarnings("unused") +public @interface EnableHttpService { + + /** + * If set, then the GemFire member binds the embedded HTTP service to the specified address. + * If this property is not set but the HTTP service is enabled using {@literal http-service-port}, + * then GemFire binds the HTTP service to the member’s local address. Used by the GemFire Pulse Web application + * and the Developer REST API service. + * + * Defaults to unset. + */ + String bindAddress() default ""; + + /** + * If non-zero, then GemFire starts an embedded HTTP service that listens on this port. The HTTP service + * is used to host the GemFire Pulse Web application and the development REST API service. If you are hosting + * the Pulse web app on your own Web server and are not using the Development REST API service, then disable + * this embedded HTTP service by setting this property to zero. Ignored if {@literal jmx-manager} + * and {@literal start-dev-rest-api} are both set to {@literal false}. + * + * Defaults to {@literal 7070}. + */ + int port() default HttpServiceConfiguration.DEFAULT_HTTP_SERVICE_PORT; + + /** + * Boolean indicating whether to require authentication for HTTP service connections. If this property is not set, + * then GemFire uses the value of {@literal cluster-ssl-require-authentication} to determine whether HTTP service + * connections require authentication. + * + * To enable SSL communications for the HTTP service, use the {@link EnableSsl} annotation and set the + * {@link EnableSsl#components()} to contain + * {@link org.springframework.data.gemfire.config.annotation.EnableSsl.Component#HTTP}. + * + * Defaults to {@literal false}. + */ + boolean sslRequireAuthentication() default false; + + /** + * If set to true, then the developer REST API service will be started when cache is created. + * The REST service can be configured using {@literal http-service-port} and {@literal http-service-bind-address} + * GemFire System Properties. + * + * Defaults to {@literal false}. + */ + boolean startDeveloperRestApi() default false; + +} diff --git a/src/main/java/org/springframework/data/gemfire/config/annotation/HttpServiceConfiguration.java b/src/main/java/org/springframework/data/gemfire/config/annotation/HttpServiceConfiguration.java new file mode 100644 index 00000000..5e082857 --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/config/annotation/HttpServiceConfiguration.java @@ -0,0 +1,68 @@ +/* + * Copyright 2012 the original author or authors. + * + * Licensed 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.springframework.data.gemfire.config.annotation; + +import java.util.Map; +import java.util.Properties; + +import org.springframework.data.gemfire.config.annotation.support.EmbeddedServiceConfigurationSupport; +import org.springframework.data.gemfire.util.PropertiesBuilder; + +/** + * The HttpServiceConfiguration class is a Spring {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar} + * that applies additional GemFire/Geode configuration by way of GemFire/Geode System properties to configure + * GemFire/Geode's embeded HTTP service and services. + * + * @author John Blum + * @see org.springframework.data.gemfire.config.annotation.EnableHttpService + * @see org.springframework.data.gemfire.config.annotation.support.EmbeddedServiceConfigurationSupport + * @see Developing REST Applications for Apache Geode + * @since 1.9.0 + */ +public class HttpServiceConfiguration extends EmbeddedServiceConfigurationSupport { + + public static final boolean DEFAULT_HTTP_SERVICE_SSL_REQUIRE_AUTHENTICATION = false; + public static final boolean DEFAULT_HTTP_SERVICE_START_DEVELOPER_REST_API = false; + + public static final int DEFAULT_HTTP_SERVICE_PORT = 7070; + + /* (non-Javadoc) */ + @Override + protected Class getAnnotationType() { + return EnableHttpService.class; + } + + /* (non-Javadoc) */ + @Override + protected Properties toGemFireProperties(Map annotationAttributes) { + PropertiesBuilder gemfireProperties = PropertiesBuilder.create(); + + gemfireProperties.setProperty("http-service-bind-address", annotationAttributes.get("bindAddress")); + + gemfireProperties.setPropertyIfNotDefault("http-service-port", + annotationAttributes.get("port"), DEFAULT_HTTP_SERVICE_PORT); + + gemfireProperties.setPropertyIfNotDefault("http-service-ssl-require-authentication", + annotationAttributes.get("sslRequireAuthentication"), DEFAULT_HTTP_SERVICE_SSL_REQUIRE_AUTHENTICATION); + + gemfireProperties.setPropertyIfNotDefault("start-dev-rest-api", + annotationAttributes.get("startDeveloperRestApi"), DEFAULT_HTTP_SERVICE_START_DEVELOPER_REST_API); + + return gemfireProperties.build(); + } +}