2 * Copyright 2016 Huawei Technologies Co., Ltd.
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.
17 package org.openo.gso.gui.servicegateway.util.register;
19 import java.io.BufferedReader;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
26 import org.apache.commons.lang.StringUtils;
27 import org.openo.baseservice.remoteservice.exception.ServiceException;
28 import org.openo.baseservice.roa.util.restclient.RestfulFactory;
29 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
30 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
31 import org.openo.gso.commsvc.common.constant.Constant;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
39 * @since GSO 0.5, 2016-8-9
41 public class RegisterUtil {
46 private static final Logger LOGGER = LoggerFactory.getLogger(RegisterUtil.class);
48 private RegisterUtil() {
52 * register the service to M-Bus by the parameter<br/>
57 * @since GSO 0.5, 2016-8-9
59 public static void registerService(final String jsonInfo) {
60 // check the parameter
61 if (StringUtils.isEmpty(jsonInfo)) {
62 LOGGER.error("RegisterUtil registerService jsonInfo is null");
66 // replace the remote IP Address by the jsonInfo
67 String bodyData = jsonInfo;
68 boolean isIPExist = bodyData.indexOf(Constant.SERVICE_KEY_IP) > Constant.ZERO;
70 // get the local IP address
71 String localIP = InetAddress.getLocalHost().getHostAddress();
73 // if the jsonInfo have the getInputIP string,start to replace the
76 if (!StringUtils.isEmpty(localIP)) {
77 bodyData = bodyData.replace(Constant.SERVICE_KEY_IP, localIP);
79 LOGGER.error("RegisterUtil registerService localIP is null");
83 } catch (UnknownHostException e) {
84 LOGGER.error("RegisterUtil registerService getHostAddress fail:", e);
86 // if get local IP failed In the isIPExist is true ,operation is
92 // register the service to M-bus by the restful Interface
94 RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(Constant.M_BUS_REGISTER_URL,
95 getRestfulParameters(bodyData));
96 if (null != restfulRsp) {
97 // Record the result of registration
98 // (201:success;415:Invalid Parameter;500:Internal Server Error)
99 LOGGER.info("RegisterUtil registerService register result:", restfulRsp.getStatus());
101 } catch (ServiceException e) {
102 LOGGER.error("RegisterUtil registerService post fail:", e);
107 * get the parameters for restful<br/>
112 * @return the RestfulParametes Instance
113 * @since GSO 0.5, 2016-8-9
115 private static RestfulParametes getRestfulParameters(final String bodyData) {
116 RestfulParametes param = new RestfulParametes();
117 param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);
118 param.setRawData(bodyData);
123 * read the service file<br/>
126 * the service File Path
128 * @since GSO 0.5, 2016-8-9
130 public static String readFile(String path) {
132 if (StringUtils.isEmpty(path)) {
136 File file = new File(path);
137 BufferedReader reader = null;
140 reader = new BufferedReader(new FileReader(file));
141 String tempString = null;
142 // Read one line at a time until the end of the null file.
143 while ((tempString = reader.readLine()) != null) {
145 laststr = laststr + tempString;
148 } catch (IOException e) {
149 LOGGER.error("GSO ReadFile fail.", e);
151 if (reader != null) {
154 } catch (IOException e1) {
155 LOGGER.error("GSO ReadFile reader close fail.", e1);