Fix java check style issue
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / wrapper / util / DiscoverUtil.java
1 /**
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.msb.sdclient.wrapper.util;
15
16 import javax.servlet.http.HttpServletRequest;
17
18 import org.apache.commons.lang3.StringUtils;
19
20 /**
21  * @ClassName: ApiRouteUtil
22  * @Description: TODO(ApiRoute工具类)
23  * @author tanghua10186366
24  * @date 2015年9月29日 下午3:19:51
25  * 
26  */
27 public class DiscoverUtil {
28
29
30     public static final String CONSUL_DEFAULT_PORT = "8500";
31
32     public static final String APIGATEWAY_SERVINCE = "apigateway";
33
34     public static final String ROUTER_SERVINCE = "router";
35
36     public static final String APIGATEWAY_SERVINCE_ALL = "all";
37
38     public static final String APIGATEWAY_SERVINCE_DEFAULT = "default";
39
40     public static final String VISUAL_RANGE_IN = "1";
41
42     public static final String VISUAL_RANGE_OUT = "0";
43
44     public static final String SERVICENAME_LINE_NAMESPACE = "-";
45
46     public static final String SPLIT_LINE = "|";
47
48     public static final String EXTERNAL_NODE_NAME = "externalService";
49
50
51     public static final String REQUEST_SUCCESS = "SUCCESS";
52
53     public static final String REQUEST_FAIL = "FAIL";
54
55     public static final String VISUAL_RANGE_LIST = "0,1";
56
57     public static final String PROTOCOL_LIST = "REST,HTTP,MQ,FTP,SNMP,UI,TCP,UDP,PORTAL";
58
59     public static final String LB_POLICY_LIST = "round-robin,ip_hash,least_conn,client_custom";
60
61     public static final String LB_PARAMS_LIST = "weight,max_fails,fail_timeout";
62
63     public static final String CHECK_TYPE_LIST = "HTTP,TCP,TTL";
64
65     public static final String CHECK_HA_ROLE_LIST = "active,standby";
66
67     public static final String CONSUL_CATALOG_URL = "/v1/catalog";
68
69     public static final String CONSUL_AGENT_URL = "/v1/agent/service";
70
71     public static final String CONSUL_AGENT_TTL_URL = "/v1/agent/check/pass/";
72
73     public static final String CONSUL_HEALTH_URL = "/v1/health/service/";
74
75     public static final String[] PUBLISH_PROTOCOL = {"TCP", "UDP", "HTTP", "REST", "UI", "PORTAL"};
76
77     public static final String[] HTTP_PROTOCOL = {"HTTP", "REST", "UI", "PORTAL"};
78
79     public static final String TCP_UDP_PORT_RANGE_START = "28001";
80
81     public static final String TCP_UDP_PORT_RANGE_END = "30000";
82
83     public static final String CONSUL_ADDRESSS = "127.0.0.1:8500";
84
85     public static final String CONSUL_REGISTER_MODE = "catalog";
86
87     // public static boolean isProtocol_tcp_udp(String protocol){
88     // return "TCP".equals(protocol) || "UDP".equals(protocol);
89     // }
90     //
91
92     public static String getRealIp(HttpServletRequest request) {
93         String ip = request.getHeader("X-Forwarded-For");
94         if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
95             // 多次反向代理后会有多个ip值,第一个ip才是真实ip
96             int index = ip.indexOf(",");
97             if (index != -1) {
98                 return ip.substring(0, index);
99             } else {
100                 return ip;
101             }
102         }
103         ip = request.getHeader("X-Real-IP");
104
105         if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
106             return ip;
107         }
108
109
110         return request.getRemoteAddr();
111
112     }
113
114     public static boolean checkVisualRangeIn(String visualRange) {
115         return checkExist(visualRange, DiscoverUtil.VISUAL_RANGE_IN, "|");
116     }
117
118     public static boolean checkVisualRangeOut(String visualRange) {
119         return checkExist(visualRange, DiscoverUtil.VISUAL_RANGE_OUT, "|");
120     }
121
122     public static boolean checkExist(String list, String value, String separator) {
123         String[] listArray = StringUtils.split(list, separator);
124
125         for (int i = 0; i < listArray.length; i++) {
126             if (value.equals(listArray[i]))
127                 return true;
128         }
129         return false;
130     }
131
132     public static boolean checkExist(String[] list, String value) {
133         for (int i = 0; i < list.length; i++) {
134             if (value.equals(list[i]))
135                 return true;
136         }
137         return false;
138     }
139
140     public static boolean contain(String[] array, String value[]) {
141         for (int i = 0; i < array.length; i++) {
142             for (int n = 0; n < value.length; n++) {
143                 if (array[i].equals(value[n])) {
144                     return true;
145                 }
146             }
147         }
148         return false;
149
150     }
151
152
153 }