Fix register micro service code
[holmes/common.git] / holmes-actions / src / main / java / org / openo / holmes / common / utils / MSBRegisterUtil.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\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.holmes.common.utils;\r
18 \r
19 import com.fasterxml.jackson.databind.ObjectMapper;\r
20 import java.io.IOException;\r
21 import lombok.extern.slf4j.Slf4j;\r
22 import org.apache.commons.lang3.StringUtils;\r
23 import org.apache.http.HttpResponse;\r
24 import org.apache.http.client.methods.HttpPost;\r
25 import org.apache.http.client.methods.HttpRequestBase;\r
26 import org.apache.http.entity.ByteArrayEntity;\r
27 import org.apache.http.impl.client.CloseableHttpClient;\r
28 import org.apache.http.impl.client.HttpClients;\r
29 import org.jvnet.hk2.annotations.Service;\r
30 import org.openo.holmes.common.api.entity.ServiceRegisterEntity;\r
31 import org.openo.holmes.common.config.MicroServiceConfig;\r
32 import org.openo.holmes.common.constant.AlarmConst;\r
33 \r
34 @Slf4j\r
35 @Service\r
36 public class MSBRegisterUtil {\r
37 \r
38     public boolean register(ServiceRegisterEntity entity) throws IOException {\r
39         CloseableHttpClient httpClient = HttpClients.createDefault();\r
40         try {\r
41             ObjectMapper mapper = new ObjectMapper();\r
42             String content = mapper.writeValueAsString(entity);\r
43             HttpPost httpPost = new HttpPost("http://" + MicroServiceConfig.getMsbServerAddr()\r
44                     + ":8086/api/microservices/v1/services?createOrUpdate=false");\r
45             if (StringUtils.isNotEmpty(content)) {\r
46                 httpPost.setEntity(new ByteArrayEntity(content.getBytes()));\r
47             }\r
48             this.setHeader(httpPost);\r
49             HttpResponse response;\r
50             try {\r
51                 response = httpClient.execute(httpPost);\r
52             } catch (Exception e) {\r
53                 log.warn("Registering the service to the bus failure", e);\r
54                 return false;\r
55             }\r
56             if (response.getStatusLine().getStatusCode() == AlarmConst.RESPONSE_STATUS_OK) {\r
57                 log.info("Registration successful service to the bus :" + response.getEntity());\r
58                 return true;\r
59             } else {\r
60                 log.warn(\r
61                         "Registering the service to the bus failure:" + response.getStatusLine().getStatusCode() + " " +\r
62                                 response.getStatusLine().getReasonPhrase() + response.getStatusLine()\r
63                                 .getProtocolVersion());\r
64                 return false;\r
65             }\r
66         } finally {\r
67             httpClient.close();\r
68         }\r
69     }\r
70 \r
71     private void setHeader(HttpRequestBase httpRequestBase) {\r
72         httpRequestBase.setHeader("Content-Type", "text/html;charset=UTF-8");\r
73         httpRequestBase.setHeader("Accept", "application/json");\r
74         httpRequestBase.setHeader("Content-Type", "application/json");\r
75     }\r
76 }