use of java.io.ObjectOutputStream in project tomcat by apache.
the class TestStandardSession method serializeThenDeserialize.
private StandardSession serializeThenDeserialize(StandardSession source) throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
source.writeObjectData(oos);
StandardSession dest = new StandardSession(TEST_MANAGER);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
dest.readObjectData(ois);
return dest;
}
use of java.io.ObjectOutputStream in project tomcat by apache.
the class TestSSLHostConfigIntegration method testSslHostConfigIsSerializable.
@Test
public void testSslHostConfigIsSerializable() throws Exception {
TesterSupport.configureClientSsl();
Tomcat tomcat = getTomcatInstance();
File appDir = new File(getBuildDirectory(), "webapps/examples");
org.apache.catalina.Context ctxt = tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());
ctxt.addApplicationListener(WsContextListener.class.getName());
TesterSupport.initSsl(tomcat);
tomcat.start();
SSLHostConfig[] sslHostConfigs = tomcat.getConnector().getProtocolHandler().findSslHostConfigs();
boolean written = false;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
for (SSLHostConfig sslHostConfig : sslHostConfigs) {
oos.writeObject(sslHostConfig);
written = true;
}
}
Assert.assertTrue(written);
}
use of java.io.ObjectOutputStream in project tomcat by apache.
the class StandardManager method doUnload.
/**
* Save any currently active sessions in the appropriate persistence
* mechanism, if any. If persistence is not supported, this method
* returns without doing anything.
*
* @exception IOException if an input/output error occurs
*/
protected void doUnload() throws IOException {
if (log.isDebugEnabled())
log.debug(sm.getString("standardManager.unloading.debug"));
if (sessions.isEmpty()) {
log.debug(sm.getString("standardManager.unloading.nosessions"));
// nothing to do
return;
}
// Open an output stream to the specified pathname, if any
File file = file();
if (file == null) {
return;
}
if (log.isDebugEnabled()) {
log.debug(sm.getString("standardManager.unloading", pathname));
}
// Keep a note of sessions that are expired
ArrayList<StandardSession> list = new ArrayList<>();
try (FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
synchronized (sessions) {
if (log.isDebugEnabled()) {
log.debug("Unloading " + sessions.size() + " sessions");
}
// Write the number of active sessions, followed by the details
oos.writeObject(Integer.valueOf(sessions.size()));
Iterator<Session> elements = sessions.values().iterator();
while (elements.hasNext()) {
StandardSession session = (StandardSession) elements.next();
list.add(session);
session.passivate();
session.writeObjectData(oos);
}
}
}
// Expire all the sessions we just wrote
if (log.isDebugEnabled()) {
log.debug("Expiring " + list.size() + " persisted sessions");
}
Iterator<StandardSession> expires = list.iterator();
while (expires.hasNext()) {
StandardSession session = expires.next();
try {
session.expire(false);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
} finally {
session.recycle();
}
}
if (log.isDebugEnabled()) {
log.debug("Unloading complete");
}
}
use of java.io.ObjectOutputStream in project tomcat by apache.
the class TestGenericPrincipal method serializeAndDeserialize.
private GenericPrincipal serializeAndDeserialize(GenericPrincipal gpIn) throws IOException, ClassNotFoundException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(gpIn);
byte[] data = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bis);
return (GenericPrincipal) ois.readObject();
}
use of java.io.ObjectOutputStream in project tomcat by apache.
the class TestCsrfPreventionFilter method testLruCacheSerializable.
@Test
public void testLruCacheSerializable() throws Exception {
LruCache<String> cache = new LruCache<>(5);
cache.add("key1");
cache.add("key2");
cache.add("key3");
cache.add("key4");
cache.add("key5");
cache.add("key6");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(cache);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
@SuppressWarnings("unchecked") LruCache<String> cache2 = (LruCache<String>) ois.readObject();
cache2.add("key7");
assertFalse(cache2.contains("key1"));
assertFalse(cache2.contains("key2"));
assertTrue(cache2.contains("key3"));
assertTrue(cache2.contains("key4"));
assertTrue(cache2.contains("key5"));
assertTrue(cache2.contains("key6"));
assertTrue(cache2.contains("key7"));
}
Aggregations