Initial code import
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / MetricsServiceWrapper.java
diff --git a/apiroute/apiroute-service/src/main/java/org/openo/msb/wrapper/MetricsServiceWrapper.java b/apiroute/apiroute-service/src/main/java/org/openo/msb/wrapper/MetricsServiceWrapper.java
new file mode 100644 (file)
index 0000000..656acb8
--- /dev/null
@@ -0,0 +1,92 @@
+/**\r
+* Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
+*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+* http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+\r
+package org.openo.msb.wrapper;\r
+\r
+import java.io.IOException;\r
+\r
+import org.apache.http.ParseException;\r
+import org.apache.http.client.methods.CloseableHttpResponse;\r
+import org.apache.http.client.methods.HttpGet;\r
+import org.apache.http.impl.client.CloseableHttpClient;\r
+import org.apache.http.impl.client.HttpClients;\r
+import org.apache.http.util.EntityUtils;\r
+import org.openo.msb.api.MetricsInfo;\r
+import org.openo.msb.wrapper.util.MetricsUtil;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;\r
+import com.fasterxml.jackson.annotation.PropertyAccessor;\r
+import com.fasterxml.jackson.databind.DeserializationFeature;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+\r
+public class MetricsServiceWrapper {\r
+\r
+       private static final Logger LOGGER = LoggerFactory\r
+                       .getLogger(MetricsServiceWrapper.class);\r
+\r
+       public static MetricsInfo getMetricsInfo() {\r
+\r
+               String metricsUrl = MetricsUtil.adminContextPath;\r
+               String metricsJson = sendGetRequest(metricsUrl);\r
+\r
+               metricsJson = metricsJson.replace("E_", "");//.replaceAll("(?![0-9])(\\.)(?![0-9])", "_").replace("-", "_")\r
+               ObjectMapper mapper = new ObjectMapper();\r
+               mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);\r
+               mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,\r
+                               false);\r
+               MetricsInfo metricsInfo = new MetricsInfo();\r
+               try {\r
+                       metricsInfo = mapper.readValue(metricsJson, MetricsInfo.class);\r
+               } catch (Exception e) {\r
+                       // TODO Auto-generated catch block\r
+                       LOGGER.error("Jackson readValue to metricsInfo throw exception", e);\r
+               }\r
+\r
+               return metricsInfo;\r
+       }\r
+\r
+       public static String sendGetRequest(String url) {\r
+               CloseableHttpClient httpClient = HttpClients.createDefault();  \r
+               HttpGet httpGet = new HttpGet(url);\r
+\r
+               try {\r
+                       CloseableHttpResponse res = httpClient.execute(httpGet);\r
+                       try {\r
+                               if (res.getStatusLine().getStatusCode() == MetricsUtil.SC_OK) {\r
+                                       return EntityUtils.toString(res.getEntity());\r
+                               }\r
+                       } finally {\r
+                               res.close();\r
+                       }\r
+               } catch (ParseException e) {\r
+                       LOGGER.error("HttpClient throw ParseException:" + url, e);\r
+               } catch (IOException e) {\r
+                       LOGGER.error("HttpClient throw IOException:" + url, e);\r
+               }\r
+               finally{\r
+                       try {\r
+                               httpClient.close();\r
+                       } catch (IOException e) {\r
+                               // TODO Auto-generated catch block\r
+                               LOGGER.error("HttpClient Close throw IOException", e);\r
+                       }\r
+               }\r
+\r
+               return null;\r
+       }\r
+}\r