Search in sources :

Example 26 with ObjectOutputStream

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 27 with ObjectOutputStream

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);
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) WsContextListener(org.apache.tomcat.websocket.server.WsContextListener) ByteArrayOutputStream(org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 28 with ObjectOutputStream

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");
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Session(org.apache.catalina.Session)

Example 29 with ObjectOutputStream

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 30 with ObjectOutputStream

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"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) LruCache(org.apache.catalina.filters.CsrfPreventionFilter.LruCache) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

ObjectOutputStream (java.io.ObjectOutputStream)958 ByteArrayOutputStream (java.io.ByteArrayOutputStream)657 ObjectInputStream (java.io.ObjectInputStream)386 ByteArrayInputStream (java.io.ByteArrayInputStream)332 IOException (java.io.IOException)312 FileOutputStream (java.io.FileOutputStream)132 Test (org.junit.Test)130 File (java.io.File)75 BufferedOutputStream (java.io.BufferedOutputStream)46 ObjectOutput (java.io.ObjectOutput)35 OutputStream (java.io.OutputStream)35 HashMap (java.util.HashMap)35 FileInputStream (java.io.FileInputStream)24 ArrayList (java.util.ArrayList)24 InputStream (java.io.InputStream)22 FileNotFoundException (java.io.FileNotFoundException)20 Serializable (java.io.Serializable)15 Test (org.testng.annotations.Test)15 NotSerializableException (java.io.NotSerializableException)14 Map (java.util.Map)13