org.hibernate.search.elasticsearch.util.impl

## Class GsonHttpEntity

- java.lang.Object
- - org.hibernate.search.elasticsearch.util.impl.GsonHttpEntity

- All Implemented Interfaces:
    Closeable, AutoCloseable, org.apache.http.HttpEntity, org.apache.http.nio.entity.HttpAsyncContentProducer, DigestSelfSigningCapable

      public final class GsonHttpEntity
      extends Object
      implements org.apache.http.HttpEntity, org.apache.http.nio.entity.HttpAsyncContentProducer, DigestSelfSigningCapable

  Optimised adapter to encode GSON objects into HttpEntity instances. The naive approach was using various StringBuilders; the objects we need to serialise into JSON might get large and this was causing the internal StringBuilder buffers to need frequent resizing and cause problems with excessive allocations. Rather than trying to guess reasonable default sizes for these buffers, we can defer the serialisation to write directly into the ByteBuffer of the HTTP client, this has the additional benefit of making the intermediary buffers short lived. The one complexity to watch for is flow control: when writing into the output buffer chances are that not all bytes are accepted; in this case we have to hold on the remaining portion of data to be written when the flow control is re-enabled. A side effect of this strategy is that the total content length which is being produced is not known in advance. Not reporting the length in advance to the Apache Http client causes it to use chunked-encoding, which is great for large blocks but not optimal for small messages. For this reason we attempt to start encoding into a small buffer upfront: if all data we need to produce fits into that then we can report the content length; if not the encoding completion will be deferred but not resetting so to avoid repeating encoding work.
  Author:
    Sanne Grinovero (C) 2017 Red Hat Inc.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `GsonHttpEntity(com.google.gson.Gson gson, List<com.google.gson.JsonObject> bodyParts)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()`
    `void` | `consumeContent()`
    `void` | `fillDigest(MessageDigest digest)`
    `InputStream` | `getContent()`
    `org.apache.http.Header` | `getContentEncoding()`
    `long` | `getContentLength()`
    `org.apache.http.Header` | `getContentType()`
    `boolean` | `isChunked()`
    `boolean` | `isRepeatable()`
    `boolean` | `isStreaming()`
    `void` | `produceContent(org.apache.http.nio.ContentEncoder encoder, org.apache.http.nio.IOControl ioctrl)`
    `void` | `writeTo(OutputStream outstream)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### GsonHttpEntity

          public GsonHttpEntity(com.google.gson.Gson gson,
                                List<com.google.gson.JsonObject> bodyParts)

  - ### Method Detail

    - #### isRepeatable

          public boolean isRepeatable()

      Specified by:
        `isRepeatable` in interface `org.apache.http.HttpEntity`
      Specified by:
        `isRepeatable` in interface `org.apache.http.nio.entity.HttpAsyncContentProducer`

    - #### isChunked

          public boolean isChunked()

      Specified by:
        `isChunked` in interface `org.apache.http.HttpEntity`

    - #### getContentLength

          public long getContentLength()

      Specified by:
        `getContentLength` in interface `org.apache.http.HttpEntity`

    - #### getContentType

          public org.apache.http.Header getContentType()

      Specified by:
        `getContentType` in interface `org.apache.http.HttpEntity`

    - #### getContentEncoding

          public org.apache.http.Header getContentEncoding()

      Specified by:
        `getContentEncoding` in interface `org.apache.http.HttpEntity`

    - #### getContent

          public InputStream getContent()
                                 throws IOException

      Specified by:
        `getContent` in interface `org.apache.http.HttpEntity`
      Throws:
        `IOException`

    - #### writeTo

          public void writeTo(OutputStream outstream)
                       throws IOException

      Specified by:
        `writeTo` in interface `org.apache.http.HttpEntity`
      Throws:
        `IOException`

    - #### isStreaming

          public boolean isStreaming()

      Specified by:
        `isStreaming` in interface `org.apache.http.HttpEntity`

    - #### consumeContent

          public void consumeContent()
                              throws IOException

      Specified by:
        `consumeContent` in interface `org.apache.http.HttpEntity`
      Throws:
        `IOException`

    - #### close

          public void close()
                     throws IOException

      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Throws:
        `IOException`

    - #### produceContent

          public void produceContent(org.apache.http.nio.ContentEncoder encoder,
                                     org.apache.http.nio.IOControl ioctrl)
                              throws IOException

      Specified by:
        `produceContent` in interface `org.apache.http.nio.entity.HttpAsyncContentProducer`
      Throws:
        `IOException`

    - #### fillDigest

          public void fillDigest(MessageDigest digest)
                          throws IOException

      Specified by:
        `fillDigest` in interface `DigestSelfSigningCapable`
      Throws:
        `IOException`

Copyright © 2006-2017 Red Hat, Inc. All Rights Reserved