Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2941e9b02f8ab5bd11e326fe8d94483e30728887 (plain) (blame)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*******************************************************************************
 * Copyright (c) 2014 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.jaxrs.client;

import java.net.URI;
import java.util.Map;
import org.eclipse.osee.jaxrs.client.JaxRsClientConstants.ConnectionType;
import org.eclipse.osee.jaxrs.client.JaxRsClientConstants.ProxyType;
import org.eclipse.osee.jaxrs.client.internal.JaxRsClientRuntime;

/**
 * @author Roberto E. Escobar
 */
public final class JaxRsClient {

   public interface JaxRsClientFactory {

      <T> T newClient(JaxRsClientConfig config, String serverAddress, Class<T> clazz);

      JaxRsWebTarget newTarget(JaxRsClientConfig config, String serverAddress);
   }

   public static JaxRsClientBuilder newBuilder() {
      JaxRsClientFactory factory = JaxRsClientRuntime.getClientFactoryInstance();
      return new JaxRsClientBuilder(factory);
   }

   public static JaxRsClientBuilder newBuilder(JaxRsClientConfig config) {
      return newBuilder().withConfig(config);
   }

   public static JaxRsClientBuilder newBuilder(Map<String, Object> properties) {
      return newBuilder().properties(properties);
   }

   public static JaxRsClient newClient() {
      return newBuilder().build();
   }

   public static JaxRsClient fromConfig(JaxRsClientConfig config) {
      return newBuilder(config).build();
   }

   public static JaxRsClient fromProperties(Map<String, Object> properties) {
      return newBuilder(properties).build();
   }

   private final JaxRsClientConfig config;
   private final JaxRsClientFactory factory;

   protected JaxRsClient(JaxRsClientFactory factory, JaxRsClientConfig config) {
      super();
      this.config = config;
      this.factory = factory;
   }

   /**
    * Creates a JAX-RS WebTarget
    * 
    * @return target
    */
   public JaxRsWebTarget target() {
      return target((String) null);
   }

   /**
    * Creates a JAX-RS WebTarget
    * 
    * @param baseAddress
    * @return target
    */
   public JaxRsWebTarget target(URI address) {
      return target(address != null ? address.toString() : null);
   }

   /**
    * Creates a JAX-RS WebTarget
    * 
    * @param baseAddress
    * @return target
    */
   public JaxRsWebTarget target(String address) {
      return factory.newTarget(config, address);
   }

   /**
    * Proxy sub-resource methods returning Objects can not be invoked. Prefer to have sub-resource methods returning
    * typed classes: interfaces, abstract classes or concrete implementations.
    * 
    * @param endpointAddress - address to the endpoint represented by clazz
    * @param clazz - JAX-RS annotated class used to create a proxy client
    * @return targetProxy
    */
   public <T> T targetProxy(URI address, Class<T> clazz) {
      return targetProxy(address != null ? address.toString() : null, clazz);
   }

   /**
    * Proxy sub-resource methods returning Objects can not be invoked. Prefer to have sub-resource methods returning
    * typed classes: interfaces, abstract classes or concrete implementations.
    * 
    * @param endpointAddress - address to the endpoint represented by clazz
    * @param clazz - JAX-RS annotated class used to create a proxy client
    * @return targetProxy
    */
   public <T> T targetProxy(String address, Class<T> clazz) {
      return factory.newClient(config, address, clazz);
   }

   /**
    * JAX-RS Client configuration
    * 
    * @return config
    */
   public JaxRsClientConfig getConfig() {
      return config;
   }

   public static final class JaxRsClientBuilder extends JaxRsClientConfig {

      private final JaxRsClientFactory factory;

      protected JaxRsClientBuilder(JaxRsClientFactory factory) {
         super();
         this.factory = factory;
      }

      private JaxRsClientBuilder withConfig(JaxRsClientConfig config) {
         this.copy(config);
         return this;
      }

      public JaxRsClientBuilder properties(Map<String, Object> src) {
         readProperties(src);
         return this;
      }

      public JaxRsClientBuilder asyncExecTimeout(long asyncExecuteTimeout) {
         setAsyncExecuteTimeout(asyncExecuteTimeout);
         return this;
      }

      public JaxRsClientBuilder asyncExecTimeoutRejection(boolean asyncExecuteTimeoutRejection) {
         setAsyncExecuteTimeoutRejection(asyncExecuteTimeoutRejection);
         return this;
      }

      public JaxRsClientBuilder followRedirects(boolean followRedirectsAutomatically) {
         setFollowRedirects(followRedirectsAutomatically);
         return this;
      }

      public JaxRsClientBuilder allowChunking(boolean chunkingAllowed) {
         setChunkingAllowed(chunkingAllowed);
         return this;
      }

      public JaxRsClientBuilder chunkingThreshold(int chunkingThreshold) {
         setChunkingThreshold(chunkingThreshold);
         return this;
      }

      public JaxRsClientBuilder chunkLength(int chunkLength) {
         setChunkLength(chunkLength);
         return this;
      }

      public JaxRsClientBuilder connectionTimeout(long connectionTimeoutInMillis) {
         setConnectionTimeout(connectionTimeoutInMillis);
         return this;
      }

