1 package ejava.examples.ejbwar.customer.client;
2
3 import java.net.URI;
4
5
6 import javax.ws.rs.core.HttpHeaders;
7 import javax.ws.rs.core.MediaType;
8 import javax.ws.rs.core.Response;
9 import javax.ws.rs.core.UriBuilder;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.apache.http.HttpResponse;
14 import org.apache.http.client.HttpClient;
15 import org.apache.http.client.methods.HttpDelete;
16 import org.apache.http.client.methods.HttpGet;
17 import org.apache.http.client.methods.HttpPost;
18 import org.apache.http.entity.StringEntity;
19 import org.apache.http.util.EntityUtils;
20
21 import ejava.examples.ejbwar.customer.bo.Customer;
22 import ejava.examples.ejbwar.customer.bo.CustomerRepresentation;
23 import ejava.examples.ejbwar.customer.bo.Customers;
24 import ejava.examples.ejbwar.customer.rs.CustomersResource;
25
26
27
28
29
30
31 public class CustomerClientImpl implements CustomerClient {
32 private static final Log log = LogFactory.getLog(CustomerClientImpl.class);
33 private HttpClient client;
34
35
36
37 private URI appURI;
38
39 public void setHttpClient(HttpClient client) {
40 this.client = client;
41 }
42 public void setAppURI(URI appURI) {
43 this.appURI = appURI;
44 }
45
46
47
48
49
50
51
52
53
54 protected <T> UriBuilder buildURI(Class<T> resourceClass, String method) {
55
56
57 return UriBuilder.fromUri(appURI)
58
59
60 .path("rest")
61
62 .path(resourceClass)
63
64 .path(resourceClass,method);
65
66
67 }
68
69 @Override
70 public Customer addCustomer(Customer customer) throws Exception {
71 URI uri = buildURI(CustomersResource.class,"addCustomer")
72 .build();
73
74
75 HttpPost post = new HttpPost(uri);
76 post.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML);
77 post.setEntity(new StringEntity(customer.toString(), "UTF-8"));
78
79
80 HttpResponse response = client.execute(post);
81 log.info(String.format("%s %s", post.getURI(), response));
82 if (Response.Status.CREATED.getStatusCode() == response.getStatusLine().getStatusCode()) {
83 return CustomerRepresentation.unmarshall(Customer.class,
84 response.getEntity().getContent());
85 }
86 log.warn(EntityUtils.toString(response.getEntity()));
87 return null;
88 }
89
90 @Override
91 public Customers findCustomersByName(String firstName, String lastName, int offset, int limit) throws Exception {
92
93 URI uri = buildURI(CustomersResource.class,"findCustomersByName")
94
95 .queryParam("firstName", firstName)
96 .queryParam("lastName", lastName)
97 .queryParam("offset", offset)
98 .queryParam("limit", limit)
99 .build();
100
101
102 HttpGet get = new HttpGet(uri);
103 get.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML);
104
105
106 HttpResponse response = client.execute(get);
107 log.info(String.format("%s %s", get.getURI(), response));
108 if (Response.Status.OK.getStatusCode() == response.getStatusLine().getStatusCode()) {
109 return CustomerRepresentation.unmarshall(Customers.class,
110 response.getEntity().getContent());
111 }
112 log.warn(EntityUtils.toString(response.getEntity()));
113 return null;
114 }
115
116 @Override
117 public Customer getCustomer(int id) throws Exception {
118 URI uri = buildURI(CustomersResource.class,"getCustomer")
119
120 .build(id);
121
122
123 HttpGet get = new HttpGet(uri);
124
125
126 HttpResponse response = client.execute(get);
127 log.info(String.format("%s %s", get.getURI(), response));
128 if (Response.Status.OK.getStatusCode() == response.getStatusLine().getStatusCode()) {
129 return CustomerRepresentation.unmarshall(Customer.class,
130 response.getEntity().getContent());
131 }
132 log.warn(EntityUtils.toString(response.getEntity()));
133 return null;
134 }
135
136 @Override
137 public boolean deleteCustomer(int id) throws Exception {
138 URI uri = buildURI(CustomersResource.class,"deleteCustomer")
139
140 .build(id);
141
142
143 HttpDelete delete = new HttpDelete(uri);
144
145
146 HttpResponse response = client.execute(delete);
147 log.info(String.format("%s %s", delete.getURI(), response));
148 if (Response.Status.OK.getStatusCode() == response.getStatusLine().getStatusCode()) {
149 EntityUtils.consume(response.getEntity());
150 return true;
151 }
152 log.warn(EntityUtils.toString(response.getEntity()));
153 return false;
154 }
155 }