Last active
January 9, 2016 21:05
-
-
Save tinusn/38c4c110f7cd1e1ec63f to your computer and use it in GitHub Desktop.
Play Framework 2.3 - Java - CORS
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
package controllers; | |
import play.*; | |
import play.mvc.*; | |
public class Application extends Controller { | |
/* | |
* Define any extra CORS headers needed for option requests (see http://enable-cors.org/server.html for more info) | |
*/ | |
public static Result preflight(String all) { | |
response().setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS"); | |
response().setHeader("Access-Control-Allow-Headers", "Origin, Content-Type, Accept"); | |
return ok(); | |
} | |
} |
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
A solution that enables CORS on play framework 2.3 using Java, using a mix of solutions from stack overflow: | |
https://stackoverflow.com/questions/25152277/play-framework-2-3-cors-headers | |
https://stackoverflow.com/questions/22144788/enable-cors-in-java-play-framework-2-2-x | |
But with the added effect that requests that throws exceptions are caught as well (the scala filters solution proposed above does not catch those requests - see https://github.com/playframework/playframework/issues/2429) |
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
import static play.core.j.JavaResults.BadRequest; | |
import static play.core.j.JavaResults.InternalServerError; | |
import static play.core.j.JavaResults.NotFound; | |
import java.util.ArrayList; | |
import java.util.List; | |
import play.GlobalSettings; | |
import play.api.mvc.Results.Status; | |
import play.libs.F.Promise; | |
import play.libs.Scala; | |
import play.mvc.Action; | |
import play.mvc.Http; | |
import play.mvc.Result; | |
import scala.Tuple2; | |
import scala.collection.Seq; | |
public class Global extends GlobalSettings { | |
private class ActionWrapper extends Action.Simple { | |
public ActionWrapper(Action<?> action) { | |
this.delegate = action; | |
} | |
@Override | |
public Promise<Result> call(Http.Context ctx) throws java.lang.Throwable { | |
Promise<Result> result = this.delegate.call(ctx); | |
Http.Response response = ctx.response(); | |
response.setHeader("Access-Control-Allow-Origin", "*"); | |
return result; | |
} | |
} | |
/* | |
* Adds the required CORS header "Access-Control-Allow-Origin" to successfull requests | |
*/ | |
@Override | |
public Action<?> onRequest(Http.Request request, java.lang.reflect.Method actionMethod) { | |
return new ActionWrapper(super.onRequest(request, actionMethod)); | |
} | |
private static class CORSResult implements Result { | |
final private play.api.mvc.Result wrappedResult; | |
public CORSResult(Status status) { | |
List<Tuple2<String, String>> list = new ArrayList<Tuple2<String, String>>(); | |
Tuple2<String, String> t = new Tuple2<String, String>("Access-Control-Allow-Origin","*"); | |
list.add(t); | |
Seq<Tuple2<String, String>> seq = Scala.toSeq(list); | |
wrappedResult = status.withHeaders(seq); | |
} | |
public play.api.mvc.Result toScala() { | |
return this.wrappedResult; | |
} | |
} | |
/* | |
* Adds the required CORS header "Access-Control-Allow-Origin" to bad requests | |
*/ | |
@Override | |
public Promise<Result> onBadRequest(Http.RequestHeader request, String error) { | |
return Promise.<Result>pure(new CORSResult(BadRequest())); | |
} | |
/* | |
* Adds the required CORS header "Access-Control-Allow-Origin" to requests that causes an exception | |
*/ | |
@Override | |
public Promise<Result> onError(Http.RequestHeader request, Throwable t) { | |
return Promise.<Result>pure(new CORSResult(InternalServerError())); | |
} | |
/* | |
* Adds the required CORS header "Access-Control-Allow-Origin" when a route was not found | |
*/ | |
@Override | |
public Promise<Result> onHandlerNotFound(Http.RequestHeader request) { | |
return Promise.<Result>pure(new CORSResult(NotFound())); | |
} | |
} |
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
# A Catch all options route | |
OPTIONS /*all @controllers.Application.preflight(all) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
u may need to cater for forbidden or unauthorized error too..