403510 HttpSession maxInactiveInterval is not serialized in HashSession

This commit is contained in:
Jan Bartel 2013-03-21 12:22:25 +11:00 committed by Joakim Erdfelt
parent 75ba10a452
commit 9ca2923a1f
3 changed files with 86 additions and 22 deletions

View File

@ -19,12 +19,12 @@
package org.eclipse.jetty.server.session;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
@ -589,41 +589,60 @@ public class HashSessionManager extends AbstractSessionManager
for (HashedSession session : _sessions.values())
session.save(reactivate);
}
/* ------------------------------------------------------------ */
public HashedSession restoreSession (InputStream is, HashedSession session) throws Exception
{
/*
* Take care of this class's fields first by calling
* defaultReadObject
*/
DataInputStream in = new DataInputStream(is);
String clusterId = in.readUTF();
in.readUTF(); // nodeId
long created = in.readLong();
long accessed = in.readLong();
int requests = in.readInt();
DataInputStream di = new DataInputStream(is);
String clusterId = di.readUTF();
di.readUTF(); // nodeId
long created = di.readLong();
long accessed = di.readLong();
int requests = di.readInt();
if (session == null)
session = (HashedSession)newSession(created, accessed, clusterId);
session.setRequests(requests);
int size = in.readInt();
int size = di.readInt();
restoreSessionAttributes(di, size, session);
try
{
int maxIdle = di.readInt();
session.setMaxInactiveInterval(maxIdle);
}
catch (EOFException e)
{
LOG.debug("No maxInactiveInterval persisted for session "+clusterId);
LOG.ignore(e);
}
return session;
}
private void restoreSessionAttributes (InputStream is, int size, HashedSession session)
throws Exception
{
if (size>0)
{
ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(in);
ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is);
for (int i=0; i<size;i++)
{
String key = ois.readUTF();
Object value = ois.readObject();
session.setAttribute(key,value);
}
ois.close();
}
else
in.close();
return session;
}
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */

View File

@ -182,7 +182,6 @@ public class HashedSession extends AbstractSession
*/
//out.writeBoolean(_invalid);
//out.writeBoolean(_doInvalidate);
//out.writeLong(_maxIdleMs);
//out.writeBoolean( _newSession);
out.writeInt(getRequests());
out.writeInt(getAttributes());
@ -194,7 +193,8 @@ public class HashedSession extends AbstractSession
oos.writeUTF(key);
oos.writeObject(doGet(key));
}
oos.close();
out.writeInt(getMaxInactiveInterval());
}
/* ------------------------------------------------------------ */

View File

@ -22,7 +22,7 @@ import java.io.File;
import junit.framework.Assert;
import org.eclipse.jetty.server.SessionManager;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.StdErrLog;
@ -71,7 +71,7 @@ public class HashSessionManagerTest
}
@Test
@Test
public void testValidSessionIdRemoval() throws Exception
{
final HashSessionManager manager = new HashSessionManager();
@ -90,4 +90,49 @@ public class HashSessionManagerTest
Assert.assertTrue("File shouldn't exist!", !new File(testDir,"validFile.session").exists());
}
@Test
public void testHashSession() throws Exception
{
File testDir = MavenTestingUtils.getTargetTestingDir("saved");
testDir.mkdirs();
HashSessionManager manager = new HashSessionManager();
manager.setStoreDirectory(testDir);
manager.setMaxInactiveInterval(5);
Assert.assertTrue(testDir.exists());
Assert.assertTrue(testDir.canWrite());
HashSessionIdManager idManager = new HashSessionIdManager();
idManager.setWorkerName("foo");
manager.setSessionIdManager(idManager);
idManager.start();
manager.start();
HashedSession session = (HashedSession)manager.newHttpSession(new Request(null, null));
String sessionId = session.getId();
session.setAttribute("one", new Integer(1));
session.setAttribute("two", new Integer(2));
//stop will persist sessions
idManager.stop();
manager.setMaxInactiveInterval(30); //change max inactive interval for *new* sessions
manager.stop();
Assert.assertTrue("File should exist!", new File(testDir, session.getId()).exists());
//start will restore sessions
idManager.start();
manager.start();
HashedSession restoredSession = (HashedSession)manager.getSession(sessionId);
Assert.assertNotNull(restoredSession);
Object o = restoredSession.getAttribute("one");
Assert.assertNotNull(o);
Assert.assertEquals(1, ((Integer)o).intValue());
Assert.assertEquals(5, restoredSession.getMaxInactiveInterval());
}
}