Skip to content

Commit

Permalink
use convertIterable to wrap java Iterables
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Sep 30, 2024
1 parent d504f46 commit ca67b05
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import java.lang.{ Iterable => JIterable }

import pekko.annotation.DoNotInherit
import pekko.http.scaladsl
import pekko.japi.Util
import pekko.http.impl.util.Util
import pekko.pattern.CircuitBreakerOpenException
import pekko.util.OptionConverters._

Expand Down Expand Up @@ -416,7 +416,8 @@ object Rejections {
s.UnsupportedRequestEncodingRejection(supported.asScala)

def unsatisfiableRange(unsatisfiableRanges: java.lang.Iterable[ByteRange], actualEntityLength: Long) =
UnsatisfiableRangeRejection(Util.immutableSeq(unsatisfiableRanges).map(_.asScala), actualEntityLength)
UnsatisfiableRangeRejection(Util.convertIterable[ByteRange, ByteRange](unsatisfiableRanges).map(_.asScala),
actualEntityLength)

def tooManyRanges(maxRanges: Int) = TooManyRangesRejection(maxRanges)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ trait Rejected extends RouteResult {

object RouteResults {
import pekko.http.scaladsl.{ server => s }
import pekko.japi.Util
import pekko.http.impl.util.JavaMapping
import pekko.http.impl.util.{ JavaMapping, Util }
import JavaMapping.Implicits._
import RoutingJavaMapping._

Expand All @@ -38,7 +37,7 @@ object RouteResults {
}

def rejected(rejections: java.lang.Iterable[Rejection]): Rejected = {
s.RouteResult.Rejected(Util.immutableSeq(rejections).map(_.asScala))
s.RouteResult.Rejected(Util.convertIterable[Rejection, Rejection](rejections).map(_.asScala))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,36 @@ import java.util.function.{ Function => JFunction }

import org.apache.pekko
import pekko.actor.ActorSystem
import pekko.http.impl.util.JavaMapping
import pekko.http.javadsl.settings.ParserSettings
import pekko.http.javadsl.settings.RoutingSettings
import pekko.dispatch.ExecutionContexts
import pekko.event.LoggingAdapter
import pekko.japi.Util
import pekko.stream.Materializer
import pekko.stream.javadsl.Source
import pekko.util.ByteString
import pekko.util.FutureConverters._

import scala.concurrent.ExecutionContextExecutor
import pekko.http.impl.model.JavaUri
import pekko.http.javadsl.model.HttpRequest
import pekko.http.javadsl.model.HttpEntity
import pekko.http.javadsl.model.RequestEntity
import pekko.http.javadsl.model.Uri
import pekko.http.impl.util.JavaMapping
import pekko.http.impl.util.Util.convertIterable
import pekko.http.javadsl.model.{ HttpEntity, HttpRequest, RequestEntity, Uri }
import pekko.http.javadsl.server._
import pekko.http.scaladsl.server.{ Directives => D }
import pekko.http.javadsl.settings.{ ParserSettings, RoutingSettings }
import pekko.http.scaladsl
import pekko.stream.Materializer
import java.util.function.Supplier
import java.util.{ List => JList }
import pekko.http.scaladsl.server.{ Directives => D }

import pekko.http.javadsl.model.HttpResponse
import pekko.http.javadsl.model.ResponseEntity
import pekko.http.javadsl.model.HttpHeader
import pekko.http.scaladsl.util.FastFuture._
import pekko.http.javadsl.server

import java.lang.{ Iterable => JIterable }
import java.util.function.Supplier
import java.util.{ List => JList }
import java.util.concurrent.CompletionStage
import java.util.function.Predicate

import pekko.dispatch.ExecutionContexts
import pekko.event.LoggingAdapter
import pekko.http.javadsl.server
import pekko.util.FutureConverters._

import scala.concurrent.ExecutionContextExecutor
import scala.concurrent.duration.FiniteDuration

abstract class BasicDirectives {
Expand All @@ -66,7 +63,8 @@ abstract class BasicDirectives {

def mapRejections(f: JFunction[JList[Rejection], JList[Rejection]], inner: Supplier[Route]): Route = RouteAdapter {
D.mapRejections(rejections =>
Util.immutableSeq(f.apply(Util.javaArrayList(rejections.map(_.asJava)))).map(_.asScala)) { inner.get.delegate }
convertIterable[Rejection, Rejection](f.apply(Util.javaArrayList(rejections.map(_.asJava)))).map(
_.asScala)) { inner.get.delegate }
}

def mapResponse(f: JFunction[HttpResponse, HttpResponse], inner: Supplier[Route]): Route = RouteAdapter {
Expand All @@ -79,7 +77,10 @@ abstract class BasicDirectives {

def mapResponseHeaders(f: JFunction[JList[HttpHeader], JList[HttpHeader]], inner: Supplier[Route]): Route =
RouteAdapter {
D.mapResponseHeaders(l => Util.immutableSeq(f.apply(Util.javaArrayList(l))).map(_.asScala)) { inner.get.delegate } // TODO try to remove map()
D.mapResponseHeaders(l =>
convertIterable[HttpHeader, HttpHeader](f.apply(Util.javaArrayList(l))).map(_.asScala)) {
inner.get.delegate
} // TODO try to remove map()
}

def mapInnerRoute(f: JFunction[Route, Route], inner: Supplier[Route]): Route = RouteAdapter {
Expand Down Expand Up @@ -155,7 +156,7 @@ abstract class BasicDirectives {
* to the list of rejections potentially coming back from the inner route.
*/
def cancelRejections(classes: JIterable[Class[_]], inner: Supplier[Route]): Route = RouteAdapter {
D.cancelRejections(Util.immutableSeq(classes): _*) { inner.get.delegate }
D.cancelRejections(convertIterable[Class[_], Class[_]](classes): _*) { inner.get.delegate }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.util.Optional
import java.util.function.Function

import org.apache.pekko
import pekko.japi.Util
import pekko.http.impl.util.Util
import pekko.http.scaladsl.model._
import pekko.http.scaladsl.model.headers.{ ByteRange, HttpChallenge, HttpEncoding }
import pekko.http.javadsl.{ model, server => jserver }
Expand Down Expand Up @@ -335,7 +335,9 @@ final case class TransformationRejection(transform: immutable.Seq[Rejection] =>
override def apply(t: Iterable[jserver.Rejection]): Iterable[jserver.Rejection] = {
// explicit collects assignment is because of unidoc failing compilation on .asScala and .asJava here
val transformed: Seq[jserver.Rejection] =
transform(Util.immutableSeq(t).collect { case r: Rejection => r }).collect { case j: jserver.Rejection => j }
transform(Util.convertIterable[jserver.Rejection, jserver.Rejection](t).collect { case r: Rejection =>
r
}).collect { case j: jserver.Rejection => j }
transformed.asJava // TODO "asJavaDeep" and optimise?
}
}
Expand Down

0 comments on commit ca67b05

Please sign in to comment.