Skip to content

Commit

Permalink
Merge pull request #174 from inoio/fix-responseParser.getContentType-…
Browse files Browse the repository at this point in the history
…deprecation

Fix responseParser.getContentType deprecation
  • Loading branch information
magro authored Jan 21, 2024
2 parents 3b09ffe + d221de8 commit d020606
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
43 changes: 23 additions & 20 deletions src/main/scala/io/ino/solrs/AsyncSolrClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ class AsyncSolrClient[F[_]] protected (private[solrs] val loadBalancer: LoadBala

@throws[RemoteSolrException]
private def validateResponse(response: Response, responseParser: ResponseParser)(implicit server: SolrServer): Unit = {
validateMimeType(responseParser.getContentType, response)
validateMimeType(responseParser.getContentTypes.asScala, response)

val httpStatus = response.getStatusCode
if(httpStatus >= 400) {
Expand All @@ -731,30 +731,33 @@ class AsyncSolrClient[F[_]] protected (private[solrs] val loadBalancer: LoadBala
}

@throws[RemoteSolrException]
protected def validateMimeType(expectedContentType: String, response: Response): Unit = {
if (expectedContentType != null) {
val expectedMimeType = getMimeType(expectedContentType).map(_.toLowerCase(Locale.ROOT)).getOrElse("")
val actualMimeType = getMimeType(response.getContentType).map(_.toLowerCase(Locale.ROOT)).getOrElse("")
if (expectedMimeType != actualMimeType) {
var msg = s"Expected mime type [$expectedMimeType] but got [$actualMimeType]."
var encoding: String = getResponseEncoding(response)
try {
// might be solved with responseParser.processResponse (like it's done for 4xx codes)
msg = msg + "\n" + IOUtils.toString(response.getResponseBodyAsStream, encoding)
}
catch {
case e: IOException =>
metrics.countRemoteException
throw new RemoteSolrException(response.getStatusCode, s"$msg Unfortunately could not parse response (for debugging) with encoding $encoding", e)
}
metrics.countRemoteException
throw new RemoteSolrException(response.getStatusCode, msg, null)
protected def validateMimeType(supportedContentTypes: Iterable[String], response: Response): Unit = {
val actualMimeType = getMimeType(response.getContentType).map(_.toLowerCase(Locale.ROOT)).getOrElse("")
val matchingMimeType = supportedContentTypes.find { supportedContentType =>
val expectedMimeType = getMimeType(supportedContentType).map(_.toLowerCase(Locale.ROOT)).getOrElse("")
expectedMimeType == actualMimeType
}

if (matchingMimeType.isEmpty) {
var msg = s"Expected a mime type of [${supportedContentTypes.mkString(", ")}] but got [$actualMimeType]."
val encoding: String = getResponseEncoding(response)
try {
// might be solved with responseParser.processResponse (like it's done for 4xx codes)
msg = msg + "\n" + IOUtils.toString(response.getResponseBodyAsStream, encoding)
}
catch {
case e: IOException =>
metrics.countRemoteException
throw new RemoteSolrException(response.getStatusCode, s"$msg Unfortunately could not parse response (for debugging) with encoding $encoding", e)
}
metrics.countRemoteException
throw new RemoteSolrException(response.getStatusCode, msg, null)

}
}

protected def getResponseEncoding(response: Response): String = {
var encoding = response.getHeader("Content-Encoding")
val encoding = response.getHeader("Content-Encoding")
if (encoding == null) "UTF-8" else encoding
}

Expand Down
13 changes: 10 additions & 3 deletions src/test/scala/io/ino/solrs/AsyncSolrClientIntegrationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import org.asynchttpclient.DefaultAsyncHttpClient
import org.apache.solr.client.solrj.SolrQuery
import org.apache.solr.client.solrj.SolrRequest
import org.apache.solr.client.solrj.SolrResponse
import org.apache.solr.client.solrj.impl.BinaryResponseParser
import org.apache.solr.client.solrj.impl.BinaryResponseParser.BINARY_CONTENT_TYPE
import org.apache.solr.client.solrj.impl.BinaryResponseParser.BINARY_CONTENT_TYPE_V2
import org.apache.solr.client.solrj.impl.{NoOpResponseParser, XMLResponseParser}
import org.apache.solr.client.solrj.request.{GenericSolrRequest, QueryRequest}
import org.apache.solr.client.solrj.response.QueryResponse
import org.mockito.ArgumentMatcher._
import org.mockito.ArgumentMatchers.anyLong
import org.mockito.Mockito.verify
import org.scalatest.concurrent.Eventually._
Expand All @@ -18,6 +20,7 @@ import org.scalatest.concurrent.PatienceConfiguration.Timeout
import org.scalatest.time.Millis
import org.scalatest.time.Span

import java.util
import scala.annotation.tailrec
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
Expand Down Expand Up @@ -123,7 +126,9 @@ class AsyncSolrClientIntegrationSpec extends StandardFunSpec with RunningSolr {
null,
new SolrQuery("cat:cat1").add("wt", "xml")
)
request.setResponseParser(new NoOpResponseParser)
request.setResponseParser(new NoOpResponseParser {
override def getContentTypes: util.Collection[String] = asList("application/xml")
})

val response = solr.execute(request)

Expand Down Expand Up @@ -151,7 +156,9 @@ class AsyncSolrClientIntegrationSpec extends StandardFunSpec with RunningSolr {
awaitReady(response)
a [RemoteSolrException] should be thrownBy await(response)
// embedded Jetty returns 404 with text/html response with error message in body
(the [RemoteSolrException] thrownBy await(response)).getMessage should include ("Expected mime type [] but got [text/html]")
val mimeTypesRegex = s"($BINARY_CONTENT_TYPE, $BINARY_CONTENT_TYPE_V2|$BINARY_CONTENT_TYPE_V2, $BINARY_CONTENT_TYPE)"
(the [RemoteSolrException] thrownBy await(response)).getMessage should include regex "Expected a mime type of " +
s"\\[$mimeTypesRegex\\] but got \\[text/html\\]"

solr.shutdown()
}
Expand Down

0 comments on commit d020606

Please sign in to comment.