Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.sksamuel.elastic4s.http

import java.io.InputStream
import java.nio.charset.Charset
import java.util.zip.GZIPInputStream

import com.sksamuel.elastic4s.Show
import org.apache.http.client.config.RequestConfig
Expand Down Expand Up @@ -31,10 +33,16 @@ class ElasticsearchJavaRestClient(client: RestClient) extends HttpClient {

def fromResponse(r: org.elasticsearch.client.Response): HttpResponse = {
val entity = Option(r.getEntity).map { entity =>
val contentEncoding = Option(entity.getContentEncoding).map(_.getValue).getOrElse("UTF-8")
implicit val codec: Codec = Codec(Charset.forName(contentEncoding))
val body = Source.fromInputStream(entity.getContent).mkString
HttpEntity.StringEntity(body, Some(contentEncoding))
val contentCharset = Option(ContentType.get(entity)).fold(Charset.forName("UTF-8"))(_.getCharset)
implicit val codec: Codec = Codec(contentCharset)

val contentStream: InputStream = {
if (isEntityGziped(entity)) new GZIPInputStream(entity.getContent)
else entity.getContent
}

val body = Source.fromInputStream(contentStream).mkString
HttpEntity.StringEntity(body, Some(contentCharset.name()))
}
val headers = r.getHeaders.map { header =>
header.getName -> header.getValue
Expand Down Expand Up @@ -63,6 +71,10 @@ class ElasticsearchJavaRestClient(client: RestClient) extends HttpClient {
}

override def close(): Unit = client.close()

private def isEntityGziped(entity: org.apache.http.HttpEntity): Boolean = {
Option(entity.getContentEncoding).flatMap(x => Option(x.getValue)).contains("gzip")
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait HttpClient extends Logging {
case class HttpResponse(statusCode: Int, entity: Option[HttpEntity.StringEntity], headers: Map[String, String])

sealed trait HttpEntity {
def contentType: Option[String]
def contentCharset: Option[String]
def get: String
}

Expand All @@ -42,15 +42,15 @@ object HttpEntity {
def apply(content: String): HttpEntity = HttpEntity(content, "application/json; charset=utf-8")
def apply(content: String, contentType: String): HttpEntity = StringEntity(content, Some(contentType))

case class StringEntity(content: String, contentType: Option[String]) extends HttpEntity {
case class StringEntity(content: String, contentCharset: Option[String]) extends HttpEntity {
def get: String = content
}

case class InputStreamEntity(content: InputStream, contentType: Option[String]) extends HttpEntity {
case class InputStreamEntity(content: InputStream, contentCharset: Option[String]) extends HttpEntity {
def get: String = Source.fromInputStream(content).getLines().mkString("\n")
}

case class FileEntity(content: File, contentType: Option[String]) extends HttpEntity {
case class FileEntity(content: File, contentCharset: Option[String]) extends HttpEntity {

import scala.collection.JavaConverters._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SttpRequestHttpClient(clientUri: ElasticsearchClientUri) extends HttpClien
params: Map[String, Any],
entity: HttpEntity): Request[String, Nothing] = {
val r = request(method, endpoint, params)
val r2 = entity.contentType.fold(r)(r.contentType)
val r2 = entity.contentCharset.fold(r)(r.contentType)
entity match {
case StringEntity(content: String, _) => r2.body(content)
case InputStreamEntity(in: InputStream, _) =>
Expand Down