2 * Copyright (c) 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.
16 package org.openo.baseservice.roa.util.clientsdk;
18 import java.io.UnsupportedEncodingException;
19 import java.net.URLEncoder;
20 import java.util.Collection;
21 import java.util.Date;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
33 * @version SDNO 0.5 28-May-2016
35 public class HttpUtil {
40 private static final String APPLICATION_JSON = "application/json";
42 private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtil.class);
49 * Check if the given array contains the given value (with case-insensitive comparison).
52 * @param array The array
53 * @param value The value to search
54 * @return true if the array contains the value
57 public static boolean containsIgnoreCase(final String[] array, final String value) {
58 for(final String str : array) {
59 if(value == null && str == null) {
62 if(value != null && value.equalsIgnoreCase(str)) {
70 * Join an array of strings with the given separator.
73 * @param array The array of strings
74 * @param separator The separator
75 * @return the resulting string
78 public static String join(final String[] array, final String separator) {
79 final int len = array.length;
83 final StringBuilder out = new StringBuilder();
85 for(int i = 1; i < len; i++) {
86 out.append(separator).append(array[i]);
88 return out.toString();
92 * Format the given parameter object into string.
95 * @param param param input
96 * @return query param string
99 public static String parameterToString(final Object param) {
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) {
110 b.append(String.valueOf(o));
114 return String.valueOf(param);
122 * @param accepts accepts accept types
123 * @return the accepts string
126 public static String selectHeaderAccept(final String[] accepts) {
127 if(accepts.length == 0) {
130 if(HttpUtil.containsIgnoreCase(accepts, APPLICATION_JSON)) {
131 return APPLICATION_JSON;
133 return HttpUtil.join(accepts, ",");
137 * select head content type
140 * @param contentTypes contentTypes content types
141 * @return the json string or the first content type
144 public static String selectHeaderContentType(final String[] contentTypes) {
145 if(contentTypes.length == 0) {
146 return APPLICATION_JSON;
148 if(HttpUtil.containsIgnoreCase(contentTypes, APPLICATION_JSON)) {
149 return APPLICATION_JSON;
151 return contentTypes[0];
155 * Escape the given string to be used as URL query value.<br/>
157 * @param str str param str
158 * @return escape string
161 public static String escapeString(final String str) {
163 return URLEncoder.encode(str, "utf8").replaceAll("\\+", "%20");
164 } catch(final UnsupportedEncodingException e) {
165 LOGGER.info("UTF8 is not supported", e);