本文整理汇总了Java中play.mvc.Http.Context类的典型用法代码示例。如果您正苦于以下问题:Java Context类的具体用法?Java Context怎么用?Java Context使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Context类属于play.mvc.Http包,在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getUsername
import play.mvc.Http.Context; //导入依赖的package包/类
@Override
public String getUsername(Context ctx) {
String[] authTokenHeaderValues = ctx.request().headers().get(AuthUtils.AUTH_HEADER_KEY);
if ((authTokenHeaderValues != null) && (authTokenHeaderValues.length == 1) && (authTokenHeaderValues[0] != null)) {
String authHeader = authTokenHeaderValues[0];
try {
JWTClaimsSet claimSet = (JWTClaimsSet) authenticator.decodeToken(authHeader);
if (new DateTime(claimSet.getExpirationTime()).isAfter(DateTime.now())) {
return claimSet.getSubject();
}
} catch (ParseException | JOSEException e) {
Logger.error("Erro na validação do token: " + e.getMessage());
}
}
return null;
}
示例2: getVerifyEmailMailingBody
import play.mvc.Http.Context; //导入依赖的package包/类
@Override
protected Body getVerifyEmailMailingBody(final String token,
final MyUsernamePasswordAuthUser user, final Context ctx) {
final boolean isSecure = getConfiguration().getBoolean(
SETTING_KEY_VERIFICATION_LINK_SECURE);
final String url = routes.Signup.verify(token).absoluteURL(
ctx.request(), isSecure);
final Lang lang = Lang.preferred(ctx.request().acceptLanguages());
final String langCode = lang.code();
final String html = getEmailTemplate(
"views.html.account.signup.email.verify_email", langCode, url,
token, user.getName(), user.getEmail());
final String text = getEmailTemplate(
"views.txt.account.signup.email.verify_email", langCode, url,
token, user.getName(), user.getEmail());
return new Body(text, html);
}
示例3: getPasswordResetMailingBody
import play.mvc.Http.Context; //导入依赖的package包/类
protected Body getPasswordResetMailingBody(final String token,
final User user, final Context ctx) {
final boolean isSecure = getConfiguration().getBoolean(
SETTING_KEY_PASSWORD_RESET_LINK_SECURE);
final String url = routes.Signup.resetPassword(token).absoluteURL(
ctx.request(), isSecure);
final Lang lang = Lang.preferred(ctx.request().acceptLanguages());
final String langCode = lang.code();
final String html = getEmailTemplate(
"views.html.account.email.password_reset", langCode, url,
token, user.name, user.email);
final String text = getEmailTemplate(
"views.txt.account.email.password_reset", langCode, url, token,
user.name, user.email);
return new Body(text, html);
}
示例4: getVerifyEmailMailingBodyAfterSignup
import play.mvc.Http.Context; //导入依赖的package包/类
protected Body getVerifyEmailMailingBodyAfterSignup(final String token,
final User user, final Context ctx) {
final boolean isSecure = getConfiguration().getBoolean(
SETTING_KEY_VERIFICATION_LINK_SECURE);
final String url = routes.Signup.verify(token).absoluteURL(
ctx.request(), isSecure);
final Lang lang = Lang.preferred(ctx.request().acceptLanguages());
final String langCode = lang.code();
final String html = getEmailTemplate(
"views.html.account.email.verify_email", langCode, url, token,
user.name, user.email);
final String text = getEmailTemplate(
"views.txt.account.email.verify_email", langCode, url, token,
user.name, user.email);
return new Body(text, html);
}
示例5: getUsername
import play.mvc.Http.Context; //导入依赖的package包/类
@Override
public String getUsername(Context ctx) {
// see if user is logged in
if (session("username") == null)
return null;
// see if the session is expired
String previousTick = session("userTime");
if (previousTick != null && !previousTick.equals("")) {
long previousT = Long.valueOf(previousTick);
long currentT = new Date().getTime();
long timeout = Long.valueOf(Play.application().configuration().getString("sessionTimeout")) * 1000 * 60;
if ((currentT - previousT) > timeout) {
// session expired
session().clear();
return null;
}
}
// update time in session
String tickString = Long.toString(new Date().getTime());
session("userTime", tickString);
return session("username");
}
示例6: createAction
import play.mvc.Http.Context; //导入依赖的package包/类
@Override
public Action<Void> createAction(Request request, Method actionMethod) {
return new Action.Simple() {
@Override
public Promise<Result> call(Context ctx) throws Throwable {
// Inject the required services into the context
injectCommonServicesIncontext(ctx);
final Language language = new Language(request.getQueryString("lang"));
if (messagesPlugin.isLanguageValid(language.getCode())) {
Logger.debug("change language to: " + language.getCode());
ctx.changeLang(language.getCode());
// Update the CAS language cookie which is relying on Spring
// framework (not really solid yet works)
Utilities.setSsoLanguage(ctx, language.getCode());
}
return delegate.call(ctx);
}
};
}
示例7: getUsername
import play.mvc.Http.Context; //导入依赖的package包/类
public String getUsername(final Context ctx, String role) {
String username = getUsernameOrIP(ctx, true);
User u = User.findByEmail(username);
if (u == null) {
// Allow access by IP address to ROLE_VIEW_JC things. username
// may be null if not from a valid IP, which will deny access.
if (role.equals(UserRole.ROLE_VIEW_JC)) {
return username;
}
} else if (u.hasRole(role) &&
(u.organization == null || u.organization.equals(OrgConfig.get().org))) {
// Allow access if this user belongs to this organization or is a
// multi-domain admin (null organization). Also, the user must
// have the required role.
return username;
}
return null;
}
示例8: getUsernameOrIP
import play.mvc.Http.Context; //导入依赖的package包/类
public String getUsernameOrIP(final Context ctx, boolean allow_ip) {
Logger.debug("Authenticator::getUsername " + ctx + ", " + allow_ip);
final AuthUser u = mAuth.getUser(ctx.session());
if (u != null) {
User the_user = User.findByAuthUserIdentity(u);
if (the_user != null) {
// If a user is logged in already, check the session timeout.
// If there is no timeout, or we can't parse it, or it's too old,
// don't count the user as being logged in.
Session sess = ctx.session();
if (sess.get("timeout") != null) {
try
{
long timeout = Long.parseLong(sess.get("timeout"));
if (System.currentTimeMillis() - timeout < 1000 * 60 * 30) {
sess.put("timeout", "" + System.currentTimeMillis());
return the_user.email;
}
}
catch (NumberFormatException e) {
}
}
}
}
// If we don't have a logged-in user, try going by IP address.
if (allow_ip && Organization.getByHost() != null) {
String sql = "select ip from allowed_ips where ip like :ip and organization_id=:org_id";
SqlQuery sqlQuery = Ebean.createSqlQuery(sql);
String address = Application.getRemoteIp();
sqlQuery.setParameter("ip", address);
sqlQuery.setParameter("org_id", Organization.getByHost().id);
// execute the query returning a List of MapBean objects
SqlRow result = sqlQuery.findUnique();
if (result != null) {
return address;
}
}
return null;
}
示例9: restrict
import play.mvc.Http.Context; //导入依赖的package包/类
@Override
public boolean restrict(List<String[]> deadBoltRoles) {
try {
if (log.isDebugEnabled()) {
log.debug("RESTRICT with handler [" + get() + "] timeout [" + DEFAULT_TIMEOUT + "] for roles " + Utilities.toString(deadBoltRoles));
}
Optional<Subject> subjectOption = getSubject(Http.Context.current(), get()).get(DEFAULT_TIMEOUT);
if (!subjectOption.isPresent()) {
if (log.isDebugEnabled()) {
log.debug("RESTRICT FALSE since no subject found");
}
return false;
}
Subject subject = subjectOption.get();
if (log.isDebugEnabled()) {
log.debug("RESTRICT Subject = " + subject);
}
return restrict(deadBoltRoles, subject);
} catch (Exception e) {
log.error("Error while checking restriction for " + Utilities.toString(deadBoltRoles), e);
return false;
}
}
示例10: beforeAuthCheck
import play.mvc.Http.Context; //导入依赖的package包/类
@Override
public Promise<Optional<Result>> beforeAuthCheck(final Http.Context ctx) {
String uid = getUserSessionManagerPlugin().getUserSessionId(ctx);
if (log.isDebugEnabled()) {
log.debug("Calling beforeAuthCheck, user in session is " + uid);
}
if (uid == null) {
return Promise.promise(new Function0<Optional<Result>>() {
public Optional<Result> apply() throws Throwable {
return Optional.of(redirectToLoginPage(ctx.request().uri()));
}
});
}
Optional<Result> emptyResult = Optional.empty();
return Promise.promise(() -> emptyResult);
}
示例11: checkPermission
import play.mvc.Http.Context; //导入依赖的package包/类
@Override
public Promise<Boolean> checkPermission(String permissionValue, DeadboltHandler deadboltHandler, Context ctx) {
boolean permissionOk = false;
try {
IUserAccount userAccount = getSecurityService().getCurrentUser();
if (userAccount != null) {
List<? extends Permission> permissions = userAccount.getPermissions();
for (Iterator<? extends Permission> iterator = permissions.iterator(); !permissionOk && iterator.hasNext();) {
Permission permission = iterator.next();
permissionOk = permission.getValue().contains(permissionValue);
}
}
} catch (Exception e) {
log.error("impossible to get the user", e);
}
final boolean permissionOkFinal = permissionOk;
return Promise.promise(() -> permissionOkFinal);
}
示例12: notify
import play.mvc.Http.Context; //导入依赖的package包/类
@Override
public Promise<Result> notify(Context ctx) {
if (log.isDebugEnabled()) {
log.debug("Notification for request " + ctx.request().path());
}
for (String pathPrefix : getRegisteredListeners().keySet()) {
if (ctx.request().path().startsWith(pathPrefix)) {
if (log.isDebugEnabled()) {
log.debug("Notification for request " + ctx.request().path() + " found matching listener " + pathPrefix);
}
return getRegisteredListeners().get(pathPrefix).notifyRequest(ctx);
}
}
if (log.isDebugEnabled()) {
log.debug("No matching listener for " + ctx.request().path());
}
return null;
}
示例13: execute
import play.mvc.Http.Context; //导入依赖的package包/类
@Override
public Promise<Result> execute(String path, Context ctx) {
for (WebCommand webCommand : webCommands) {
if (webCommand.isCompatible(path, ctx)) {
try {
return webCommand.call(path, ctx);
} catch (Exception e) {
log.error("Error while calling the web command", e);
return Promise.promise(() -> Controller.badRequest());
}
}
}
log.info("No compatible command found for path " + path);
if (log.isDebugEnabled()) {
log.debug("No compatible command found for path " + path);
}
return Promise.promise(() -> Controller.badRequest());
}
示例14: getUsername
import play.mvc.Http.Context; //导入依赖的package包/类
@Override
public String getUsername(Context ctx) {
String[] authTokenHeaderValues = ctx
.request()
.headers()
.get(SecurityController.AUTH_TOKEN_HEADER.toLowerCase());
if ((authTokenHeaderValues != null) && (authTokenHeaderValues.length == 1)
&& (authTokenHeaderValues[0] != null)) {
User user = userService.getUserByAuthToken(authTokenHeaderValues[0]);
if (isAuthenticatedUser(user)) {
ctx.args.put("user", user);
return user.getEmail();
}
}
return null;
}
示例15: renderTemplate
import play.mvc.Http.Context; //导入依赖的package包/类
@Test
public void renderTemplate() throws InstantiationException, IllegalAccessException {
Request requestMock = mock(Request.class);
Context.current.set(new Context(1l, mock(RequestHeader.class), requestMock, //
Collections.<String, String>emptyMap(), // sessionData
Collections.<String, String>emptyMap(), // flashData
Collections.<String, Object>emptyMap()));
when(requestMock.username()).thenReturn("nom_de_test");
Html html = views.html.index.render("Your new application is ready.");
assertThat(contentType(html)).isEqualTo("text/html");
assertThat(contentAsString(html)).contains("Your new application is ready.");
assertThat(contentAsString(html)).contains("Bonjour nom_de_test!");
}