1 package info.ejava.examples.jaxrs.todos.client;
2
3 import java.net.URI;
4
5 import javax.ws.rs.client.Client;
6 import javax.ws.rs.client.Entity;
7 import javax.ws.rs.client.WebTarget;
8 import javax.ws.rs.core.MediaType;
9 import javax.ws.rs.core.Response;
10 import javax.ws.rs.core.UriBuilder;
11
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import ejava.util.jaxb.JAXBUtil;
16 import ejava.util.json.JsonbUtil;
17 import info.ejava.examples.jaxrs.todos.dto.TodoItemDTO;
18 import info.ejava.examples.jaxrs.todos.dto.TodoListDTO;
19
20 public class TodosJaxRsClientImpl implements TodosJaxRsClient {
21 private static final Logger logger = LoggerFactory.getLogger(TodosJaxRsClientImpl.class);
22 private URI baseUrl;
23 private Client client;
24 private MediaType mediaType = MediaType.APPLICATION_JSON_TYPE;
25
26 public TodosJaxRsClientImpl(Client client, URI baseUrl, String mediaType) {
27 this.client = client;
28 this.baseUrl = baseUrl;
29 setMediaType(mediaType);
30 }
31 public void setMediaType(MediaType mediaType) {
32 this.mediaType = mediaType;
33 }
34 public void setMediaType(String mediaType) {
35 setMediaType(MediaType.valueOf(mediaType));
36 }
37
38 private UriBuilder getBaseUrl(String...path) {
39 UriBuilder builder = UriBuilder.fromUri(baseUrl);
40 if (path!=null) {
41 for (String p:path) {
42 builder = builder.path(p);
43 }
44 }
45 return builder;
46 }
47
48 private String marshal(Object object) {
49 try {
50 if (object==null) {
51 return null;
52 } else if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) {
53 return JsonbUtil.marshal(object);
54 } else if (MediaType.APPLICATION_SVG_XML_TYPE.equals(mediaType)) {
55 return JAXBUtil.marshal(object);
56 } else {
57 return object.toString();
58 }
59 } catch (Exception ex) {
60 return "";
61 }
62 }
63
64 @Override
65 public Response deleteAll() {
66 URI uri = getBaseUrl(TODO_LISTS_PATH).build();
67 WebTarget target = client.target(uri);
68 logger.debug("DELETE {}", target.getUri());
69
70 return target.request(mediaType)
71 .buildDelete()
72 .invoke();
73 }
74
75 @Override
76 public Response getTodoLists(Integer offset, Integer limit) {
77 URI uri = getBaseUrl(TODO_LISTS_PATH).build();
78
79 WebTarget target = client.target(uri);
80 if (offset!=null) {
81 target=target.queryParam(OFFSET, offset);
82 }
83 if (limit!=null) {
84 target=target.queryParam(LIMIT, limit);
85 }
86 logger.debug("GET {}", target.getUri());
87
88 return target.request(mediaType)
89 .buildGet()
90 .invoke();
91 }
92
93 @Override
94 public Response createTodoList(TodoListDTO todoList) {
95 URI uri = getBaseUrl(TODO_LISTS_PATH).build();
96 WebTarget target = client.target(uri);
97
98 Response response = target.request(mediaType)
99 .buildPost(Entity.entity(todoList, mediaType, todoList.getClass().getAnnotations()))
100 .invoke();
101 logger.debug("POST {}\n{}\n=>{}/{}\n"
102 + "Location: {}\n"
103 + "Content-Location: {}", target.getUri(),
104 marshal(todoList),
105 response.getStatusInfo(), response.getStatus(),
106 response.getHeaderString("Location"),
107 response.getHeaderString("Content-Location")
108 );
109 return response;
110 }
111
112 @Override
113 public Response getTodoList(String listName) {
114 URI uri = getBaseUrl(TODO_LIST_PATH).build(listName);
115 WebTarget target = client.target(uri);
116 logger.debug("GET {}", target.getUri());
117
118 return target.request(mediaType)
119 .buildGet()
120 .invoke();
121 }
122
123 @Override
124 public Response deleteTodoList(String listName) {
125 URI uri = getBaseUrl(TODO_LIST_PATH).build(listName);
126 WebTarget target = client.target(uri);
127 logger.debug("DELETE {}", target.getUri());
128
129 return target.request(mediaType)
130 .buildDelete()
131 .invoke();
132 }
133
134 @Override
135 public Response renameTodoList(String oldName, String newName) {
136 URI uri = getBaseUrl(TODO_LIST_PATH).build(oldName);
137 WebTarget target = client.target(uri)
138 .queryParam(NAME_PARAM, newName);
139 logger.debug("POST {}", target.getUri());
140
141 return target.request(mediaType)
142 .buildPost(null)
143 .invoke();
144 }
145
146 @Override
147 public Response addTodoItem(String listName, TodoItemDTO item) {
148 URI uri = getBaseUrl(TODO_LIST_PATH, TODO_ITEMS_PATH).build(listName);
149 WebTarget target = client.target(uri);
150 logger.debug("POST {}", target.getUri());
151
152 Entity<TodoItemDTO> entity = item==null ?
153 null :
154 Entity.entity(item, mediaType, item.getClass().getAnnotations());
155
156 return target.request(mediaType)
157 .buildPost(entity)
158 .invoke();
159 }
160
161 @Override
162 public Response updateTodoItem(String listName, TodoItemDTO item) {
163 URI uri = getBaseUrl(TODO_LIST_PATH, TODO_ITEM_PATH).build(listName, item.getName());
164 WebTarget target = client.target(uri);
165 logger.debug("PUT {}", target.getUri());
166
167 return target.request(mediaType)
168 .buildPut(Entity.entity(item, mediaType, item.getClass().getAnnotations()))
169 .invoke();
170 }
171
172 @Override
173 public Response deleteTodoItem(String listName, String itemName) {
174 URI uri = getBaseUrl(TODO_LIST_PATH, TODO_ITEM_PATH).build(listName, itemName);
175 WebTarget target = client.target(uri);
176 logger.debug("DELETE {}", target.getUri());
177
178 return target.request(mediaType)
179 .buildDelete()
180 .invoke();
181 }
182 }