update link to upper-constraints.txt
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / util / RegExpTestUtil.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.apiroute.wrapper.util;
15
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 public class RegExpTestUtil {
20
21
22     private final static String API_KEY_PATTERN = "/api/(?<servicename>[^/]+)(/(?<version>[^/]*)).*";
23
24     private final static String IUI_KEY_PATTERN = "/iui/(?<servicename>[^/]+)/.*";
25
26     public static boolean hostRegExpTest(String host) {
27
28         String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
29                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
30                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
31                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)" + ":(\\d{1,5})$";
32         return Pattern.matches(hostReg, host);
33
34     }
35
36     public static boolean ipRegExpTest(String ip) {
37
38         String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
39                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
40                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
41                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
42         return Pattern.matches(hostReg, ip);
43
44     }
45
46     public static boolean portRegExpTest(String port) {
47
48         String hostReg = "^\\d{1,5}$";
49         return Pattern.matches(hostReg, port);
50
51     }
52
53     public static boolean versionRegExpTest(String version) {
54
55         String versionReg = "^v\\d+(\\.\\d+)?$";
56         return Pattern.matches(versionReg, version);
57
58     }
59
60     public static boolean urlRegExpTest(String url) {
61         if (url.equals("/"))
62             return true;
63
64         String urlReg = "^\\/.*((?!\\/).)$";
65         return Pattern.matches(urlReg, url);
66
67     }
68
69     public static boolean apiRouteUrlRegExpTest(String url) {
70
71         String urlReg = "^\\/" + ConfigUtil.getInstance().getAPI_ROOT_PATH() + "\\/.*$";
72         return Pattern.matches(urlReg, url);
73
74     }
75
76     public static boolean iuiRouteUrlRegExpTest(String url) {
77
78         String urlReg = "^\\/" + ConfigUtil.getInstance().getIUI_ROOT_PATH() + "\\/.*$";
79         return Pattern.matches(urlReg, url);
80
81     }
82
83     public static String[] apiServiceNameMatch4URL(String url) {
84         Pattern redisKeyPattern = Pattern.compile(API_KEY_PATTERN);
85         Matcher matcher = redisKeyPattern.matcher(url + "/");
86         if (matcher.matches()) {
87             String version;
88             if (versionRegExpTest(matcher.group("version"))) {
89                 version = matcher.group("version");
90             } else {
91                 version = "";
92             }
93             return new String[] {matcher.group("servicename"), version};
94         } else {
95             return null;
96         }
97     }
98
99
100     public static String iuiServiceNameMatch4URL(String url) {
101         Pattern redisKeyPattern = Pattern.compile(IUI_KEY_PATTERN);
102         Matcher matcher = redisKeyPattern.matcher(url + "/");
103         if (matcher.matches()) {
104             return matcher.group("servicename");
105         } else {
106             return null;
107         }
108     }
109
110 }