2 * Copyright 2016 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.nfvo.jujuvnfmadapter.common;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 import javax.servlet.http.HttpServletRequest;
26 import org.apache.commons.io.IOUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
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;
37 * String utility class.<br>
42 * @version NFVO 0.5 Sep 12, 2016
44 public final class StringUtil {
46 private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class);
48 private StringUtil() {
54 * Check whther the string is valid or not.<br>
60 public static boolean isValidString(String str) {
61 return str != null && !"".equals(str.trim());
66 * Check whether the Url is valid or not.<br>
72 public static boolean isValidUrl(String url) {
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();
82 * Get the JSON string from input http context.<br>
88 @SuppressWarnings("unchecked")
89 public static <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
91 InputStream vnfInput = vnfReq.getInputStream();
92 String vnfJsonStr = IOUtils.toString(vnfInput);
93 JSONTokener vnfJsonTokener = new JSONTokener(vnfJsonStr);
95 if(vnfJsonTokener.nextClean() == Character.codePointAt("{", 0)) {
96 return (T)JSONObject.fromObject(vnfJsonStr);
99 vnfJsonTokener.back();
101 if(vnfJsonTokener.nextClean() == Character.codePointAt("[", 0)) {
102 return (T)JSONArray.fromObject(vnfJsonStr);
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);
115 * Translate sites to site array.<br>
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]);
134 * Check whether the string is valid or not.<br>
140 public static boolean isValidAnyString(String... fields) {
141 for(String str : fields) {
142 if(!isValidString(str)) {