Skip to content

Instantly share code, notes, and snippets.

View danielreuterwall's full-sized avatar

Daniel Reuterwall danielreuterwall

View GitHub Profile
import java.lang.annotation.*;
import play.mvc.*;
import play.mvc.Http.*;
public class CorsComposition {
/**
* Wraps the annotated action in an <code>CorsAction</code>.
*/
@With(CorsAction.class)
public static class CorsAction extends Action.Simple {
public Result call(Context context) throws Throwable{
Response response = context.response();
response.setHeader("Access-Control-Allow-Origin", *);
//Handle preflight requests
if(context.request().method().equals("OPTIONS")) {
response.setHeader("Access-Control-Allow-Methods", "POST");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
@With(CorsAction.class)
public class CorsController extends Controller {
public static Result cors() {
return ok("Cors enabled action");
}
}
public static class CorsAction extends Action.Simple {
public Result call(Context context) throws Throwable{
Response response = context.response();
response.setHeader("Access-Control-Allow-Origin", *);
response.setHeader("Access-Control-Allow-Headers","X-Requested-With");
return delegate.call(context);
}
}
@danielreuterwall
danielreuterwall / BasicAuth.java
Created April 9, 2013 18:07
BasicAuth.java for basic auth with Play 2.0
package security.basicauth;
import java.io.IOException;
import java.lang.reflect.Method;
import play.Application;
import play.Logger;
import play.GlobalSettings;
import play.Configuration;
import play.mvc.Http.Request;
@danielreuterwall
danielreuterwall / Global.java
Last active December 16, 2015 00:29
Global.java for basic auth with Play 2.0
import java.lang.reflect.Method;
import play.Application;
import play.Logger;
import play.GlobalSettings;
import play.Configuration;
import play.mvc.Http.Request;
import play.mvc.Action;
import security.basicauth.BasicAuth;
@danielreuterwall
danielreuterwall / index.scala
Last active December 15, 2015 18:30
Playtuts welcome message
@(message: String)
@import play.api.Play
@main("Welcome to Play 2.0") {
<h1>The current environment is @Play.current.configuration.getString("environment.name")</h1>
}
@danielreuterwall
danielreuterwall / gist:5284369
Created April 1, 2013 11:11
Action composition to enable Cors from your Play API.
package controllers;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import play.mvc.Action;
import play.mvc.Http.Response;
import play.mvc.With;
@danielreuterwall
danielreuterwall / gist:4995599
Created February 20, 2013 13:38
Dynamics CRM snippet
<h1>Testar</h1>
@danielreuterwall
danielreuterwall / gist:4130675
Created November 22, 2012 11:26
Basic authorization in Play
import java.io.IOException;
import java.lang.reflect.Method;
import play.Application;
import play.Logger;
import play.GlobalSettings;
import play.Configuration;
import play.mvc.Http.Request;
import play.mvc.Action;
import play.mvc.Http;