You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2015/09/30 03:56:05 UTC
svn commit: r1705955 - in
/sling/trunk/bundles/extensions/healthcheck/core/src:
main/java/org/apache/sling/hc/core/impl/
main/java/org/apache/sling/hc/core/impl/servlet/
test/java/org/apache/sling/hc/core/impl/servlet/
Author: cziegeler
Date: Wed Sep 30 01:56:04 2015
New Revision: 1705955
URL: http://svn.apache.org/viewvc?rev=1705955&view=rev
Log:
SLING-5076 : Allow health check servlet to be disabled. Apply patch from Georg Henzler
Modified:
sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/JmxAdjustableStatusForTesting.java
sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/servlet/HealthCheckExecutorServlet.java
sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/servlet/HealthCheckExecutorServletTest.java
Modified: sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/JmxAdjustableStatusForTesting.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/JmxAdjustableStatusForTesting.java?rev=1705955&r1=1705954&r2=1705955&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/JmxAdjustableStatusForTesting.java (original)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/JmxAdjustableStatusForTesting.java Wed Sep 30 01:56:04 2015
@@ -51,9 +51,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Allows to dynamically add a health check that returns WARN or CRITICAL for certain tags for testing purposes. Uses an MBean to add/remove the DynamicTestingHealthCheck dynamically. */
-@Component(
- metatype = false,
- immediate = true)
+@Component
public class JmxAdjustableStatusForTesting {
private static final Logger LOG = LoggerFactory.getLogger(JmxAdjustableStatusForTesting.class);
@@ -99,7 +97,7 @@ public class JmxAdjustableStatusForTesti
Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put(HealthCheck.NAME, "JMX-adjustable Testing Check");
props.put(HealthCheck.TAGS, tags);
-
+
healthCheckRegistration = bundleContext.registerService(HealthCheck.class.getName(), healthCheck, props);
}
Modified: sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/servlet/HealthCheckExecutorServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/servlet/HealthCheckExecutorServlet.java?rev=1705955&r1=1705954&r2=1705955&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/servlet/HealthCheckExecutorServlet.java (original)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/servlet/HealthCheckExecutorServlet.java Wed Sep 30 01:56:04 2015
@@ -38,6 +38,7 @@ import org.apache.felix.scr.annotations.
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.hc.api.Result;
import org.apache.sling.hc.api.Result.Status;
import org.apache.sling.hc.api.execution.HealthCheckExecutionOptions;
@@ -80,7 +81,8 @@ public class HealthCheckExecutorServlet
}
}
- static final Param PARAM_TAGS = new Param("tags", "Comma-separated list of health checks tags to select");
+ static final Param PARAM_TAGS = new Param("tags",
+ "Comma-separated list of health checks tags to select - can also be specified via path, e.g. /system/health/tag1,tag2.json");
static final Param PARAM_FORMAT = new Param("format", "Output format, html|json|jsonp|txt - an extension in the URL overrides this");
static final Param PARAM_HTTP_STATUS = new Param("httpStatus", "Specify HTTP result code, for example"
+ " CRITICAL:503 (status 503 if result >= CRITICAL)"
@@ -115,11 +117,17 @@ public class HealthCheckExecutorServlet
private static final String CACHE_CONTROL_VALUE = "no-cache";
private static final String SERVLET_PATH_DEFAULT = "/system/health";
+
public static final String PROPERTY_SERVLET_PATH = "servletPath";
@Property(name = PROPERTY_SERVLET_PATH, label = "Path",
description = "Servlet path (defaults to " + SERVLET_PATH_DEFAULT + " in order to not be accessible via Apache/Internet)", value = SERVLET_PATH_DEFAULT)
private String servletPath;
+ public static final String PROPERTY_DISABLED = "disabled";
+ @Property(name = PROPERTY_DISABLED, label = "Disabled",
+ description = "Allows to disable the servlet if required for security reasons", boolValue = false)
+ private boolean disabled;
+
@Reference
private HttpService httpService;
@@ -137,18 +145,30 @@ public class HealthCheckExecutorServlet
@Activate
protected final void activate(final ComponentContext context) {
+ final Dictionary<?, ?> properties = context.getProperties();
+ this.servletPath = (String) properties.get(PROPERTY_SERVLET_PATH);
+ this.disabled = PropertiesUtil.toBoolean(properties.get(PROPERTY_DISABLED), false);
+
+ if (disabled) {
+ LOG.info("Health Check Servlet is disabled by configuration");
+ return;
+ }
+
try {
- final Dictionary<?, ?> properties = context.getProperties();
- this.servletPath = (String) properties.get(PROPERTY_SERVLET_PATH);
LOG.debug("Registering {} to path {}", getClass().getSimpleName(), this.servletPath);
this.httpService.registerServlet(this.servletPath, this, null, null);
} catch (Exception e) {
- LOG.error("Could not register health check servlet: "+e, e);
+ LOG.error("Could not register health check servlet: " + e, e);
}
+
}
@Deactivate
public void deactivate(final ComponentContext componentContext) {
+ if (disabled) {
+ return;
+ }
+
try {
LOG.debug("Unregistering path {}", this.servletPath);
this.httpService.unregister(this.servletPath);
@@ -160,7 +180,12 @@ public class HealthCheckExecutorServlet
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
- final String[] tags = StringUtils.defaultIfEmpty(request.getParameter(PARAM_TAGS.name), "").split("[, ;]+");
+ String tagsStr = StringUtils.defaultIfEmpty(StringUtils.substringBeforeLast(request.getPathInfo(), "."), "").replace("/", "");
+ if (StringUtils.isBlank(tagsStr)) {
+ // if not provided via path use parameter or default
+ tagsStr = StringUtils.defaultIfEmpty(request.getParameter(PARAM_TAGS.name), "");
+ }
+ final String[] tags = tagsStr.split("[, ;]+");
String format = StringUtils.substringAfterLast(request.getPathInfo(), ".");
if (StringUtils.isBlank(format)) {
Modified: sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/servlet/HealthCheckExecutorServletTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/servlet/HealthCheckExecutorServletTest.java?rev=1705955&r1=1705954&r2=1705955&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/servlet/HealthCheckExecutorServletTest.java (original)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/servlet/HealthCheckExecutorServletTest.java Wed Sep 30 01:56:04 2015
@@ -116,11 +116,10 @@ public class HealthCheckExecutorServletT
public void testDoGetJson() throws ServletException, IOException {
String testTag = "testTag";
- doReturn(testTag).when(request).getParameter(HealthCheckExecutorServlet.PARAM_TAGS.name);
doReturn("true").when(request).getParameter(HealthCheckExecutorServlet.PARAM_COMBINE_TAGS_WITH_OR.name);
int timeout = 5000;
doReturn(timeout + "").when(request).getParameter(HealthCheckExecutorServlet.PARAM_OVERRIDE_GLOBAL_TIMEOUT.name);
- doReturn("/result.json").when(request).getPathInfo();
+ doReturn("/" + testTag + ".json").when(request).getPathInfo();
List<HealthCheckExecutionResult> executionResults = getExecutionResults(Result.Status.WARN);
HealthCheckExecutionOptions options = new HealthCheckExecutionOptions();
options.setCombineTagsWithOr(true);
@@ -141,10 +140,10 @@ public class HealthCheckExecutorServletT
String testTag = "testTag";
doReturn(testTag).when(request).getParameter(HealthCheckExecutorServlet.PARAM_TAGS.name);
+ doReturn("txt").when(request).getParameter(HealthCheckExecutorServlet.PARAM_FORMAT.name);
doReturn("true").when(request).getParameter(HealthCheckExecutorServlet.PARAM_COMBINE_TAGS_WITH_OR.name);
int timeout = 5000;
doReturn(timeout + "").when(request).getParameter(HealthCheckExecutorServlet.PARAM_OVERRIDE_GLOBAL_TIMEOUT.name);
- doReturn("/result.txt").when(request).getPathInfo();
List<HealthCheckExecutionResult> executionResults = getExecutionResults(Result.Status.WARN);
HealthCheckExecutionOptions options = new HealthCheckExecutionOptions();
options.setCombineTagsWithOr(true);