summaryrefslogtreecommitdiffstats
path: root/rest-client/src/main/java/org/openo/baseservice/roa/util/clientsdk/HttpUtil.java
blob: 3c6493c633bc083dea1e02c98443569c782a9b43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openo.baseservice.roa.util.clientsdk;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Http utilities.
 * <br/>
 * <p>
 * </p>
 * 
 * @author
 * @version SDNO 0.5 28-May-2016
 */
public class HttpUtil {

    /**
     * 
     */
    private static final String APPLICATION_JSON = "application/json";

    private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtil.class);

    private HttpUtil() {

    }

    /**
     * Check if the given array contains the given value (with case-insensitive comparison).
     * <br/>
     * 
     * @param array The array
     * @param value The value to search
     * @return true if the array contains the value
     * @since SDNO 0.5
     */
    public static boolean containsIgnoreCase(final String[] array, final String value) {
        for(final String str : array) {
            if(value == null && str == null) {
                return true;
            }
            if(value != null && value.equalsIgnoreCase(str)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Join an array of strings with the given separator.
     * <br/>
     * 
     * @param array The array of strings
     * @param separator The separator
     * @return the resulting string
     * @since SDNO 0.5
     */
    public static String join(final String[] array, final String separator) {
        final int len = array.length;
        if(len == 0) {
            return "";
        }
        final StringBuilder out = new StringBuilder();
        out.append(array[0]);
        for(int i = 1; i < len; i++) {
            out.append(separator).append(array[i]);
        }
        return out.toString();
    }

    /**
     * Format the given parameter object into string.
     * <br/>
     * 
     * @param param param input
     * @return query param string
     * @since SDNO 0.5
     */
    public static String parameterToString(final Object param) {
        if(param == null) {
            return "";
        } else if(param instanceof Date) {
            return Long.toString(((Date)param).getTime());
        } else if(param instanceof Collection) {
            final StringBuilder b = new StringBuilder();
            for(final Object o : (Collection)param) {
                if(b.length() > 0) {
                    b.append(',');
                }
                b.append(String.valueOf(o));
            }
            return b.toString();
        } else {
            return String.valueOf(param);
        }
    }

    /**
     * get accept header
     * <br/>
     * 
     * @param accepts accepts accept types
     * @return the accepts string
     * @since SDNO 0.5
     */
    public static String selectHeaderAccept(final String[] accepts) {
        if(accepts.length == 0) {
            return null;
        }
        if(HttpUtil.containsIgnoreCase(accepts, APPLICATION_JSON)) {
            return APPLICATION_JSON;
        }
        return HttpUtil.join(accepts, ",");
    }

    /**
     * select head content type
     * <br/>
     * 
     * @param contentTypes contentTypes content types
     * @return the json string or the first content type
     * @since SDNO 0.5
     */
    public static String selectHeaderContentType(final String[] contentTypes) {
        if(contentTypes.length == 0) {
            return APPLICATION_JSON;
        }
        if(HttpUtil.containsIgnoreCase(contentTypes, APPLICATION_JSON)) {
            return APPLICATION_JSON;
        }
        return contentTypes[0];
    }

    /**
     * Escape the given string to be used as URL query value.<br/>
     * 
     * @param str str param str
     * @return escape string
     * @since SDNO 0.5
     */
    public static String escapeString(final String str) {
        try {
            return URLEncoder.encode(str, "utf8").replaceAll("\\+", "%20");
        } catch(final UnsupportedEncodingException e) {
            LOGGER.info("UTF8 is not supported", e);
            return str;
        }
    }
}