OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
177 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.net;
15
 
16
import java.io.IOException;
17
import java.io.InputStream;
18
import java.io.OutputStream;
19
import java.net.URL;
20
import java.nio.charset.StandardCharsets;
21
import java.util.Base64;
22
import java.util.Set;
23
import java.util.zip.GZIPInputStream;
24
import java.util.zip.GZIPOutputStream;
25
 
26
import javax.net.ssl.HttpsURLConnection;
27
import javax.net.ssl.SSLSocketFactory;
28
 
29
public class HTTPClient {
30
 
31
    private static final int MIN_GZIP_SIZE = 64;
32
 
33
    static public class ServerException extends RuntimeException {
34
        private final int responseCode;
35
        private final boolean authenticateError;
36
 
37
        protected ServerException(int responseCode, boolean authenticateError) {
38
            super("Response code was " + responseCode);
39
            this.responseCode = responseCode;
40
            this.authenticateError = authenticateError;
41
        }
42
 
43
        public final int getResponseCode() {
44
            return this.responseCode;
45
        }
46
 
47
        public final boolean isAuthenticateError() {
48
            return this.authenticateError;
49
        }
50
    }
51
 
52
    static public final class Response {
53
 
54
        protected final static Response create(HttpsURLConnection con, final Set<Integer> okCodes) throws IOException {
55
            final boolean success = okCodes == null ? con.getResponseCode() == 200 : okCodes.contains(con.getResponseCode());
56
            return new Response(success, con.getResponseCode(), con.getResponseMessage(), con.getContentEncoding(), con.getContentType());
57
        }
58
 
59
        private final boolean success;
60
        private final int code;
61
        private final String message;
62
        private final String contentEncoding, contentType;
63
 
64
        protected Response(boolean success, int code, String message, String contentEncoding, String contentType) {
65
            super();
66
            this.success = success;
67
            this.code = code;
68
            this.message = message;
69
            this.contentEncoding = contentEncoding;
70
            this.contentType = contentType;
71
        }
72
 
73
        public final int getCode() {
74
            return this.code;
75
        }
76
 
77
        public final boolean isSuccess() {
78
            return this.success;
79
        }
80
 
81
        public final String getMessage() {
82
            return this.message;
83
        }
84
 
85
        public final String getContentEncoding() {
86
            return this.contentEncoding;
87
        }
88
 
89
        public final String getContentType() {
90
            return this.contentType;
91
        }
92
    }
93
 
94
    private final String url;
95
    private SSLSocketFactory socketFactory;
96
    private String token;
97
    private boolean throwException = true;
98
 
99
    public HTTPClient(final String url) {
100
        this.url = url;
101
    }
102
 
103
    public final SSLSocketFactory getSocketFactory() {
104
        return this.socketFactory;
105
    }
106
 
107
    public final void setSocketFactory(SSLSocketFactory socketFactory) {
108
        this.socketFactory = socketFactory;
109
    }
110
 
111
    public final void setToken(String token) {
112
        this.token = token;
113
    }
114
 
115
    public final boolean hasToken() {
116
        return this.getToken() != null;
117
    }
118
 
119
    protected final String getToken() {
120
        return this.token;
121
    }
122
 
123
    public final void setThrowException(boolean throwException) {
124
        this.throwException = throwException;
125
    }
126
 
127
    public final boolean throwsException() {
128
        return this.throwException;
129
    }
130
 
131
    public final Response checkResponseCode(final HttpsURLConnection con) throws IOException {
132
        return checkResponseCode(con, null);
133
    }
134
 
135
    public final Response checkResponseCode(final HttpsURLConnection con, final Set<Integer> okCodes) throws IOException {
136
        final Response res = Response.create(con, okCodes);
137
        if (this.throwsException() && !res.isSuccess())
138
            throw new ServerException(res.getCode(), con.getHeaderField("WWW-Authenticate") != null);
139
        return res;
140
    }
141
 
142
    public final HttpsURLConnection openConnection(final String path) throws IOException {
143
        final HttpsURLConnection con = (HttpsURLConnection) new URL(this.url + path).openConnection();
144
        con.setRequestProperty("Accept-Encoding", "gzip");
145
        if (this.getSocketFactory() != null)
146
            con.setSSLSocketFactory(this.getSocketFactory());
147
        if (getToken() != null)
148
            con.setRequestProperty("Authorization", "Bearer " + Base64.getEncoder().encodeToString(getToken().getBytes(StandardCharsets.UTF_8)));
149
        return con;
150
    }
151
 
152
    public final InputStream getInputStream(final HttpsURLConnection con) throws IOException {
153
        return "gzip".equals(con.getContentEncoding()) ? new GZIPInputStream(con.getInputStream()) : con.getInputStream();
154
    }
155
 
156
    public final HttpsURLConnection send(final HttpsURLConnection con, final String params) throws IOException {
157
        return this.send(con, params, true);
158
    }
159
 
160
    public final HttpsURLConnection send(final HttpsURLConnection con, final String params, final boolean allowGzip) throws IOException {
161
        return this.send(con, params.getBytes(StandardCharsets.UTF_8), allowGzip);
162
    }
163
 
164
    public final HttpsURLConnection send(final HttpsURLConnection con, final byte[] toSend, final boolean allowGzip) throws IOException {
165
        final boolean useGzip = allowGzip && toSend.length >= MIN_GZIP_SIZE;
166
        if (useGzip)
167
            con.setRequestProperty("Content-Encoding", "gzip");
168
        con.setRequestMethod("POST");
169
        con.setDoOutput(true);
170
        try (final OutputStream o = useGzip ? new GZIPOutputStream(con.getOutputStream()) : con.getOutputStream()) {
171
            o.write(toSend);
172
        }
173
        return con;
174
    }
175
}