Skip to content

Commit

Permalink
Consistent API to get headers from play.mvc.Http.RequestHeader (#7464)
Browse files Browse the repository at this point in the history
* Add new Http.Headers to have a consistent API

This is both consistent with the Scala play.api.mvc.Headers (part of it
since this Java version is not as complete as the Scala one) and also
with the headers APIs in play-ws.

* Add asJava to play.api.mvc.RequestHeader

* RequestBuilder API consistent with RequestHeader

* Headers.hasHeader -> Headers.contains

* Remove header getters from RequestBuilder
  • Loading branch information
marcospereira authored and gmethvin committed Jun 22, 2017
1 parent 9903224 commit fdbe643
Show file tree
Hide file tree
Showing 18 changed files with 395 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ private CompletionStage<Result> handleTokenError(Http.Context ctx, RequestHeader
}

CSRFErrorHandler handler = configurator.apply(this.configuration);
return handler.handle(new play.core.j.RequestHeaderImpl(request), msg);
return handler.handle(request.asJava(), msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ case class CSRFConfig(

import java.{ util => ju }

import play.core.j.{ RequestHeaderImpl => JRequestHeaderImpl }
import play.mvc.Http.{ RequestHeader => JRequestHeader }

import scala.compat.java8.FunctionConverters._
Expand All @@ -69,14 +68,14 @@ case class CSRFConfig(
def withSecureCookie(isSecure: Boolean) = copy(secureCookie = isSecure)
def withHttpOnlyCookie(isHttpOnly: Boolean) = copy(httpOnlyCookie = isHttpOnly)
def withCreateIfNotFound(pred: ju.function.Predicate[JRequestHeader]) =
copy(createIfNotFound = pred.asScala.compose(new JRequestHeaderImpl(_)))
copy(createIfNotFound = pred.asScala.compose(_.asJava))
def withPostBodyBuffer(bufsize: Long) = copy(postBodyBuffer = bufsize)
def withSignTokens(signTokens: Boolean) = copy(signTokens = signTokens)
def withMethods(checkMethod: ju.function.Predicate[String]) = copy(checkMethod = checkMethod.asScala)
def withContentTypes(checkContentType: ju.function.Predicate[Optional[String]]) =
copy(checkContentType = checkContentType.asScala.compose(_.asJava))
def withShouldProtect(shouldProtect: ju.function.Predicate[JRequestHeader]) =
copy(shouldProtect = shouldProtect.asScala.compose(new JRequestHeaderImpl(_)))
copy(shouldProtect = shouldProtect.asScala.compose(_.asJava))
def withBypassCorsTrustedOrigins(bypass: Boolean) = copy(bypassCorsTrustedOrigins = bypass)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ case class GzipFilterConfig(
def withShouldGzip(shouldGzip: (RequestHeader, Result) => Boolean): GzipFilterConfig = copy(shouldGzip = shouldGzip)

def withShouldGzip(shouldGzip: BiFunction[play.mvc.Http.RequestHeader, play.mvc.Result, Boolean]): GzipFilterConfig =
withShouldGzip((req: RequestHeader, res: Result) => shouldGzip.asScala(new j.RequestHeaderImpl(req), res.asJava))
withShouldGzip((req: RequestHeader, res: Result) => shouldGzip.asScala(req.asJava, res.asJava))

def withChunkedThreshold(threshold: Int): GzipFilterConfig = copy(chunkedThreshold = threshold)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import javax.inject.Inject;

import play.api.mvc.Handler;
import play.core.j.RequestHeaderImpl;
import play.mvc.Http.RequestHeader;
import scala.Tuple2;

Expand All @@ -22,6 +21,6 @@ public DefaultHttpRequestHandler(play.api.http.JavaCompatibleHttpRequestHandler
@Override
public HandlerForRequest handlerForRequest(RequestHeader request) {
Tuple2<play.api.mvc.RequestHeader, Handler> result = underlying.handlerForRequest(request.asScala());
return new HandlerForRequest(new RequestHeaderImpl(result._1()), result._2());
return new HandlerForRequest(result._1().asJava(), result._2());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import akka.util.ByteString;
import play.api.mvc.Handler;
import play.core.Execution;
import play.core.j.RequestHeaderImpl;
import play.libs.streams.Accumulator;
import play.mvc.Http.RequestHeader;
import scala.runtime.AbstractFunction1;
Expand Down Expand Up @@ -38,7 +37,7 @@ public Accumulator<ByteString, Result> apply(RequestHeader requestHeader) {

@Override
public play.api.libs.streams.Accumulator<ByteString, play.api.mvc.Result> apply(play.api.mvc.RequestHeader rh) {
return apply(new RequestHeaderImpl(rh))
return apply(rh.asJava())
.map(Result::asScala, Execution.trampoline())
.asScala();
}
Expand Down
3 changes: 1 addition & 2 deletions framework/src/play/src/main/java/play/mvc/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import akka.stream.Materializer;
import play.core.j.AbstractFilter;
import play.core.j.RequestHeaderImpl;
import play.mvc.Http.RequestHeader;
import scala.Function1;
import scala.compat.java8.FutureConverters;
Expand Down Expand Up @@ -39,7 +38,7 @@ public Future<play.api.mvc.Result> apply(
return FutureConverters.toScala(
Filter.this.apply(
(rh) -> FutureConverters.toJava(next.apply(rh.asScala())).thenApply(play.api.mvc.Result::asJava),
new RequestHeaderImpl(requestHeader)
requestHeader.asJava()
).thenApply(Result::asScala)
);
}
Expand Down
Loading

0 comments on commit fdbe643

Please sign in to comment.