浏览代码

fix #1889 call flush()

Shinsuke Sugaya 6 年之前
父节点
当前提交
26e63e37ae

+ 3 - 2
src/main/java/org/codelibs/fess/api/json/JsonApiManager.java

@@ -55,11 +55,13 @@ import org.codelibs.fess.helper.UserInfoHelper;
 import org.codelibs.fess.mylasta.direction.FessConfig;
 import org.codelibs.fess.util.ComponentUtil;
 import org.codelibs.fess.util.DocumentUtil;
+import org.codelibs.fess.util.EsUtil;
 import org.codelibs.fess.util.FacetResponse;
 import org.codelibs.fess.util.FacetResponse.Field;
 import org.dbflute.optional.OptionalThing;
 import org.elasticsearch.common.xcontent.ToXContent;
 import org.elasticsearch.common.xcontent.XContentFactory;
+import org.elasticsearch.common.xcontent.XContentType;
 import org.elasticsearch.script.Script;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -381,8 +383,7 @@ public class JsonApiManager extends BaseJsonApiManager {
     }
 
     protected String toGeoRequestString(final GeoInfo geoInfo) {
-        try (OutputStream out =
-                geoInfo.toQueryBuilder().toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS).getOutputStream()) {
+        try (OutputStream out = EsUtil.getXContentOutputStream(geoInfo.toQueryBuilder(), XContentType.JSON)) {
             return ((ByteArrayOutputStream) out).toString(Constants.UTF_8);
         } catch (final Exception e) {
             return "{\"error\":\"" + detailedMessage(e) + "\"}";

+ 8 - 4
src/main/java/org/codelibs/fess/entity/PingResponse.java

@@ -19,12 +19,13 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 
+import org.apache.commons.text.StringEscapeUtils;
 import org.codelibs.core.lang.StringUtil;
 import org.codelibs.fess.Constants;
+import org.codelibs.fess.util.EsUtil;
 import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
 import org.elasticsearch.cluster.health.ClusterHealthStatus;
-import org.elasticsearch.common.xcontent.ToXContent;
-import org.elasticsearch.common.xcontent.XContentFactory;
+import org.elasticsearch.common.xcontent.XContentType;
 
 public class PingResponse {
     private final int status;
@@ -39,10 +40,13 @@ public class PingResponse {
         status = response.getStatus() == ClusterHealthStatus.RED ? 1 : 0;
         clusterName = response.getClusterName();
         clusterStatus = response.getStatus().toString();
-        try (OutputStream out = response.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS).getOutputStream()) {
+        try (OutputStream out = EsUtil.getXContentOutputStream(response, XContentType.JSON)) {
             message = ((ByteArrayOutputStream) out).toString(Constants.UTF_8);
+            if (StringUtil.isBlank(message)) {
+                message = "{}";
+            }
         } catch (final IOException e) {
-            message = "{ \"error\" : \"" + e.getMessage() + "\"}";
+            message = "{ \"error\" : \"" + StringEscapeUtils.escapeJson(e.getMessage()) + "\"}";
         }
     }
 

+ 1 - 0
src/main/java/org/codelibs/fess/timer/SystemMonitorTarget.java

@@ -189,6 +189,7 @@ public class SystemMonitorTarget implements TimeoutTarget {
             builder.startObject();
             response.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS);
             builder.endObject();
+            builder.flush();
             try (OutputStream out = builder.getOutputStream()) {
                 stats = ((ByteArrayOutputStream) out).toString(Constants.UTF_8);
             }

+ 32 - 0
src/main/java/org/codelibs/fess/util/EsUtil.java

@@ -0,0 +1,32 @@
+package org.codelibs.fess.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.elasticsearch.common.xcontent.ToXContent;
+import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.elasticsearch.common.xcontent.XContentFactory;
+import org.elasticsearch.common.xcontent.XContentType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class EsUtil {
+
+    private static final Logger logger = LoggerFactory.getLogger(EsUtil.class);
+
+    private EsUtil() {
+    }
+
+    public static OutputStream getXContentOutputStream(final ToXContent xContent, final XContentType xContentType) {
+        try (final XContentBuilder builder = xContent.toXContent(XContentFactory.contentBuilder(xContentType), ToXContent.EMPTY_PARAMS)) {
+            builder.flush();
+            return builder.getOutputStream();
+        } catch (IOException e) {
+            if (logger.isDebugEnabled()) {
+                logger.debug("Failed to print the output.", e);
+            }
+            return new ByteArrayOutputStream();
+        }
+    }
+}