2 * Copyright 2016 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openo.msb.wrapper;
18 import java.io.IOException;
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;
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;
36 public class MetricsServiceWrapper {
38 private static final Logger LOGGER = LoggerFactory
39 .getLogger(MetricsServiceWrapper.class);
41 public static MetricsInfo getMetricsInfo() {
43 String metricsUrl = MetricsUtil.adminContextPath;
44 String metricsJson = sendGetRequest(metricsUrl);
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,
51 MetricsInfo metricsInfo = new MetricsInfo();
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);
62 public static String sendGetRequest(String url) {
63 CloseableHttpClient httpClient = HttpClients.createDefault();
64 HttpGet httpGet = new HttpGet(url);
67 CloseableHttpResponse res = httpClient.execute(httpGet);
69 if (res.getStatusLine().getStatusCode() == MetricsUtil.SC_OK) {
70 return EntityUtils.toString(res.getEntity());
75 } catch (ParseException e) {
76 LOGGER.error("HttpClient throw ParseException:" + url, e);
77 } catch (IOException e) {
78 LOGGER.error("HttpClient throw IOException:" + url, e);
83 } catch (IOException e) {
84 // TODO Auto-generated catch block
85 LOGGER.error("HttpClient Close throw IOException", e);