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