fec194619ad20d175e1f22310c0bfebcbf8316de
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / MetricsServiceWrapper.java
1 /**\r
2  * Copyright 2016 ZTE, Inc. and others.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.openo.msb.wrapper;\r
18 \r
19 import java.io.IOException;\r
20 \r
21 import org.apache.http.ParseException;\r
22 import org.apache.http.client.methods.CloseableHttpResponse;\r
23 import org.apache.http.client.methods.HttpGet;\r
24 import org.apache.http.impl.client.CloseableHttpClient;\r
25 import org.apache.http.impl.client.HttpClients;\r
26 import org.apache.http.util.EntityUtils;\r
27 import org.openo.msb.api.MetricsInfo;\r
28 import org.openo.msb.wrapper.util.MetricsUtil;\r
29 import org.slf4j.Logger;\r
30 import org.slf4j.LoggerFactory;\r
31 \r
32 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;\r
33 import com.fasterxml.jackson.annotation.PropertyAccessor;\r
34 import com.fasterxml.jackson.databind.DeserializationFeature;\r
35 import com.fasterxml.jackson.databind.ObjectMapper;\r
36 \r
37 public class MetricsServiceWrapper {\r
38 \r
39         private static final Logger LOGGER = LoggerFactory\r
40                         .getLogger(MetricsServiceWrapper.class);\r
41 \r
42         public static MetricsInfo getMetricsInfo() {\r
43 \r
44                 String metricsUrl = MetricsUtil.adminContextPath;\r
45                 String metricsJson = sendGetRequest(metricsUrl);\r
46 \r
47                 metricsJson = metricsJson.replace("E_", "");//.replaceAll("(?![0-9])(\\.)(?![0-9])", "_").replace("-", "_")\r
48                 ObjectMapper mapper = new ObjectMapper();\r
49                 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);\r
50                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,\r
51                                 false);\r
52                 MetricsInfo metricsInfo = new MetricsInfo();\r
53                 try {\r
54                         metricsInfo = mapper.readValue(metricsJson, MetricsInfo.class);\r
55                 } catch (Exception e) {\r
56                         // TODO Auto-generated catch block\r
57                         LOGGER.error("Jackson readValue to metricsInfo throw exception", e);\r
58                 }\r
59 \r
60                 return metricsInfo;\r
61         }\r
62 \r
63         public static String sendGetRequest(String url) {\r
64                 CloseableHttpClient httpClient = HttpClients.createDefault();  \r
65                 HttpGet httpGet = new HttpGet(url);\r
66 \r
67                 try {\r
68                         CloseableHttpResponse res = httpClient.execute(httpGet);\r
69                         try {\r
70                                 if (res.getStatusLine().getStatusCode() == MetricsUtil.SC_OK) {\r
71                                         return EntityUtils.toString(res.getEntity());\r
72                                 }\r
73                         } finally {\r
74                                 res.close();\r
75                         }\r
76                 } catch (ParseException e) {\r
77                         LOGGER.error("HttpClient throw ParseException:" + url, e);\r
78                 } catch (IOException e) {\r
79                         LOGGER.error("HttpClient throw IOException:" + url, e);\r
80                 }\r
81                 finally{\r
82                         try {\r
83                                 httpClient.close();\r
84                         } catch (IOException e) {\r
85                                 // TODO Auto-generated catch block\r
86                                 LOGGER.error("HttpClient Close throw IOException", e);\r
87                         }\r
88                 }\r
89 \r
90                 return null;\r
91         }\r
92 }\r