d7a81161502e8ac3f6dbf4dd32ec7f7d014e4354
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / util / RegExpTestUtil.java
1 /**
2  * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
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.openo.msb.wrapper.util;
17
18 import java.util.regex.Pattern;
19
20 public class RegExpTestUtil {
21   
22  
23     
24   public static boolean  hostRegExpTest(String host){
25       
26       String hostReg = "^(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)"
30               +":(\\d{1,5})$";   
31       return Pattern.matches(hostReg, host); 
32       
33   }
34   
35   public static boolean  ipRegExpTest(String ip){
36       
37       String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."  
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)\\."  
40               +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";  
41       return Pattern.matches(hostReg, ip); 
42       
43   }
44   
45  public static boolean  portRegExpTest(String port){
46       
47       String hostReg = "^\\d{1,5}$";  
48       return Pattern.matches(hostReg, port); 
49       
50   }
51   
52 public static boolean versionRegExpTest(String version){
53       
54       String versionReg = "^v\\d+(\\.\\d+)?$";  
55       return Pattern.matches(versionReg, version); 
56       
57   }
58
59 public static boolean urlRegExpTest(String url){
60     if(url.equals("/")) return true;
61     
62     String urlReg = "^\\/.*((?!\\/).)$";  
63     return Pattern.matches(urlReg, url); 
64     
65 }
66
67 public static boolean apiRouteUrlRegExpTest(String url){
68     
69     String urlReg = "^\\/"+RouteUtil.API_ROOT_PATH+"\\/.*$";  
70     return Pattern.matches(urlReg, url); 
71     
72 }
73
74 public static boolean iuiRouteUrlRegExpTest(String url){
75     
76     String urlReg = "^\\/"+RouteUtil.IUI_ROOT_PATH+"\\/.*$";  
77     return Pattern.matches(urlReg, url); 
78     
79 }
80
81
82   
83   
84   public static void main(String[] args) {
85       System.out.println(urlRegExpTest("/api "));
86 //      System.out.println("api".startsWith("/"));
87   }
88 }