OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 180 Rev 182
Line 30... Line 30...
30
import java.security.cert.X509Certificate;
30
import java.security.cert.X509Certificate;
31
import java.util.Enumeration;
31
import java.util.Enumeration;
32
import java.util.LinkedHashMap;
32
import java.util.LinkedHashMap;
33
import java.util.Map;
33
import java.util.Map;
34
import java.util.Map.Entry;
34
import java.util.Map.Entry;
-
 
35
import java.util.regex.Pattern;
35
 
36
 
36
import javax.net.ssl.HostnameVerifier;
37
import javax.net.ssl.HostnameVerifier;
37
import javax.net.ssl.HttpsURLConnection;
38
import javax.net.ssl.HttpsURLConnection;
38
import javax.net.ssl.SSLContext;
39
import javax.net.ssl.SSLContext;
39
import javax.net.ssl.SSLSession;
40
import javax.net.ssl.SSLSession;
Line 182... Line 183...
182
        }
183
        }
183
 
184
 
184
        return content;
185
        return content;
185
    }
186
    }
186
 
187
 
187
    /**
-
 
188
     * Encode for POST message application/x-www-form-urlencoded
-
 
189
     */
-
 
190
    static public final String urlEncode(final String... kv) {
188
    static public final String urlEncode(final String... kv) {
-
 
189
        return urlEncode(false, kv);
-
 
190
    }
-
 
191
 
-
 
192
    static public final String urlEncode(final boolean spaceAsPlus, final String... kv) {
191
        final int size = kv.length;
193
        final int size = kv.length;
192
        if (size % 2 != 0)
194
        if (size % 2 != 0)
193
            throw new IllegalArgumentException("Odd number of items : " + size);
195
            throw new IllegalArgumentException("Odd number of items : " + size);
194
        final LinkedHashMap<String, Object> map = new LinkedHashMap<>(size / 2, 1);
196
        final LinkedHashMap<String, Object> map = new LinkedHashMap<>(size / 2, 1);
195
        for (int i = 0; i < size; i += 2) {
197
        for (int i = 0; i < size; i += 2) {
196
            map.put(kv[i], kv[i + 1]);
198
            map.put(kv[i], kv[i + 1]);
197
        }
199
        }
198
        return urlEncode(map);
200
        return urlEncode(map, spaceAsPlus);
199
    }
201
    }
200
 
202
 
201
    /**
203
    /**
202
     * Encode for POST message application/x-www-form-urlencoded
204
     * Encode pairs in application/x-www-form-urlencoded format. Although both the
-
 
205
     * <a href="https://url.spec.whatwg.org/#urlencoded-serializing">URL standard</a> and the
-
 
206
     * <a href="https://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1">HTML standard</a>
-
 
207
     * specifies <code>spaceAsPlus</code> some applications expect the opposite (e.g. POST with
-
 
208
     * jetty).
-
 
209
     * 
-
 
210
     * @param map the values to encode.
-
 
211
     * @param spaceAsPlus <code>true</code> to use '+' for a space, <code>false</code> to use '%20'.
-
 
212
     * @return the encoded string.
-
 
213
     * @see #percentEncode(String, String, boolean)
203
     */
214
     */
204
    static public final String urlEncode(final Map<String, ?> map) {
215
    static public final String urlEncode(final Map<String, ?> map, final boolean spaceAsPlus) {
205
        if (map.isEmpty())
216
        if (map.isEmpty())
206
            return "";
217
            return "";
207
        final String charset = StandardCharsets.UTF_8.name();
218
        final String charset = StandardCharsets.UTF_8.name();
208
        final StringBuilder sb = new StringBuilder(256);
219
        final StringBuilder sb = new StringBuilder(256);
209
        for (final Entry<String, ?> e : map.entrySet()) {
220
        for (final Entry<String, ?> e : map.entrySet()) {
210
            final Object value = e.getValue();
221
            final Object value = e.getValue();
211
            // Avoid null and "null" confusion.
222
            // Avoid null and "null" confusion.
212
            if (value != null) {
223
            if (value != null) {
213
                try {
224
                try {
214
                    sb.append(URLEncoder.encode(e.getKey(), charset).replace("+", "%20"));
225
                    sb.append(percentEncode(e.getKey(), charset, spaceAsPlus));
215
                    sb.append('=');
226
                    sb.append('=');
216
                    sb.append(URLEncoder.encode(String.valueOf(value), charset).replace("+", "%20"));
227
                    sb.append(percentEncode(String.valueOf(value), charset, spaceAsPlus));
217
                    sb.append('&');
228
                    sb.append('&');
218
                } catch (UnsupportedEncodingException exn) {
229
                } catch (UnsupportedEncodingException exn) {
219
                    throw new IllegalStateException("UTF-8 should be standard", exn);
230
                    throw new IllegalStateException("UTF-8 should be standard", exn);
220
                }
231
                }
221
            }
232
            }
Line 223... Line 234...
223
        // remove last '&'
234
        // remove last '&'
224
        sb.setLength(sb.length() - 1);
235
        sb.setLength(sb.length() - 1);
225
        return sb.toString();
236
        return sb.toString();
226
    }
237
    }
227
 
238
 
-
 
239
    static private final Pattern SPACE_PATTERN = Pattern.compile("+", Pattern.LITERAL);
-
 
240
 
-
 
241
    /**
-
 
242
     * Percent encode a string.
-
 
243
     * 
-
 
244
     * @param s the string to encode.
-
 
245
     * @param charset the charset to use.
-
 
246
     * @param spaceAsPlus <code>true</code> to use '+' for a space (as per
-
 
247
     *        <a href="https://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1">HTML</a> and
-
 
248
     *        <a href=
-
 
249
     *        "https://url.spec.whatwg.org/#urlencoded-serializing">application/x-www-form-urlencoded</a>
-
 
250
     *        of URL Standard), <code>false</code> to use '%20' (as per
-
 
251
     *        <a href="https://url.spec.whatwg.org/#path-state">Path part</a> of URL Standard).
-
 
252
     * @return the encoded string.
-
 
253
     * @throws UnsupportedEncodingException if charset is unknown.
-
 
254
     * @see <a href="https://url.spec.whatwg.org/#string-percent-encode-after-encoding">URL
-
 
255
     *      standard</a>
-
 
256
     */
-
 
257
    static public final String percentEncode(final String s, final String charset, final boolean spaceAsPlus) throws UnsupportedEncodingException {
-
 
258
        final String withPlus = URLEncoder.encode(s, charset);
-
 
259
        if (spaceAsPlus)
-
 
260
            return withPlus;
-
 
261
        return SPACE_PATTERN.matcher(withPlus).replaceAll("%20");
-
 
262
    }
-
 
263
 
228
    // Create a trust manager that does not validate certificate chains
264
    // Create a trust manager that does not validate certificate chains
229
    static private final TrustManager[] TRUSTALL_MANAGERS = new TrustManager[] { new X509TrustManager() {
265
    static private final TrustManager[] TRUSTALL_MANAGERS = new TrustManager[] { new X509TrustManager() {
230
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
266
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
231
            return null;
267
            return null;
232
        }
268
        }