Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session的void setAttribute(@NonNull String name, @Nullable Object value)方法存储attribute失败 #507

Open
zhangjibiao opened this issue Aug 3, 2023 · 0 comments

Comments

@zhangjibiao
Copy link

发现这个方法只能在req.getValidSession()获得新session对象后在本次request中调用才能存储,一次HTTP请求-响应结束后,后面再获取这个session再存储attribute会存储不了,因为没有序列号到文件上。

attribute存储在mAttributes中,通过writeObject方法序列化到文件中,后面再通过readObject反序列成为对象

    public void writeObject(@NonNull ObjectOutputStream stream) throws IOException {
        stream.writeObject(id);
        stream.writeLong(createdTime);
        stream.writeLong(lastAccessedTime);
        stream.writeInt(maxInactiveInterval);
        stream.writeBoolean(isNew);
        stream.writeBoolean(isValid);
        stream.writeInt(mAttributes.size());
        String keys[] = mAttributes.keySet().toArray(EMPTY_ARRAY);
        for (String key: keys) {
            Object value = mAttributes.get(key);
            if (value != null && value instanceof Serializable) {
                stream.writeObject(key);
                stream.writeObject(value);
            }
        }
    }

writeObject方法仅在replace方法中有调用,
replace方法在add方法中调用

    @Override
    public boolean replace(@NonNull StandardSession session) throws IOException {
        Assert.notNull(session, "The session can not be null.");

        String id = session.getId();
        if (TextUtils.isEmpty(id)) {
            throw new IllegalStateException("The session id can not be empty or null.");
        }

        ObjectOutputStream writer = null;
        try {
            if (!IOUtils.createFolder(mDirectory)) {
                return false;
            }

            File file = new File(mDirectory, id);
            if (!IOUtils.createNewFile(file)) {
                return false;
            }

            writer = new ObjectOutputStream(new FileOutputStream(file));
            session.writeObject(writer);
            return true;
        } catch (IOException e) {
            IOUtils.delFileOrFolder(new File(mDirectory, id));
            throw e;
        } finally {
            IOUtils.closeQuietly(writer);
        }
    }
    @Override
    public void add(@NonNull Session session) throws IOException {
        if (session instanceof StandardSession && session.isNew()) {
            StandardSession standardSession = (StandardSession) session;
            standardSession.setNew(false);
            mStore.replace(standardSession);
        }
    }

需要满足 session.isNew(),isNew属性仅在newSession()方法中被赋值为true(反序列化时可以忽略,不会改变值)。
后被赋值为false,自此这个session的isNew属性一直为false,再也没有机会重新序列化到文件中,也存储不了attribute

newSession()方法仅在getValidSession()时会调用。也就是仅在getValidSession()调用后,才会存储到文件中,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant