Skip to content

Commit

Permalink
use convertIterable to wrap java Iterables (#613)
Browse files Browse the repository at this point in the history
* use convertIterable to wrap java Iterables

* Update ConnectionContext.scala

* Update MediaTypes.java

* Update MediaTypes.java
  • Loading branch information
pjfanning authored Oct 1, 2024
1 parent 9a7d503 commit 7412c43
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public static MediaType.Binary applicationBinary(
: org.apache.pekko.http.scaladsl.model.MediaType.NotCompressible$.MODULE$;

scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.applicationBinary(subType, comp, fileEx);
}
Expand All @@ -478,7 +478,7 @@ public static MediaType.Binary applicationBinary(
(org.apache.pekko.http.scaladsl.model.MediaType.Compressibility) compressibility;

scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.applicationBinary(subType, comp, fileEx);
}
Expand All @@ -489,7 +489,7 @@ public static MediaType.WithFixedCharset applicationWithFixedCharset(
(org.apache.pekko.http.scaladsl.model.HttpCharset) charset;

scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.applicationWithFixedCharset(
subType, cs, fileEx);
Expand All @@ -498,7 +498,7 @@ public static MediaType.WithFixedCharset applicationWithFixedCharset(
public static MediaType.WithOpenCharset applicationWithOpenCharset(
String subType, String... fileExtensions) {
scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.applicationWithOpenCharset(
subType, fileEx);
Expand All @@ -512,7 +512,7 @@ public static MediaType.Binary audio(
: org.apache.pekko.http.scaladsl.model.MediaType.NotCompressible$.MODULE$;

scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.audio(subType, comp, fileEx);
}
Expand All @@ -523,7 +523,7 @@ public static MediaType.Binary audio(
(org.apache.pekko.http.scaladsl.model.MediaType.Compressibility) compressibility;

scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.audio(subType, comp, fileEx);
}
Expand All @@ -536,7 +536,7 @@ public static MediaType.Binary image(
: org.apache.pekko.http.scaladsl.model.MediaType.NotCompressible$.MODULE$;

scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.image(subType, comp, fileEx);
}
Expand All @@ -547,7 +547,7 @@ public static MediaType.Binary image(
(org.apache.pekko.http.scaladsl.model.MediaType.Compressibility) compressibility;

scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.image(subType, comp, fileEx);
}
Expand All @@ -560,7 +560,7 @@ public static MediaType.Binary message(
: org.apache.pekko.http.scaladsl.model.MediaType.NotCompressible$.MODULE$;

scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.message(subType, comp, fileEx);
}
Expand All @@ -571,14 +571,14 @@ public static MediaType.Binary message(
(org.apache.pekko.http.scaladsl.model.MediaType.Compressibility) compressibility;

scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.message(subType, comp, fileEx);
}

public static MediaType.WithOpenCharset text(String subType, String... fileExtensions) {
scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.text(subType, fileEx);
}
Expand All @@ -591,7 +591,7 @@ public static MediaType.Binary video(
: org.apache.pekko.http.scaladsl.model.MediaType.NotCompressible$.MODULE$;

scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.video(subType, comp, fileEx);
}
Expand All @@ -602,7 +602,7 @@ public static MediaType.Binary video(
(org.apache.pekko.http.scaladsl.model.MediaType.Compressibility) compressibility;

scala.collection.immutable.Seq<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions));
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions));

return org.apache.pekko.http.scaladsl.model.MediaType.video(subType, comp, fileEx);
}
Expand Down Expand Up @@ -638,8 +638,7 @@ public static MediaType.Binary customBinary(
: org.apache.pekko.http.scaladsl.model.MediaType.NotCompressible$.MODULE$;

scala.collection.immutable.List<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions))
.toList();
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions)).toList();

scala.collection.immutable.Map<String, String> p = Util.convertMapToScala(params);

Expand All @@ -658,8 +657,7 @@ public static MediaType.Binary customBinary(
(org.apache.pekko.http.scaladsl.model.MediaType.Compressibility) compressibility;

scala.collection.immutable.List<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions))
.toList();
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions)).toList();

scala.collection.immutable.Map<String, String> p = Util.convertMapToScala(params);

Expand All @@ -678,8 +676,7 @@ public static MediaType.WithFixedCharset customWithFixedCharset(
(org.apache.pekko.http.scaladsl.model.HttpCharset) charset;

scala.collection.immutable.List<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions))
.toList();
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions)).toList();

scala.collection.immutable.Map<String, String> p = Util.convertMapToScala(params);

Expand All @@ -694,8 +691,7 @@ public static MediaType.WithOpenCharset customWithOpenCharset(
boolean allowArbitrarySubtypes,
String... fileExtensions) {
scala.collection.immutable.List<String> fileEx =
org.apache.pekko.japi.Util.<String>immutableSeq(java.util.Arrays.asList(fileExtensions))
.toList();
Util.<String, String>convertIterable(java.util.Arrays.asList(fileExtensions)).toList();

scala.collection.immutable.Map<String, String> p = Util.convertMapToScala(params);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import java.util.{ Collection => JCollection, Optional }

import org.apache.pekko
import pekko.annotation.{ ApiMayChange, DoNotInherit }
import pekko.http.impl.util.Util
import pekko.http.scaladsl
import pekko.japi.Util
import pekko.stream.TLSClientAuth
import pekko.util.OptionConverters._
import com.typesafe.sslconfig.pekko.PekkoSSLConfig
Expand Down Expand Up @@ -80,8 +80,8 @@ object ConnectionContext {
scaladsl.ConnectionContext.https(
sslContext,
sslConfig.toScala,
enabledCipherSuites.toScala.map(Util.immutableSeq(_)),
enabledProtocols.toScala.map(Util.immutableSeq(_)),
enabledCipherSuites.toScala.map(Util.convertIterable[String, String](_)),
enabledProtocols.toScala.map(Util.convertIterable[String, String](_)),
clientAuth.toScala,
sslParameters.toScala)

Expand All @@ -97,8 +97,8 @@ object ConnectionContext {
scaladsl.ConnectionContext.https(
sslContext,
None,
enabledCipherSuites.toScala.map(Util.immutableSeq(_)),
enabledProtocols.toScala.map(Util.immutableSeq(_)),
enabledCipherSuites.toScala.map(Util.convertIterable[String, String](_)),
enabledProtocols.toScala.map(Util.convertIterable[String, String](_)),
clientAuth.toScala,
sslParameters.toScala)

Expand Down
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 7412c43

Please sign in to comment.