Created
October 20, 2012 13:58
-
-
Save joergviola/3923363 to your computer and use it in GitHub Desktop.
Adding headers to Play Java async results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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