View Javadoc
1   /*
2    * Copyright 2012-2017 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.entity;
17  
18  import java.io.IOException;
19  
20  import org.codelibs.core.lang.StringUtil;
21  import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
22  import org.elasticsearch.cluster.health.ClusterHealthStatus;
23  import org.elasticsearch.common.xcontent.ToXContent;
24  import org.elasticsearch.common.xcontent.XContentBuilder;
25  import org.elasticsearch.common.xcontent.XContentFactory;
26  
27  public class PingResponse {
28      private final int status;
29  
30      private final String clusterName;
31  
32      private final String clusterStatus;
33  
34      private String message = StringUtil.EMPTY;
35  
36      public PingResponse(final ClusterHealthResponse response) {
37          status = response.getStatus() == ClusterHealthStatus.RED ? 1 : 0;
38          clusterName = response.getClusterName();
39          clusterStatus = response.getStatus().toString();
40          try {
41              final XContentBuilder builder = XContentFactory.jsonBuilder();
42              builder.startObject();
43              response.toXContent(builder, ToXContent.EMPTY_PARAMS);
44              builder.endObject();
45              message = builder.string();
46          } catch (final IOException e) {
47              message = "{ \"error\" : \"" + e.getMessage() + "\"}";
48          }
49      }
50  
51      public int getStatus() {
52          return status;
53      }
54  
55      public String getClusterName() {
56          return clusterName;
57      }
58  
59      public String getClusterStatus() {
60          return clusterStatus;
61      }
62  
63      public String getMessage() {
64          return message;
65      }
66  
67  }