Java 类play.mvc.Scope.Session 实例源码

项目:restcommander    文件:SessionTest.java   
@Test
public void testSessionManipulationMethods() {
    mockRequestAndResponse();
    Session session = Session.restore();
    assertFalse(session.changed);

    session.change();
    assertTrue(session.changed);

    // Reset
    session.changed = false;
    session.put("username", "Alice");
    assertTrue(session.changed);

    session.changed = false;
    session.remove("username");
    assertTrue(session.changed);

    session.changed = false;
    session.clear();
    assertTrue(session.changed);
}
项目:restcommander    文件:SessionTest.java   
@Test
public void testSendOnlyIfChanged() {
    // Mock secret
    Play.secretKey = "0112358";

    Session session;
    setSendOnlyIfChangedConstant(true);
    mockRequestAndResponse();

    // Change nothing in the session
    session = Session.restore();
    session.save();
    assertNull(Response.current().cookies.get(Scope.COOKIE_PREFIX + "_SESSION"));

    mockRequestAndResponse();
    // Change the session
    session = Session.restore();
    session.put("username", "Bob");
    session.save();

    Cookie sessionCookie = Response.current().cookies.get(Scope.COOKIE_PREFIX + "_SESSION");
    assertNotNull(sessionCookie);
    assertTrue(sessionCookie.value.contains("username"));
    assertTrue(sessionCookie.value.contains("Bob"));
}
项目:MRIdb    文件:ClipboardExporter.java   
public static List<File> getExports(Session session) {
    String exports = session.get(Application.EXPORTS);
    List<File> archives = new ArrayList<File>();
    if (exports != null) {
        List<String> exportList = new ArrayList(Arrays.asList(exports.split(",")));
        for (Iterator<String> i = exportList.iterator(); i.hasNext();) {
            String export = i.next();
            File tmpDir = new File(Properties.getDownloads(), export.split(":")[0]);
            if (tmpDir.exists()) {
                archives.add(new File(tmpDir.getParentFile(), String.format("%s.7z", export.split(":")[1])));
            } else {
                i.remove();
            }
        }
        if (exportList.isEmpty()) {
            session.remove(Application.EXPORTS);
        } else {
            session.put(Application.EXPORTS, StringUtils.join(exportList, ","));
        }
    }
    return archives;
}
项目:MyDMAM    文件:Secure.java   
public static HashSet<String> getSessionPrivileges() throws DisconnectedUser {
    String username = connected();
    if (username == null) {
        throw new DisconnectedUser("(No set)");
    }

    @SuppressWarnings("unchecked")
    HashSet<String> privileges = Cache.get("user:" + username + ":privileges", HashSet.class);

    if (privileges != null) {
        Cache.set("user:" + username + ":privileges", privileges, MyDMAM.getPlayBootstrapper().getSessionTTL());
        return privileges;
    }

    Session.current().clear();

    throw new DisconnectedUser(username);
}
项目:MyDMAM    文件:Secure.java   
public static void logout() throws Throwable {
    try {
        String username = connected();
        if (username != null) {
            Cache.delete("user:" + username + ":privileges");
        }
        Session.current().clear();

        UserNG user = MyDMAM.getPlayBootstrapper().getAuth().getByUserKey(username);
        if (user == null) {
            Loggers.Play.info("User " + username + " went tries to sign off.");
        } else {
            String long_name = user.getFullname();
            if (long_name == null) {
                long_name = user.getName();
            }
            Loggers.Play.info(long_name + " went tries to sign off.");
        }

    } catch (Exception e) {
        Loggers.Play.error("Error during sign off: " + getUserSessionInformation());
        throw e;
    }
    flash.success("secure.logout");
    login();
}
项目:playorm    文件:AddLogContextPlugin.java   
private void setupMDC(Session session) {
    if(session.get("sid") == null) {
        session.put("sid", fetchLast()+"");
    }

    String sid = session.get("sid");
    String username = session.get("username");
    MDC.put("sessionid", sid);
    MDC.put("user", ""+username);
}
项目:restcommander    文件:SessionTest.java   
@Test
public void testSendAlways() {
    Session session;
    setSendOnlyIfChangedConstant(false);

    mockRequestAndResponse();

    // Change nothing in the session
    session = Session.restore();
    session.save();
    assertNotNull(Response.current().cookies.get(Scope.COOKIE_PREFIX + "_SESSION"));
}
项目:MRIdb    文件:ClipboardExporter.java   
public ClipboardExporter(Clipboard clipboard, File tmpDir, String password, Session session, String username, boolean preferMultiframe, String niftiMultiframeScript) {
    this.clipboard = clipboard;
    this.tmpDir = tmpDir;
    this.password = password;
    this.session = session;
    this.username = username;
    this.preferMultiframe = preferMultiframe;
    this.niftiMultiframeScript = niftiMultiframeScript;
    addExport();
}
项目:MRIdb    文件:ClipboardExporter.java   
public static File getExport(String name, Session session) {
    String exports = session.get(Application.EXPORTS);
    if (exports != null) {
        for (String export : Arrays.asList(exports.split(","))) {
            if (name.equals(export.split(":")[1])) {
                return new File(Properties.getDownloads(), String.format("%s.7z", name));
            }
        }
    }
    return null;
}
项目:MyDMAM    文件:Secure.java   
/**
 * This method returns the current connected username
 * @return null, if no user
 */
