Skip to content

Commit b840e1f

Browse files
committed
Rename Body annotation to Content.
1 parent bf902d7 commit b840e1f

File tree

6 files changed

+49
-47
lines changed

6 files changed

+49
-47
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,21 @@ protected String getKey(String name) { ... }
184184
For example, given the preceding request, the key with name "contactID" would be "jsmith" and the key with name "addressType" would be "home".
185185

186186
### Custom Body Content
187-
The `Body` annotation can be used to associate custom body content with a service method. Annotated methods can access decoded content via the `getBody()` method. For example:
187+
The `Content` annotation can be used to associate custom body content with a service method. Annotated methods can access the decoded data via the `getContent()` method.
188+
189+
For example, the following method might be used to create a new account using data passed in the request body:
188190

189191
```java
190192
@RequestMethod("POST")
191-
@Body(Content.class)
192-
public uploadContent() {
193-
Content content = getBody();
193+
@Content(Account.class)
194+
public createAccount() {
195+
Account account = getContent();
194196

195197
...
196198
}
197199
```
198200

199-
By default, body data is assumed to be JSON. However, subclasses can override the `decodeBody()` method to support other representations.
200-
201-
The decoded body is coerced to the declared type using the `BeanAdapter#adapt()` method, which is discussed in more detail later. If the decoded content cannot be converted to the specified type, an HTTP 415 response will be returned.
201+
By default, body data is assumed to be JSON. However, subclasses can override the `decodeContent()` method to support other representations. If the provided data cannot be deserialized to the specified type, an HTTP 415 response will be returned.
202202

203203
### Return Values
204204
Return values are converted to their JSON equivalents as follows:
@@ -326,7 +326,9 @@ The `WebServiceProxy` class is used to issue API requests to a server. A Swift v
326326
* `method` - the HTTP method to execute
327327
* `url` - the URL of the requested resource
328328

329-
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 a custom body is provided via the `setBody()` method or the request body is generated by a custom request handler (specified via the `setRequestHandler()` method), `POST` arguments will be sent in the query string.
329+
Request headers and arguments are specified via the `setHeaders()` and `setArguments()` methods, respectively. Custom body content can be supplied via the `setContent()` method.
330+
331+
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 a custom body is provided either via `setContent()` or by a custom request handler (specified via the `setRequestHandler()` method), `POST` arguments will be sent in the query string.
330332

