Skip to content

Instantly share code, notes, and snippets.

@joergviola
Created October 20, 2012 13:58
Show Gist options
  • Save joergviola/3923363 to your computer and use it in GitHub Desktop.
Save joergviola/3923363 to your computer and use it in GitHub Desktop.
Adding headers to Play Java async results
public static Result someAction() {
Promise<Result> promise = Akka.asPromise(....);
Promise<Result> modProm = applyHeader(promise);
return async(modProm);
}
private static Promise<Result> applyHeader(Promise<Result> promise) {
return promise.map(new Function<Result, Result>() {
@Override
public Result apply(Result result) throws Throwable {
if (result.getWrappedResult() instanceof SimpleResult) {
SimpleResult pr = (SimpleResult) result.getWrappedResult();
Tuple2<String, String> ac = new Tuple2<String, String>(
"Access-Control-Allow-Origin", "*");
ArrayList<Tuple2<String, String>> list = new ArrayList<Tuple2<String, String>>();
list.add(ac);
scala.collection.immutable.List<Tuple2<String, String>> headers =
JavaConversions.asBuffer(list).toList();
final PlainResult withHeaders =
pr.withHeaders(headers).as("text/json");
return new Result() {
@Override
public play.api.mvc.Result getWrappedResult() {
return withHeaders;
}
};
} else
return result;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment