CommonLibrary(util/rest-client) code upload.
[vfc/nfvo/wfengine.git] / rest-client / src / main / java / org / openo / baseservice / roa / util / clientsdk / HttpUtil.java
1 /*
2  * Copyright (c) 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 package org.openo.baseservice.roa.util.clientsdk;
17
18 import java.io.UnsupportedEncodingException;
19 import java.net.URLEncoder;
20 import java.util.Collection;
21 import java.util.Date;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Http utilities.
28  * <br/>
29  * <p>
30  * </p>
31  * 
32  * @author
33  * @version SDNO 0.5 28-May-2016
34  */
35 public class HttpUtil {
36
37     /**
38      * 
39      */
40     private static final String APPLICATION_JSON = "application/json";
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtil.class);
43
44     private HttpUtil() {
45
46     }
47
48     /**
49      * Check if the given array contains the given value (with case-insensitive comparison).
50      * <br/>
51      * 
52      * @param array The array
53      * @param value The value to search
54      * @return true if the array contains the value
55      * @since SDNO 0.5
56      */
57     public static boolean containsIgnoreCase(final String[] array, final String value) {
58         for(final String str : array) {
59             if(value == null && str == null) {
60                 return true;
61             }
62             if(value != null && value.equalsIgnoreCase(str)) {
63                 return true;
64             }
65         }
66         return false;
67     }
68
69     /**
70      * Join an array of strings with the given separator.
71      * <br/>
72      * 
73      * @param array The array of strings
74      * @param separator The separator
75      * @return the resulting string
76      * @since SDNO 0.5
77      */
78     public static String join(final String[] array, final String separator) {
79         final int len = array.length;
80         if(len == 0) {
81             return "";
82         }
83         final StringBuilder out = new StringBuilder();
84         out.append(array[0]);
85         for(int i = 1; i < len; i++) {
86             out.append(separator).append(array[i]);
87         }
88         return out.toString();
89     }
90
91     /**
92      * Format the given parameter object into string.
93      * <br/>
94      * 
95      * @param param param input
96      * @return query param string
97      * @since SDNO 0.5
98      */
99     public static String parameterToString(final Object param) {
100         if(param == null) {
101             return "";
102         } else if(param instanceof Date) {
103             return Long.toString(((Date)param).getTime());
104         } else if(param instanceof Collection) {
105             final StringBuilder b = new StringBuilder();
106             for(final Object o : (Collection)param) {
107                 if(b.length() > 0) {
108                     b.append(',');
109                 }
110                 b.append(String.valueOf(o));
111             }
112             return b.toString();
113         } else {
114             return String.valueOf(param);
115         }
116     }
117
118     /**
119      * get accept header
120      * <br/>
121      * 
122      * @param accepts accepts accept types
123      * @return the accepts string
124      * @since SDNO 0.5
125      */
126     public static String selectHeaderAccept(final String[] accepts) {
127         if(accepts.length == 0) {
128             return null;
129         }
130         if(HttpUtil.containsIgnoreCase(accepts, APPLICATION_JSON)) {
131             return APPLICATION_JSON;
132         }
133         return HttpUtil.join(accepts, ",");
134     }
135
136     /**
137      * select head content type
138      * <br/>
139      * 
140      * @param contentTypes contentTypes content types
141      * @return the json string or the first content type
142      * @since SDNO 0.5
143      */
144     public static String selectHeaderContentType(final String[] contentTypes) {
145         if(contentTypes.length == 0) {
146             return APPLICATION_JSON;
147         }
148         if(HttpUtil.containsIgnoreCase(contentTypes, APPLICATION_JSON)) {
149             return APPLICATION_JSON;
150         }
151         return contentTypes[0];
152     }
153
154     /**
155      * Escape the given string to be used as URL query value.<br/>
156      * 
157      * @param str str param str
158      * @return escape string
159      * @since SDNO 0.5
160      */
161     public static String escapeString(final String str) {
162         try {
163             return URLEncoder.encode(str, "utf8").replaceAll("\\+", "%20");
164         } catch(final UnsupportedEncodingException e) {
165             LOGGER.info("UTF8 is not supported", e);
166             return str;
167         }
168     }
169 }