forked from howtoprogram/Java-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookRepositoryImplApacheHttpClient.java
More file actions
151 lines (125 loc) · 5.47 KB
/
Copy pathBookRepositoryImplApacheHttpClient.java
File metadata and controls
151 lines (125 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.howtoprogram.repository;
import java.util.concurrent.Future;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.nio.client.util.HttpAsyncClientUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.howtoprogram.domain.Book;
public class BookRepositoryImplApacheHttpClient {
private static final String URI_BOOK = "http://localhost:8080/v1/books";
public void deleteBook(Long id) throws Exception {
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
try {
httpclient.start();
// Create a delete method instance.
HttpDelete request = new HttpDelete(URI_BOOK + "/" + id);
Future<HttpResponse> future = httpclient.execute(request, null);
// Wait and retrieve the result
HttpResponse response = future.get();
System.out.println("Response code:" + response.getStatusLine().getStatusCode());
// Determine whether the request was successfully or not
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) {
throw new RuntimeException("Failed to delete the book with id:" + id);
}
} finally {
HttpAsyncClientUtils.closeQuietly(httpclient);
}
}
public Book updateBook(Book book) throws Exception {
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
Book createdBook = null;
try {
httpclient.start();
// Create a HttpPut instance.
HttpPut request = new HttpPut(URI_BOOK + "/" + book.getId());
request.setHeader("Content-type", "application/json");
// Create new instance of ObjectMapper
ObjectMapper mapper = new ObjectMapper();
String jsonBook = mapper.writeValueAsString(book);
StringEntity entity = new StringEntity(jsonBook);
// Set the entity for the request
request.setEntity(entity);
Future<HttpResponse> future = httpclient.execute(request, null);
// Wait and retrieve the result
HttpResponse response = future.get();
System.out.println("Response code:" + response.getStatusLine().getStatusCode());
// Determine whether the request was successfully or not
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// Get back the updated book
createdBook = mapper.readValue(response.getEntity().getContent(), Book.class);
}
} finally {
HttpAsyncClientUtils.closeQuietly(httpclient);
}
return createdBook;
}
public Book createBook(Book book) throws Exception {
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
Book createdBook = null;
try {
httpclient.start();
// Create a delete method instance.
HttpPost request = new HttpPost(URI_BOOK);
request.setHeader("Content-type", "application/json");
// Create new instance of ObjectMapper
ObjectMapper mapper = new ObjectMapper();
String jsonBook = mapper.writeValueAsString(book);
StringEntity entity = new StringEntity(jsonBook);
// Set the entity for the request
request.setEntity(entity);
Future<HttpResponse> future = httpclient.execute(request, null);
// Wait and retrieve the result
HttpResponse response = future.get();
System.out.println("Response code:" + response.getStatusLine().getStatusCode());
// Determine whether the request was successfully or not
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
// Get back the created book
createdBook = mapper.readValue(response.getEntity().getContent(), Book.class);
}
} finally {
HttpAsyncClientUtils.closeQuietly(httpclient);
}
return createdBook;
}
public Book[] getAllBooks() throws Exception {
Book[] books = null;
// Create an asyn HttpClient
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
try {
httpclient.start();
HttpGet request = new HttpGet(URI_BOOK);
Future<HttpResponse> future = httpclient.execute(request, null);
// Wait and retrieve the result
HttpResponse response = future.get();
System.out.println("Response code:" + response.getStatusLine().getStatusCode());
// Determine whether the request was successfully or not
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity = response.getEntity();
// Create a Jackson ObjectMapper to convert the JSON response to Java objects
ObjectMapper mapper = new ObjectMapper();
// Read the inputstream and convert to an array of Book
books = mapper.readValue(httpEntity.getContent(), Book[].class);
}
} finally {
HttpAsyncClientUtils.closeQuietly(httpclient);
}
return books;
}
public static void main(String[] args) throws Exception {
BookRepositoryImplApacheHttpClient bookRepository = new BookRepositoryImplApacheHttpClient();
// Getting the first book from the RESTful service
Book book = bookRepository.getAllBooks()[0];
bookRepository.deleteBook(book.getId());
}
public Book findBookById(Long id) {
return null;
}
}