You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2015/08/12 16:07:24 UTC

svn commit: r1695522 - in /sling/trunk/bundles/extensions/healthcheck/core/src: main/java/org/apache/sling/hc/core/impl/servlet/ test/java/org/apache/sling/hc/core/impl/servlet/

Author: bdelacretaz
Date: Wed Aug 12 14:07:24 2015
New Revision: 1695522

URL: http://svn.apache.org/r1695522
Log:
SLING-4862 - minimalistic txt renderer + fix missing jsonp parameter help text

Added:
    sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/servlet/ResultTxtSerializer.java
Modified:
    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/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=1695522&r1=1695521&r2=1695522&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 Aug 12 14:07:24 2015
@@ -81,28 +81,30 @@ public class HealthCheckExecutorServlet
     }
     
     static final Param PARAM_TAGS = new Param("tags", "Comma-separated list of health checks tags to select");
-    static final Param PARAM_FORMAT = new Param("format", "Output format, html or json - an extension in the URL overrides this");
+    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)"
             + " or CRITICAL:503,HEALTH_CHECK_ERROR:500,OK:418 for more specific HTTP status");
 
     static final Param PARAM_COMBINE_TAGS_WITH_OR = new Param("combineTagsWithOr", "Combine tags with OR, active by default. Set to false to combine with AND");
     static final Param PARAM_FORCE_INSTANT_EXECUTION = new Param("forceInstantExecution", "Parameter for the HealthCheckExecutionOptions");
-    static final Param PARAM_OVERRIDE_GLOBAL_TIMEOUT = new Param("timeout", "Override th globale HealthCheckExecutionOptions timeout");
+    static final Param PARAM_OVERRIDE_GLOBAL_TIMEOUT = new Param("timeout", "Override the global HealthCheckExecutionOptions timeout");
 
     static final Param PARAM_INCLUDE_DEBUG = new Param("hcDebug", "Include the DEBUG output of the Health Checks");
     
+    static final String JSONP_CALLBACK_DEFAULT = "processHealthCheckResults";
+    static final Param PARAM_JSONP_CALLBACK = new Param("callback", "name of the JSONP callback function to use, defaults to " + JSONP_CALLBACK_DEFAULT);
+
     static final Param [] PARAM_LIST = { PARAM_TAGS, PARAM_FORMAT, PARAM_HTTP_STATUS, PARAM_COMBINE_TAGS_WITH_OR, 
-        PARAM_FORCE_INSTANT_EXECUTION, PARAM_OVERRIDE_GLOBAL_TIMEOUT, PARAM_INCLUDE_DEBUG};
+        PARAM_FORCE_INSTANT_EXECUTION, PARAM_OVERRIDE_GLOBAL_TIMEOUT, PARAM_INCLUDE_DEBUG, PARAM_JSONP_CALLBACK};
 
     static final String FORMAT_HTML = "html";
     static final String FORMAT_JSON = "json";
     static final String FORMAT_JSONP = "jsonp";
-
-    static final String PARAM_JSONP_CALLBACK = "callback";
-    static final String JSONP_CALLBACK_DEFAULT = "processHealthCheckResults";
+    static final String FORMAT_TXT = "txt";
 
     private static final String CONTENT_TYPE_HTML = "text/html";
+    private static final String CONTENT_TYPE_TXT = "text/plain";
     private static final String CONTENT_TYPE_JSON = "application/json";
     private static final String CONTENT_TYPE_JSONP = "application/javascript";
     private static final String STATUS_HEADER_NAME = "X-Health";
@@ -127,6 +129,9 @@ public class HealthCheckExecutorServlet
 
     @Reference
     ResultJsonSerializer jsonSerializer;
+    
+    @Reference
+    ResultTxtSerializer txtSerializer;
 
     @Activate
     protected final void activate(final ComponentContext context) {
@@ -196,15 +201,23 @@ public class HealthCheckExecutorServlet
         } else if (FORMAT_JSON.equals(format)) {
             sendJsonResponse(overallResult, executionResults, null, response, includeDebug);
         } else if (FORMAT_JSONP.equals(format)) {
-            String jsonpCallback = StringUtils.defaultIfEmpty(request.getParameter(PARAM_JSONP_CALLBACK), JSONP_CALLBACK_DEFAULT);
+            String jsonpCallback = StringUtils.defaultIfEmpty(request.getParameter(PARAM_JSONP_CALLBACK.name), JSONP_CALLBACK_DEFAULT);
             sendJsonResponse(overallResult, executionResults, jsonpCallback, response, includeDebug);
+        } else if (FORMAT_TXT.equals(format)) {
+            sendTxtResponse(overallResult, response);
         } else {
             response.setContentType("text/plain");
-            response.getWriter().println("Invalid format " + format + " - supported formats: html|json|jsonp");
+            response.getWriter().println("Invalid format " + format + " - supported formats: html|json|jsonp|txt");
         }
 
     }
 
