Unfortunately there isn't a good way to create and use Filters from Java yet. But you can do what you need pretty easily with Scala.
Create a new file app/filters/AddResponseHeader.scala
containing:
package filters
import play.api.mvc._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
object AddResponseHeader extends Filter {
def apply(f: (RequestHeader) => Future[SimpleResult])(rh: RequestHeader): Future[SimpleResult] = {
val result = f(rh)
result.map(_.withHeaders("FOO" -> "bar"))
}
}
And create a new file app/Global.scala
containing:
import filters.AddResponseHeader
import play.api.mvc.WithFilters
object Global extends WithFilters(AddResponseHeader)
That should apply a new response header to every response.
UPDATE: There is a way to use this in a Global.java
file:
@Override
public <T extends EssentialFilter> Class<T>[] filters() {
return new Class[] {AddResponseHeader.class};
}
And also change the object AddResponseHeader
above to class AddResponseHeader
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…