2
2
3
3
import org .javawebstack .abstractdata .AbstractMapper ;
4
4
import org .javawebstack .abstractdata .NamingPolicy ;
5
+ import org .javawebstack .httpclient .implementation .IHTTPRequestImplementation ;
6
+ import org .javawebstack .httpclient .implementation .JavaNetHTTPRequestImplementation ;
5
7
import org .javawebstack .httpclient .interceptor .RequestInterceptor ;
8
+ import org .javawebstack .httpclient .websocket .WebSocket ;
9
+ import org .javawebstack .httpclient .websocket .WebSocketHandler ;
6
10
11
+ import java .io .IOException ;
7
12
import java .net .HttpCookie ;
8
13
import java .nio .charset .StandardCharsets ;
9
14
import java .util .*;
15
+ import java .util .function .Supplier ;
10
16
11
17
public class HTTPClient {
12
18
13
19
private AbstractMapper abstractMapper = new AbstractMapper ()
14
20
.setNamingPolicy (NamingPolicy .SNAKE_CASE );
15
21
private int timeout = 5000 ;
16
- private String baseUrl = "" ;
22
+ private String baseUrl ;
17
23
private Map <String , String []> defaultHeaders = new HashMap <>();
18
24
private Map <String , String > defaultQuery = new HashMap <>();
19
25
private List <HttpCookie > defaultCookies = new ArrayList <>();
20
26
27
+ private Supplier <? extends IHTTPRequestImplementation > httpImplementation = JavaNetHTTPRequestImplementation ::new ;
28
+
21
29
private RequestInterceptor beforeInterceptor ;
22
30
private RequestInterceptor afterInterceptor ;
23
31
@@ -31,45 +39,56 @@ public HTTPClient(String baseUrl) {
31
39
this .baseUrl = baseUrl ;
32
40
}
33
41
34
- public HTTPClient () { }
42
+ public HTTPClient () {
43
+ this ("" );
44
+ }
35
45
36
- public HTTPClient debug (){
46
+ public HTTPClient debug () {
37
47
this .debug = true ;
38
48
return this ;
39
49
}
40
50
41
- public boolean isDebug (){
51
+ public boolean isDebug () {
42
52
return debug ;
43
53
}
44
54
45
- public HTTPClient autoCookies (){
55
+ public Supplier <? extends IHTTPRequestImplementation > getHttpImplementation () {
56
+ return httpImplementation ;
57
+ }
58
+
59
+ public HTTPClient httpImplementation (Supplier <? extends IHTTPRequestImplementation > implementation ) {
60
+ this .httpImplementation = implementation ;
61
+ return this ;
62
+ }
63
+
64
+ public HTTPClient autoCookies () {
46
65
autoCookies = true ;
47
66
return this ;
48
67
}
49
68
50
- public boolean isAutoCookies (){
69
+ public boolean isAutoCookies () {
51
70
return autoCookies ;
52
71
}
53
72
54
- public HTTPClient setSSLVerification (boolean sslVerification ){
73
+ public HTTPClient setSSLVerification (boolean sslVerification ) {
55
74
this .sslVerification = sslVerification ;
56
75
return this ;
57
76
}
58
77
59
- public boolean isSSLVerification (){
78
+ public boolean isSSLVerification () {
60
79
return this .sslVerification ;
61
80
}
62
81
63
- public HTTPClient abstractMapper (AbstractMapper mapper ){
82
+ public HTTPClient abstractMapper (AbstractMapper mapper ) {
64
83
this .abstractMapper = mapper ;
65
84
return this ;
66
85
}
67
86
68
- public AbstractMapper getAbstractMapper (){
87
+ public AbstractMapper getAbstractMapper () {
69
88
return abstractMapper ;
70
89
}
71
90
72
- public HTTPClient timeout (int timeout ){
91
+ public HTTPClient timeout (int timeout ) {
73
92
this .timeout = timeout ;
74
93
return this ;
75
94
}
@@ -78,35 +97,35 @@ public int getTimeout() {
78
97
return timeout ;
79
98
}
80
99
81
- public HTTPClient header (String key , String ... values ){
100
+ public HTTPClient header (String key , String ... values ) {
82
101
defaultHeaders .put (key , values );
83
102
return this ;
84
103
}
85
104
86
- public HTTPClient query (String key , String value ){
105
+ public HTTPClient query (String key , String value ) {
87
106
defaultQuery .put (key , value );
88
107
return this ;
89
108
}
90
109
91
- public HTTPClient cookie (HttpCookie cookie ){
110
+ public HTTPClient cookie (HttpCookie cookie ) {
92
111
removeCookie (cookie .getName ());
93
112
defaultCookies .add (cookie );
94
113
return this ;
95
114
}
96
115
97
- public HTTPClient removeCookie (String name ){
98
- for (HttpCookie cookie : new HashSet <>(defaultCookies )){
116
+ public HTTPClient removeCookie (String name ) {
117
+ for (HttpCookie cookie : new HashSet <>(defaultCookies )) {
99
118
if (cookie .getName ().equalsIgnoreCase (name ))
100
119
defaultCookies .remove (cookie );
101
120
}
102
121
return this ;
103
122
}
104
123
105
- public List <HttpCookie > getDefaultCookies (){
124
+ public List <HttpCookie > getDefaultCookies () {
106
125
return defaultCookies ;
107
126
}
108
127
109
- public HTTPClient setDefaultCookies (List <HttpCookie > cookies ){
128
+ public HTTPClient setDefaultCookies (List <HttpCookie > cookies ) {
110
129
this .defaultCookies = cookies ;
111
130
return this ;
112
131
}
@@ -124,16 +143,16 @@ public HTTPClient setDefaultQuery(Map<String, String> defaultQuery) {
124
143
return this ;
125
144
}
126
145
127
- public HTTPClient setDefaultHeaders (Map <String , String []> defaultHeaders ){
146
+ public HTTPClient setDefaultHeaders (Map <String , String []> defaultHeaders ) {
128
147
this .defaultHeaders = defaultHeaders ;
129
148
return this ;
130
149
}
131
150
132
- public HTTPClient authorization (String type , String value ){
151
+ public HTTPClient authorization (String type , String value ) {
133
152
return header ("Authorization" , type + " " + value );
134
153
}
135
154
136
- public HTTPClient bearer (String token ){
155
+ public HTTPClient bearer (String token ) {
137
156
return authorization ("Bearer" , token );
138
157
}
139
158
@@ -152,15 +171,28 @@ public String getBaseUrl() {
152
171
return baseUrl ;
153
172
}
154
173
155
- public HTTPRequest request (String method , String path ){
174
+ public HTTPRequest request (String method , String path ) {
156
175
return new HTTPRequest (this , method , path );
157
176
}
158
177
159
- public HTTPRequest get (String path ){
178
+ public WebSocket webSocket (String path , WebSocketHandler handler ) throws IOException {
179
+ return webSocket (path , handler , null );
180
+ }
181
+
182
+ public WebSocket webSocket (String path , WebSocketHandler handler , Map <String , String > additionalHeaders ) throws IOException {
183
+ HTTPClientSocket socket = new HTTPClientSocket (getBaseUrl () + ((path .startsWith ("/" ) || path .startsWith ("http://" ) || path .startsWith ("https://" )) ? "" : "/" ) + path , !isSSLVerification ());
184
+ if (additionalHeaders != null )
185
+ additionalHeaders .forEach (socket ::setRequestHeader );
186
+ WebSocket webSocket = new WebSocket (socket , handler );
187
+ new Thread (webSocket ).start ();
188
+ return webSocket ;
189
+ }
190
+
191
+ public HTTPRequest get (String path ) {
160
192
return request ("GET" , path );
161
193
}
162
194
163
- public HTTPClient before (RequestInterceptor requestInterceptor ){
195
+ public HTTPClient before (RequestInterceptor requestInterceptor ) {
164
196
beforeInterceptor = requestInterceptor ;
165
197
return this ;
166
198
}
@@ -177,23 +209,23 @@ public RequestInterceptor getAfterInterceptor() {
177
209
return afterInterceptor ;
178
210
}
179
211
180
- public HTTPRequest post (String path ){
212
+ public HTTPRequest post (String path ) {
181
213
return request ("POST" , path );
182
214
}
183
215
184
- public HTTPRequest post (String path , Object body ){
216
+ public HTTPRequest post (String path , Object body ) {
185
217
return post (path ).jsonBody (body );
186
218
}
187
219
188
- public HTTPRequest put (String path ){
220
+ public HTTPRequest put (String path ) {
189
221
return request ("PUT" , path );
190
222
}
191
223
192
- public HTTPRequest put (String path , Object body ){
224
+ public HTTPRequest put (String path , Object body ) {
193
225
return put (path ).jsonBody (body );
194
226
}
195
227
196
- public HTTPRequest delete (String path ){
228
+ public HTTPRequest delete (String path ) {
197
229
return request ("DELETE" , path );
198
230
}
199
231
0 commit comments