msb protocol synch change
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / MetricsServiceWrapper.java
1 /**
2  * Copyright 2016 ZTE Corporation.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.msb.wrapper;
17
18 import java.io.IOException;
19
20 import org.apache.http.ParseException;
21 import org.apache.http.client.methods.CloseableHttpResponse;
22 import org.apache.http.client.methods.HttpGet;
23 import org.apache.http.impl.client.CloseableHttpClient;
24 import org.apache.http.impl.client.HttpClients;
25 import org.apache.http.util.EntityUtils;
26 import org.openo.msb.api.MetricsInfo;
27 import org.openo.msb.wrapper.util.MetricsUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
32 import com.fasterxml.jackson.annotation.PropertyAccessor;
33 import com.fasterxml.jackson.databind.DeserializationFeature;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35
36 public class MetricsServiceWrapper {
37
38         private static final Logger LOGGER = LoggerFactory
39                         .getLogger(MetricsServiceWrapper.class);
40
41         public static MetricsInfo getMetricsInfo() {
42
43                 String metricsUrl = MetricsUtil.adminContextPath;
44                 String metricsJson = sendGetRequest(metricsUrl);
45
46                 metricsJson = metricsJson.replace("E_", "");//.replaceAll("(?![0-9])(\\.)(?![0-9])", "_").replace("-", "_")
47                 ObjectMapper mapper = new ObjectMapper();
48                 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
49                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
50                                 false);
51                 MetricsInfo metricsInfo = new MetricsInfo();
52                 try {
53                         metricsInfo = mapper.readValue(metricsJson, MetricsInfo.class);
54                 } catch (Exception e) {
55                         // TODO Auto-generated catch block
56                         LOGGER.error("Jackson readValue to metricsInfo throw exception", e);
57                 }
58
59                 return metricsInfo;
60         }
61
62         public static String sendGetRequest(String url) {
63                 CloseableHttpClient httpClient = HttpClients.createDefault();  
64                 HttpGet httpGet = new HttpGet(url);
65
66                 try {
67                         CloseableHttpResponse res = httpClient.execute(httpGet);
68                         try {
69                                 if (res.getStatusLine().getStatusCode() == MetricsUtil.SC_OK) {
70                                         return EntityUtils.toString(res.getEntity());
71                                 }
72                         } finally {
73                                 res.close();
74                         }
75                 } catch (ParseException e) {
76                         LOGGER.error("HttpClient throw ParseException:" + url, e);
77                 } catch (IOException e) {
78                         LOGGER.error("HttpClient throw IOException:" + url, e);
79                 }
80                 finally{
81                         try {
82                                 httpClient.close();
83                         } catch (IOException e) {
84                                 // TODO Auto-generated catch block
85                                 LOGGER.error("HttpClient Close throw IOException", e);
86                         }
87                 }
88
89                 return null;
90         }
91 }