1 package info.ejava.examples.jaxrs.todos.client;
2
3 import javax.ws.rs.core.Response;
4
5 public class ResponseUtil {
6 static <T> T getEntity(Response response, Class<T> type) {
7 if (Response.Status.Family.SUCCESSFUL.equals(response.getStatusInfo().getFamily())) {
8 return response.readEntity(type, type.getAnnotations());
9 } else {
10 throw new IllegalStateException(String.format("error response[%d %s]: %s",
11 response.getStatus(),
12 response.getStatusInfo(),
13 response.readEntity(String.class))
14 );
15 }
16 }
17
18 static <T> void assertSuccess(String message, Response response) {
19 if (!Response.Status.Family.SUCCESSFUL.equals(response.getStatusInfo().getFamily())) {
20 throw new IllegalStateException(String.format(message + ", error response[%d %s]: %s",
21 response.getStatus(),
22 response.getStatusInfo(),
23 response.readEntity(String.class))
24 );
25 } else {
26 response.close();
27 }
28 }
29 }