+    private void sendTxtResponse(final Result overallResult, final HttpServletResponse response) throws IOException {
+        response.setContentType(CONTENT_TYPE_TXT);
+        response.setCharacterEncoding("UTF-8");
+        response.getWriter().write(txtSerializer.serialize(overallResult));
+    }
+
     private void sendJsonResponse(final Result overallResult, final List<HealthCheckExecutionResult> executionResults, final String jsonpCallback,
             final HttpServletResponse response, boolean includeDebug)
             throws IOException {
@@ -213,6 +226,7 @@ public class HealthCheckExecutorServlet
         } else {
             response.setContentType(CONTENT_TYPE_JSON);
         }
+        response.setCharacterEncoding("UTF-8");
 
         String resultJson = this.jsonSerializer.serialize(overallResult, executionResults, jsonpCallback, includeDebug);
         PrintWriter writer = response.getWriter();
@@ -223,6 +237,7 @@ public class HealthCheckExecutorServlet
             final HttpServletRequest request, final HttpServletResponse response, boolean includeDebug)
             throws IOException {
         response.setContentType(CONTENT_TYPE_HTML);
+        response.setCharacterEncoding("UTF-8");
         response.setHeader(STATUS_HEADER_NAME, overallResult.toString());
         response.getWriter().append(this.htmlSerializer.serialize(overallResult, executionResults, getHtmlHelpText(), includeDebug));
     }

Added: sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/servlet/ResultTxtSerializer.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/servlet/ResultTxtSerializer.java?rev=1695522&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/servlet/ResultTxtSerializer.java (added)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/servlet/ResultTxtSerializer.java Wed Aug 12 14:07:24 2015
@@ -0,0 +1,30 @@
+/*
+ * 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 SF 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.sling.hc.core.impl.servlet;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.hc.api.Result;
+
+@Component
+@Service(ResultTxtSerializer.class)
+public class ResultTxtSerializer {
+    public String serialize(final Result overallResult) {
+        return overallResult.getStatus().toString();
+    }
+}

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=1695522&r1=1695521&r2=1695522&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 Aug 12 14:07:24 2015
@@ -72,6 +72,9 @@ public class HealthCheckExecutorServletT
     private ResultJsonSerializer jsonSerializer;
 
     @Mock
+    private ResultTxtSerializer txtSerializer;
+
+    @Mock
     private ServiceReference hcServiceRef;
 
     @Mock
@@ -86,6 +89,7 @@ public class HealthCheckExecutorServletT
         healthCheckExecutorServlet.healthCheckExecutor = healthCheckExecutor;
         healthCheckExecutorServlet.htmlSerializer = htmlSerializer;
         healthCheckExecutorServlet.jsonSerializer = jsonSerializer;
+        healthCheckExecutorServlet.txtSerializer = txtSerializer;
 
         doReturn(500L).when(hcServiceRef).getProperty(Constants.SERVICE_ID);
         doReturn(writer).when(response).getWriter();
@@ -103,6 +107,7 @@ public class HealthCheckExecutorServletT
         healthCheckExecutorServlet.doGet(request, response);
 
         verifyZeroInteractions(jsonSerializer);
+        verifyZeroInteractions(txtSerializer);
         verify(htmlSerializer)
                 .serialize(resultEquals(new Result(Result.Status.CRITICAL, "Overall Status CRITICAL")), eq(executionResults), contains("Supported URL parameters"), eq(false));
     }
@@ -125,11 +130,35 @@ public class HealthCheckExecutorServletT
         healthCheckExecutorServlet.doGet(request, response);
 
         verifyZeroInteractions(htmlSerializer);
+        verifyZeroInteractions(txtSerializer);
         verify(jsonSerializer).serialize(resultEquals(new Result(Result.Status.WARN, "Overall Status WARN")), eq(executionResults), anyString(),
                 eq(false));
 
     }
 
+    @Test
+    public void testDoGetTxt() 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.txt").when(request).getPathInfo();
+        List<HealthCheckExecutionResult> executionResults = getExecutionResults(Result.Status.WARN);
+        HealthCheckExecutionOptions options = new HealthCheckExecutionOptions();
+        options.setCombineTagsWithOr(true);
+        options.setOverrideGlobalTimeout(timeout);
+        doReturn(executionResults).when(healthCheckExecutor).execute(options, testTag);
+
+        healthCheckExecutorServlet.doGet(request, response);
+
+        verifyZeroInteractions(htmlSerializer);
+        verifyZeroInteractions(jsonSerializer);
+        verify(txtSerializer).serialize(resultEquals(new Result(Result.Status.WARN, "Overall Status WARN")));
+
+    }
+
     private List<HealthCheckExecutionResult> getExecutionResults(Result.Status worstStatus) {
         List<HealthCheckExecutionResult> results = new ArrayList<HealthCheckExecutionResult>();
         results.add(new ExecutionResult(new HealthCheckMetadata(hcServiceRef), new Result(worstStatus, worstStatus.name()), 100));