Search in sources :

Example 41 with Marshaller

use of org.apache.ignite.marshaller.Marshaller in project ignite by apache.

the class WebSessionSelfTest method testSingleRequest.

/**
 * Tests single request to a server. Checks modification attribute in cache.
 *
 * @param cfg Configuration.
 * @throws Exception If failed.
 */
private void testSingleRequest(String cfg) throws Exception {
    Server srv = null;
    try {
        srv = startServer(TEST_JETTY_PORT, cfg, null, new SessionCreateServlet(keepBinary()));
        URLConnection conn = new URL("http://localhost:" + TEST_JETTY_PORT + "/ignitetest/test").openConnection();
        conn.connect();
        try (BufferedReader rdr = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
            String sesId = rdr.readLine();
            if (!keepBinary()) {
                IgniteCache<String, HttpSession> cache = G.ignite().cache(getCacheName());
                assertNotNull(cache);
                HttpSession ses = cache.get(sesId);
                assertNotNull(ses);
                assertEquals("val1", ses.getAttribute("key1"));
            } else {
                final IgniteCache<String, WebSessionEntity> cache = G.ignite().cache(getCacheName());
                assertNotNull(cache);
                final WebSessionEntity entity = cache.get(sesId);
                assertNotNull(entity);
                final byte[] data = entity.attributes().get("key1");
                assertNotNull(data);
                final Marshaller marshaller = G.ignite().configuration().getMarshaller();
                final String val = marshaller.unmarshal(data, getClass().getClassLoader());
                assertEquals("val1", val);
            }
        }
    } finally {
        stopServer(srv);
    }
}
Also used : Marshaller(org.apache.ignite.marshaller.Marshaller) Server(org.eclipse.jetty.server.Server) HttpSession(javax.servlet.http.HttpSession) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL)

Example 42 with Marshaller

use of org.apache.ignite.marshaller.Marshaller in project ignite by apache.

the class WebSessionSelfTest method testChangeSessionId.

/**
 * Tests session id change.
 *
 * @throws Exception Exception If failed.
 */
public void testChangeSessionId() throws Exception {
    String newWebSesId;
    Server srv = null;
    try {
        srv = startServer(TEST_JETTY_PORT, "/modules/core/src/test/config/websession/example-cache.xml", null, new SessionIdChangeServlet());
        Ignite ignite = G.ignite();
        URLConnection conn = new URL("http://localhost:" + TEST_JETTY_PORT + "/ignitetest/chngsesid").openConnection();
        conn.connect();
        try (BufferedReader rdr = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
            // checks if the old session object is invalidated.
            String oldId = rdr.readLine();
            assertNotNull(oldId);
            // id from genuine session
            String newGenSesId = rdr.readLine();
            assertNotNull(newGenSesId);
            assertFalse(newGenSesId.equals(oldId));
            // id from replicated session
            newWebSesId = rdr.readLine();
            assertNotNull(newWebSesId);
            assertTrue(newGenSesId.equals(newWebSesId));
            if (!keepBinary()) {
                IgniteCache<String, HttpSession> cache = ignite.cache(getCacheName());
                assertNotNull(cache);
                Thread.sleep(1000);
                HttpSession ses = cache.get(newWebSesId);
                assertNotNull(ses);
                assertEquals("val1", ses.getAttribute("key1"));
            } else {
                IgniteCache<String, WebSessionEntity> cache = ignite.cache(getCacheName());
                assertNotNull(cache);
                Thread.sleep(1000);
                WebSessionEntity ses = cache.get(newWebSesId);
                assertNotNull(ses);
                final Marshaller marshaller = ignite.configuration().getMarshaller();
                assertEquals("val1", marshaller.<String>unmarshal(ses.attributes().get("key1"), getClass().getClassLoader()));
            }
        }
        conn = new URL("http://localhost:" + TEST_JETTY_PORT + "/ignitetest/simple").openConnection();
        conn.addRequestProperty("Cookie", "JSESSIONID=" + newWebSesId);
        conn.connect();
        try (BufferedReader rdr = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
            // checks if it can be handled with the subsequent request.
            String sesId = rdr.readLine();
            assertTrue(newWebSesId.equals(sesId));
            String attr = rdr.readLine();
            assertEquals("val1", attr);
            String reqSesValid = rdr.readLine();
            assertEquals("true", reqSesValid);
            assertEquals("invalidated", rdr.readLine());
        }
    } finally {
        stopServer(srv);
    }
}
Also used : Marshaller(org.apache.ignite.marshaller.Marshaller) Server(org.eclipse.jetty.server.Server) HttpSession(javax.servlet.http.HttpSession) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL) Ignite(org.apache.ignite.Ignite)

Example 43 with Marshaller

use of org.apache.ignite.marshaller.Marshaller in project ignite by apache.

the class AbstractDiscoverySelfTest method writeObject.

/**
 * @param node Grid node.
 * @throws IOException If write failed.
 */
