OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 180 Rev 185
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
Line 11... Line 11...
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.utils.net;
14
 package org.openconcerto.utils.net;
15
 
15
 
-
 
16
import org.openconcerto.utils.StreamUtils;
-
 
17
 
16
import java.io.IOException;
18
import java.io.IOException;
17
import java.io.InputStream;
19
import java.io.InputStream;
18
import java.io.OutputStream;
20
import java.io.OutputStream;
19
import java.net.URL;
21
import java.net.URL;
20
import java.nio.charset.StandardCharsets;
22
import java.nio.charset.StandardCharsets;
Line 32... Line 34...
32
 
34
 
33
    static public class ServerException extends RuntimeException {
35
    static public class ServerException extends RuntimeException {
34
        private final int responseCode;
36
        private final int responseCode;
35
        private final boolean authenticateError;
37
        private final boolean authenticateError;
36
 
38
 
37
        protected ServerException(int responseCode, boolean authenticateError) {
39
        private ServerException(final String message, final int responseCode, final boolean authenticateError) {
38
            super("Response code was " + responseCode);
40
            super(message);
39
            this.responseCode = responseCode;
41
            this.responseCode = responseCode;
40
            this.authenticateError = authenticateError;
42
            this.authenticateError = authenticateError;
41
        }
43
        }
42
 
44
 
-
 
45
        protected ServerException(int responseCode, boolean authenticateError) {
-
 
46
            this("Response code was " + responseCode, responseCode, authenticateError);
-
 
47
        }
-
 
48
 
-
 
49
        protected ServerException(Response response, String errorBody, boolean authenticateError) {
-
 
50
            this(response.toString() + (errorBody == null ? "" : "\n" + errorBody), response.getCode(), authenticateError);
-
 
51
        }
-
 
52
 
43
        public final int getResponseCode() {
53
        public final int getResponseCode() {
44
            return this.responseCode;
54
            return this.responseCode;
45
        }
55
        }
46
 
56
 
47
        public final boolean isAuthenticateError() {
57
        public final boolean isAuthenticateError() {
Line 51... Line 61...
51
 
61
 
52
    static public final class Response {
62
    static public final class Response {
53
 
63
 
54
        protected final static Response create(HttpsURLConnection con, final Set<Integer> okCodes) throws IOException {
64
        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());
65
            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());
66
            return new Response(con.getURL().toExternalForm(), success, con.getResponseCode(), con.getResponseMessage(), con.getContentEncoding(), con.getContentType());
57
        }
67
        }
58
 
68
 
-
 
69
        private final String url;
59
        private final boolean success;
70
        private final boolean success;
60
        private final int code;
71
        private final int code;
61
        private final String message;
72
        private final String message;
62
        private final String contentEncoding, contentType;
73
        private final String contentEncoding, contentType;
63
 
74
 
64
        protected Response(boolean success, int code, String message, String contentEncoding, String contentType) {
75
        protected Response(final String url, boolean success, int code, String message, String contentEncoding, String contentType) {
65
            super();
76
            super();
-
 
77
            this.url = url;
66
            this.success = success;
78
            this.success = success;
67
            this.code = code;
79
            this.code = code;
68
            this.message = message;
80
            this.message = message;
69
            this.contentEncoding = contentEncoding;
81
            this.contentEncoding = contentEncoding;
70
            this.contentType = contentType;
82
            this.contentType = contentType;
71
        }
83
        }
72
 
84
 
-
 
85
        public final String getURL() {
-
 
86
            return this.url;
-
 
87
        }
-
 
88
 
73
        public final int getCode() {
89
        public final int getCode() {
74
            return this.code;
90
            return this.code;
75
        }
91
        }
76
 
92
 
77
        public final boolean isSuccess() {
93
        public final boolean isSuccess() {
Line 87... Line 103...
87
        }
103
        }
88
 
104
 
89
        public final String getContentType() {
105
        public final String getContentType() {
90
            return this.contentType;
106
            return this.contentType;
91
        }
107
        }
-
 
108
 
-
 
109
        @Override
-
 
110
        public String toString() {
-
 
111
            return this.getClass().getSimpleName() + (this.isSuccess() ? " SUCCESS (" : " ERROR (") + this.getCode() + ") \"" + this.getMessage() + "\" to " + this.url;
-
 
112
        }
92
    }
113
    }
93
 
114
 
94
    private final String url;
115
    private final String url;
95
    private SSLSocketFactory socketFactory;
116
    private SSLSocketFactory socketFactory;
96
    private String token;
117
    private String token;
Line 98... Line 119...
98
 
119
 
99
    public HTTPClient(final String url) {
120
    public HTTPClient(final String url) {
100
        this.url = url;
121
        this.url = url;
101
    }
122
    }
102
 
123
 
-
 
124
    public final String getURL() {
-
 
125
        return this.url;
-
 
126
    }
-
 
127
 
103
    public final SSLSocketFactory getSocketFactory() {
128
    public final SSLSocketFactory getSocketFactory() {
104
        return this.socketFactory;
129
        return this.socketFactory;
105
    }
130
    }
106
 
131
 
107
    public final void setSocketFactory(SSLSocketFactory socketFactory) {
132
    public final void setSocketFactory(SSLSocketFactory socketFactory) {
Line 132... Line 157...
132
        return checkResponseCode(con, null);
157
        return checkResponseCode(con, null);
133
    }
158
    }
134
 
159
 
135
    public final Response checkResponseCode(final HttpsURLConnection con, final Set<Integer> okCodes) throws IOException {
160
    public final Response checkResponseCode(final HttpsURLConnection con, final Set<Integer> okCodes) throws IOException {
136
        final Response res = Response.create(con, okCodes);
161
        final Response res = Response.create(con, okCodes);
137
        if (this.throwsException() && !res.isSuccess())
162
        if (this.throwsException() && !res.isSuccess()) {
-
 
163
            final String errorBody;
-
 
164
            try (final InputStream errorStream = con.getErrorStream()) {
-
 
165
                errorBody = errorStream == null ? null : new String(StreamUtils.read(errorStream), StandardCharsets.UTF_8);
-
 
166
            }
138
            throw new ServerException(res.getCode(), con.getHeaderField("WWW-Authenticate") != null);
167
            throw new ServerException(res, errorBody, con.getHeaderField("WWW-Authenticate") != null);
-
 
168
        }
139
        return res;
169
        return res;
140
    }
170
    }
141
 
171
 
142
    public final HttpsURLConnection openConnection(final String path) throws IOException {
172
    public final HttpsURLConnection openConnection(final String path) throws IOException {
143
        final HttpsURLConnection con = (HttpsURLConnection) new URL(this.url + path).openConnection();
173
        final HttpsURLConnection con = (HttpsURLConnection) new URL(this.url + path).openConnection();