Skip to content

Commit ae46a0b

Browse files
committed
Add WebServiceProxy class.
1 parent f4cceef commit ae46a0b

File tree

7 files changed

+1001
-2
lines changed

7 files changed

+1001
-2
lines changed

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![Maven Central](https://img.shields.io/maven-central/v/org.httprpc/httprpc.svg)](http://repo1.maven.org/maven2/org/httprpc/httprpc/)
33

44
# Introduction
5-
HTTP-RPC is an open-source framework for implementing RESTful and REST-like web services in Java. It is extremely lightweight and requires only a Java runtime environment and a servlet container. The entire framework is distributed as a single JAR file that is about 65KB in size, making it an ideal choice for applications where a minimal footprint is desired.
5+
HTTP-RPC is an open-source framework for implementing RESTful and REST-like web services in Java. It is extremely lightweight and requires only a Java runtime environment and a servlet container. The entire framework is distributed as a single JAR file that is about 76KB in size, making it an ideal choice for applications where a minimal footprint is desired.
66

77
This guide introduces the HTTP-RPC framework and provides an overview of its key features.
88

@@ -23,6 +23,7 @@ This guide introduces the HTTP-RPC framework and provides an overview of its key
2323
* [TemplateEncoder](#templateencoder)
2424
* [BeanAdapter](#beanadapter)
2525
* [ResultSetAdapter and Parameters](#resultsetadapter-and-parameters)
26+
* [WebServiceProxy](#webserviceproxy)
2627
* [Kotlin Support](#kotlin-support)
2728
* [Additional Information](#additional-information)
2829

@@ -47,6 +48,8 @@ The HTTP-RPC framework includes the following classes:
4748
* `RequestParameter` - annotation that associates a custom request parameter name with a method argument
4849
* `ResourcePath` - annotation that associates a resource path with a service method
4950
* `Response` - annotation that associates a custom response description with a service method
51+
* `WebServiceException` - exception thrown when a service operation returns an error
52+
* `WebServiceProxy` - web service invocation proxy
5053
* `WebService` - abstract base class for web services
5154
* `org.httprpc.io`
5255
* `CSVDecoder` - class that decodes an iterable sequence of values from CSV
@@ -1066,5 +1069,51 @@ Data returned by the service might look like this:
10661069
}
10671070
```
10681071

1072+
# WebServiceProxy
1073+
The `WebServiceProxy` class is used to issue API requests to a server. This class provides a single constructor that accepts the following arguments:
1074+
1075+
* `method` - the HTTP method to execute
1076+
* `url` - the URL of the requested resource
1077+
1078+
Request headers and arguments are specified via the `setHeaders()` and `setArguments()` methods, respectively. Like HTML forms, arguments are submitted either via the query string or in the request body. Arguments for `GET`, `PUT`, and `DELETE` requests are always sent in the query string. `POST` arguments are typically sent in the request body, and may be submitted as either "application/x-www-form-urlencoded" or "multipart/form-data" (specified via the proxy's `setEncoding()` method). However, if the request body is provided via a custom request handler (specified via the `setRequestHandler()` method), `POST` arguments will be sent in the query string.
1079+
1080+
The `toString()` method is generally used to convert an argument to its string representation. However, `Date` instances are automatically converted to a long value representing epoch time. Additionally, `Iterable` instances represent multi-value parameters and behave similarly to `<select multiple>` tags in HTML. Further, when using the multi-part encoding, `URL` and `Iterable<URL>` values represent file uploads, and behave similarly to `<input type="file">` tags in HTML forms.
1081+
1082+
Service operations are invoked via one of the following methods:
1083+
1084+
```java
1085+
public <T> T invoke() throws IOException { ... }
1086+
1087+
public <T> T invoke(ResponseHandler<T> responseHandler) throws IOException { ... }
1088+
```
1089+
1090+
The first version automatically deserializes a successful server response using `JSONDecoder`. The second allows a caller to provide a custom response handler. `ResponseHandler` is a functional interface that is defined as follows:
1091+
1092+
```java
1093+
public interface ResponseHandler<T> {
1094+
public T decodeResponse(InputStream inputStream, String contentType, Map<String, String> headers) throws IOException;
1095+
}
1096+
```
1097+
1098+
If a service returns an error response, a `WebServiceException` will be thrown. If the content type of the response is "text/plain", the body of the response will be provided in the exception message.
1099+
1100+
### Example
1101+
The following code snippet demonstrates how `WebServiceProxy` might be used to access the operations of the simple math service discussed earlier:
1102+
1103+
```java
1104+
WebServiceProxy webServiceProxy = new WebServiceProxy("GET", new URL("http://localhost:8080/httprpc-server/math/sum"));
1105+
1106+
HashMap<String, Object> arguments = new HashMap<>();
1107+
1108+
arguments.put("a", 4);
1109+
arguments.put("b", 2);
1110+
1111+
webServiceProxy.setArguments(arguments);
1112+
1113+
Number result = webServiceProxy.invoke();
1114+
1115+
System.out.println(result); // 6.0
1116+
```
1117+
10691118
# Additional Information
10701119
This guide introduced the HTTP-RPC framework and provided an overview of its key features. For additional information, see the [examples](https://github.com/gk-brown/HTTP-RPC/tree/master/httprpc-test/src/main/java/org/httprpc/test).

httprpc/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ plugins {
1919
}
2020

2121
group = 'org.httprpc'
22-
version = '6.5'
22+
version = '6.6'
2323

2424
repositories {
2525
mavenCentral()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package org.httprpc;
16+
17+
import java.io.IOException;
18+
19+
/**
20+
* Thrown to indicate that a service operation returned an error.
21+
*/
22+
public class WebServiceException extends IOException {
23+
private static final long serialVersionUID = 0;
24+
25+
private int statusCode;
26+
27+
/**
28+
* Constructs a new web service exception.
29+
*
30+
* @param message
31+
* The error message returned by the service.
32+
*
33+
* @param statusCode
34+
* The HTTP status code returned by the service.
35+
*/
36+
public WebServiceException(String message, int statusCode) {
37+
super(message);
38+
39+
this.statusCode = statusCode;
40+
}
41+
42+
/**
43+
* Returns the HTTP status code returned by the service.
44+
*
45+
* @return
46+
* The HTTP status code returned by the service.
47+
*/
48+
public int getStatusCode() {
49+
return statusCode;
50+
}
51+
}

0 commit comments

Comments
 (0)