restlet/restlet-framework-java — pull request #969 by boillodmanuel (2014-10-26 20:01:22+00:00)
Title: Issue161 add exception conversion

**Work**

Done:
- Add exception conversion in `ConverterService`
- Add content negotiation in `StatusFilter`
- Replace @Status `value` type from `String` to `int`
- Remove code in `ServerResource`

Not done:
- Retrieve response entity in `ClientResource` after an error occurs in the `ServerResource`

**Use cases explained**

- **Simple request returning no entity (no content)**

> Not changed

```
curl -x PUT -H 'Accept: text/html’ ‘http://server.com/api/test'
```

Response headers:

```
HTTP/1.1 204 No Content
```

Response: N/A

- **Exception not annotated with @Status and with Content-Type "text/html"**

> Not changed

```
curl -x PUT -H 'Accept: text/html’ ‘http://server.com/api/exception'
```

Response headers:

```
HTTP/1.1 500 Internal Server Error
Content-type: text/html; charset=UTF-8
Content-length: 486
```

Response:

```html

Status page

Internal Server Error

The server encountered an unexpected condition which prevented it from fulfilling the request

You can get technical details here.

Please continue your visit at our home page.

```

- **Exception not annotated with @Status and with Content-Type "application/json" or without Content-Type**

> New: **Content Negotiation**

```
curl -x PUT -H 'Accept: application/json’ ‘http://server.com/api/exception'
```

Response headers:

```
HTTP/1.1 500 Internal Server Error
Content-type: application/json
```

Response:

```json
{
"code": 500,
"description": "The server encountered an unexpected condition which prevented it from fulfilling the request",
"reasonPhrase": "Internal Server Error"
}
```

- **Exception annotated with @Status(400) and with Content-Type "text/html"**

> New: **Status code**

```
curl -x PUT -H 'Accept: text/html’ ‘http://server.com/api/notfound'
```

Response headers:

```
HTTP/1.1 404 Not Found
Content-type: text/html; charset=UTF-8
Content-length: 439
```

Response:

```html

Status page

Not Found

The server has not found anything matching the request URI

You can get technical details here.

Please continue your visit at our home page.

```

- **Exception annotated with @Status(400) and with Content-Type "application/json" or without Content-Type**

> New: **Status Code** and **Content Negotiation**

```
curl -x PUT -H 'Accept: application/json’ ‘http://server.com/api/notfound'
```

Response headers:

```
HTTP/1.1 404 Not Found
Content-type: application/json
```

Response:

```json
{
"code":404,
"reasonPhrase":"Not Found",
"description":"The server has not found anything matching the request URI"
}
```

- **Exception annotated with @Status(value=400, serializeable=true) and with Content-Type "application/json" or without Content-Type**

> New: **Status Code**, **Exception properties serialization** and **Content Negotiation**

```
curl -x PUT -H 'Accept: application/json’ ‘http://server.com/api/badparameter'
```

Response headers:

```
HTTP/1.1 404 Not Found
Content-type: application/json
```

Response:

```json
{
"message": "Parameter age is not of the expected type: number. Value: male",
"name": "age",
"type": "number",
"value": "male"
}
```

Exception class:

```java
@Status(value = 400, serializeProperties = true)
public class BadParameterTypeException extends RuntimeException {

private final String name;
private final String value;
private final String expectedType;

public BadParameterTypeException(String name, String value, String expectedType) {
super("Parameter " + name + " is not of the expected type: " + expectedType + ". Value: " + value);
this.name = name;
this.value = value;
this.expectedType = expectedType;
}

@Override
public String getMessage() {
return super.getMessage();
}

public String getName() {
return name;
}

public String getValue() {
return value;
}

public String getExpectedType() {
return expectedType;
}
}
```