331333
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.
332334

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public String toString() {
226226
private Map<String, ?> headers = emptyMap();
227227
private Map<String, ?> arguments = emptyMap();
228228

229-
private Object body;
229+
private Object content;
230230

231231
private RequestHandler requestHandler = null;
232232

@@ -354,23 +354,23 @@ public void setArguments(Map<String, ?> arguments) {
354354
}
355355

356356
/**
357-
* Returns the request body.
357+
* Returns the request content.
358358
*
359359
* @return
360-
* A value representing the request body, or <code>null</code> for no body.
360+
* A value representing the request content, or <code>null</code> for no content.
361361
*/
362-
public Object getBody() {
363-
return body;
362+
public Object getContent() {
363+
return content;
364364
}
365365

366366
/**
367-
* Returns the request body.
367+
* Returns the request content.
368368
*
369-
* @param body
370-
* A value representing the request body, or <code>null</code> if no body has been set.
369+
* @param content
370+
* A value representing the request content, or <code>null</code> if no content has been set.
371371
*/
372-
public void setBody(Object body) {
373-
this.body = body;
372+
public void setContent(Object content) {
373+
this.content = content;
374374
}
375375

376376
/**
@@ -471,7 +471,7 @@ public <T> T invoke() throws IOException {
471471
public <T> T invoke(ResponseHandler<T> responseHandler) throws IOException {
472472
URL url;
473473
RequestHandler requestHandler;
474-
if (body != null && this.requestHandler == null) {
474+
if (content != null && this.requestHandler == null) {
475475
url = this.url;
476476

477477
requestHandler = new RequestHandler() {
@@ -484,7 +484,7 @@ public String getContentType() {
484484
public void encodeRequest(OutputStream outputStream) throws IOException {
485485
JSONEncoder jsonEncoder = new JSONEncoder();
486486

487-
jsonEncoder.write(body, outputStream);
487+
jsonEncoder.write(content, outputStream);
488488
}
489489
};
490490
} else if (method.equalsIgnoreCase("POST") && this.requestHandler == null) {

httprpc-server/src/main/java/org/httprpc/Body.java renamed to httprpc-server/src/main/java/org/httprpc/Content.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
import java.lang.annotation.Target;
77

88
/**
9-
* Annotation that associates a body type with a service method.
9+
* Annotation that associates custom body content with a service method.
1010
*/
1111
@Retention(RetentionPolicy.RUNTIME)
1212
@Target(ElementType.METHOD)
13-
public @interface Body {
13+
public @interface Content {
1414
/**
1515
* @return
16-
* The body type.
16+
* The content type.
1717
*/
1818
Class<?> value();
1919
}

httprpc-server/src/main/java/org/httprpc/WebService.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected URLConnection openConnection(URL url) {
130130
private ThreadLocal<List<String>> keyList = new ThreadLocal<>();
131131
private ThreadLocal<Map<String, String>> keyMap = new ThreadLocal<>();
132132

133-
private ThreadLocal<Object> body = new ThreadLocal<>();
133+
private ThreadLocal<Object> content = new ThreadLocal<>();
134134

135135
private static ConcurrentHashMap<Class<?>, WebService> services = new ConcurrentHashMap<>();
136136

@@ -367,11 +367,11 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
367367

368368
Object result;
369369
try {
370-
Body body = handler.method.getAnnotation(Body.class);
370+
Content content = handler.method.getAnnotation(Content.class);
371371

372-
if (body != null) {
372+
if (content != null) {
373373
try {
374-
this.body.set(decodeBody(request, body.value()));
374+
this.content.set(decodeContent(request, content.value()));
375375
} catch (Exception exception) {
376376
response.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
377377
return;
@@ -421,7 +421,7 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
421421
this.keyList.set(null);
422422
this.keyMap.set(null);
423423

424-
this.body.set(null);
424+
this.content.set(null);
425425
}
426426

427427
if (response.isCommitted()) {
@@ -567,17 +567,17 @@ protected String getKey(String name) {
567567
}
568568

569569
/**
570-
* Returns the body content associated with the current request.
570+
* Returns the content associated with the current request.
571571
*
572572
* @param <T>
573-
* The body type.
573+
* The content type.
574574
*
575575
* @return
576-
* The body content, or <code>null</code> if no body was provided.
576+
* The request content, or <code>null</code> if no content was provided.
577577
*/
578578
@SuppressWarnings("unchecked")
579-
protected <T> T getBody() {
580-
return (T)body.get();
579+
protected <T> T getContent() {
580+
return (T)content.get();
581581
}
582582

583583
/**
@@ -598,21 +598,21 @@ protected boolean isAuthorized(HttpServletRequest request, Method method) {
598598
}
599599

600600
/**
601-
* Decodes the body of a service request.
601+
* Decodes the content of a service request.
602602
*
603603
* @param request
604604
* The servlet request.
605605
*
606606
* @param type
607-
* The body type.
607+
* The content type.
608608
*
609609
* @return
610-
* The decoded body content.
610+
* The decoded content.
611611
*
612612
* @throws IOException
613-
* If an exception occurs while decoding the body.
613+
* If an exception occurs while decoding the content.
614614
*/
615-
protected Object decodeBody(HttpServletRequest request, Class<?> type) throws IOException {
615+
protected Object decodeContent(HttpServletRequest request, Class<?> type) throws IOException {
616616
JSONDecoder jsonDecoder = new JSONDecoder();
617617

618618
return BeanAdapter.adapt(jsonDecoder.read(request.getInputStream()), type);
@@ -801,12 +801,12 @@ private void describeResource(String path, Resource resource, XMLStreamWriter xm
801801

802802
xmlStreamWriter.writeCharacters(entry.getKey().toUpperCase());
803803

804-
Body body = handler.method.getAnnotation(Body.class);
804+
Content content = handler.method.getAnnotation(Content.class);
805805

806-
if (body != null) {
806+
if (content != null) {
807807
xmlStreamWriter.writeCharacters(" (");
808808

809-
describeType(body.value(), xmlStreamWriter);
809+
describeType(content.value(), xmlStreamWriter);
810810

811811
xmlStreamWriter.writeCharacters(")");
812812
}

httprpc-test/src/main/java/org/httprpc/test/TestService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package org.httprpc.test;
1616

17-
import org.httprpc.Body;
17+
import org.httprpc.Content;
1818
import org.httprpc.Description;
1919
import org.httprpc.RequestMethod;
2020
import org.httprpc.ResourcePath;
@@ -104,7 +104,7 @@ public static class TestList extends ArrayList<Integer> {
104104
public static class TestMap extends HashMap<String, Double> {
105105
}
106106

107-
public interface Content {
107+
public interface Body {
108108
String getString();
109109
List<String> getStrings();
110110
int getNumber();
@@ -259,9 +259,9 @@ public Response testPost(String string, List<String> strings, int number, boolea
259259
}
260260

261261
@RequestMethod("POST")
262-
@Body(Content.class)
263-
public Content testPost(int id) {
264-
return getBody();
262+
@Content(Body.class)
263+
public Body testPost(int id) {
264+
return getContent();
265265
}
266266

267267
@RequestMethod("POST")

httprpc-test/src/test/java/org/httprpc/WebServiceProxyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public void testBodyPost() throws IOException {
263263
(resourcePath) -> null, (method, url) -> {
264264
WebServiceProxy webServiceProxy = new WebServiceProxy(method, url);
265265

266-
webServiceProxy.setBody(content);
266+
webServiceProxy.setContent(content);
267267

268268
return webServiceProxy;
269269
});

0 commit comments

Comments
 (0)