public static String connected() {
    String raw_user = Session.current().get("username");
    if (raw_user == null) {
        return null;
    }
    return Crypto.decryptAES(raw_user);
}
项目:restcommander    文件:FastTags.java   
public static void _authenticityToken(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
    out.println("<input type=\"hidden\" name=\"authenticityToken\" value=\"" + Session.current().getAuthenticityToken() + "\">");
}
项目:MRIdb    文件:Log.java   
@PrePersist
private void prePersist() {
    timestamp = new Date();
    username = Session.current().get("username");
}
项目:MyDMAM    文件:Secure.java   
public static void authenticate(@Required String username, @Required String password, String domainidx, boolean remember) throws Throwable {
    String remote_address = request.remoteAddress;

    if (Validation.hasErrors()) {
        rejectUser();
        return;
    }

    if (AccessControl.validThisIP(remote_address) == false) {
        Loggers.Play.warn("Refuse IP addr for user username: " + username + ", domainidx: " + domainidx + ", remote_address: " + remote_address);
        rejectUser();
        return;
    }

    UserNG authuser = null;

    if (MyDMAM.getPlayBootstrapper().getAuth().isForceSelectDomain()) {
        String domain_name = null;

        try {
            domain_name = MyDMAM.getPlayBootstrapper().getAuth().getDeclaredDomainList().get(Integer.valueOf(domainidx));
        } catch (Exception e) {
        }
        authuser = MyDMAM.getPlayBootstrapper().getAuth().authenticateWithThisDomain(remote_address, username.trim().toLowerCase(), password, domain_name, Lang.getLocale().getLanguage());
    } else {
        authuser = MyDMAM.getPlayBootstrapper().getAuth().authenticate(remote_address, username.trim().toLowerCase(), password, Lang.getLocale().getLanguage());
    }

    if (authuser == null) {
        Loggers.Play.error("Can't login username: " + username + ", domainidx: " + domainidx + ", " + getUserSessionInformation());
        AccessControl.failedAttempt(remote_address, username);
        rejectUser();
    }

    username = authuser.getKey();

    AccessControl.releaseIP(remote_address);

    Session.current().put("username", Crypto.encryptAES(username));

    Cache.set("user:" + username + ":privileges", authuser.getUser_groups_roles_privileges(), MyDMAM.getPlayBootstrapper().getSessionTTL());

    String long_name = authuser.getFullname();
    if (long_name == null) {
        long_name = authuser.getName();
    }

    Loggers.Play.info(long_name + " has a successful authentication, with privileges: " + getSessionPrivileges().toString() + ". User key: " + username);

    redirect("Application.index");
}
项目:MyDMAM    文件:Secure.java   
/**
 * Indicate if a user is currently connected
 * @return true if the user is connected
 */
public static boolean isConnected() {
    return Session.current().contains("username");
}