0e819d3f3e492a18f93447143dd17d267c2e51ca
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / main / java / org / openo / nfvo / jujuvnfmadapter / common / StringUtil.java
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
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
17 package org.openo.nfvo.jujuvnfmadapter.common;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import javax.servlet.http.HttpServletRequest;
25
26 import org.apache.commons.io.IOUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONException;
32 import net.sf.json.JSONObject;
33 import net.sf.json.util.JSONTokener;
34
35 /**
36  * 
37  * String utility class.<br>
38  * <p>
39  * </p>
40  * 
41  * @author
42  * @version     NFVO 0.5  Sep 12, 2016
43  */
44 public final class StringUtil {
45
46     private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class);
47
48     private StringUtil() {
49
50     }
51
52     /**
53      * 
54      * Check whther the string is valid or not.<br>
55      * 
56      * @param str
57      * @return
58      * @since  NFVO 0.5
59      */
60     public static boolean isValidString(String str) {
61         return str != null && !"".equals(str.trim());
62     }
63
64     /**
65      * 
66      * Check whether the Url is valid or not.<br>
67      * 
68      * @param url
69      * @return
70      * @since  NFVO 0.5
71      */
72     public static boolean isValidUrl(String url) {
73         String reg =
74                 "http[s]?://(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)){3}:[0-9]{2,5}";
75         Pattern pattern = Pattern.compile(reg);
76         Matcher matcher = pattern.matcher(url);
77         return matcher.matches();
78     }
79
80     /**
81      * 
82      * Get the JSON string from input http context.<br>
83      * 
84      * @param vnfReq
85      * @return
86      * @since  NFVO 0.5
87      */
88     @SuppressWarnings("unchecked")
89     public static <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
90         try {
91             InputStream vnfInput = vnfReq.getInputStream();
92             String vnfJsonStr = IOUtils.toString(vnfInput);
93             JSONTokener vnfJsonTokener = new JSONTokener(vnfJsonStr);
94
95             if(vnfJsonTokener.nextClean() == Character.codePointAt("{", 0)) {
96                 return (T)JSONObject.fromObject(vnfJsonStr);
97             }
98
99             vnfJsonTokener.back();
100
101             if(vnfJsonTokener.nextClean() == Character.codePointAt("[", 0)) {
102                 return (T)JSONArray.fromObject(vnfJsonStr);
103             }
104         } catch(IOException e) {
105             LOG.error("function=getJsonFromContext, msg=IOException occurs, e={}.", e);
106         } catch(JSONException e) {
107             LOG.error("function=getJsonFromContext, msg=JSONException occurs, e={}.", e);
108         }
109
110         return null;
111     }
112
113     /**
114      * 
115      * Translate sites to site array.<br>
116      * 
117      * @param sites
118      * @return
119      * @since  NFVO 0.5
120      */
121     public static JSONArray transSitesToArray(String sites) {
122         String[] siteList = sites.split("&");
123         int siteSize = siteList.length;
124         JSONArray siteArray = new JSONArray();
125         for(int i = 0; i < siteSize; i++) {
126             siteArray.add(siteList[i]);
127         }
128
129         return siteArray;
130     }
131
132     /**
133      * 
134      * Check whether the string is valid or not.<br>
135      * 
136      * @param fields
137      * @return
138      * @since  NFVO 0.5
139      */
140     public static boolean isValidAnyString(String... fields) {
141         for(String str : fields) {
142             if(!isValidString(str)) {
143                 return false;
144             }
145         }
146         return true;
147     }
148 }