Skip to content

Commit 50685b3

Browse files
committed
Add fluent invocation support to WebServiceProxy.
1 parent 6ca49f0 commit 50685b3

File tree

3 files changed

+252
-174
lines changed

3 files changed

+252
-174
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ If a service returns an error response, the default error handler will throw a `
366366
The following code snippet demonstrates how `WebServiceProxy` might be used to access the operations of the simple math service discussed earlier:
367367

368368
```java
369-
WebServiceProxy webServiceProxy = new WebServiceProxy("GET", new URL(serverURL, "math/sum"));
369+
WebServiceProxy webServiceProxy = new WebServiceProxy("GET", new URL(baseURL, "math/sum"));
370370

371371
// GET /math/sum?a=2&b=4
372372
webServiceProxy.setArguments(mapOf(
@@ -384,6 +384,24 @@ webServiceProxy.setArguments(mapOf(
384384
System.out.println(webServiceProxy.invoke(Double.class)); // 6.0
385385
```
386386

387+
### Fluent Invocation
388+
`WebServiceProxy` also supports a fluent (i.e. chained) invocation model. For example, the following code is equivalent to the previous example:
389+
390+
```java
391+
// GET /math/sum?a=2&b=4
392+
System.out.println(WebServiceProxy.get(baseURL, "math/sum").setArguments(mapOf(
393+
entry("a", 4),
394+
entry("b", 2)
395+
)).invoke(Double.class)); // 6.0
396+
397+
// GET /math/sum?values=1&values=2&values=3
398+
System.out.println(WebServiceProxy.get(baseURL, "math/sum").setArguments(mapOf(
399+
entry("values", listOf(1, 2, 3))
400+
)).invoke(Double.class)); // 6.0
401+
```
402+
403+
POST, PUT, and DELETE operations are also supported.
404+
387405
## JSONEncoder and JSONDecoder
388406
The `JSONEncoder` class is used internally by `WebService` and `WebServiceProxy` to serialize request and response data. However, it can also be used by application code. For example:
389407

httprpc-client/src/main/java/org/httprpc/WebServiceProxy.java

Lines changed: 176 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ public interface ErrorHandler {
156156
* Constructs a new web service proxy.
157157
*
158158
* @param method
159-
* The service method.
159+
* The HTTP method.
160160
*
161161
* @param url
162-
* The service URL.
162+
* The resource URL.
163163
*/
164164
public WebServiceProxy(String method, URL url) {
165165
if (method == null) {
@@ -170,25 +170,25 @@ public WebServiceProxy(String method, URL url) {
170170
throw new IllegalArgumentException();
171171
}
172172

173-
this.method = method;
173+
this.method = method.toUpperCase();
174174
this.url = url;
175175
}
176176

177177
/**
178-
* Returns the service method.
178+
* Returns the HTTP method.
179179
*
180180
* @return
181-
* The service method.
181+
* The HTTP method.
182182
*/
183183
public String getMethod() {
184184
return method;
185185
}
186186

187187
/**
188-
* Returns the service URL.
188+
* Returns the resource URL.
189189
*
190190
* @return
191-
* The service URL.
191+
* The resource URL.
192192
*/
193193
public URL getURL() {
194194
return url;
@@ -209,13 +209,18 @@ public Encoding getEncoding() {
209209
*
210210
* @param encoding
211211
* The encoding used for POST requests.
212+
*
213+
* @return
214+
* The web service proxy.
212215
*/
213-
public void setEncoding(Encoding encoding) {
216+
public WebServiceProxy setEncoding(Encoding encoding) {
214217
if (encoding == null) {
215218
throw new IllegalArgumentException();
216219
}
217220

218221
this.encoding = encoding;
222+
223+
return this;
219224
}
220225

221226
/**
@@ -233,13 +238,18 @@ public void setEncoding(Encoding encoding) {
233238
*
234239
* @param headers
235240
* The header map.
241+
*
242+
* @return
243+
* The web service proxy.
236244
*/
237-
public void setHeaders(Map<String, ?> headers) {
245+
public WebServiceProxy setHeaders(Map<String, ?> headers) {
238246
if (headers == null) {
239247
throw new IllegalArgumentException();
240248
}
241249

242250
this.headers = headers;
251+
252+
return this;
243253
}
244254

245255
/**
@@ -257,13 +267,18 @@ public void setHeaders(Map<String, ?> headers) {
257267
*
258268
* @param arguments
259269
* The argument map.
270+
*
271+
* @return
272+
* The web service proxy.
260273
*/
261-
public void setArguments(Map<String, ?> arguments) {
274+
public WebServiceProxy setArguments(Map<String, ?> arguments) {
262275
if (arguments == null) {
263276
throw new IllegalArgumentException();
264277
}
265278

266279
this.arguments = arguments;
280+
281+
return this;
267282
}
268283

269284
/**
@@ -281,9 +296,14 @@ public Object getBody() {
281296
*
282297
* @param body
283298
* A value representing the body content, or <code>null</code> for no body.
299+
*
300+
* @return
301+
* The web service proxy.
284302
*/
285-
public void setBody(Object body) {
303+
public WebServiceProxy setBody(Object body) {
286304
this.body = body;
305+
306+
return this;
287307
}
288308

289309
/**
@@ -301,9 +321,14 @@ public RequestHandler getRequestHandler() {
301321
*
302322
* @param requestHandler
303323
* The request handler, or <code>null</code> for the default request handler.
324+
*
325+
* @return
326+
* The web service proxy.
304327
*/
305-
public void setRequestHandler(RequestHandler requestHandler) {
328+
public WebServiceProxy setRequestHandler(RequestHandler requestHandler) {
306329
this.requestHandler = requestHandler;
330+
331+
return this;
307332
}
308333

309334
/**
@@ -321,9 +346,14 @@ public ErrorHandler getErrorHandler() {
321346
*
322347
* @param errorHandler
323348
* The error handler, or <code>null</code> for the default error handler.
349+
*
350+
* @return
351+
* The web service proxy.
324352
*/
325-
public void setErrorHandler(ErrorHandler errorHandler) {
353+
public WebServiceProxy setErrorHandler(ErrorHandler errorHandler) {
326354
this.errorHandler = errorHandler;
355+
356+
return this;
327357
}
328358

329359
/**
@@ -341,9 +371,14 @@ public int getConnectTimeout() {
341371
*
342372
* @param connectTimeout
343373
* The connect timeout.
374+
*
375+
* @return
376+
* The web service proxy.
344377
*/
345-
public void setConnectTimeout(int connectTimeout) {
378+
public WebServiceProxy setConnectTimeout(int connectTimeout) {
346379
this.connectTimeout = connectTimeout;
380+
381+
return this;
347382
}
348383

349384
/**
@@ -361,13 +396,18 @@ public int getReadTimeout() {
361396
*
362397
* @param readTimeout
363398
* The read timeout.
399+
*
400+
* @return
401+
* The web service proxy.
364402
*/
365-
public void setReadTimeout(int readTimeout) {
403+
public WebServiceProxy setReadTimeout(int readTimeout) {
366404
this.readTimeout = readTimeout;
405+
406+
return this;
367407
}
368408

369409
/**
370-
* Invokes the service method.
410+
* Invokes the service operation.
371411
*
372412
* @param <T>
373413
* The result type.
@@ -384,7 +424,7 @@ public <T> T invoke() throws IOException {
384424
}
385425

386426
/**
387-
* Invokes the service method.
427+
* Invokes the service operation.
388428
*
389429
* @param <T>
390430
* The result type.
@@ -407,7 +447,7 @@ public <T> T invoke(Class<? extends T> type) throws IOException {
407447
}
408448

409449
/**
410-
* Invokes the service method.
450+
* Invokes the service operation.
411451
*
412452
* @param <T>
413453
* The result type.
@@ -428,7 +468,7 @@ public <T> T invoke(ResponseHandler<? extends T> responseHandler) throws IOExcep
428468

429469
URL url;
430470
RequestHandler requestHandler;
431-
if (method.equalsIgnoreCase("POST") && body == null && this.requestHandler == null) {
471+
if (method.equals("POST") && body == null && this.requestHandler == null) {
432472
url = this.url;
433473

434474
requestHandler = new RequestHandler() {
@@ -683,4 +723,121 @@ private static Object getParameterValue(Object argument) {
683723
return argument;
684724
}
685725
}
726+
727+
/**
728+
* Creates a web service proxy representing a GET request.
729+
*
730+
* @param url
731+
* The resource URL.
732+
*
733+
* @return
734+
* The new web service proxy.
735+
*/
736+
public static WebServiceProxy get(URL url) {
737+
return new WebServiceProxy("GET", url);
738+
}
739+
740+
/**
741+
* Creates a web service proxy representing a GET request.
742+
*
743+
* @param baseURL
744+
* The base URL.
745+
*
746+
* @param path
747+
* The path to the resource, relative to the base URL.
748+
*
749+
* @return
750+
* The new web service proxy.
751+
*/
752+
public static WebServiceProxy get(URL baseURL, String path) throws IOException {
753+
return get(new URL(baseURL, path));
754+
}
755+
756+
/**
757+
* Creates a web service proxy representing a POST request.
758+
*
759+
* @param url
760+
* The resource URL.
761+
*
762+
* @return
763+
* The new web service proxy.
764+
*/
765+
public static WebServiceProxy post(URL url) {
766+
return new WebServiceProxy("POST", url);
767+
}
768+
769+
/**
770+
* Creates a web service proxy representing a POST request.
771+
*
772+
* @param baseURL
773+
* The base URL.
774+
*
775+
* @param path
776+
* The path to the resource, relative to the base URL.
777+
*
778+
* @return
779+
* The new web service proxy.
780+
*/
781+
public static WebServiceProxy post(URL baseURL, String path) throws IOException {
782+
return post(new URL(baseURL, path));
783+
}
784+
785+
/**
786+
* Creates a web service proxy representing a PUT request.
787+
*
788+
* @param url
789+
* The resource URL.
790+
*
791+
* @return
792+
* The new web service proxy.
793+
*/
794+
public static WebServiceProxy put(URL url) {
795+
return new WebServiceProxy("PUT", url);
796+
}
797+
798+
/**
799+
* Creates a web service proxy representing a PUT request.
800+
*
801+
* @param baseURL
802+
* The base URL.
803+
*
804+
* @param path
805+
* The path to the resource, relative to the base URL.
806+
*
807+
* @return
808+
* The new web service proxy.
809+
*/
810+
public static WebServiceProxy put(URL baseURL, String path) throws IOException {
811+
return put(new URL(baseURL, path));
812+
}
813+
814+
/**
815+
* Creates a web service proxy representing a DELETE request.
816+
*
817+
* @param url
818+
* The resource URL.
819+
*
820+
* @return
821+
* The new web service proxy.
822+
*/
823+
public static WebServiceProxy delete(URL url) {
824+
return new WebServiceProxy("DELETE", url);
825+
}
826+
827+
/**
828+
* Creates a web service proxy representing a DELETE request.
829+
*
830+
* @param baseURL
831+
* The base URL.
832+
*
833+
* @param path
834+
* The path to the resource, relative to the base URL.
835+
*
836+
* @return
837+
* The new web service proxy.
838+
*/
839+
public static WebServiceProxy delete(URL baseURL, String path) throws IOException {
840+
return delete(new URL(baseURL, path));
841+
}
686842
}
843+

0 commit comments

Comments
 (0)