ok2c opened a new pull request, #616:
URL: https://github.com/apache/httpcomponents-core/pull/616

   I have added four convenience classes: `AsyncClientPipeline`, 
`AsyncServerPipeline`, `AsyncJsonClientPipeline`, and`AsyncJsonServerPipeline` 
that help put together message processing pipelines using a very simple, fluent 
DSL. 
   
   The pipeline assembly DSL makes quite a big difference for JSON processing 
especially. 
   
   I am open to ideas of better naming or other improvements. If I hear no 
objections I will commit the changes in a couple of days. There is still plenty 
of time to polish or even revert.
   
   ```
   AsyncJsonClientPipeline.assemble(objectMapper)
           .request()
           .post(uri)
           .asObject(new BasicNameValuePair("name", "value"))
           .response()
           .asObject(RequestData.class)
           .result(new FutureCallback<Message<HttpResponse, RequestData>>() {
   
               @Override
               public void completed(final Message<HttpResponse, RequestData> 
m) {
                   System.out.println("Response status: " + m.head().getCode());
                   System.out.println(m.body());
                   latch.countDown();
               }
   
               @Override
               public void failed(final Exception ex) {
                   ex.printStackTrace(System.out);
                   latch.countDown();
               }
   
               @Override
               public void cancelled() {
                   latch.countDown();
               }
   
           })
           .create(),
   ```
   
   ```
   AsyncJsonServerPipeline.assemble(objectMapper)
           // Read GET / HEAD requests by consuming content stream as JSON nodes
           .request(Method.GET, Method.HEAD, Method.POST, Method.PUT, 
Method.PATCH)
           .asJsonNode()
           // Write out responses by streaming out content of JSON object
           .response()
           .asObject(RequestData.class)
           // Map exceptions to a response message
           .errorMessage(Throwable::getMessage)
           // Generate a response to a request
           .handle((m, context) -> {
               final HttpRequest request = m.head();
               final RequestData rd = new RequestData();
               try {
                   rd.setUrl(request.getUri());
               } catch (final URISyntaxException ex) {
                   throw new ProtocolException("Invalid request URI");
               }
               rd.generateHeaders(request.getHeaders());
               rd.setJson(m.body());
               rd.setData(Objects.toString(m.error()));
   
               final HttpCoreContext coreContext = 
HttpCoreContext.cast(context);
               final EndpointDetails endpointDetails = 
coreContext.getEndpointDetails();
   
               final InetSocketAddress remoteAddress = (InetSocketAddress) 
endpointDetails.getRemoteAddress();
               rd.setOrigin(Objects.toString(remoteAddress.getAddress()));
   
               return Message.of(new BasicHttpResponse(HttpStatus.SC_OK), rd);
           })
           .supplier();
   ``` 
   
   I am aware of some code duplication. The choice was between a very ugly 
coupling between classes with lots unnecessary details spilling into the public 
API and loose coupling with some code duplication. I opted for the latter.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to