Fix the issue health check for https failed
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / wrapper / util / RegExpTestUtil.java
1 /**
2  * Copyright 2016-2018 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 java.util.regex.Pattern;
17
18 import org.apache.commons.lang3.StringUtils;
19
20 public class RegExpTestUtil {
21
22     public static boolean httpUrlRegExpTest(String url) {
23
24
25
26         String httpUrlReg = "^(|https:\\/\\/|http:\\/\\/)(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
27                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
28                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
29                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)" + ":(\\d{1,5}).*$";
30         return Pattern.matches(httpUrlReg, url);
31
32     }
33
34     public static boolean hostRegExpTest(String host) {
35
36         String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
37                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
38                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
39                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)" + ":(\\d{1,5})$";
40         return Pattern.matches(hostReg, host);
41
42     }
43
44     public static boolean ipRegExpTest(String ip) {
45
46         String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
47                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
48                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
49                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
50         return Pattern.matches(hostReg, ip);
51
52     }
53
54     public static boolean portRegExpTest(String port) {
55
56         String hostReg = "^\\d{1,5}$";
57         if (Pattern.matches(hostReg, port)) {
58             int iport = Integer.parseInt(port);
59             if (iport >= 1 && iport <= 65535)
60                 return true;
61         }
62
63         return false;
64
65     }
66
67     public static boolean versionRegExpTest(String version) {
68
69         String versionReg = "^v\\d+(\\.\\d+)?$";
70         return Pattern.matches(versionReg, version);
71
72     }
73
74     public static boolean urlRegExpTest(String url) {
75         if (url.equals("/"))
76             return true;
77
78         String urlReg = "^\\/.*((?!\\/).)$";
79         return Pattern.matches(urlReg, url);
80
81     }
82
83     public static boolean serviceNameRegExpTest(String serviceName) {
84
85         String serviceNameReg = "^([0-9a-zA-Z]|-|_)*$";
86         return Pattern.matches(serviceNameReg, serviceName);
87
88     }
89
90     public static boolean apiRouteUrlRegExpTest(String url) {
91
92         String urlReg = "^\\/api\\/.*$";
93         return Pattern.matches(urlReg, url);
94
95     }
96
97     public static boolean labelRegExpTest(String label) {
98
99         String labelReg = "^\\S+:\\S+$";
100         String[] labelArray = StringUtils.split(label, ",");
101         for (int i = 0; i < labelArray.length; i++) {
102             if (!Pattern.matches(labelReg, labelArray[i])) {
103                 return false;
104             }
105         }
106         return true;
107
108     }
109
110
111     public static void main(String[] args) {
112
113         System.out.println(httpUrlRegExpTest("/wqew"));
114     }
115 }