private void writeObject(ClusterNode node) throws Exception {
    Marshaller marshaller = getTestResources().getMarshaller();
    OutputStream out = new NullOutputStream();
    try {
        marshaller.marshal(node, out);
    } finally {
        U.close(out, null);
    }
}
Also used : Marshaller(org.apache.ignite.marshaller.Marshaller) OutputStream(java.io.OutputStream)

Example 44 with Marshaller

use of org.apache.ignite.marshaller.Marshaller in project ignite by apache.

the class SocketStreamerUnmarshalVulnerabilityTest method testExploit.

/**
 * @param positive Positive.
 */
private void testExploit(boolean positive) throws Exception {
    try {
        Ignite ignite = startGrid();
        SocketStreamer<Exploit, Integer, String> sockStmr = null;
        try (IgniteDataStreamer<Integer, String> stmr = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
            stmr.allowOverwrite(true);
            stmr.autoFlushFrequency(10);
            sockStmr = new SocketStreamer<>();
            sockStmr.setIgnite(ignite);
            sockStmr.setStreamer(stmr);
            sockStmr.setPort(port);
            sockStmr.setSingleTupleExtractor(new StreamSingleTupleExtractor<Exploit, Integer, String>() {

                @Override
                public Map.Entry<Integer, String> extract(Exploit msg) {
                    return new IgniteBiTuple<>(1, "val");
                }
            });
            sockStmr.start();
            try (Socket sock = new Socket(InetAddress.getLocalHost(), port);
                OutputStream os = new BufferedOutputStream(sock.getOutputStream())) {
                Marshaller marsh = new JdkMarshaller();
                byte[] msg = marsh.marshal(new Exploit());
                os.write(msg.length >>> 24);
                os.write(msg.length >>> 16);
                os.write(msg.length >>> 8);
                os.write(msg.length);
                os.write(msg);
            } catch (IOException | IgniteCheckedException e) {
                throw new IgniteException(e);
            }
            boolean res = GridTestUtils.waitForCondition(new GridAbsPredicate() {

                @Override
                public boolean apply() {
                    return SHARED.get();
                }
            }, 3000L);
            if (positive)
                assertTrue(res);
            else
                assertFalse(res);
        } finally {
            if (sockStmr != null)
                sockStmr.stop();
        }
    } finally {
        stopAllGrids();
    }
}
Also used : JdkMarshaller(org.apache.ignite.marshaller.jdk.JdkMarshaller) Marshaller(org.apache.ignite.marshaller.Marshaller) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) JdkMarshaller(org.apache.ignite.marshaller.jdk.JdkMarshaller) Ignite(org.apache.ignite.Ignite) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 45 with Marshaller

use of org.apache.ignite.marshaller.Marshaller in project ignite by apache.

the class IgniteTestResources method getMarshaller.

/**
 * @return Marshaller.
 * @throws IgniteCheckedException If failed.
 */
@SuppressWarnings("unchecked")
public static synchronized Marshaller getMarshaller() throws IgniteCheckedException {
    String marshallerName = System.getProperty(MARSH_CLASS_NAME, GridTestProperties.getProperty(GridTestProperties.MARSH_CLASS_NAME));
    Marshaller marsh;
    if (marshallerName == null)
        marsh = new BinaryMarshaller();
    else {
        try {
            Class<? extends Marshaller> cls = (Class<? extends Marshaller>) Class.forName(marshallerName);
            marsh = cls.newInstance();
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            throw new IgniteCheckedException("Failed to create test marshaller [marshaller=" + marshallerName + ']', e);
        }
    }
    marsh.setContext(new MarshallerContextTestImpl());
    if (marsh instanceof BinaryMarshaller) {
        BinaryContext ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), new IgniteConfiguration(), new NullLogger());
        IgniteUtils.invoke(BinaryMarshaller.class, marsh, "setBinaryContext", ctx, new IgniteConfiguration());
    }
    return marsh;
}
Also used : BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) Marshaller(org.apache.ignite.marshaller.Marshaller) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) MarshallerContextTestImpl(org.apache.ignite.marshaller.MarshallerContextTestImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) NullLogger(org.apache.ignite.logger.NullLogger) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryContext(org.apache.ignite.internal.binary.BinaryContext)

Aggregations

Marshaller (org.apache.ignite.marshaller.Marshaller)46 Externalizable (java.io.Externalizable)8 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)8 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 HttpURLConnection (java.net.HttpURLConnection)5 URL (java.net.URL)5 URLConnection (java.net.URLConnection)5 ArrayList (java.util.ArrayList)5 HttpSession (javax.servlet.http.HttpSession)5 IgniteException (org.apache.ignite.IgniteException)5 JdkMarshaller (org.apache.ignite.marshaller.jdk.JdkMarshaller)5 Ignite (org.apache.ignite.Ignite)4 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)4 Server (org.eclipse.jetty.server.Server)4 File (java.io.File)2 OutputStream (java.io.OutputStream)2 BinaryConfiguration (org.apache.ignite.configuration.BinaryConfiguration)2 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)2 BinaryContext (org.apache.ignite.internal.binary.BinaryContext)2 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)2