diff --git a/http/src/main/scala/org/apache/pekko/http/javadsl/unmarshalling/Unmarshaller.scala b/http/src/main/scala/org/apache/pekko/http/javadsl/unmarshalling/Unmarshaller.scala index 7c20d377d..5a20c42e9 100644 --- a/http/src/main/scala/org/apache/pekko/http/javadsl/unmarshalling/Unmarshaller.scala +++ b/http/src/main/scala/org/apache/pekko/http/javadsl/unmarshalling/Unmarshaller.scala @@ -14,6 +14,7 @@ package org.apache.pekko.http.javadsl.unmarshalling import java.util.concurrent.CompletionStage +import java.util.Optional import org.apache.pekko import pekko.actor.ClassicActorSystemProvider @@ -21,14 +22,12 @@ import pekko.annotation.InternalApi import pekko.http.impl.model.JavaQuery import pekko.http.impl.util.JavaMapping import pekko.http.impl.util.JavaMapping.Implicits._ -import pekko.http.javadsl.model._ +import pekko.http.{ javadsl => jm } +import jm.model._ import pekko.http.scaladsl.model.{ ContentTypeRange, ContentTypes } import pekko.http.scaladsl.unmarshalling import pekko.http.scaladsl.unmarshalling.FromEntityUnmarshaller -import pekko.http.scaladsl.unmarshalling.Unmarshaller.{ - EnhancedFromEntityUnmarshaller, - UnsupportedContentTypeException -} +import pekko.http.scaladsl.unmarshalling.Unmarshaller.EnhancedFromEntityUnmarshaller import pekko.http.scaladsl.util.FastFuture import pekko.stream.{ Materializer, SystemMaterializer } import pekko.util.ByteString @@ -79,6 +78,7 @@ object Unmarshaller extends pekko.http.javadsl.unmarshalling.Unmarshallers { unmarshalling.Unmarshaller.strict[HttpRequest, RequestEntity](_.entity) def forMediaType[B](t: MediaType, um: Unmarshaller[HttpEntity, B]): Unmarshaller[HttpEntity, B] = { + import pekko.http.scaladsl.unmarshalling.Unmarshaller.UnsupportedContentTypeException unmarshalling.Unmarshaller.withMaterializer[HttpEntity, B] { implicit ex => implicit mat => jEntity => { val entity = jEntity.asScala @@ -124,6 +124,40 @@ object Unmarshaller extends pekko.http.javadsl.unmarshalling.Unmarshallers { implicit mi: JavaMapping[JI, SI]): unmarshalling.Unmarshaller[JI, O] = um.asInstanceOf[unmarshalling.Unmarshaller[JI, O]] // since guarantee provided by existence of `mi` + class UnsupportedContentTypeException( + private val _supported: java.util.Set[jm.model.ContentTypeRange], + private val _actualContentType: Optional[jm.model.ContentType]) + extends RuntimeException(_supported.asScala.mkString( + s"Unsupported Content-Type [${_actualContentType.asScala}], supported: ", ", ", "")) { + + def this(supported: jm.model.ContentTypeRange*) = { + this(supported.toSet.asJava, Optional.empty[jm.model.ContentType]()) + } + + def this(supported: java.util.Set[jm.model.ContentTypeRange]) = { + this(supported, Optional.empty[jm.model.ContentType]()) + } + + def this(contentType: Optional[jm.model.ContentType], supported: jm.model.ContentTypeRange*) = { + this(supported.toSet.asJava, contentType) + } + + def toScala(): pekko.http.scaladsl.unmarshalling.Unmarshaller.UnsupportedContentTypeException = + pekko.http.scaladsl.unmarshalling.Unmarshaller.UnsupportedContentTypeException( + _supported.asScala.toSet.asInstanceOf[Set[pekko.http.scaladsl.model.ContentTypeRange]], + _actualContentType.asScala) + + def getSupported(): java.util.Set[jm.model.ContentTypeRange] = _supported + + def getActualContentType(): Optional[jm.model.ContentType] = _actualContentType + + override def equals(that: Any): Boolean = that match { + case that: UnsupportedContentTypeException => + that._supported == this._supported && that._actualContentType == this._actualContentType + case _ => false + } + } + } trait UnmarshallerBase[-A, B] diff --git a/http/src/main/scala/org/apache/pekko/http/scaladsl/unmarshalling/Unmarshaller.scala b/http/src/main/scala/org/apache/pekko/http/scaladsl/unmarshalling/Unmarshaller.scala index 2385d2f06..2743f9859 100644 --- a/http/src/main/scala/org/apache/pekko/http/scaladsl/unmarshalling/Unmarshaller.scala +++ b/http/src/main/scala/org/apache/pekko/http/scaladsl/unmarshalling/Unmarshaller.scala @@ -15,15 +15,19 @@ package org.apache.pekko.http.scaladsl.unmarshalling import org.apache.pekko import pekko.event.Logging +import pekko.http.{ javadsl => jm } import pekko.http.scaladsl.model._ import pekko.http.scaladsl.util.FastFuture import pekko.http.scaladsl.util.FastFuture._ +import pekko.http.impl.util.JavaMapping.Implicits._ import pekko.stream.Materializer +import pekko.util.OptionConverters._ +import scala.collection.JavaConverters._ import scala.concurrent.{ ExecutionContext, Future } import scala.util.control.{ NoStackTrace, NonFatal } -trait Unmarshaller[-A, B] extends pekko.http.javadsl.unmarshalling.Unmarshaller[A, B] { +trait Unmarshaller[-A, B] extends jm.unmarshalling.Unmarshaller[A, B] { implicit final def asScala: Unmarshaller[A, B] = this @@ -164,21 +168,29 @@ object Unmarshaller * [[pekko.http.scaladsl.unmarshalling.Unmarshaller]] instead. */ final class UnsupportedContentTypeException( - val supported: Set[ContentTypeRange], - val actualContentType: Option[ContentType]) - extends RuntimeException(supported.mkString( - s"Unsupported Content-Type [$actualContentType], supported: ", ", ", "")) with Product with Serializable { + private val _supported: java.util.Set[jm.model.ContentTypeRange], + private val _actualContentType: java.util.Optional[jm.model.ContentType]) + extends jm.unmarshalling.Unmarshaller.UnsupportedContentTypeException(_supported, _actualContentType) with Product + with Serializable { + + val supported: Set[ContentTypeRange] = + _supported.asScala.toSet.asInstanceOf[Set[pekko.http.scaladsl.model.ContentTypeRange]] + val actualContentType: Option[ContentType] = _actualContentType.asScala + + def this(supported: Set[ContentTypeRange], actualContentType: Option[ContentType]) = + this(supported.asJava.asInstanceOf[java.util.Set[jm.model.ContentTypeRange]], + actualContentType.toJava.asInstanceOf[java.util.Optional[jm.model.ContentType]]) @deprecated("for binary compatibility", since = "Akka HTTP 10.1.9") def this(supported: Set[ContentTypeRange]) = this(supported, None) @deprecated("for binary compatibility", since = "Akka HTTP 10.1.9") def copy(supported: Set[ContentTypeRange]): UnsupportedContentTypeException = - new UnsupportedContentTypeException(supported, this.actualContentType) + new UnsupportedContentTypeException(supported, actualContentType) @deprecated("for binary compatibility", since = "Akka HTTP 10.1.9") def copy$default$1(supported: Set[ContentTypeRange]): UnsupportedContentTypeException = - new UnsupportedContentTypeException(supported, this.actualContentType) + new UnsupportedContentTypeException(supported, actualContentType) @deprecated("for binary compatibility", since = "Akka HTTP 10.1.9") def copy( @@ -190,7 +202,7 @@ object Unmarshaller override def equals(that: Any): Boolean = that match { case that: UnsupportedContentTypeException => - that.canEqual(this) && that.supported == this.supported && that.actualContentType == this.actualContentType + that.canEqual(this) && super.equals(this) case _ => false } override def productArity: Int = 1