      public JaxRsClientBuilder connectionType(ConnectionType connectionType) {
         setConnectionType(connectionType);
         return this;
      }

      public JaxRsClientBuilder maxRetransmits(int maxRetransmits) {
         setMaxRetransmits(maxRetransmits);
         return this;
      }

      public JaxRsClientBuilder nonProxyHosts(String nonProxyHosts) {
         setNonProxyHosts(nonProxyHosts);
         return this;
      }

      public JaxRsClientBuilder proxyAddress(String proxyAddress) {
         setProxyAddress(proxyAddress);
         return this;
      }

      public JaxRsClientBuilder proxyAuthorizationType(String proxyAuthorizationType) {
         setProxyAuthorizationType(proxyAuthorizationType);
         return this;
      }

      public JaxRsClientBuilder proxyPassword(String proxyPassword) {
         setProxyPassword(proxyPassword);
         return this;
      }

      public JaxRsClientBuilder proxyType(ProxyType proxyType) {
         setProxyType(proxyType);
         return this;
      }

      public JaxRsClientBuilder proxyUsername(String proxyUsername) {
         setProxyUsername(proxyUsername);
         return this;
      }

      public JaxRsClientBuilder receiveTimeout(long receiveTimeoutInMillis) {
         setReceiveTimeout(receiveTimeoutInMillis);
         return this;
      }

      public JaxRsClientBuilder authorizationType(String serverAuthorizationType) {
         setServerAuthorizationType(serverAuthorizationType);
         return this;
      }

      public JaxRsClientBuilder password(String serverPassword) {
         setServerPassword(serverPassword);
         return this;
      }

      public JaxRsClientBuilder username(String serverUsername) {
         setServerUsername(serverUsername);
         return this;
      }

      public JaxRsClientBuilder createThreadSafeProxyClients(boolean createThreadSafeClients) {
         setCreateThreadSafeProxyClients(createThreadSafeClients);
         return this;
      }

      public JaxRsClientBuilder proxyClientSubResourcesInheritHeaders(boolean inheritHeaders) {
         setProxyClientSubResourcesInheritHeaders(inheritHeaders);
         return this;
      }

      public JaxRsClientBuilder oAuthAuthorizeUri(String oauthAuthorizeUri) {
         setOAuthAuthorizeUri(oauthAuthorizeUri);
         return this;
      }

      public JaxRsClientBuilder oAuthTokenUri(String oauthTokenUri) {
         setOAuthTokenUri(oauthTokenUri);
         return this;
      }

      public JaxRsClientBuilder oAuthTokenValidationUri(String oauthTokenValidationUri) {
         setOAuthTokenValidationUri(oauthTokenValidationUri);
         return this;
      }

      public JaxRsClientBuilder oAuthClientId(String oauthClientId) {
         setOAuthClientId(oauthClientId);
         return this;
      }

      public JaxRsClientBuilder oAuthClientSecret(String oauthClientSecret) {
         setOAuthClientSecret(oauthClientSecret);
         return this;
      }

      public JaxRsClientBuilder oAuthRedirectUri(String oauthRedirectUri) {
         setOAuthRedirectUri(oauthRedirectUri);
         return this;
      }

      public JaxRsClientBuilder oAuthScopes(String oauthScopes) {
         setOAuthScopes(oauthScopes);
         return this;
      }

      public JaxRsClientBuilder oAuthSecretKeyAlgorithm(String oauthSecretKeyAlgorithm) {
         setOAuthSecretKeyAlgorithm(oauthSecretKeyAlgorithm);
         return this;
      }

      public JaxRsClientBuilder oAuthEncodedSecretKey(String oauthEncodedSecretKey) {
         setOAuthEncodedSecretKey(oauthEncodedSecretKey);
         return this;
      }

      public JaxRsClientBuilder oAuthFailOnRefreshTokenError(boolean oauthFailsOnRefreshTokenError) {
         setOAuthFailsOnRefreshTokenError(oauthFailsOnRefreshTokenError);
         return this;
      }

      public JaxRsClientBuilder oAuthTokenStore(JaxRsTokenStore oauthTokenStore) {
         setOAuthTokenStore(oauthTokenStore);
         return this;
      }

      public JaxRsClientBuilder oAuthConfirmHandler(JaxRsConfirmAccessHandler oauthTokenHandler) {
         setOAuthConfirmAccessHandler(oauthTokenHandler);
         return this;
      }

      public JaxRsClientBuilder oAuthCacheEnabled(boolean oauthCacheEnabled) {
         setOAuthCacheEnabled(oauthCacheEnabled);
         return this;
      }

      public JaxRsClientBuilder oAuthCacheMaxSize(int oauthCacheMaxSize) {
         setOAuthCacheMaxSize(oauthCacheMaxSize);
         return this;
      }

      public JaxRsClientBuilder oAuthCacheEvictTimeoutMillis(long oauthCacheEvictTimeoutMillis) {
         setOAuthCacheEvictTimeoutMillis(oauthCacheEvictTimeoutMillis);
         return this;
      }

      public JaxRsClient build() {
         return new JaxRsClient(factory, copy());
      }
   }

}

Back to the top