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