Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Commit

Permalink
PLAYP-3
Browse files Browse the repository at this point in the history
make callback method from CallbackController asynchronous
  • Loading branch information
miremond committed May 5, 2014
1 parent 3455457 commit 7e663ec
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import play.libs.F.Function0;
import play.libs.F.Promise;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Results;
Expand All @@ -52,7 +54,7 @@ public class CallbackController extends Controller {
* @return the redirection to the saved request
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Result callback() {
public static Promise<Result> callback() {
// clients group from config
final Clients clientsGroup = Config.getClients();

Expand All @@ -64,45 +66,52 @@ public static Result callback() {
logger.debug("client : {}", client);

// get credentials
Credentials credentials = null;
try {
credentials = client.getCredentials(context);
logger.debug("credentials : {}", credentials);
} catch (final RequiresHttpAction e) {
// requires some specific HTTP action
final int code = context.getResponseStatus();
logger.debug("requires HTTP action : {}", code);
if (code == HttpConstants.UNAUTHORIZED) {
return unauthorized(Config.getErrorPage401()).as(Constants.HTML_CONTENT_TYPE);
} else if (code == HttpConstants.TEMP_REDIRECT) {
return Results.status(HttpConstants.TEMP_REDIRECT);
} else if (code == HttpConstants.OK) {
final String content = context.getResponseContent();
logger.debug("render : {}", content);
return ok(content).as(Constants.HTML_CONTENT_TYPE);
Promise<Result> promise = Promise.promise(new Function0<Result>() {
public Result apply() {
Credentials credentials = null;
try {
credentials = client.getCredentials(context);
logger.debug("credentials : {}", credentials);

} catch (final RequiresHttpAction e) {
// requires some specific HTTP action
final int code = context.getResponseStatus();
logger.debug("requires HTTP action : {}", code);
if (code == HttpConstants.UNAUTHORIZED) {
return unauthorized(Config.getErrorPage401()).as(Constants.HTML_CONTENT_TYPE);
} else if (code == HttpConstants.TEMP_REDIRECT) {
return Results.status(HttpConstants.TEMP_REDIRECT);
} else if (code == HttpConstants.OK) {
final String content = context.getResponseContent();
logger.debug("render : {}", content);
return ok(content).as(Constants.HTML_CONTENT_TYPE);
}
final String message = "Unsupported HTTP action : " + code;
logger.error(message);
throw new TechnicalException(message);
}

// get user profile
final CommonProfile profile = client.getUserProfile(credentials, context);
logger.debug("profile : {}", profile);

// get or create sessionId
final String sessionId = StorageHelper.getOrCreationSessionId(session());

// save user profile only if it's not null
if (profile != null) {
StorageHelper.saveProfile(sessionId, profile);
}

// get requested url
final String requestedUrl = StorageHelper.getRequestedUrl(sessionId, client.getName());

// retrieve saved request and redirect
return redirect(defaultUrl(requestedUrl, Config.getDefaultSuccessUrl()));
}
final String message = "Unsupported HTTP action : " + code;
logger.error(message);
throw new TechnicalException(message);
}

// get user profile
final CommonProfile profile = client.getUserProfile(credentials, context);
logger.debug("profile : {}", profile);

// get or create sessionId
final String sessionId = StorageHelper.getOrCreationSessionId(session());

// save user profile only if it's not null
if (profile != null) {
StorageHelper.saveProfile(sessionId, profile);
}

// get requested url
final String requestedUrl = StorageHelper.getRequestedUrl(sessionId, client.getName());
});

// retrieve saved request and redirect
return redirect(defaultUrl(requestedUrl, Config.getDefaultSuccessUrl()));
return promise;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.concurrent.Callable;

import org.pac4j.core.client.BaseClient;
import org.pac4j.core.client.Client;
Expand All @@ -36,7 +35,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import play.libs.Akka;
import play.libs.F.Function0;
import play.libs.F.Promise;
import play.mvc.Action;
import play.mvc.Http.Context;
Expand Down Expand Up @@ -98,9 +97,9 @@ public Promise<SimpleResult> call(final Context context) throws Throwable {
// get client
final Client<Credentials, UserProfile> client = Config.getClients().findClient(clientName);
logger.debug("client : {}", client);
@SuppressWarnings("deprecation")
Promise<SimpleResult> promiseOfResult = Akka.future(new Callable<SimpleResult>() {
public SimpleResult call() {
Promise<SimpleResult> promise = Promise.promise(new Function0<SimpleResult>() {
@SuppressWarnings("rawtypes")
public SimpleResult apply() {
try {
// and compute redirection url
JavaWebContext webContext = new JavaWebContext(context.request(), context.response(), context
Expand All @@ -123,7 +122,7 @@ public SimpleResult call() {
}
}
});
return promiseOfResult;
return promise;
}

private SimpleResult convertToPromise(RedirectAction action) {
Expand Down

0 comments on commit 7e663ec

Please sign in to comment.