Skip to content

Commit

Permalink
Add UnsupportedContentTypeException javadsl
Browse files Browse the repository at this point in the history
  • Loading branch information
mdedetrich committed Dec 21, 2023
1 parent 61a7dd3 commit ffbd611
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,20 @@
package org.apache.pekko.http.javadsl.unmarshalling

import java.util.concurrent.CompletionStage
import java.util.Optional

import org.apache.pekko
import pekko.actor.ClassicActorSystemProvider
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit ffbd611

Please sign in to comment.