From 67cef441b21ffc900ad5fb112cbd43434d281760 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 5 Nov 2018 14:05:02 +0100 Subject: [PATCH 1/3] Support direct buffers in hpack (#3058) Signed-off-by: Greg Wilkins --- .../eclipse/jetty/http2/hpack/Huffman.java | 30 +--- .../eclipse/jetty/http2/hpack/HpackTest.java | 9 +- .../jetty/http2/hpack/HuffmanTest.java | 3 +- .../jetty/io/jmh/ByteBufferBenchmark.java | 154 ++++++++++++++++++ 4 files changed, 169 insertions(+), 27 deletions(-) create mode 100644 jetty-jmh/src/main/java/org/eclipse/jetty/io/jmh/ByteBufferBenchmark.java diff --git a/jetty-http2/http2-hpack/src/main/java/org/eclipse/jetty/http2/hpack/Huffman.java b/jetty-http2/http2-hpack/src/main/java/org/eclipse/jetty/http2/hpack/Huffman.java index ddb2c4a97df..a0ed7b538e1 100644 --- a/jetty-http2/http2-hpack/src/main/java/org/eclipse/jetty/http2/hpack/Huffman.java +++ b/jetty-http2/http2-hpack/src/main/java/org/eclipse/jetty/http2/hpack/Huffman.java @@ -350,23 +350,16 @@ public class Huffman return decode(buffer,buffer.remaining()); } - public static String decode(ByteBuffer buffer,int length) throws HpackException.CompressionException + public static String decode(ByteBuffer buffer, int length) throws HpackException.CompressionException { StringBuilder out = new StringBuilder(length*2); int node = 0; int current = 0; int bits = 0; - byte[] array = buffer.array(); - int position=buffer.position(); - int start=buffer.arrayOffset()+position; - int end=start+length; - buffer.position(position+length); - - - for (int i=start; i= 8) @@ -460,10 +453,6 @@ public class Huffman { long current = 0; int n = 0; - - byte[] array = buffer.array(); - int p=buffer.arrayOffset()+buffer.position(); - int len = s.length(); for (int i=0;i= 8) { n -= 8; - array[p++]=(byte)(current >> n); + buffer.put((byte)(current >> n)); } } - if (n > 0) + if (n > 0) { - current <<= (8 - n); - current |= (0xFF >>> n); - array[p++]=(byte)current; + current <<= (8 - n); + current |= (0xFF >>> n); + buffer.put((byte)(current)); } - - buffer.position(p-buffer.arrayOffset()); + } } diff --git a/jetty-http2/http2-hpack/src/test/java/org/eclipse/jetty/http2/hpack/HpackTest.java b/jetty-http2/http2-hpack/src/test/java/org/eclipse/jetty/http2/hpack/HpackTest.java index 355ffb2953f..5962fdfb65e 100644 --- a/jetty-http2/http2-hpack/src/test/java/org/eclipse/jetty/http2/hpack/HpackTest.java +++ b/jetty-http2/http2-hpack/src/test/java/org/eclipse/jetty/http2/hpack/HpackTest.java @@ -19,10 +19,9 @@ package org.eclipse.jetty.http2.hpack; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.Matchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.fail; import java.nio.ByteBuffer; @@ -51,7 +50,7 @@ public class HpackTest { HpackEncoder encoder = new HpackEncoder(); HpackDecoder decoder = new HpackDecoder(4096,8192); - ByteBuffer buffer = BufferUtil.allocate(16*1024); + ByteBuffer buffer = BufferUtil.allocateDirect(16*1024); HttpFields fields0 = new HttpFields(); fields0.add(HttpHeader.CONTENT_TYPE,"text/html"); @@ -104,7 +103,7 @@ public class HpackTest { HpackEncoder encoder = new HpackEncoder(); HpackDecoder decoder = new HpackDecoder(4096,164); - ByteBuffer buffer = BufferUtil.allocate(16*1024); + ByteBuffer buffer = BufferUtil.allocateDirect(16*1024); HttpFields fields0 = new HttpFields(); fields0.add("1234567890","1234567890123456789012345678901234567890"); @@ -144,7 +143,7 @@ public class HpackTest { HpackEncoder encoder = new HpackEncoder(200,200); HpackDecoder decoder = new HpackDecoder(200,1024); - ByteBuffer buffer = BufferUtil.allocate(16*1024); + ByteBuffer buffer = BufferUtil.allocateDirect(16*1024); HttpFields fields0 = new HttpFields(); fields0.add("123456789012345678901234567890123456788901234567890","value"); diff --git a/jetty-http2/http2-hpack/src/test/java/org/eclipse/jetty/http2/hpack/HuffmanTest.java b/jetty-http2/http2-hpack/src/test/java/org/eclipse/jetty/http2/hpack/HuffmanTest.java index bf7a935458c..c909d383d54 100644 --- a/jetty-http2/http2-hpack/src/test/java/org/eclipse/jetty/http2/hpack/HuffmanTest.java +++ b/jetty-http2/http2-hpack/src/test/java/org/eclipse/jetty/http2/hpack/HuffmanTest.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.http2.hpack; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.nio.BufferOverflowException; import java.nio.ByteBuffer; import java.util.Locale; import java.util.stream.Stream; @@ -79,7 +80,7 @@ public class HuffmanTest assertThrows(IllegalArgumentException.class, () -> Huffman.octetsNeeded(s)); - assertThrows(IllegalArgumentException.class, + assertThrows(BufferOverflowException.class, () -> Huffman.encode(BufferUtil.allocate(32), s)); } } diff --git a/jetty-jmh/src/main/java/org/eclipse/jetty/io/jmh/ByteBufferBenchmark.java b/jetty-jmh/src/main/java/org/eclipse/jetty/io/jmh/ByteBufferBenchmark.java new file mode 100644 index 00000000000..eb2ba3ec9b2 --- /dev/null +++ b/jetty-jmh/src/main/java/org/eclipse/jetty/io/jmh/ByteBufferBenchmark.java @@ -0,0 +1,154 @@ +// +// ======================================================================== +// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.io.jmh; + +import java.nio.ByteBuffer; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Threads; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + + +@State(Scope.Benchmark) +@Threads(4) +@Warmup(iterations = 7, time = 500, timeUnit = TimeUnit.MILLISECONDS) +@Measurement(iterations = 7, time = 500, timeUnit = TimeUnit.MILLISECONDS) +public class ByteBufferBenchmark +{ + public long test(ByteBuffer buffer) + { + buffer.clear(); + while(buffer.hasRemaining()) + { + int size = ThreadLocalRandom.current().nextInt(1024); + byte[] bytes = new byte[size]; + ThreadLocalRandom.current().nextBytes(bytes); + buffer.put(bytes,0,Math.min(bytes.length,buffer.remaining())); + } + + buffer.flip(); + + long sum = 0; + while(buffer.hasRemaining()) + sum += buffer.get(); + + return sum; + } + + + public long testArray(ByteBuffer buffer) + { + buffer.clear(); + byte[] array = buffer.array(); + int offset = buffer.arrayOffset(); + int end = offset + buffer.remaining(); + while(offset Date: Tue, 6 Nov 2018 09:29:43 +0100 Subject: [PATCH 2/3] Issue #3060 WriteFlusher fails callback on all Throwables Signed-off-by: Greg Wilkins --- jetty-io/src/main/java/org/eclipse/jetty/io/WriteFlusher.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/WriteFlusher.java b/jetty-io/src/main/java/org/eclipse/jetty/io/WriteFlusher.java index a2d9c8fa008..d567f45577d 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/WriteFlusher.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/WriteFlusher.java @@ -294,7 +294,7 @@ abstract public class WriteFlusher else fail(callback); } - catch (IOException e) + catch (Throwable e) { if (DEBUG) LOG.debug("write exception", e); @@ -366,7 +366,7 @@ abstract public class WriteFlusher else fail(callback); } - catch (IOException e) + catch (Throwable e) { if (DEBUG) LOG.debug("completeWrite exception", e); From 871f73cdf617063fae254268776a63715a80a977 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Tue, 6 Nov 2018 10:03:48 +0100 Subject: [PATCH 3/3] Jetty 9.4.x 2932 switchable classloader for session attribute values (#2964) --- .../session-configuration-infinispan.adoc | 41 ++++ .../session/GCloudSessionDataStore.java | 195 ++++++--------- .../session/HazelcastSessionDataStore.java | 48 ++-- .../HazelcastSessionDataStoreFactory.java | 14 +- .../session/SessionDataSerializer.java | 110 +++++++++ jetty-infinispan/pom.xml | 17 ++ .../config/etc/sessions/infinispan/remote.xml | 61 ++++- .../infinispan/InfinispanSessionData.java | 69 ++++++ .../InfinispanSessionDataStore.java | 137 +++++------ .../InfinispanSessionLegacyConverter.java | 224 +++++++++++++++++ .../infinispan/SessionDataMarshaller.java | 106 ++++++++ .../session/infinispan/WebAppMarshaller.java | 1 + .../src/main/resources/session.proto | 19 ++ .../jetty-memcached-sessions/pom.xml | 2 +- .../session-data-cache/xmemcached.mod | 2 +- .../session/MemcachedSessionDataMap.java | 55 +++-- .../nosql/mongodb/MongoSessionDataStore.java | 232 ++++++++---------- .../authentication/SessionAuthentication.java | 20 +- .../session/AbstractSessionDataStore.java | 119 +++++---- .../server/session/FileSessionDataStore.java | 160 ++++-------- .../server/session/JDBCSessionDataStore.java | 165 +++++-------- .../server/session/NullSessionDataStore.java | 28 +-- .../jetty/server/session/SessionData.java | 90 ++++++- .../util/ClassLoadingObjectInputStream.java | 36 ++- .../jetty/server/session/FileTestHelper.java | 41 +--- .../test-gcloud-sessions/pom.xml | 1 - .../session/ClusteredOrphanedSessionTest.java | 8 +- .../ClusteredSessionScavengingTest.java | 7 +- .../session/GCloudSessionTestSupport.java | 67 ++--- .../session/InvalidationSessionTest.java | 6 +- .../session/HazelcastTestHelper.java | 23 +- .../test-infinispan-sessions/pom.xml | 10 + .../remote/RemoteInfinispanTestSupport.java | 38 ++- .../jetty/server/session/JdbcTestHelper.java | 33 +-- .../test-memcached-sessions/pom.xml | 6 + .../sessions/MemcachedTestHelper.java | 22 +- .../mongodb/MongoSessionDataStoreTest.java | 64 +++++ .../jetty/nosql/mongodb/MongoTestHelper.java | 113 ++++++--- .../server/session/TestSessionDataStore.java | 2 +- .../session/DeleteUnloadableSessionTest.java | 26 +- .../session/SessionEvictionFailureTest.java | 23 +- .../src/main/java/com/acme/SessionDump.java | 5 + 42 files changed, 1562 insertions(+), 884 deletions(-) create mode 100644 jetty-hazelcast/src/main/java/org/eclipse/jetty/hazelcast/session/SessionDataSerializer.java create mode 100644 jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionData.java create mode 100644 jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionLegacyConverter.java create mode 100644 jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/SessionDataMarshaller.java create mode 100644 jetty-infinispan/src/main/resources/session.proto diff --git a/jetty-documentation/src/main/asciidoc/administration/sessions/session-configuration-infinispan.adoc b/jetty-documentation/src/main/asciidoc/administration/sessions/session-configuration-infinispan.adoc index 07528662522..67e99562104 100644 --- a/jetty-documentation/src/main/asciidoc/administration/sessions/session-configuration-infinispan.adoc +++ b/jetty-documentation/src/main/asciidoc/administration/sessions/session-configuration-infinispan.adoc @@ -179,3 +179,44 @@ In a clustered environment, there is a risk of the last access time of the sessi This allows the possibility that a node may prematurely expire the session, even though it is in use by another node. Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - there is no point in setting a `savePeriod` that is larger than the `maxIdleTime`. ____ + +==== Converting session format for jetty-9.4.13 + +From jetty-9.4.13 onwards, we have changed the format of the serialized session when using a remote cache (ie using hotrod). +Prior to release 9.4.13 we used the default Infinispan serialization, however this was not able to store sufficient information to allow jetty to properly deserialize session attributes in all circumstances. +See issue https://github.com/eclipse/jetty.project/issues/2919 for more background. + +We have provided a conversion program which will convert any sessions stored in Infinispan to the new format. +____ +[IMPORTANT] +We recommend that you backup your stored sessions before running the conversion program. +____ + +How to use the converter: + +[source, screen, subs="{sub-order}"] +---- +java -cp servlet-api-3.1.jar:jetty-util-9.4.13.jar:jetty-server-9.4.13.jar:infinispan-remote-9.1.0.Final.jar:jetty-infinispan-9.4.13.jar:[other classpath] org.eclipse.jetty.session.infinispan.InfinispanSessionLegacyConverter + +Usage: InfinispanSessionLegacyConverter [-Dhost=127.0.0.1] [-Dverbose=true|false] [check] +---- + +The classpath:: +Must contain the servlet-api, jetty-util, jetty-server, jetty-infinispan and infinispan-remote jars. If your sessions contain attributes that use application classes, you will also need to also put those classes onto the classpath. If your session has been authenticated, you may also need to include the jetty-security and jetty-http jars on the classpath. +Parameters:: +When used with no arguments the usage message is printed. When used with the `cache-name` parameter the conversion is performed. When used with both `cache-name` and `check` parameters, sessions are checked for whether or not they are converted. + + -Dhost::: you can optionally provide a system property with the address of your remote Infinispan server. Defaults to the localhost. + -Dverbose::: defaults to false. If true, prints more comprehensive stacktrace information about failures. Useful to diagnose why a session is not converted. + cache-name::: the name of the remote cache containing your sessions. This is mandatory. + check::: the optional check command will verify sessions have been converted. Use it _after_ doing the conversion. + +To perform the conversion, run the InfinispanSessionLegacyConverter with just the `cache-name`, and optionally the `host` system property. +The following command will attempt to convert all sessions in the cached named `my-remote-cache` on the machine `myhost`, ensuring that application classes in the `/my/custom/classes` directory are on the classpath: + +[source, screen, subs="{sub-order}"] +---- +java -cp servlet-api-3.1.jar:jetty-util-9.4.13.jar:jetty-server-9.4.13.jar:infinispan-remote-9.1.0.Final.jar:jetty-infinispan-9.4.13.jar:/my/custom/classes org.eclipse.jetty.session.infinispan.InfinispanSessionLegacyConverter -Dhost=myhost my-remote-cache +---- + +If the converter fails to convert a session, an error message and stacktrace will be printed and the conversion will abort. The failed session should be untouched, however _it is prudent to take a backup of your cache before attempting the conversion_. diff --git a/jetty-gcloud/jetty-gcloud-session-manager/src/main/java/org/eclipse/jetty/gcloud/session/GCloudSessionDataStore.java b/jetty-gcloud/jetty-gcloud-session-manager/src/main/java/org/eclipse/jetty/gcloud/session/GCloudSessionDataStore.java index c8a6f30fca6..cd85db19ccb 100644 --- a/jetty-gcloud/jetty-gcloud-session-manager/src/main/java/org/eclipse/jetty/gcloud/session/GCloudSessionDataStore.java +++ b/jetty-gcloud/jetty-gcloud-session-manager/src/main/java/org/eclipse/jetty/gcloud/session/GCloudSessionDataStore.java @@ -22,9 +22,7 @@ package org.eclipse.jetty.gcloud.session; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.util.HashSet; -import java.util.Map; import java.util.Set; -import java.util.concurrent.atomic.AtomicReference; import org.eclipse.jetty.server.session.AbstractSessionDataStore; import org.eclipse.jetty.server.session.SessionContext; @@ -324,18 +322,14 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore _lastSaved = lastSaved; } - /** - * @see java.lang.Object#toString() - */ + @Override public String toString() { return String.format("%s==%s:%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",this.getClass().getName(), _kind,_accessed,_attributes,_contextPath,_cookieSetTime,_createTime,_expiry,_id,_lastAccessed,_lastNode,_maxInactive,_vhost); } - - - + } @@ -437,9 +431,7 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore return _maxRetries; } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStart() - */ + @Override protected void doStart() throws Exception { @@ -466,9 +458,7 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore super.doStart(); } - /** - * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStop() - */ + @Override protected void doStop() throws Exception { @@ -501,12 +491,8 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore } - - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#load(java.lang.String) - */ @Override - public SessionData load(String id) throws Exception + public SessionData doLoad(String id) throws Exception { if (LOG.isDebugEnabled()) LOG.debug("Loading session {} from DataStore", id); @@ -529,9 +515,7 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore } } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#delete(java.lang.String) - */ + @Override public boolean delete(String id) throws Exception { @@ -540,9 +524,7 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore return true; } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#getExpired(Set) - */ + @Override public Set doGetExpired(Set candidates) { @@ -701,9 +683,6 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String) - */ @Override public boolean exists(String id) throws Exception { @@ -766,6 +745,7 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore } } + /** * Check to see if the given time is in the past. * @@ -780,9 +760,7 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore return timestamp < System.currentTimeMillis(); } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(java.lang.String, org.eclipse.jetty.server.session.SessionData, long) - */ + @Override public void doStore(String id, SessionData data, long lastSaveTime) throws Exception { @@ -874,6 +852,8 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore return false; } } + + /** * Generate a gcloud datastore Entity from SessionData * @param session the session data @@ -889,28 +869,27 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore Entity entity = null; //serialize the attribute map - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(session.getAllAttributes()); - oos.flush(); - - //turn a session into an entity - entity = Entity.newBuilder(key) - .set(_model.getId(), session.getId()) - .set(_model.getContextPath(), session.getContextPath()) - .set(_model.getVhost(), session.getVhost()) - .set(_model.getAccessed(), session.getAccessed()) - .set(_model.getLastAccessed(), session.getLastAccessed()) - .set(_model.getCreateTime(), session.getCreated()) - .set(_model.getCookieSetTime(), session.getCookieSet()) - .set(_model.getLastNode(),session.getLastNode()) - .set(_model.getExpiry(), session.getExpiry()) - .set(_model.getMaxInactive(), session.getMaxInactiveMs()) - .set(_model.getLastSaved(), session.getLastSaved()) - .set(_model.getAttributes(), BlobValue.newBuilder(Blob.copyFrom(baos.toByteArray())).setExcludeFromIndexes(true).build()).build(); + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos)) + { + SessionData.serializeAttributes(session, oos); - - return entity; + //turn a session into an entity + entity = Entity.newBuilder(key) + .set(_model.getId(), session.getId()) + .set(_model.getContextPath(), session.getContextPath()) + .set(_model.getVhost(), session.getVhost()) + .set(_model.getAccessed(), session.getAccessed()) + .set(_model.getLastAccessed(), session.getLastAccessed()) + .set(_model.getCreateTime(), session.getCreated()) + .set(_model.getCookieSetTime(), session.getCookieSet()) + .set(_model.getLastNode(),session.getLastNode()) + .set(_model.getExpiry(), session.getExpiry()) + .set(_model.getMaxInactive(), session.getMaxInactiveMs()) + .set(_model.getLastSaved(), session.getLastSaved()) + .set(_model.getAttributes(), BlobValue.newBuilder(Blob.copyFrom(baos.toByteArray())).setExcludeFromIndexes(true).build()).build(); + return entity; + } } /** @@ -924,79 +903,51 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore if (entity == null) return null; - final AtomicReference reference = new AtomicReference<>(); - final AtomicReference exception = new AtomicReference<>(); - Runnable load = new Runnable() - { - @Override - public void run () - { - try - { - //turn an Entity into a Session - String id = entity.getString(_model.getId()); - String contextPath = entity.getString(_model.getContextPath()); - String vhost = entity.getString(_model.getVhost()); - long accessed = entity.getLong(_model.getAccessed()); - long lastAccessed = entity.getLong(_model.getLastAccessed()); - long createTime = entity.getLong(_model.getCreateTime()); - long cookieSet = entity.getLong(_model.getCookieSetTime()); - String lastNode = entity.getString(_model.getLastNode()); + //turn an Entity into a Session + String id = entity.getString(_model.getId()); + String contextPath = entity.getString(_model.getContextPath()); + String vhost = entity.getString(_model.getVhost()); + long accessed = entity.getLong(_model.getAccessed()); + long lastAccessed = entity.getLong(_model.getLastAccessed()); + long createTime = entity.getLong(_model.getCreateTime()); + long cookieSet = entity.getLong(_model.getCookieSetTime()); + String lastNode = entity.getString(_model.getLastNode()); - long lastSaved = 0; - //for compatibility with previously saved sessions, lastSaved may not be present - try - { - lastSaved = entity.getLong(_model.getLastSaved()); - } - catch (DatastoreException e) - { - LOG.ignore(e); - } - long expiry = entity.getLong(_model.getExpiry()); - long maxInactive = entity.getLong(_model.getMaxInactive()); - Blob blob = (Blob) entity.getBlob(_model.getAttributes()); - - SessionData session = newSessionData (id, createTime, accessed, lastAccessed, maxInactive); - session.setLastNode(lastNode); - session.setContextPath(contextPath); - session.setVhost(vhost); - session.setCookieSet(cookieSet); - session.setLastNode(lastNode); - session.setLastSaved(lastSaved); - session.setExpiry(expiry); - try (ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(blob.asInputStream())) - { - Object o = ois.readObject(); - session.putAllAttributes((Map)o); - } - catch (Exception e) - { - throw new UnreadableSessionDataException (id, _context, e); - } - reference.set(session); - } - catch (Exception e) - { - exception.set(e); - } - } - }; - - //ensure this runs in the context classloader - _context.run(load); - - if (exception.get() != null) + long lastSaved = 0; + //for compatibility with previously saved sessions, lastSaved may not be present + try { - throw exception.get(); + lastSaved = entity.getLong(_model.getLastSaved()); } - - return reference.get(); + catch (DatastoreException e) + { + LOG.ignore(e); + } + long expiry = entity.getLong(_model.getExpiry()); + long maxInactive = entity.getLong(_model.getMaxInactive()); + Blob blob = (Blob) entity.getBlob(_model.getAttributes()); + + SessionData session = newSessionData (id, createTime, accessed, lastAccessed, maxInactive); + session.setLastNode(lastNode); + session.setContextPath(contextPath); + session.setVhost(vhost); + session.setCookieSet(cookieSet); + session.setLastNode(lastNode); + session.setLastSaved(lastSaved); + session.setExpiry(expiry); + try (ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(blob.asInputStream())) + { + SessionData.deserializeAttributes(session, ois); + } + catch (Exception e) + { + throw new UnreadableSessionDataException (id, _context, e); + } + return session; + } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating() - */ + @ManagedAttribute(value="does gcloud serialize session data", readonly=true) @Override public boolean isPassivating() @@ -1005,14 +956,10 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#toString() - */ @Override public String toString() { return String.format("%s[namespace=%s,backoff=%d,maxRetries=%d,maxResults=%d,indexes=%b]",super.toString(), _namespace, _backoff, _maxRetries, _maxResults,_indexesPresent); } - } diff --git a/jetty-hazelcast/src/main/java/org/eclipse/jetty/hazelcast/session/HazelcastSessionDataStore.java b/jetty-hazelcast/src/main/java/org/eclipse/jetty/hazelcast/session/HazelcastSessionDataStore.java index 26ff7c7f30c..8e813da6c35 100644 --- a/jetty-hazelcast/src/main/java/org/eclipse/jetty/hazelcast/session/HazelcastSessionDataStore.java +++ b/jetty-hazelcast/src/main/java/org/eclipse/jetty/hazelcast/session/HazelcastSessionDataStore.java @@ -18,7 +18,10 @@ package org.eclipse.jetty.hazelcast.session; -import com.hazelcast.core.IMap; +import java.util.Collections; +import java.util.Set; +import java.util.stream.Collectors; + import org.eclipse.jetty.server.session.AbstractSessionDataStore; import org.eclipse.jetty.server.session.SessionContext; import org.eclipse.jetty.server.session.SessionData; @@ -28,10 +31,7 @@ import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; -import java.util.Collections; -import java.util.Set; -import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Collectors; +import com.hazelcast.core.IMap; /** * Session data stored in Hazelcast @@ -52,36 +52,24 @@ public class HazelcastSessionDataStore } @Override - public SessionData load( String id ) - throws Exception + public SessionData doLoad( String id ) + throws Exception { - - final AtomicReference reference = new AtomicReference<>(); - final AtomicReference exception = new AtomicReference<>(); - - //ensure the load runs in the context classloader scope - _context.run( () -> { - try - { - if (LOG.isDebugEnabled()) - LOG.debug( "Loading session {} from hazelcast", id ); - - SessionData sd = sessionDataMap.get( getCacheKey( id ) ); - reference.set(sd); - } - catch (Exception e) - { - exception.set(new UnreadableSessionDataException(id, _context, e)); - } - } ); - - if (exception.get() != null) + try { - throw exception.get(); + if (LOG.isDebugEnabled()) + LOG.debug( "Loading session {} from hazelcast", id ); + + SessionData sd = sessionDataMap.get( getCacheKey( id ) ); + return sd; + } + catch (Exception e) + { + throw new UnreadableSessionDataException(id, _context, e); } - return reference.get(); } + @Override public boolean delete( String id ) throws Exception diff --git a/jetty-hazelcast/src/main/java/org/eclipse/jetty/hazelcast/session/HazelcastSessionDataStoreFactory.java b/jetty-hazelcast/src/main/java/org/eclipse/jetty/hazelcast/session/HazelcastSessionDataStoreFactory.java index 7fce2facb04..aa076cf4011 100644 --- a/jetty-hazelcast/src/main/java/org/eclipse/jetty/hazelcast/session/HazelcastSessionDataStoreFactory.java +++ b/jetty-hazelcast/src/main/java/org/eclipse/jetty/hazelcast/session/HazelcastSessionDataStoreFactory.java @@ -23,10 +23,12 @@ import com.hazelcast.client.config.ClientConfig; import com.hazelcast.client.config.XmlClientConfigBuilder; import com.hazelcast.config.Config; import com.hazelcast.config.MapConfig; +import com.hazelcast.config.SerializerConfig; import com.hazelcast.config.XmlConfigBuilder; import com.hazelcast.core.Hazelcast; import com.hazelcast.core.HazelcastInstance; import org.eclipse.jetty.server.session.AbstractSessionDataStoreFactory; +import org.eclipse.jetty.server.session.SessionData; import org.eclipse.jetty.server.session.SessionDataStore; import org.eclipse.jetty.server.session.SessionDataStoreFactory; import org.eclipse.jetty.server.session.SessionHandler; @@ -68,7 +70,12 @@ public class HazelcastSessionDataStoreFactory { if ( configurationLocation == null ) { - hazelcastInstance = HazelcastClient.newHazelcastClient( new ClientConfig() ); + ClientConfig config = new ClientConfig(); + SerializerConfig sc = new SerializerConfig(). + setImplementation(new SessionDataSerializer()). + setTypeClass(SessionData.class); + config.getSerializationConfig().addSerializerConfig(sc); + hazelcastInstance = HazelcastClient.newHazelcastClient(config); } else { @@ -82,7 +89,12 @@ public class HazelcastSessionDataStoreFactory Config config; if ( configurationLocation == null ) { + + SerializerConfig sc = new SerializerConfig(). + setImplementation(new SessionDataSerializer()). + setTypeClass(SessionData.class); config = new Config(); + config.getSerializationConfig().addSerializerConfig(sc); // configure a default Map if null if ( mapConfig == null ) { diff --git a/jetty-hazelcast/src/main/java/org/eclipse/jetty/hazelcast/session/SessionDataSerializer.java b/jetty-hazelcast/src/main/java/org/eclipse/jetty/hazelcast/session/SessionDataSerializer.java new file mode 100644 index 00000000000..e5dc9d3514c --- /dev/null +++ b/jetty-hazelcast/src/main/java/org/eclipse/jetty/hazelcast/session/SessionDataSerializer.java @@ -0,0 +1,110 @@ +// +// ======================================================================== +// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + + +package org.eclipse.jetty.hazelcast.session; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; + +import org.eclipse.jetty.server.session.SessionData; +import org.eclipse.jetty.util.ClassLoadingObjectInputStream; + +import com.hazelcast.nio.ObjectDataInput; +import com.hazelcast.nio.ObjectDataOutput; +import com.hazelcast.nio.serialization.StreamSerializer; + +/** + * SessionDataSerializer + * + * Handles serialization on behalf of the SessionData object, and + * ensures that we use jetty's classloading knowledge. + */ +public class SessionDataSerializer implements StreamSerializer +{ + public static final int __TYPEID = 99; + @Override + public int getTypeId() + { + return __TYPEID; + } + + @Override + public void destroy() + { + } + + @Override + public void write(ObjectDataOutput out, SessionData data) throws IOException + { + out.writeUTF(data.getId()); + out.writeUTF(data.getContextPath()); + out.writeUTF(data.getVhost()); + + out.writeLong(data.getAccessed()); + out.writeLong(data.getLastAccessed()); + out.writeLong(data.getCreated()); + out.writeLong(data.getCookieSet()); + out.writeUTF(data.getLastNode()); + + out.writeLong(data.getExpiry()); + out.writeLong(data.getMaxInactiveMs()); + + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos)) + { + SessionData.serializeAttributes(data, oos); + out.writeByteArray(baos.toByteArray()); + } + } + + @Override + public SessionData read(ObjectDataInput in) throws IOException + { + String id = in.readUTF(); + String contextPath = in.readUTF(); + String vhost = in.readUTF(); + + long accessed = in.readLong(); + long lastAccessed = in.readLong(); + long created = in.readLong(); + long cookieSet = in.readLong(); + String lastNode = in.readUTF(); + long expiry = in.readLong(); + long maxInactiveMs = in.readLong(); + + SessionData sd = new SessionData(id, contextPath, vhost, created, accessed, lastAccessed, maxInactiveMs); + + ByteArrayInputStream bais = new ByteArrayInputStream(in.readByteArray()); + try (ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(bais)) + { + SessionData.deserializeAttributes(sd, ois); + } + catch(ClassNotFoundException e) + { + throw new IOException(e); + } + sd.setCookieSet(cookieSet); + sd.setLastNode(lastNode); + sd.setExpiry(expiry); + return sd; + } + +} diff --git a/jetty-infinispan/pom.xml b/jetty-infinispan/pom.xml index 6ad0e56f451..921c93aaf4b 100644 --- a/jetty-infinispan/pom.xml +++ b/jetty-infinispan/pom.xml @@ -40,10 +40,27 @@ infinispan-core ${infinispan.version} + + org.infinispan.protostream + protostream + 4.1.0.Final + org.eclipse.jetty jetty-server ${project.version} + + org.infinispan + infinispan-client-hotrod + 9.1.0.Final + provided + + + org.infinispan + infinispan-remote-query-client + 9.1.0.Final + provided + diff --git a/jetty-infinispan/src/main/config/etc/sessions/infinispan/remote.xml b/jetty-infinispan/src/main/config/etc/sessions/infinispan/remote.xml index 5f2bb705404..356cd48b29c 100644 --- a/jetty-infinispan/src/main/config/etc/sessions/infinispan/remote.xml +++ b/jetty-infinispan/src/main/config/etc/sessions/infinispan/remote.xml @@ -4,15 +4,72 @@ + + + + + + + + + + + /resources/hotrod-client.properties + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + /session.proto + + + + + + + + + + + + - + - + + diff --git a/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionData.java b/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionData.java new file mode 100644 index 00000000000..dbca0652dc3 --- /dev/null +++ b/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionData.java @@ -0,0 +1,69 @@ +package org.eclipse.jetty.session.infinispan; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.util.Map; + +import org.eclipse.jetty.server.session.SessionData; +import org.eclipse.jetty.util.ClassLoadingObjectInputStream; + +/** + * InfinispanSessionData + * + * Specialization of SessionData to hold the attributes as a serialized byte + * array. This is necessary because to deserialize the attributes correctly, we + * need to know which classloader to use, which is normally provided as the + * thread context classloader. However, infinispan marshalling uses a thread + * pool and thus these threads have no knowledge of the correct classloader to + * use. + * + */ +public class InfinispanSessionData extends SessionData +{ + protected byte[] _serializedAttributes; + + public InfinispanSessionData(String id, String cpath, String vhost, long created, long accessed, long lastAccessed, long maxInactiveMs) + { + super(id, cpath, vhost, created, accessed, lastAccessed, maxInactiveMs); + } + + public InfinispanSessionData(String id, String cpath, String vhost, long created, long accessed, long lastAccessed, long maxInactiveMs, + Map attributes) + { + super(id, cpath, vhost, created, accessed, lastAccessed, maxInactiveMs, attributes); + } + + public byte[] getSerializedAttributes() + { + return _serializedAttributes; + } + + public void setSerializedAttributes(byte[] serializedAttributes) + { + _serializedAttributes = serializedAttributes; + } + + public void deserializeAttributes() throws ClassNotFoundException, IOException + { + if (_serializedAttributes == null) return; + + try (ByteArrayInputStream bais = new ByteArrayInputStream(_serializedAttributes); + ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(bais)) + { + SessionData.deserializeAttributes(this, ois); + _serializedAttributes = null; + } + } + + public void serializeAttributes() throws IOException + { + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos)) + { + SessionData.serializeAttributes(this, oos); + _serializedAttributes = baos.toByteArray(); + } + } +} \ No newline at end of file diff --git a/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionDataStore.java b/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionDataStore.java index 5d649b76bfd..fa21de58ade 100644 --- a/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionDataStore.java +++ b/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionDataStore.java @@ -45,7 +45,6 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore private final static Logger LOG = Log.getLogger("org.eclipse.jetty.server.session"); - /** * Clustered cache of sessions */ @@ -54,6 +53,8 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore private int _infinispanIdleTimeoutSec; + private boolean _passivating; + /** * Get the clustered cache instance. @@ -77,50 +78,55 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore this._cache = cache; } - - - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#load(String) - */ - @Override - public SessionData load(String id) throws Exception - { - final AtomicReference reference = new AtomicReference<>(); - final AtomicReference exception = new AtomicReference<>(); - - Runnable load = new Runnable() - { - @Override - public void run () - { - try - { - if (LOG.isDebugEnabled()) - LOG.debug("Loading session {} from infinispan", id); - - SessionData sd = (SessionData)_cache.get(getCacheKey(id)); - reference.set(sd); - } - catch (Exception e) - { - exception.set(new UnreadableSessionDataException(id, _context, e)); - } - } - }; - - //ensure the load runs in the context classloader scope - _context.run(load); - - if (exception.get() != null) - throw exception.get(); - - return reference.get(); + + @Override + protected void doStart() throws Exception + { + super.doStart(); + if (_cache == null) + throw new IllegalStateException ("No cache"); + + try + { + _passivating = false; + Class remoteClass = Thread.currentThread().getContextClassLoader().loadClass("org.infinispan.client.hotrod.RemoteCache"); + if (remoteClass.isAssignableFrom(_cache.getClass())) + _passivating = true; + } + catch (ClassNotFoundException e) + { + //expected if not running with remote cache + } } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#delete(String) - */ + + + @Override + public SessionData doLoad(String id) throws Exception + { + try + { + if (LOG.isDebugEnabled()) + LOG.debug("Loading session {} from infinispan", id); + + InfinispanSessionData sd = (InfinispanSessionData)_cache.get(getCacheKey(id)); + if (isPassivating() && sd != null) + { + if (LOG.isDebugEnabled()) + LOG.debug("Deserializing session attributes for {}", id); + sd.deserializeAttributes(); + } + + return sd; + } + catch (Exception e) + { + throw new UnreadableSessionDataException(id, _context, e); + } + } + + @Override public boolean delete(String id) throws Exception { @@ -129,9 +135,7 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore return (_cache.remove(getCacheKey(id)) != null); } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#getExpired(Set) - */ + @Override public Set doGetExpired(Set candidates) { @@ -199,9 +203,7 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore return expired; } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(String, SessionData, long) - */ + @Override public void doStore(String id, SessionData data, long lastSaveTime) throws Exception { @@ -210,9 +212,9 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore //scavenges the session before this timeout occurs, the session will be removed. //NOTE: that no session listeners can be called for this. if (data.getMaxInactiveMs() > 0 && getInfinispanIdleTimeoutSec() > 0) - _cache.put(getCacheKey(id), data, -1, TimeUnit.MILLISECONDS, getInfinispanIdleTimeoutSec(), TimeUnit.SECONDS); + _cache.put(getCacheKey(id), (InfinispanSessionData)data, -1, TimeUnit.MILLISECONDS, getInfinispanIdleTimeoutSec(), TimeUnit.SECONDS); else - _cache.put(getCacheKey(id), data); + _cache.put(getCacheKey(id), (InfinispanSessionData)data); if (LOG.isDebugEnabled()) LOG.debug("Session {} saved to infinispan, expires {} ", id, data.getExpiry()); @@ -225,35 +227,15 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore } - - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating() - */ @ManagedAttribute(value="does store serialize sessions", readonly=true) @Override public boolean isPassivating() { - //TODO run in the _context to ensure classloader is set - try - { - Class remoteClass = Thread.currentThread().getContextClassLoader().loadClass("org.infinispan.client.hotrod.RemoteCache"); - if (remoteClass.isAssignableFrom(_cache.getClass())) - { - return true; - } - return false; - } - catch (ClassNotFoundException e) - { - return false; - } + return _passivating; } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String) - */ @Override public boolean exists(String id) throws Exception { @@ -299,6 +281,13 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore + @Override + public SessionData newSessionData(String id, long created, long accessed, long lastAccessed, long maxInactiveMs) + { + return new InfinispanSessionData(id, _context.getCanonicalContextPath(), _context.getVhost(), created, accessed, lastAccessed, maxInactiveMs); + } + + /** * @param sec the infinispan-specific idle timeout in sec or 0 if not set */ @@ -315,15 +304,9 @@ public class InfinispanSessionDataStore extends AbstractSessionDataStore } - - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#toString() - */ @Override public String toString() { return String.format("%s[cache=%s,idleTimeoutSec=%d]",super.toString(), (_cache==null?"":_cache.getName()),_infinispanIdleTimeoutSec); } - - } diff --git a/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionLegacyConverter.java b/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionLegacyConverter.java new file mode 100644 index 00000000000..2571e52bcc0 --- /dev/null +++ b/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/InfinispanSessionLegacyConverter.java @@ -0,0 +1,224 @@ +// +// ======================================================================== +// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + + +package org.eclipse.jetty.session.infinispan; + +import java.util.List; +import java.util.Properties; +import java.util.stream.Collectors; + +import org.eclipse.jetty.server.session.SessionData; +import org.infinispan.client.hotrod.RemoteCache; +import org.infinispan.client.hotrod.RemoteCacheManager; +import org.infinispan.client.hotrod.configuration.ConfigurationBuilder; +import org.infinispan.client.hotrod.marshall.ProtoStreamMarshaller; +import org.infinispan.protostream.FileDescriptorSource; +import org.infinispan.protostream.SerializationContext; + + +/** + * InfinispanSessionLegacyConverter + * + * Converts sessions saved in the old serialization + * format into the new protobuf-based serialization. + * + * Use the -Dverbose=true system property to + * print out more information about conversion failures. + * + */ +public class InfinispanSessionLegacyConverter +{ + RemoteCacheManager _protoManager; + RemoteCache _protoCache; + RemoteCacheManager _legacyManager; + RemoteCache _legacyCache; + boolean _verbose = false; + + + public InfinispanSessionLegacyConverter (String cacheName) + throws Exception + { + //legacy serialization + _legacyManager = new RemoteCacheManager(); + _legacyCache = _legacyManager.getCache(cacheName); + + //new protobuf based + String host = System.getProperty("host", "127.0.0.1"); + _verbose = Boolean.getBoolean("verbose"); + + Properties properties = new Properties(); + ConfigurationBuilder clientBuilder = new ConfigurationBuilder(); + clientBuilder.withProperties(properties).addServer().host(host).marshaller(new ProtoStreamMarshaller()); + _protoManager = new RemoteCacheManager(clientBuilder.build()); + FileDescriptorSource fds = new FileDescriptorSource(); + fds.addProtoFiles("/session.proto"); + SerializationContext serCtx = ProtoStreamMarshaller.getSerializationContext(_protoManager); + serCtx.registerProtoFiles(fds); + serCtx.registerMarshaller(new SessionDataMarshaller()); + _protoCache = _protoManager.getCache(cacheName); + } + + /** + * Convert all sessions to protobuf format sessions. + */ + public void convert () + { + long conversions = 0; + List keys = null; + + //Get all sessions stored in the legacy format + try + { + keys = _legacyCache.keySet().stream().collect(Collectors.toList()); + } + catch (Exception e) + { + System.err.println("Error listing legacy sessions, assuming previously converted. Run again using 'check' argument to verify conversion"); + if (_verbose) e.printStackTrace(); + System.exit(1); + } + + for (String s:keys) + { + SessionData data = null; + try + { + data = (SessionData)_legacyCache.get(s); + } + catch (Exception e) + { + System.err.println("Read of session "+s+" failed. Assuming session already converted and skipping."); + if (_verbose) e.printStackTrace(); + continue; + } + + if (data != null) + { + try + { + _legacyCache.remove(s); + } + catch (Exception e) + { + System.err.println("Remove legacy session failed for "+s+" skipping conversion."); + if (_verbose) e.printStackTrace(); + continue; + } + + try + { + InfinispanSessionData isd = new InfinispanSessionData(data.getId(), data.getContextPath(), data.getVhost(), data.getCreated(), + data.getAccessed(), data.getLastAccessed(), data.getMaxInactiveMs()); + isd.putAllAttributes(data.getAllAttributes()); + isd.setExpiry(data.getExpiry()); + isd.setCookieSet(data.getCookieSet()); + isd.setLastSaved(data.getLastSaved()); + isd.setLastNode(data.getLastNode()); + // now write it out to the protobuf format + _protoCache.put(s, isd); + System.err.println("Converted " + s); + conversions++; + } + catch (Exception e) + { + if (_verbose) e.printStackTrace(); + System.err.println("Conversion failed for " + s + " re-instating legacy session."); + try + { + _legacyCache.put(s, data); + } + catch (Exception x) + { + System.err.println("FAILED REINSTATING SESSION " + s + ". ABORTING."); + x.printStackTrace(); + System.exit(1); + } + } + } + else + System.err.println("Unreadable legacy session "+s); + } + + System.err.println("Total sessions converted: "+conversions); + } + + /** + * Retrieve the sessions using protobuf format and print them out to + * confirm they're ok. + */ + public void checkConverted () + { + List keys = null; + try + { + keys = _protoCache.keySet().stream().collect(Collectors.toList()); + } + catch (Exception e) + { + System.err.println("Unable to read converted sessions, assuming still in legacy format. Run again without 'check' option to convert."); + e.printStackTrace(); + System.exit(1); + } + + for (String s:keys) + { + InfinispanSessionData converted = (InfinispanSessionData)_protoCache.get(s); + if (converted != null) + { + System.err.println("OK: "+converted); + converted.getKeys().stream().forEach((ss)->{System.err.println(ss+":"+converted.getAttribute(ss));}); + } + else + System.err.println("Failed: "+s); + } + + System.err.println("Total converted sessions: "+keys.size()); + } + + public static final void usage () + { + System.err.println("Usage: InfinispanSessionLegacyConverter [-Dhost=127.0.0.1] [-Dverbose=true] [check]"); + } + + public static final void main (String... args) + { + if (args == null || args.length < 1) + { + usage(); + System.exit(1); + } + + try + { + InfinispanSessionLegacyConverter converter = new InfinispanSessionLegacyConverter(args[0]); + + if (args.length == 1) + converter.convert(); + else if (args[1].equals("check")) + converter.checkConverted(); + else + usage(); + } + catch (Exception e) + { + System.err.println("Conversion failure"); + e.printStackTrace(); + } + } +} diff --git a/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/SessionDataMarshaller.java b/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/SessionDataMarshaller.java new file mode 100644 index 00000000000..0d380d4cb22 --- /dev/null +++ b/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/SessionDataMarshaller.java @@ -0,0 +1,106 @@ +// +// ======================================================================== +// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.session.infinispan; + +import java.io.IOException; + +import org.infinispan.protostream.MessageMarshaller; + +/** + * SessionDataMarshaller + * + * A marshaller for converting a SessionData object into protobuf format which + * gives greater control over serialization/deserialization. We use that extra + * control to ensure that session attributes can be deserialized using either + * the container class loader or the webapp classloader, as appropriate. + */ +public class SessionDataMarshaller implements MessageMarshaller +{ + /** + * The version of the serializer. + */ + private static final int VERSION = 0; + + @Override + public Class getJavaClass() + { + return InfinispanSessionData.class; + } + + @Override + public String getTypeName() + { + return "org_eclipse_jetty_session_infinispan.InfinispanSessionData"; + } + + @Override + public InfinispanSessionData readFrom(ProtoStreamReader in) throws IOException + { + int version = in.readInt("version");// version of serialized session + String id = in.readString("id"); // session id + String cpath = in.readString("contextPath"); // context path + String vhost = in.readString("vhost"); // first vhost + + long accessed = in.readLong("accessed");// accessTime + long lastAccessed = in.readLong("lastAccessed"); // lastAccessTime + long created = in.readLong("created"); // time created + long cookieSet = in.readLong("cookieSet");// time cookie was set + String lastNode = in.readString("lastNode"); // name of last node + // managing + + long expiry = in.readLong("expiry"); + long maxInactiveMs = in.readLong("maxInactiveMs"); + + InfinispanSessionData sd = new InfinispanSessionData(id, cpath, vhost, created, accessed, lastAccessed, maxInactiveMs); + sd.setCookieSet(cookieSet); + sd.setLastNode(lastNode); + sd.setExpiry(expiry); + + if (version == 0) + { + byte[] attributeArray = in.readBytes("attributes"); + sd.setSerializedAttributes(attributeArray); + return sd; + } + else + throw new IOException("Unrecognized infinispan session version " + version); + } + + @Override + public void writeTo(ProtoStreamWriter out, InfinispanSessionData sdata) throws IOException + { + out.writeInt("version", VERSION); + out.writeString("id", sdata.getId()); // session id + out.writeString("contextPath", sdata.getContextPath()); // context path + out.writeString("vhost", sdata.getVhost()); // first vhost + + out.writeLong("accessed", sdata.getAccessed());// accessTime + out.writeLong("lastAccessed", sdata.getLastAccessed()); // lastAccessTime + out.writeLong("created", sdata.getCreated()); // time created + out.writeLong("cookieSet", sdata.getCookieSet());// time cookie was set + out.writeString("lastNode", sdata.getLastNode()); // name of last node + // managing + + out.writeLong("expiry", sdata.getExpiry()); + out.writeLong("maxInactiveMs", sdata.getMaxInactiveMs()); + + sdata.serializeAttributes(); + out.writeBytes("attributes", sdata.getSerializedAttributes()); + } +} diff --git a/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/WebAppMarshaller.java b/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/WebAppMarshaller.java index 741b2d0a074..1d3c52e5eef 100644 --- a/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/WebAppMarshaller.java +++ b/jetty-infinispan/src/main/java/org/eclipse/jetty/session/infinispan/WebAppMarshaller.java @@ -49,6 +49,7 @@ import org.jboss.marshalling.ContextClassResolver; * </Call> * */ +@Deprecated public class WebAppMarshaller extends AbstractJBossMarshaller { diff --git a/jetty-infinispan/src/main/resources/session.proto b/jetty-infinispan/src/main/resources/session.proto new file mode 100644 index 00000000000..ecadbaaf66c --- /dev/null +++ b/jetty-infinispan/src/main/resources/session.proto @@ -0,0 +1,19 @@ +package org_eclipse_jetty_session_infinispan; + +message InfinispanSessionData +{ + required int32 version = 1; + required string id = 2; + required string contextPath = 3; + required string vhost = 4; + + required sint64 accessed = 5; + required sint64 lastAccessed = 6; + required sint64 created = 7; + required sint64 cookieSet = 8; + required string lastNode = 9; + + required sint64 expiry = 10; + required sint64 maxInactiveMs = 11; + required bytes attributes = 12; +} \ No newline at end of file diff --git a/jetty-memcached/jetty-memcached-sessions/pom.xml b/jetty-memcached/jetty-memcached-sessions/pom.xml index fe4b89009f4..f76ba891883 100644 --- a/jetty-memcached/jetty-memcached-sessions/pom.xml +++ b/jetty-memcached/jetty-memcached-sessions/pom.xml @@ -19,7 +19,7 @@ com.googlecode.xmemcached xmemcached - 2.0.0 + 2.4.5 org.slf4j diff --git a/jetty-memcached/jetty-memcached-sessions/src/main/config/modules/sessions/session-data-cache/xmemcached.mod b/jetty-memcached/jetty-memcached-sessions/src/main/config/modules/sessions/session-data-cache/xmemcached.mod index 0618c02950a..77a21ea759f 100644 --- a/jetty-memcached/jetty-memcached-sessions/src/main/config/modules/sessions/session-data-cache/xmemcached.mod +++ b/jetty-memcached/jetty-memcached-sessions/src/main/config/modules/sessions/session-data-cache/xmemcached.mod @@ -11,7 +11,7 @@ session-store slf4j-api [files] -maven://com.googlecode.xmemcached/xmemcached/2.0.0|lib/xmemcached/xmemcached-2.0.0.jar +maven://com.googlecode.xmemcached/xmemcached/2.4.5|lib/xmemcached/xmemcached-2.4.5.jar [lib] lib/jetty-memcached-sessions-${jetty.version}.jar diff --git a/jetty-memcached/jetty-memcached-sessions/src/main/java/org/eclipse/jetty/memcached/session/MemcachedSessionDataMap.java b/jetty-memcached/jetty-memcached-sessions/src/main/java/org/eclipse/jetty/memcached/session/MemcachedSessionDataMap.java index fbeff56d73d..1b3d01da1ee 100644 --- a/jetty-memcached/jetty-memcached-sessions/src/main/java/org/eclipse/jetty/memcached/session/MemcachedSessionDataMap.java +++ b/jetty-memcached/jetty-memcached-sessions/src/main/java/org/eclipse/jetty/memcached/session/MemcachedSessionDataMap.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.memcached.session; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.util.List; @@ -25,12 +26,14 @@ import java.util.List; import org.eclipse.jetty.server.session.SessionContext; import org.eclipse.jetty.server.session.SessionData; import org.eclipse.jetty.server.session.SessionDataMap; +import org.eclipse.jetty.util.ClassLoadingObjectInputStream; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.component.AbstractLifeCycle; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.XMemcachedClientBuilder; +import net.rubyeye.xmemcached.transcoders.SerializingTranscoder; @@ -50,6 +53,39 @@ public class MemcachedSessionDataMap extends AbstractLifeCycle implements Sessio protected XMemcachedClientBuilder _builder; + /** + * SessionDataTranscoder + * + * We override memcached deserialization to use our classloader-aware + * ObjectInputStream. + */ + public static class SessionDataTranscoder extends SerializingTranscoder + { + + @Override + protected Object deserialize(byte[] in) + { + Object rv = null; + + if (in != null) + { + try (ByteArrayInputStream bis = new ByteArrayInputStream(in); + ClassLoadingObjectInputStream is = new ClassLoadingObjectInputStream(bis)) + { + rv = is.readObject(); + } + catch (IOException e) + { + log.error("Caught IOException decoding " + in.length + " bytes of data", e); + } + catch (ClassNotFoundException e) + { + log.error("Caught CNFE decoding " + in.length + " bytes of data", e); + } + } + return rv; + } + } /** @@ -117,14 +153,12 @@ public class MemcachedSessionDataMap extends AbstractLifeCycle implements Sessio } - /** - * @see org.eclipse.jetty.server.session.SessionDataMap#initialize(org.eclipse.jetty.server.session.SessionContext) - */ @Override public void initialize(SessionContext context) { try { + _builder.setTranscoder(new SessionDataTranscoder ()); _client = _builder.build(); _client.setEnableHeartBeat(isHeartbeats()); } @@ -134,9 +168,7 @@ public class MemcachedSessionDataMap extends AbstractLifeCycle implements Sessio } } - /** - * @see org.eclipse.jetty.server.session.SessionDataMap#load(java.lang.String) - */ + @Override public SessionData load(String id) throws Exception { @@ -145,19 +177,13 @@ public class MemcachedSessionDataMap extends AbstractLifeCycle implements Sessio } - /** - * @see org.eclipse.jetty.server.session.SessionDataMap#store(java.lang.String, org.eclipse.jetty.server.session.SessionData) - */ @Override public void store(String id, SessionData data) throws Exception { _client.set(id, _expirySec, data); } - - /** - * @see org.eclipse.jetty.server.session.SessionDataMap#delete(java.lang.String) - */ + @Override public boolean delete(String id) throws Exception { @@ -176,7 +202,6 @@ public class MemcachedSessionDataMap extends AbstractLifeCycle implements Sessio _client.shutdown(); _client = null; } - } - + } } diff --git a/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.java b/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.java index 106df261ec1..80e456f4aac 100644 --- a/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.java +++ b/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.java @@ -19,15 +19,19 @@ package org.eclipse.jetty.nosql.mongodb; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; -import java.util.concurrent.atomic.AtomicReference; import org.eclipse.jetty.nosql.NoSqlSessionDataStore; import org.eclipse.jetty.server.session.SessionContext; import org.eclipse.jetty.server.session.SessionData; import org.eclipse.jetty.server.session.UnreadableSessionDataException; +import org.eclipse.jetty.util.ClassLoadingObjectInputStream; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.log.Log; @@ -86,9 +90,9 @@ import com.mongodb.WriteResult; * *

* In MongoDB, the nesting level is indicated by "." separators for the key name. Thus to - * interact with a session attribute, the key is composed of: - * "context".unique_context_name.attribute_name - * Eg "context"."0_0_0_0:_testA"."A" + * interact with session fields, the key is composed of: + * "context".unique_context_name.field_name + * Eg "context"."0_0_0_0:_testA"."lastSaved" * * */ @@ -128,6 +132,8 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore public final static String __LAST_ACCESSED = "lastAccessed"; + public final static String __ATTRIBUTES = "attributes"; + /** * Time this session will expire, based on last access time and maxIdle */ @@ -178,107 +184,98 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore } - - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#load(String) - */ @Override - public SessionData load(String id) throws Exception + public SessionData doLoad(String id) throws Exception { - final AtomicReference reference = new AtomicReference<>(); - final AtomicReference exception = new AtomicReference<>(); - Runnable r = new Runnable() + DBObject sessionDocument = _dbSessions.findOne(new BasicDBObject(__ID, id)); + + try { - @Override - public void run () + if (LOG.isDebugEnabled()) + LOG.debug("id={} loaded={}", id, sessionDocument); + + if (sessionDocument == null) + return null; + + Boolean valid = (Boolean)sessionDocument.get(__VALID); + + if (LOG.isDebugEnabled()) + LOG.debug("id={} valid={}", id, valid); + if (valid == null || !valid) + return null; + + Object version = MongoUtils.getNestedValue(sessionDocument, getContextSubfield(__VERSION)); + Long lastSaved = (Long)MongoUtils.getNestedValue(sessionDocument, getContextSubfield(__LASTSAVED)); + String lastNode = (String)MongoUtils.getNestedValue(sessionDocument, getContextSubfield(__LASTNODE)); + byte[] attributes = (byte[])MongoUtils.getNestedValue(sessionDocument, getContextSubfield(__ATTRIBUTES)); + + Long created = (Long)sessionDocument.get(__CREATED); + Long accessed = (Long)sessionDocument.get(__ACCESSED); + Long lastAccessed = (Long)sessionDocument.get(__LAST_ACCESSED); + Long maxInactive = (Long)sessionDocument.get(__MAX_IDLE); + Long expiry = (Long)sessionDocument.get(__EXPIRY); + + NoSqlSessionData data = null; + + // get the session for the context + DBObject sessionSubDocumentForContext = (DBObject)MongoUtils.getNestedValue(sessionDocument,getContextField()); + + if (LOG.isDebugEnabled()) LOG.debug("attrs {}", sessionSubDocumentForContext); + + if (sessionSubDocumentForContext != null) { - try + if (LOG.isDebugEnabled()) + LOG.debug("Session {} present for context {}", id, _context); + + //only load a session if it exists for this context + data = (NoSqlSessionData)newSessionData(id, created, accessed, (lastAccessed == null? accessed:lastAccessed), maxInactive); + data.setVersion(version); + data.setExpiry(expiry); + data.setContextPath(_context.getCanonicalContextPath()); + data.setVhost(_context.getVhost()); + data.setLastSaved(lastSaved); + data.setLastNode(lastNode); + + if (attributes == null) { - DBObject sessionDocument = _dbSessions.findOne(new BasicDBObject(__ID, id)); - - if (LOG.isDebugEnabled()) - LOG.debug("id={} loaded={}", id, sessionDocument); - - if (sessionDocument == null) - return; - - Boolean valid = (Boolean)sessionDocument.get(__VALID); - - if (LOG.isDebugEnabled()) - LOG.debug("id={} valid={}", id, valid); - if (valid == null || !valid) - return; - - Object version = MongoUtils.getNestedValue(sessionDocument, getContextSubfield(__VERSION)); - Long lastSaved = (Long)MongoUtils.getNestedValue(sessionDocument, getContextSubfield(__LASTSAVED)); - String lastNode = (String)MongoUtils.getNestedValue(sessionDocument, getContextSubfield(__LASTNODE)); - - Long created = (Long)sessionDocument.get(__CREATED); - Long accessed = (Long)sessionDocument.get(__ACCESSED); - Long lastAccessed = (Long)sessionDocument.get(__LAST_ACCESSED); - Long maxInactive = (Long)sessionDocument.get(__MAX_IDLE); - Long expiry = (Long)sessionDocument.get(__EXPIRY); - - NoSqlSessionData data = null; - - // get the session for the context - DBObject sessionSubDocumentForContext = (DBObject)MongoUtils.getNestedValue(sessionDocument,getContextField()); - - if (LOG.isDebugEnabled()) LOG.debug("attrs {}", sessionSubDocumentForContext); - - if (sessionSubDocumentForContext != null) + //legacy attribute storage format: the attributes are all fields in the document + Map map = new HashMap<>(); + for (String name : sessionSubDocumentForContext.keySet()) { - if (LOG.isDebugEnabled()) - LOG.debug("Session {} present for context {}", id, _context); - - //only load a session if it exists for this context - data = (NoSqlSessionData)newSessionData(id, created, accessed, (lastAccessed == null? accessed:lastAccessed), maxInactive); - data.setVersion(version); - data.setExpiry(expiry); - data.setContextPath(_context.getCanonicalContextPath()); - data.setVhost(_context.getVhost()); - data.setLastSaved(lastSaved); - data.setLastNode(lastNode); - - HashMap attributes = new HashMap<>(); - for (String name : sessionSubDocumentForContext.keySet()) - { - //skip special metadata attribute which is not one of the actual session attributes - if ( __METADATA.equals(name) ) - continue; - String attr = MongoUtils.decodeName(name); - Object value = MongoUtils.decodeValue(sessionSubDocumentForContext.get(name)); - attributes.put(attr,value); - } - - data.putAllAttributes(attributes); + //skip special metadata attribute which is not one of the actual session attributes + if ( __METADATA.equals(name) ) + continue; + String attr = MongoUtils.decodeName(name); + Object value = MongoUtils.decodeValue(sessionSubDocumentForContext.get(name)); + map.put(attr,value); } - else - { - if (LOG.isDebugEnabled()) - LOG.debug("Session {} not present for context {}", id, _context); - } - - reference.set(data); + data.putAllAttributes(map); } - catch (Exception e) + else { - exception.set(new UnreadableSessionDataException(id, _context, e)); + //attributes have special serialized format + try (ByteArrayInputStream bais = new ByteArrayInputStream(attributes); + ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(bais);) + { + SessionData.deserializeAttributes(data, ois); + } } } - }; - - _context.run(r); - - if (exception.get() != null) - throw exception.get(); - - return reference.get(); + else + { + if (LOG.isDebugEnabled()) + LOG.debug("Session {} not present for context {}", id, _context); + } + + return data; + } + catch (Exception e) + { + throw (new UnreadableSessionDataException(id, _context, e)); + } } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#delete(String) - */ + @Override public boolean delete(String id) throws Exception { @@ -290,7 +287,7 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore * associated with this session */ BasicDBObject mongoKey = new BasicDBObject(__ID, id); - + //DBObject sessionDocument = _dbSessions.findOne(mongoKey,_version_1); DBObject sessionDocument = _dbSessions.findOne(new BasicDBObject(__ID, id)); @@ -336,9 +333,6 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String) - */ @Override public boolean exists(String id) throws Exception { @@ -371,9 +365,6 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#getExpired(Set) - */ @Override public Set doGetExpired(Set candidates) { @@ -459,9 +450,7 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore return expiredSessions; } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#initialize(org.eclipse.jetty.server.session.SessionContext) - */ + public void initialize (SessionContext context) throws Exception { if (isStarted()) @@ -470,9 +459,7 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore ensureIndexes(); } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(String, SessionData, long) - */ + @Override public void doStore(String id, SessionData data, long lastSaveTime) throws Exception { @@ -483,7 +470,7 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore BasicDBObject update = new BasicDBObject(); boolean upsert = false; BasicDBObject sets = new BasicDBObject(); - BasicDBObject unsets = new BasicDBObject(); + Object version = ((NoSqlSessionData)data).getVersion(); @@ -532,29 +519,17 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore sets.put(__ACCESSED, data.getAccessed()); sets.put(__LAST_ACCESSED, data.getLastAccessed()); - - Set names = ((NoSqlSessionData)data).takeDirtyAttributes(); - - if (lastSaveTime <= 0) - { - names.addAll(((NoSqlSessionData)data).getAllAttributeNames()); // note dirty may include removed names - } - - - for (String name : names) + + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos);) { - Object value = data.getAttribute(name); - if (value == null) - unsets.put(getContextField() + "." + MongoUtils.encodeName(name),1); - else - sets.put(getContextField() + "." + MongoUtils.encodeName(name), MongoUtils.encodeName(value)); + SessionData.serializeAttributes(data, oos); + sets.put(getContextSubfield(__ATTRIBUTES), baos.toByteArray()); } // Do the upsert if (!sets.isEmpty()) update.put("$set",sets); - if (!unsets.isEmpty()) - update.put("$unset",unsets); WriteResult res = _dbSessions.update(key,update,upsert,false,WriteConcern.SAFE); if (LOG.isDebugEnabled()) @@ -584,9 +559,6 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore //TODO perhaps index on expiry time? } - - - /*------------------------------------------------------------ */ private String getContextField () { return __CONTEXT + "." + getCanonicalContextId(); @@ -613,9 +585,6 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating() - */ @ManagedAttribute(value="does store serialize sessions", readonly=true) @Override public boolean isPassivating() @@ -624,14 +593,11 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#toString() - */ + @Override public String toString() { return String.format("%s[collection=%s]", super.toString(),getDBCollection()); } - } diff --git a/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/SessionAuthentication.java b/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/SessionAuthentication.java index 016564c578f..1b1ee3dddb0 100644 --- a/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/SessionAuthentication.java +++ b/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/SessionAuthentication.java @@ -59,6 +59,15 @@ public class SessionAuthentication extends AbstractUserAuthentication implements } + @Override + public UserIdentity getUserIdentity() + { + if (_userIdentity == null) + throw new IllegalStateException("!UserIdentity"); + return super.getUserIdentity(); + } + + private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { @@ -66,10 +75,17 @@ public class SessionAuthentication extends AbstractUserAuthentication implements SecurityHandler security=SecurityHandler.getCurrentSecurityHandler(); if (security==null) - throw new IllegalStateException("!SecurityHandler"); + { + if (LOG.isDebugEnabled()) LOG.debug("!SecurityHandler"); + return; + } + LoginService login_service=security.getLoginService(); if (login_service==null) - throw new IllegalStateException("!LoginService"); + { + if (LOG.isDebugEnabled()) LOG.debug("!LoginService"); + return; + } _userIdentity=login_service.login(_name,_credentials, null); LOG.debug("Deserialized and relogged in {}",this); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSessionDataStore.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSessionDataStore.java index 0d06bf9e626..95037c85e47 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSessionDataStore.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSessionDataStore.java @@ -22,6 +22,7 @@ package org.eclipse.jetty.server.session; import java.util.Set; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; @@ -53,6 +54,16 @@ public abstract class AbstractSessionDataStore extends ContainerLifeCycle implem * @throws Exception if unable to store data */ public abstract void doStore(String id, SessionData data, long lastSaveTime) throws Exception; + + /** + * Load the session from persistent store. + * + * @param id the id of the session to load + * @return the re-inflated session + * + * @throws Exception if unable to load the session + */ + public abstract SessionData doLoad (String id) throws Exception; /** @@ -64,10 +75,6 @@ public abstract class AbstractSessionDataStore extends ContainerLifeCycle implem */ public abstract Set doGetExpired (Set candidates); - - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#initialize(org.eclipse.jetty.server.session.SessionContext) - */ @Override public void initialize (SessionContext context) throws Exception { @@ -76,47 +83,84 @@ public abstract class AbstractSessionDataStore extends ContainerLifeCycle implem _context = context; } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#store(java.lang.String, org.eclipse.jetty.server.session.SessionData) - */ + + + @Override + public SessionData load(String id) throws Exception + { + final AtomicReference reference = new AtomicReference(); + final AtomicReference exception = new AtomicReference(); + + Runnable r = new Runnable() + { + @Override + public void run () + { + try + { + reference.set(doLoad(id)); + } + catch (Exception e) + { + exception.set(e); + } + } + }; + + _context.run(r); + if (exception.get() != null) + throw exception.get(); + + return reference.get(); + } + + @Override public void store(String id, SessionData data) throws Exception { if (data == null) return; + final AtomicReference exception = new AtomicReference(); - long lastSave = data.getLastSaved(); - long savePeriodMs = (_savePeriodSec <=0? 0: TimeUnit.SECONDS.toMillis(_savePeriodSec)); - - if (LOG.isDebugEnabled()) - LOG.debug("Store: id={}, dirty={}, lsave={}, period={}, elapsed={}", id,data.isDirty(), data.getLastSaved(), savePeriodMs, (System.currentTimeMillis()-lastSave)); - - //save session if attribute changed or never been saved or time between saves exceeds threshold - if (data.isDirty() || (lastSave <= 0) || ((System.currentTimeMillis()-lastSave) > savePeriodMs)) + Runnable r = new Runnable() { - //set the last saved time to now - data.setLastSaved(System.currentTimeMillis()); - try + @Override + public void run () { - //call the specific store method, passing in previous save time - doStore(id, data, lastSave); - data.setDirty(false); //only undo the dirty setting if we saved it - } - catch (Exception e) - { - //reset last save time if save failed - data.setLastSaved(lastSave); - throw e; - } - } + long lastSave = data.getLastSaved(); + long savePeriodMs = (_savePeriodSec <=0? 0: TimeUnit.SECONDS.toMillis(_savePeriodSec)); + + if (LOG.isDebugEnabled()) + LOG.debug("Store: id={}, dirty={}, lsave={}, period={}, elapsed={}", id,data.isDirty(), data.getLastSaved(), savePeriodMs, (System.currentTimeMillis()-lastSave)); + + //save session if attribute changed or never been saved or time between saves exceeds threshold + if (data.isDirty() || (lastSave <= 0) || ((System.currentTimeMillis()-lastSave) > savePeriodMs)) + { + //set the last saved time to now + data.setLastSaved(System.currentTimeMillis()); + try + { + //call the specific store method, passing in previous save time + doStore(id, data, lastSave); + data.setDirty(false); //only undo the dirty setting if we saved it + } + catch (Exception e) + { + //reset last save time if save failed + data.setLastSaved(lastSave); + exception.set(e); + } + } + }; + }; + + _context.run(r); + if (exception.get() != null) + throw exception.get(); } - - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#getExpired(java.util.Set) - */ @Override public Set getExpired(Set candidates) { @@ -131,11 +175,6 @@ public abstract class AbstractSessionDataStore extends ContainerLifeCycle implem } - - - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#newSessionData(java.lang.String, long, long, long, long) - */ @Override public SessionData newSessionData(String id, long created, long accessed, long lastAccessed, long maxInactiveMs) { @@ -201,10 +240,6 @@ public abstract class AbstractSessionDataStore extends ContainerLifeCycle implem _savePeriodSec = savePeriodSec; } - - /** - * @see java.lang.Object#toString() - */ @Override public String toString() { diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/FileSessionDataStore.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/FileSessionDataStore.java index 4afee55c473..82b2f7edbc4 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/FileSessionDataStore.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/FileSessionDataStore.java @@ -31,15 +31,11 @@ import java.io.OutputStream; import java.nio.file.FileVisitOption; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; import org.eclipse.jetty.util.ClassLoadingObjectInputStream; import org.eclipse.jetty.util.MultiException; @@ -277,80 +273,53 @@ public class FileSessionDataStore extends AbstractSessionDataStore } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#load(java.lang.String) - */ + @Override - public SessionData load(String id) throws Exception + public SessionData doLoad(String id) throws Exception { - final AtomicReference reference = new AtomicReference(); - final AtomicReference exception = new AtomicReference(); - Runnable r = new Runnable() + //load session info from its file + String idWithContext = getIdWithContext(id); + String filename = _sessionFileMap.get(idWithContext); + if (filename == null) { - @Override - public void run () + if (LOG.isDebugEnabled()) + LOG.debug("Unknown file {}",idWithContext); + return null; + } + File file = new File (_storeDir, filename); + if (!file.exists()) + { + if (LOG.isDebugEnabled()) + LOG.debug("No such file {}",filename); + return null; + } + + try (FileInputStream in = new FileInputStream(file)) + { + SessionData data = load(in, id); + data.setLastSaved(file.lastModified()); + return data; + } + catch (UnreadableSessionDataException e) + { + if (isDeleteUnrestorableFiles() && file.exists() && file.getParentFile().equals(_storeDir)) { - //load session info from its file - String idWithContext = getIdWithContext(id); - String filename = _sessionFileMap.get(idWithContext); - if (filename == null) + try { - if (LOG.isDebugEnabled()) - LOG.debug("Unknown file {}",idWithContext); - return; + delete(id); + LOG.warn("Deleted unrestorable file for session {}", id); } - File file = new File (_storeDir, filename); - if (!file.exists()) + catch (Exception x) { - if (LOG.isDebugEnabled()) - LOG.debug("No such file {}",filename); - return; - } - - try (FileInputStream in = new FileInputStream(file)) - { - SessionData data = load(in, id); - data.setLastSaved(file.lastModified()); - reference.set(data); - } - catch (UnreadableSessionDataException e) - { - if (isDeleteUnrestorableFiles() && file.exists() && file.getParentFile().equals(_storeDir)) - { - try - { - delete(id); - LOG.warn("Deleted unrestorable file for session {}", id); - } - catch (Exception x) - { - LOG.warn("Unable to delete unrestorable file {} for session {}", filename, id, x); - } - } - - exception.set(e); - } - catch (Exception e) - { - exception.set(e); - } + LOG.warn("Unable to delete unrestorable file {} for session {}", filename, id, x); + } } - }; - - //ensure this runs with the context classloader set - _context.run(r); - - if (exception.get() != null) - throw exception.get(); - - return reference.get(); + throw e; + } } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(java.lang.String, org.eclipse.jetty.server.session.SessionData, long) - */ @Override public void doStore(String id, SessionData data, long lastSaveTime) throws Exception { @@ -473,10 +442,6 @@ public class FileSessionDataStore extends AbstractSessionDataStore } - - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating() - */ @Override @ManagedAttribute(value="are sessions serialized by this store", readonly=true) public boolean isPassivating() @@ -485,11 +450,7 @@ public class FileSessionDataStore extends AbstractSessionDataStore } - - - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String) - */ + @Override public boolean exists(String id) throws Exception { @@ -507,8 +468,10 @@ public class FileSessionDataStore extends AbstractSessionDataStore return (expiry > System.currentTimeMillis()); //hasn't yet expired } - /* ------------------------------------------------------------ */ + /** + * Save the session data. + * * @param os the output stream to save to * @param id identity of the session * @param data the info of the session @@ -528,14 +491,8 @@ public class FileSessionDataStore extends AbstractSessionDataStore out.writeLong(data.getExpiry()); out.writeLong(data.getMaxInactiveMs()); - List keys = new ArrayList(data.getKeys()); - out.writeInt(keys.size()); ObjectOutputStream oos = new ObjectOutputStream(out); - for (String name:keys) - { - oos.writeUTF(name); - oos.writeObject(data.getAttribute(name)); - } + SessionData.serializeAttributes(data, oos); } @@ -650,7 +607,9 @@ public class FileSessionDataStore extends AbstractSessionDataStore /** - * @param is inputstream containing session data + * Load the session data from a file. + * + * @param is file input stream containing session data * @param expectedId the id we've been told to load * @return the session data * @throws Exception @@ -685,8 +644,8 @@ public class FileSessionDataStore extends AbstractSessionDataStore data.setMaxInactiveMs(maxIdle); // Attributes - restoreAttributes(di, di.readInt(), data); - + ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is); + SessionData.deserializeAttributes(data, ois); return data; } catch (Exception e) @@ -695,39 +654,10 @@ public class FileSessionDataStore extends AbstractSessionDataStore } } - /** - * @param is inputstream containing session data - * @param size number of attributes - * @param data the data to restore to - * @throws Exception - */ - protected void restoreAttributes (InputStream is, int size, SessionData data) - throws Exception - { - if (size>0) - { - // input stream should not be closed here - Map attributes = new HashMap(); - ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is); - for (int i=0; i reference = new AtomicReference(); - final AtomicReference exception = new AtomicReference(); - - Runnable r = new Runnable() + try (Connection connection = _dbAdaptor.getConnection(); + PreparedStatement statement = _sessionTableSchema.getLoadStatement(connection, id, _context); + ResultSet result = statement.executeQuery()) { - @Override - public void run () - { - try (Connection connection = _dbAdaptor.getConnection(); - PreparedStatement statement = _sessionTableSchema.getLoadStatement(connection, id, _context); - ResultSet result = statement.executeQuery()) - { - SessionData data = null; - if (result.next()) - { - data = newSessionData(id, - result.getLong(_sessionTableSchema.getCreateTimeColumn()), - result.getLong(_sessionTableSchema.getAccessTimeColumn()), - result.getLong(_sessionTableSchema.getLastAccessTimeColumn()), - result.getLong(_sessionTableSchema.getMaxIntervalColumn())); - data.setCookieSet(result.getLong(_sessionTableSchema.getCookieTimeColumn())); - data.setLastNode(result.getString(_sessionTableSchema.getLastNodeColumn())); - data.setLastSaved(result.getLong(_sessionTableSchema.getLastSavedTimeColumn())); - data.setExpiry(result.getLong(_sessionTableSchema.getExpiryTimeColumn())); - data.setContextPath(_context.getCanonicalContextPath()); - data.setVhost(_context.getVhost()); + SessionData data = null; + if (result.next()) + { + data = newSessionData(id, + result.getLong(_sessionTableSchema.getCreateTimeColumn()), + result.getLong(_sessionTableSchema.getAccessTimeColumn()), + result.getLong(_sessionTableSchema.getLastAccessTimeColumn()), + result.getLong(_sessionTableSchema.getMaxIntervalColumn())); + data.setCookieSet(result.getLong(_sessionTableSchema.getCookieTimeColumn())); + data.setLastNode(result.getString(_sessionTableSchema.getLastNodeColumn())); + data.setLastSaved(result.getLong(_sessionTableSchema.getLastSavedTimeColumn())); + data.setExpiry(result.getLong(_sessionTableSchema.getExpiryTimeColumn())); + data.setContextPath(_context.getCanonicalContextPath()); + data.setVhost(_context.getVhost()); - try (InputStream is = _dbAdaptor.getBlobInputStream(result, _sessionTableSchema.getMapColumn()); - ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is)) - { - Object o = ois.readObject(); - data.putAllAttributes((Map)o); - } - catch (Exception e) - { - throw new UnreadableSessionDataException (id, _context, e); - } - - if (LOG.isDebugEnabled()) - LOG.debug("LOADED session {}", data); - } - else - if (LOG.isDebugEnabled()) - LOG.debug("No session {}", id); - - reference.set(data); + try (InputStream is = _dbAdaptor.getBlobInputStream(result, _sessionTableSchema.getMapColumn()); + ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is)) + { + SessionData.deserializeAttributes(data, ois); } catch (Exception e) { - exception.set(e); + throw new UnreadableSessionDataException (id, _context, e); } + + if (LOG.isDebugEnabled()) + LOG.debug("LOADED session {}", data); } - }; + else + if (LOG.isDebugEnabled()) + LOG.debug("No session {}", id); - //ensure this runs with context classloader set - _context.run(r); - if (exception.get() != null) - throw exception.get(); - - return reference.get(); + return data; + } } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#delete(java.lang.String) - */ @Override public boolean delete(String id) throws Exception { @@ -712,10 +679,6 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore - - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(String, SessionData, long) - */ @Override public void doStore(String id, SessionData data, long lastSaveTime) throws Exception { @@ -762,17 +725,17 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore statement.setLong(10, data.getExpiry()); statement.setLong(11, data.getMaxInactiveMs()); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(data.getAllAttributes()); - oos.flush(); - byte[] bytes = baos.toByteArray(); - - ByteArrayInputStream bais = new ByteArrayInputStream(bytes); - statement.setBinaryStream(12, bais, bytes.length);//attribute map as blob - statement.executeUpdate(); - if (LOG.isDebugEnabled()) - LOG.debug("Inserted session "+data); + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos)) + { + SessionData.serializeAttributes(data, oos); + byte[] bytes = baos.toByteArray(); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + statement.setBinaryStream(12, bais, bytes.length);//attribute map as blob + statement.executeUpdate(); + if (LOG.isDebugEnabled()) + LOG.debug("Inserted session "+data); + } } } } @@ -793,26 +756,25 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore statement.setLong(5, data.getExpiry()); statement.setLong(6, data.getMaxInactiveMs()); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(data.getAllAttributes()); - oos.flush(); - byte[] bytes = baos.toByteArray(); - ByteArrayInputStream bais = new ByteArrayInputStream(bytes); - statement.setBinaryStream(7, bais, bytes.length);//attribute map as blob - statement.executeUpdate(); + try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos)) + { + SessionData.serializeAttributes(data, oos); + byte[] bytes = baos.toByteArray(); + try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) + { + statement.setBinaryStream(7, bais, bytes.length);//attribute map as blob + statement.executeUpdate(); - if (LOG.isDebugEnabled()) - LOG.debug("Updated session "+data); + if (LOG.isDebugEnabled()) + LOG.debug("Updated session "+data); + } + } } } } - - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#getExpired(Set) - */ @Override public Set doGetExpired(Set candidates) { @@ -940,10 +902,6 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore - - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating() - */ @Override @ManagedAttribute(value="does this store serialize sessions", readonly=true) public boolean isPassivating() @@ -954,9 +912,6 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String) - */ @Override public boolean exists(String id) throws Exception @@ -988,11 +943,3 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore } } } - - - - - - - - diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/NullSessionDataStore.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/NullSessionDataStore.java index b27e17e6f87..f1c9914a826 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/NullSessionDataStore.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/NullSessionDataStore.java @@ -33,28 +33,20 @@ import org.eclipse.jetty.util.annotation.ManagedObject; public class NullSessionDataStore extends AbstractSessionDataStore { - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#load(java.lang.String) - */ @Override - public SessionData load(String id) throws Exception + public SessionData doLoad(String id) throws Exception { return null; } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#newSessionData(java.lang.String, long, long, long, long) - */ @Override public SessionData newSessionData(String id, long created, long accessed, long lastAccessed, long maxInactiveMs) { return new SessionData(id, _context.getCanonicalContextPath(), _context.getVhost(), created, accessed, lastAccessed, maxInactiveMs); } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#delete(java.lang.String) - */ + @Override public boolean delete(String id) throws Exception { @@ -62,9 +54,7 @@ public class NullSessionDataStore extends AbstractSessionDataStore } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(java.lang.String, org.eclipse.jetty.server.session.SessionData, long) - */ + @Override public void doStore(String id, SessionData data, long lastSaveTime) throws Exception { @@ -72,9 +62,7 @@ public class NullSessionDataStore extends AbstractSessionDataStore } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#getExpired(Set) - */ + @Override public Set doGetExpired(Set candidates) { @@ -82,9 +70,7 @@ public class NullSessionDataStore extends AbstractSessionDataStore } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating() - */ + @ManagedAttribute(value="does this store serialize sessions", readonly=true) @Override public boolean isPassivating() @@ -93,9 +79,7 @@ public class NullSessionDataStore extends AbstractSessionDataStore } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String) - */ + @Override public boolean exists(String id) { diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionData.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionData.java index 6d9e1f91a2f..6a70f16a53b 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionData.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionData.java @@ -23,9 +23,11 @@ import java.io.IOException; import java.io.Serializable; import java.util.Collections; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import org.eclipse.jetty.util.ClassLoadingObjectInputStream; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -57,6 +59,85 @@ public class SessionData implements Serializable protected boolean _dirty; protected long _lastSaved; //time in msec since last save + + /** + * Serialize the attribute map of the session. + * + * This special handling allows us to record which classloader should be used to load the value of the + * attribute: either the container classloader (which could be the application loader ie null, or jetty's + * startjar loader) or the webapp's classloader. + * + * @param data the SessionData for which to serialize the attributes + * @param out the stream to which to serialize + * @throws IOException + */ + public static void serializeAttributes (SessionData data, java.io.ObjectOutputStream out) + throws IOException + { + int entries = data._attributes.size(); + out.writeObject(entries); + for (Entry entry: data._attributes.entrySet()) + { + out.writeUTF(entry.getKey()); + ClassLoader loader = entry.getValue().getClass().getClassLoader(); + boolean isServerLoader = false; + + if (loader == Thread.currentThread().getContextClassLoader()) //is it the webapp classloader? + isServerLoader = false; + else if (loader == Thread.currentThread().getContextClassLoader().getParent() || loader == SessionData.class.getClassLoader() || loader == null) // is it the container loader? + isServerLoader = true; + else + throw new IOException ("Unknown loader"); // we don't know what loader to use + + out.writeBoolean(isServerLoader); + out.writeObject(entry.getValue()); + } + } + + /** + * De-serialize the attribute map of a session. + * + * When the session was serialized, we will have recorded which classloader should be used to + * recover the attribute value. The classloader could be the container classloader, or the + * webapp classloader. + * + * @param data the SessionData for which to deserialize the attribute map + * @param in the serialized stream + * @throws IOException + * @throws ClassNotFoundException + */ + public static void deserializeAttributes (SessionData data, java.io.ObjectInputStream in) + throws IOException, ClassNotFoundException + { + Object o = in.readObject(); + if (o instanceof Integer) + { + //new serialization was used + if (!(ClassLoadingObjectInputStream.class.isAssignableFrom(in.getClass()))) + throw new IOException ("Not ClassLoadingObjectInputStream"); + + data._attributes = new ConcurrentHashMap<>(); + int entries = ((Integer)o).intValue(); + for (int i=0; i < entries; i++) + { + String name = in.readUTF(); //attribute name + boolean isServerClassLoader = in.readBoolean(); //use server or webapp classloader to load + + Object value = ((ClassLoadingObjectInputStream)in).readObject(isServerClassLoader?SessionData.class.getClassLoader():Thread.currentThread().getContextClassLoader()); + data._attributes.put(name, value); + } + } + else + { + LOG.info("Legacy serialization detected for {}", data.getId()); + //legacy serialization was used, we have just deserialized the + //entire attribute map + data._attributes = new ConcurrentHashMap(); + data.putAllAttributes((Map)o); + } + } + + public SessionData (String id, String cpath, String vhost, long created, long accessed, long lastAccessed, long maxInactiveMs) { this(id, cpath, vhost, created, accessed, lastAccessed, maxInactiveMs, new ConcurrentHashMap()); @@ -335,24 +416,21 @@ public class SessionData implements Serializable out.writeUTF(_id); //session id out.writeUTF(_contextPath); //context path out.writeUTF(_vhost); //first vhost - out.writeLong(_accessed);//accessTime out.writeLong(_lastAccessed); //lastAccessTime out.writeLong(_created); //time created out.writeLong(_cookieSet);//time cookie was set out.writeUTF(_lastNode); //name of last node managing - out.writeLong(_expiry); out.writeLong(_maxInactiveMs); - out.writeObject(_attributes); + serializeAttributes(this, out); } private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { _id = in.readUTF(); _contextPath = in.readUTF(); - _vhost = in.readUTF(); - + _vhost = in.readUTF(); _accessed = in.readLong();//accessTime _lastAccessed = in.readLong(); //lastAccessTime _created = in.readLong(); //time created @@ -360,7 +438,7 @@ public class SessionData implements Serializable _lastNode = in.readUTF(); //last managing node _expiry = in.readLong(); _maxInactiveMs = in.readLong(); - _attributes = (Map)in.readObject(); + deserializeAttributes(this, in); } public boolean isExpiredAt (long time) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/ClassLoadingObjectInputStream.java b/jetty-util/src/main/java/org/eclipse/jetty/util/ClassLoadingObjectInputStream.java index d3f2e1912ea..f7107e1fc11 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/ClassLoadingObjectInputStream.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/ClassLoadingObjectInputStream.java @@ -34,6 +34,20 @@ import java.lang.reflect.Proxy; */ public class ClassLoadingObjectInputStream extends ObjectInputStream { + + protected static class ClassLoaderThreadLocal extends ThreadLocal + { + protected static final ClassLoader UNSET = new ClassLoader() {}; + @Override + protected ClassLoader initialValue() + { + return UNSET; + } + + } + + private ThreadLocal _classloader = new ClassLoaderThreadLocal(); + /* ------------------------------------------------------------ */ public ClassLoadingObjectInputStream(java.io.InputStream in) throws IOException { @@ -46,13 +60,33 @@ public class ClassLoadingObjectInputStream extends ObjectInputStream super(); } + + public Object readObject (ClassLoader loader) + throws IOException, ClassNotFoundException + { + try + { + _classloader.set(loader); + return readObject(); + } + finally + { + _classloader.set(ClassLoaderThreadLocal.UNSET); + } + } + + /* ------------------------------------------------------------ */ @Override public Class resolveClass (java.io.ObjectStreamClass cl) throws IOException, ClassNotFoundException { try { - return Class.forName(cl.getName(), false, Thread.currentThread().getContextClassLoader()); + ClassLoader loader = _classloader.get(); + if (ClassLoaderThreadLocal.UNSET == loader) + loader = Thread.currentThread().getContextClassLoader(); + + return Class.forName(cl.getName(), false, loader); } catch (ClassNotFoundException e) { diff --git a/tests/test-sessions/test-file-sessions/src/test/java/org/eclipse/jetty/server/session/FileTestHelper.java b/tests/test-sessions/test-file-sessions/src/test/java/org/eclipse/jetty/server/session/FileTestHelper.java index 320cd7f7f8b..6757dd8abcb 100644 --- a/tests/test-sessions/test-file-sessions/src/test/java/org/eclipse/jetty/server/session/FileTestHelper.java +++ b/tests/test-sessions/test-file-sessions/src/test/java/org/eclipse/jetty/server/session/FileTestHelper.java @@ -31,9 +31,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.nio.file.Files; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; import java.util.Map; import org.eclipse.jetty.util.ClassLoadingObjectInputStream; @@ -161,9 +158,8 @@ public class FileTestHelper { String filename = ""+expiry+"_"+contextPath+"_"+vhost+"_"+id; File file = new File(_tmpDir, filename); - try(FileOutputStream fos = new FileOutputStream(file,false)) + try (FileOutputStream fos = new FileOutputStream(file,false); DataOutputStream out = new DataOutputStream(fos)) { - DataOutputStream out = new DataOutputStream(fos); out.writeUTF(id); out.writeUTF(contextPath); out.writeUTF(vhost); @@ -177,14 +173,9 @@ public class FileTestHelper if (attributes != null) { - List keys = new ArrayList(attributes.keySet()); - out.writeInt(keys.size()); + SessionData tmp = new SessionData(id,contextPath, vhost, created, accessed, lastAccessed, maxIdle); ObjectOutputStream oos = new ObjectOutputStream(out); - for (String name:keys) - { - oos.writeUTF(name); - oos.writeObject(attributes.get(name)); - } + SessionData.serializeAttributes(tmp, oos); } } } @@ -197,10 +188,8 @@ public class FileTestHelper File file = new File(_tmpDir, filename); assertTrue(file.exists()); - try (FileInputStream in = new FileInputStream(file)) + try (FileInputStream in = new FileInputStream(file); DataInputStream di = new DataInputStream(in)) { - DataInputStream di = new DataInputStream(in); - String id = di.readUTF(); String contextPath = di.readUTF(); String vhost = di.readUTF(); @@ -223,28 +212,18 @@ public class FileTestHelper assertEquals(data.getExpiry(), expiry); assertEquals(data.getMaxInactiveMs(), maxIdle); - Map attributes = new HashMap<>(); - - int size = di.readInt(); - if (size > 0) - { - ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(di); - for (int i=0; imaven-surefire-plugin true - GCloudTestSuite diff --git a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredOrphanedSessionTest.java b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredOrphanedSessionTest.java index fb192d31b60..6aea4e330cf 100644 --- a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredOrphanedSessionTest.java +++ b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredOrphanedSessionTest.java @@ -45,16 +45,10 @@ public class ClusteredOrphanedSessionTest extends AbstractClusteredOrphanedSessi @AfterAll public static void tearDown () throws Exception { + __testSupport.deleteSessions(); __testSupport.tearDown(); } - @AfterAll - public static void teardown () throws Exception - { - __testSupport.deleteSessions(); - } - - /** * @see org.eclipse.jetty.server.session.AbstractTestBase#createSessionDataStoreFactory() diff --git a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredSessionScavengingTest.java b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredSessionScavengingTest.java index 31393388e63..b4be6a648f7 100644 --- a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredSessionScavengingTest.java +++ b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredSessionScavengingTest.java @@ -43,14 +43,9 @@ public class ClusteredSessionScavengingTest extends AbstractClusteredSessionScav @AfterAll public static void tearDown () throws Exception - { - __testSupport.tearDown(); - } - - @AfterAll - public static void teardown () throws Exception { __testSupport.deleteSessions(); + __testSupport.tearDown(); } diff --git a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/GCloudSessionTestSupport.java b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/GCloudSessionTestSupport.java index 477524ed523..9842201f77c 100644 --- a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/GCloudSessionTestSupport.java +++ b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/GCloudSessionTestSupport.java @@ -130,36 +130,37 @@ public class GCloudSessionTestSupport long lastAccessed, long maxIdle, long expiry, long cookieset, long lastSaved, Map attributes) - throws Exception + throws Exception { //serialize the attribute map - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - if (attributes != null) + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(attributes); - oos.flush(); - } + if (attributes != null) + { + SessionData tmp = new SessionData(id, contextPath, vhost, created, accessed, lastAccessed, maxIdle); + ObjectOutputStream oos = new ObjectOutputStream(baos); + SessionData.serializeAttributes(tmp, oos); + } - //turn a session into an entity - Entity.Builder builder = Entity.newBuilder(_keyFactory.newKey(contextPath+"_"+vhost+"_"+id)) - .set(EntityDataModel.ID, id) - .set(EntityDataModel.CONTEXTPATH, contextPath) - .set(EntityDataModel.VHOST, vhost) - .set(EntityDataModel.ACCESSED, accessed) - .set(EntityDataModel.LASTACCESSED, lastAccessed) - .set(EntityDataModel.CREATETIME, created) - .set(EntityDataModel.COOKIESETTIME, cookieset) - .set(EntityDataModel.LASTNODE, lastNode) - .set(EntityDataModel.EXPIRY, expiry) - .set(EntityDataModel.MAXINACTIVE, maxIdle) - .set(EntityDataModel.LASTSAVED, lastSaved); - if (attributes != null) + //turn a session into an entity + Entity.Builder builder = Entity.newBuilder(_keyFactory.newKey(contextPath+"_"+vhost+"_"+id)) + .set(EntityDataModel.ID, id) + .set(EntityDataModel.CONTEXTPATH, contextPath) + .set(EntityDataModel.VHOST, vhost) + .set(EntityDataModel.ACCESSED, accessed) + .set(EntityDataModel.LASTACCESSED, lastAccessed) + .set(EntityDataModel.CREATETIME, created) + .set(EntityDataModel.COOKIESETTIME, cookieset) + .set(EntityDataModel.LASTNODE, lastNode) + .set(EntityDataModel.EXPIRY, expiry) + .set(EntityDataModel.MAXINACTIVE, maxIdle) + .set(EntityDataModel.LASTSAVED, lastSaved); + if (attributes != null) builder.set(EntityDataModel.ATTRIBUTES, BlobValue.newBuilder(Blob.copyFrom(baos.toByteArray())).setExcludeFromIndexes(true).build()); + Entity entity = builder.build(); - Entity entity = builder.build(); - - _ds.put(entity); + _ds.put(entity); + } } public boolean checkSessionPersisted (SessionData data) @@ -183,21 +184,27 @@ public class GCloudSessionTestSupport assertEquals(data.getMaxInactiveMs(), entity.getLong(EntityDataModel.MAXINACTIVE)); Blob blob = (Blob) entity.getBlob(EntityDataModel.ATTRIBUTES); - Map attributes = new HashMap<>(); + SessionData tmp = new SessionData(data.getId(),entity.getString(EntityDataModel.CONTEXTPATH), + entity.getString(EntityDataModel.VHOST), + entity.getLong(EntityDataModel.CREATETIME), + entity.getLong(EntityDataModel.ACCESSED), + entity.getLong(EntityDataModel.LASTACCESSED), + entity.getLong(EntityDataModel.MAXINACTIVE)); + try (ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(blob.asInputStream())) { - Object o = ois.readObject(); - attributes.putAll((Map)o); + SessionData.deserializeAttributes(tmp, ois); } + //same number of attributes - assertEquals(data.getAllAttributes().size(), attributes.size()); + assertEquals(data.getAllAttributes().size(), tmp.getAllAttributes().size()); //same keys - assertTrue(data.getKeys().equals(attributes.keySet())); + assertTrue(data.getKeys().equals(tmp.getAllAttributes().keySet())); //same values for (String name:data.getKeys()) { - assertTrue(data.getAttribute(name).equals(attributes.get(name))); + assertTrue(data.getAttribute(name).equals(tmp.getAttribute(name))); } return true; diff --git a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/InvalidationSessionTest.java b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/InvalidationSessionTest.java index 3e7809aa82a..837c7749524 100644 --- a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/InvalidationSessionTest.java +++ b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/InvalidationSessionTest.java @@ -45,14 +45,10 @@ public class InvalidationSessionTest extends AbstractClusteredInvalidationSessio @AfterAll public static void tearDown () throws Exception { + __testSupport.deleteSessions(); __testSupport.tearDown(); } - @AfterAll - public static void teardown () throws Exception - { - __testSupport.deleteSessions(); - } /** * @see org.eclipse.jetty.server.session.AbstractTestBase#createSessionDataStoreFactory() diff --git a/tests/test-sessions/test-hazelcast-sessions/src/test/java/org/eclipse/jetty/hazelcast/session/HazelcastTestHelper.java b/tests/test-sessions/test-hazelcast-sessions/src/test/java/org/eclipse/jetty/hazelcast/session/HazelcastTestHelper.java index f36df160add..c76761aad5c 100644 --- a/tests/test-sessions/test-hazelcast-sessions/src/test/java/org/eclipse/jetty/hazelcast/session/HazelcastTestHelper.java +++ b/tests/test-sessions/test-hazelcast-sessions/src/test/java/org/eclipse/jetty/hazelcast/session/HazelcastTestHelper.java @@ -27,6 +27,8 @@ import java.util.concurrent.TimeUnit; import com.hazelcast.config.JoinConfig; import com.hazelcast.config.MulticastConfig; import com.hazelcast.config.NetworkConfig; +import com.hazelcast.config.SerializerConfig; + import org.eclipse.jetty.server.session.SessionData; import org.eclipse.jetty.server.session.SessionDataStoreFactory; @@ -45,12 +47,21 @@ public class HazelcastTestHelper static final String _hazelcastInstanceName = "SESSION_TEST_"+Long.toString( TimeUnit.NANOSECONDS.toMillis(System.nanoTime())); static final String _name = Long.toString( TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) ); - static final HazelcastInstance _instance = Hazelcast.getOrCreateHazelcastInstance( new Config() // - .setInstanceName(_hazelcastInstanceName ) // - .setNetworkConfig( new NetworkConfig().setJoin( // - new JoinConfig().setMulticastConfig( // - new MulticastConfig().setEnabled( false ) ) ) ) // - .addMapConfig( new MapConfig().setName(_name) ) ); + + static SerializerConfig _serializerConfig; + + static HazelcastInstance _instance; + + static + { + _serializerConfig = new SerializerConfig().setImplementation(new SessionDataSerializer()).setTypeClass(SessionData.class); + Config config = new Config(); + config.setInstanceName(_hazelcastInstanceName ); + config.setNetworkConfig( new NetworkConfig().setJoin(new JoinConfig().setMulticastConfig(new MulticastConfig().setEnabled(false)))); + config.addMapConfig(new MapConfig().setName(_name)); + config.getSerializationConfig().addSerializerConfig(_serializerConfig); + _instance = Hazelcast.getOrCreateHazelcastInstance(config); + } public HazelcastTestHelper () { diff --git a/tests/test-sessions/test-infinispan-sessions/pom.xml b/tests/test-sessions/test-infinispan-sessions/pom.xml index e218d29a188..674c92d13d0 100644 --- a/tests/test-sessions/test-infinispan-sessions/pom.xml +++ b/tests/test-sessions/test-infinispan-sessions/pom.xml @@ -11,6 +11,7 @@ http://www.eclipse.org/jetty ${project.groupId}.sessions.infinispan + 127.0.0.1 @@ -114,6 +115,12 @@ 9.1.0.Final test + + org.infinispan + infinispan-remote-query-client + 9.1.0.Final + test + @@ -134,6 +141,9 @@ **/*.java + + ${hotrod.host} + diff --git a/tests/test-sessions/test-infinispan-sessions/src/test/java/org/eclipse/jetty/server/session/remote/RemoteInfinispanTestSupport.java b/tests/test-sessions/test-infinispan-sessions/src/test/java/org/eclipse/jetty/server/session/remote/RemoteInfinispanTestSupport.java index a960f2774fc..02e4faedd52 100644 --- a/tests/test-sessions/test-infinispan-sessions/src/test/java/org/eclipse/jetty/server/session/remote/RemoteInfinispanTestSupport.java +++ b/tests/test-sessions/test-infinispan-sessions/src/test/java/org/eclipse/jetty/server/session/remote/RemoteInfinispanTestSupport.java @@ -21,10 +21,21 @@ package org.eclipse.jetty.server.session.remote; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import java.util.Properties; import org.eclipse.jetty.server.session.SessionData; +import org.eclipse.jetty.session.infinispan.InfinispanSessionData; +import org.eclipse.jetty.session.infinispan.SessionDataMarshaller; +import org.infinispan.client.hotrod.configuration.ConfigurationBuilder; import org.infinispan.client.hotrod.RemoteCache; import org.infinispan.client.hotrod.RemoteCacheManager; +import org.infinispan.client.hotrod.marshall.ProtoStreamMarshaller; +import org.infinispan.protostream.FileDescriptorSource; +import org.infinispan.protostream.SerializationContext; + + /** * RemoteInfinispanTestSupport @@ -34,7 +45,7 @@ import org.infinispan.client.hotrod.RemoteCacheManager; public class RemoteInfinispanTestSupport { public static final String DEFAULT_CACHE_NAME = "session_test_cache"; - public RemoteCache _cache; + public RemoteCache _cache; private String _name; public static RemoteCacheManager _manager; @@ -42,11 +53,24 @@ public class RemoteInfinispanTestSupport { try { - _manager = new RemoteCacheManager(); + String host = System.getProperty("hotrod.host","127.0.0.1"); + Properties properties = new Properties(); + + ConfigurationBuilder clientBuilder = new ConfigurationBuilder(); + clientBuilder.withProperties(properties).addServer().host(host).marshaller(new ProtoStreamMarshaller()); + + _manager = new RemoteCacheManager(clientBuilder.build()); + + FileDescriptorSource fds = new FileDescriptorSource(); + fds.addProtoFiles("/session.proto"); + + SerializationContext serCtx = ProtoStreamMarshaller.getSerializationContext(_manager); + serCtx.registerProtoFiles(fds); + serCtx.registerMarshaller(new SessionDataMarshaller()); } catch (Exception e) { - e.printStackTrace(); + fail(e); } } @@ -65,7 +89,7 @@ public class RemoteInfinispanTestSupport - public RemoteCache getCache () + public RemoteCache getCache () { return _cache; } @@ -82,8 +106,7 @@ public class RemoteInfinispanTestSupport } - - @SuppressWarnings("unchecked") + public void createSession (SessionData data) throws Exception { @@ -111,7 +134,8 @@ public class RemoteInfinispanTestSupport if (obj == null) return false; - SessionData saved = (SessionData)obj; + InfinispanSessionData saved = (InfinispanSessionData)obj; + saved.deserializeAttributes(); assertEquals(data.getId(), saved.getId()); assertEquals(data.getContextPath(), saved.getContextPath()); diff --git a/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java b/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java index 36e074efce9..73f6466dab4 100644 --- a/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java +++ b/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java @@ -210,24 +210,26 @@ public class JdbcTestHelper assertEquals(data.getContextPath(), result.getString(CONTEXT_COL)); assertEquals(data.getVhost(), result.getString("virtualHost")); - Map attributes = new HashMap<>(); Blob blob = result.getBlob(MAP_COL); + SessionData tmp = new SessionData(data.getId(), data.getContextPath(), data.getVhost(), result.getLong(CREATE_COL), + result.getLong(ACCESS_COL), result.getLong(LAST_ACCESS_COL),result.getLong(MAX_IDLE_COL)); + try (InputStream is = blob.getBinaryStream(); ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is)) { - Object o = ois.readObject(); - attributes.putAll((Map)o); + + SessionData.deserializeAttributes(tmp, ois); } //same number of attributes - assertEquals(data.getAllAttributes().size(), attributes.size()); + assertEquals(data.getAllAttributes().size(), tmp.getAllAttributes().size()); //same keys - assertTrue(data.getKeys().equals(attributes.keySet())); + assertTrue(data.getKeys().equals(tmp.getAllAttributes().keySet())); //same values for (String name:data.getKeys()) { - assertTrue(data.getAttribute(name).equals(attributes.get(name))); + assertTrue(data.getAttribute(name).equals(tmp.getAttribute(name))); } } finally @@ -303,15 +305,18 @@ public class JdbcTestHelper if (attributes != null) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - Map emptyMap = Collections.emptyMap(); - oos.writeObject(emptyMap); - oos.flush(); - byte[] bytes = baos.toByteArray(); + SessionData tmp = new SessionData (id, contextPath, vhost, created, accessed, lastAccessed, maxIdle); + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos);) + { + SessionData.serializeAttributes(tmp, oos); + byte[] bytes = baos.toByteArray(); - ByteArrayInputStream bais = new ByteArrayInputStream(bytes); - statement.setBinaryStream(12, bais, bytes.length);//attribute map as blob + try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);) + { + statement.setBinaryStream(12, bais, bytes.length); + } + } } else statement.setBinaryStream(12, new ByteArrayInputStream("".getBytes()), 0); diff --git a/tests/test-sessions/test-memcached-sessions/pom.xml b/tests/test-sessions/test-memcached-sessions/pom.xml index 5655c1e91cc..5e1bb72f753 100644 --- a/tests/test-sessions/test-memcached-sessions/pom.xml +++ b/tests/test-sessions/test-memcached-sessions/pom.xml @@ -62,6 +62,12 @@ jetty-test-helper test + + org.slf4j + slf4j-simple + 1.7.9 + test + diff --git a/tests/test-sessions/test-memcached-sessions/src/test/java/org/eclipse/jetty/memcached/sessions/MemcachedTestHelper.java b/tests/test-sessions/test-memcached-sessions/src/test/java/org/eclipse/jetty/memcached/sessions/MemcachedTestHelper.java index ebe3590bf37..49a02bcd38f 100644 --- a/tests/test-sessions/test-memcached-sessions/src/test/java/org/eclipse/jetty/memcached/sessions/MemcachedTestHelper.java +++ b/tests/test-sessions/test-memcached-sessions/src/test/java/org/eclipse/jetty/memcached/sessions/MemcachedTestHelper.java @@ -48,29 +48,22 @@ public class MemcachedTestHelper private int _loadCount = 0; - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating() - */ @Override public boolean isPassivating() { return true; } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String) - */ + @Override public boolean exists(String id) throws Exception { return _store.get(id) != null; } - /** - * @see org.eclipse.jetty.server.session.SessionDataMap#load(java.lang.String) - */ + @Override - public SessionData load(String id) throws Exception + public SessionData doLoad(String id) throws Exception { _loadCount++; return _store.get(id); @@ -86,18 +79,12 @@ public class MemcachedTestHelper return _loadCount; } - /** - * @see org.eclipse.jetty.server.session.SessionDataMap#delete(java.lang.String) - */ @Override public boolean delete(String id) throws Exception { return (_store.remove(id) != null); } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(java.lang.String, org.eclipse.jetty.server.session.SessionData, long) - */ @Override public void doStore(String id, SessionData data, long lastSaveTime) throws Exception { @@ -105,9 +92,6 @@ public class MemcachedTestHelper } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doGetExpired(java.util.Set) - */ @Override public Set doGetExpired(Set candidates) { diff --git a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStoreTest.java b/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStoreTest.java index 7c2d6e61184..1fade684ab4 100644 --- a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStoreTest.java +++ b/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStoreTest.java @@ -19,11 +19,24 @@ package org.eclipse.jetty.nosql.mongodb; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jetty.server.session.AbstractSessionDataStoreFactory; import org.eclipse.jetty.server.session.AbstractSessionDataStoreTest; +import org.eclipse.jetty.server.session.SessionContext; import org.eclipse.jetty.server.session.SessionData; +import org.eclipse.jetty.server.session.SessionDataStore; import org.eclipse.jetty.server.session.SessionDataStoreFactory; +import org.eclipse.jetty.servlet.ServletContextHandler; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * MongoSessionDataStoreTest @@ -81,4 +94,55 @@ public class MongoSessionDataStoreTest extends AbstractSessionDataStoreTest { return MongoTestHelper.checkSessionPersisted(data); } + + + /** + * Test that a session stored in the legacy attribute + * format can be read. + * + * @throws Exception + */ + @Test + public void testReadLegacySession() throws Exception + { + //create the SessionDataStore + ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); + context.setContextPath("/legacy"); + SessionDataStoreFactory factory = createSessionDataStoreFactory(); + ((AbstractSessionDataStoreFactory)factory).setGracePeriodSec(GRACE_PERIOD_SEC); + SessionDataStore store = factory.getSessionDataStore(context.getSessionHandler()); + SessionContext sessionContext = new SessionContext("foo", context.getServletContext()); + store.initialize(sessionContext); + + //persist an old-style session + + Map attributes = new HashMap<>(); + attributes.put("attribute1", "attribute1value"); + attributes.put("attribute2", new ArrayList(Arrays.asList("1", "2", "3"))); + MongoTestHelper.createLegacySession("1234", + sessionContext.getCanonicalContextPath(), sessionContext.getVhost(), + "foo", + 1000L, System.currentTimeMillis()-1000L, System.currentTimeMillis()-2000L, + -1, -1, + attributes ); + + store.start(); + + //test that we can retrieve it + SessionData loaded = store.load("1234"); + assertNotNull(loaded); + assertEquals("1234", loaded.getId()); + assertEquals(1000L, loaded.getCreated()); + + assertEquals("attribute1value", loaded.getAttribute("attribute1")); + assertNotNull(loaded.getAttribute("attribute2")); + + + //test that we can write it + store.store("1234", loaded); + + //and that it has now been written out with the new format + MongoTestHelper.checkSessionPersisted(loaded); + + } } diff --git a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/MongoTestHelper.java b/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/MongoTestHelper.java index cd5698257e3..47c536ff7cc 100644 --- a/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/MongoTestHelper.java +++ b/tests/test-sessions/test-mongodb-sessions/src/test/java/org/eclipse/jetty/nosql/mongodb/MongoTestHelper.java @@ -22,26 +22,23 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.ObjectOutputStream; import java.net.UnknownHostException; -import java.sql.Date; -import java.util.HashMap; import java.util.Map; -import com.mongodb.MongoClient; -import org.eclipse.jetty.nosql.NoSqlSessionDataStore.NoSqlSessionData; import org.eclipse.jetty.server.session.SessionData; +import org.eclipse.jetty.util.ClassLoadingObjectInputStream; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.log.Logger; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.DBObject; -import com.mongodb.Mongo; +import com.mongodb.MongoClient; import com.mongodb.MongoException; import com.mongodb.WriteConcern; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; /** @@ -148,10 +145,9 @@ public class MongoTestHelper MongoSessionDataStore.__CONTEXT + "." + data.getVhost().replace('.', '_') + ":" + data.getContextPath() +"."+MongoSessionDataStore.__LASTSAVED); String lastNode = (String)MongoUtils.getNestedValue(sessionDocument, MongoSessionDataStore.__CONTEXT + "." + data.getVhost().replace('.', '_') + ":" + data.getContextPath() +"."+MongoSessionDataStore.__LASTNODE); - + byte[] attributes = (byte[])MongoUtils.getNestedValue(sessionDocument, + MongoSessionDataStore.__CONTEXT + "." + data.getVhost().replace('.', '_') + ":" + data.getContextPath() +"."+MongoSessionDataStore.__ATTRIBUTES); - LOG.info("DA:{} MA:{}", data.getAccessed(), accessed); - LOG.info("DLA:{} DLA:{}",data.getLastAccessed(),lastAccessed); assertEquals(data.getCreated(), created.longValue()); assertEquals(data.getAccessed(), accessed.longValue()); assertEquals(data.getLastAccessed(), lastAccessed.longValue()); @@ -168,26 +164,24 @@ public class MongoTestHelper assertNotNull(sessionSubDocumentForContext); - Map attributes = new HashMap<>(); - for (String name : sessionSubDocumentForContext.keySet()) + if (! data.getAllAttributes().isEmpty()) { - //skip special metadata attribute which is not one of the actual session attributes - if (MongoSessionDataStore.__METADATA.equals(name) ) - continue; - String attr = MongoUtils.decodeName(name); - Object value = MongoUtils.decodeValue(sessionSubDocumentForContext.get(name)); - attributes.put(attr, value); + assertNotNull(attributes); + SessionData tmp = new SessionData(data.getId(), data.getContextPath(), data.getVhost(), created.longValue(), accessed.longValue(), lastAccessed.longValue(), maxInactive.longValue()); + try (ByteArrayInputStream bais = new ByteArrayInputStream(attributes);ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(bais)) + { + SessionData.deserializeAttributes(tmp, ois); + } + + //same keys + assertTrue(data.getKeys().equals(tmp.getKeys())); + //same values + for (String name:data.getKeys()) + { + assertTrue(data.getAttribute(name).equals(tmp.getAttribute(name))); + } } - //same keys - assertTrue(data.getKeys().equals(attributes.keySet())); - //same values - for (String name:data.getKeys()) - { - assertTrue(data.getAttribute(name).equals(attributes.get(name))); - } - - return true; } @@ -226,17 +220,18 @@ public class MongoTestHelper if (attributes != null) { - for (String name : attributes.keySet()) + SessionData tmp = new SessionData (id, contextPath, vhost, created, accessed, lastAccessed, maxIdle, attributes); + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)) { - Object value = attributes.get(name); - sets.put(MongoSessionDataStore.__CONTEXT + "." + vhost.replace('.', '_') + ":" + contextPath+ "." + MongoUtils.encodeName(name), - MongoUtils.encodeName(value)); + SessionData.serializeAttributes(tmp, oos); + sets.put(MongoSessionDataStore.__CONTEXT + "." + vhost.replace('.', '_') + ":" + contextPath +"."+MongoSessionDataStore.__ATTRIBUTES, baos.toByteArray()); } } + update.put("$set",sets); collection.update(key,update,upsert,false,WriteConcern.SAFE); } - + public static void createSession (String id, String contextPath, String vhost, String lastNode, long created, long accessed, @@ -269,6 +264,53 @@ public class MongoTestHelper sets.put(MongoSessionDataStore.__ACCESSED, accessed); sets.put(MongoSessionDataStore.__LAST_ACCESSED, lastAccessed); + if (attributes != null) + { + SessionData tmp = new SessionData (id, contextPath, vhost, created, accessed, lastAccessed, maxIdle, attributes); + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos);) + { + SessionData.serializeAttributes(tmp, oos); + sets.put(MongoSessionDataStore.__CONTEXT + "." + vhost.replace('.', '_') + ":" + contextPath +"."+MongoSessionDataStore.__ATTRIBUTES, baos.toByteArray()); + } + } + + update.put("$set",sets); + collection.update(key,update,upsert,false,WriteConcern.SAFE); + } + + + public static void createLegacySession (String id, String contextPath, String vhost, + String lastNode, long created, long accessed, + long lastAccessed, long maxIdle, long expiry, + Map attributes) + throws Exception + { + //make old-style session to test if we can retrieve it + DBCollection collection = _mongoClient.getDB(DB_NAME).getCollection(COLLECTION_NAME); + + // Form query for upsert + BasicDBObject key = new BasicDBObject(MongoSessionDataStore.__ID, id); + + // Form updates + BasicDBObject update = new BasicDBObject(); + boolean upsert = false; + BasicDBObject sets = new BasicDBObject(); + + Object version = new Long(1); + + // New session + upsert = true; + sets.put(MongoSessionDataStore.__CREATED,created); + sets.put(MongoSessionDataStore.__VALID,true); + sets.put(MongoSessionDataStore.__CONTEXT + "." + vhost.replace('.', '_') + ":" + contextPath +"."+MongoSessionDataStore.__VERSION,version); + sets.put(MongoSessionDataStore.__CONTEXT + "." + vhost.replace('.', '_') + ":" + contextPath +"."+MongoSessionDataStore.__LASTSAVED, System.currentTimeMillis()); + sets.put(MongoSessionDataStore.__CONTEXT + "." + vhost.replace('.', '_') + ":" + contextPath +"."+MongoSessionDataStore.__LASTNODE, lastNode); + sets.put(MongoSessionDataStore.__MAX_IDLE, maxIdle); + sets.put(MongoSessionDataStore.__EXPIRY, expiry); + sets.put(MongoSessionDataStore.__ACCESSED, accessed); + sets.put(MongoSessionDataStore.__LAST_ACCESSED, lastAccessed); + if (attributes != null) { for (String name : attributes.keySet()) @@ -281,9 +323,4 @@ public class MongoTestHelper update.put("$set",sets); collection.update(key,update,upsert,false,WriteConcern.SAFE); } - - - - - } diff --git a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestSessionDataStore.java b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestSessionDataStore.java index cdd5f468a3a..20baeb75033 100644 --- a/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestSessionDataStore.java +++ b/tests/test-sessions/test-sessions-common/src/main/java/org/eclipse/jetty/server/session/TestSessionDataStore.java @@ -63,7 +63,7 @@ public class TestSessionDataStore extends AbstractSessionDataStore @Override - public SessionData load(String id) throws Exception + public SessionData doLoad(String id) throws Exception { SessionData sd = _map.get(id); if (sd == null) diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DeleteUnloadableSessionTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DeleteUnloadableSessionTest.java index 54aa6e71546..1565fb3f96b 100644 --- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DeleteUnloadableSessionTest.java +++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/DeleteUnloadableSessionTest.java @@ -65,37 +65,29 @@ public class DeleteUnloadableSessionTest String unloadableId = null; - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating() - */ + @Override public boolean isPassivating() { return true; } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String) - */ + @Override public boolean exists(String id) throws Exception { return o != null; } - /** - * @see org.eclipse.jetty.server.session.SessionDataMap#load(java.lang.String) - */ + @Override - public SessionData load(String id) throws Exception + public SessionData doLoad(String id) throws Exception { unloadableId = id; throw new UnreadableSessionDataException(id, null, new Exception("fake")); } - /** - * @see org.eclipse.jetty.server.session.SessionDataMap#delete(java.lang.String) - */ + @Override public boolean delete(String id) throws Exception { @@ -107,18 +99,14 @@ public class DeleteUnloadableSessionTest return false; } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(java.lang.String, org.eclipse.jetty.server.session.SessionData, long) - */ + @Override public void doStore(String id, SessionData data, long lastSaveTime) throws Exception { //pretend it was saved } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doGetExpired(java.util.Set) - */ + @Override public Set doGetExpired(Set candidates) { diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionEvictionFailureTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionEvictionFailureTest.java index f9fde56b1bb..27687b4c82a 100644 --- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionEvictionFailureTest.java +++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionEvictionFailureTest.java @@ -67,45 +67,33 @@ public class SessionEvictionFailureTest _nextStoreResult = results; } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#isPassivating() - */ @Override public boolean isPassivating() { return false; } - /** - * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String) - */ + @Override public boolean exists(String id) throws Exception { return true; } - /** - * @see org.eclipse.jetty.server.session.SessionDataMap#load(java.lang.String) - */ + @Override - public SessionData load(String id) throws Exception + public SessionData doLoad(String id) throws Exception { return null; } - /** - * @see org.eclipse.jetty.server.session.SessionDataMap#delete(java.lang.String) - */ @Override public boolean delete(String id) throws Exception { return false; } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(java.lang.String, org.eclipse.jetty.server.session.SessionData, long) - */ + @Override public void doStore(String id, SessionData data, long lastSaveTime) throws Exception { @@ -115,9 +103,6 @@ public class SessionEvictionFailureTest } } - /** - * @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doGetExpired(java.util.Set) - */ @Override public Set doGetExpired(Set candidates) { diff --git a/tests/test-webapps/test-jetty-webapp/src/main/java/com/acme/SessionDump.java b/tests/test-webapps/test-jetty-webapp/src/main/java/com/acme/SessionDump.java index 3756043714d..f2ca605a78f 100644 --- a/tests/test-webapps/test-jetty-webapp/src/main/java/com/acme/SessionDump.java +++ b/tests/test-webapps/test-jetty-webapp/src/main/java/com/acme/SessionDump.java @@ -31,6 +31,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import org.eclipse.jetty.util.MultiMap; + /** * Test Servlet Sessions. */ @@ -83,6 +85,7 @@ public class SessionDump extends HttpServlet session = request.getSession(true); session.setAttribute("test","value"); session.setAttribute("obj", new ObjectAttributeValue(System.currentTimeMillis())); + session.setAttribute("WEBCL", new MultiMap()); } else if (session!=null) { @@ -140,6 +143,8 @@ public class SessionDump extends HttpServlet } else { + if (session.getAttribute("WEBCL") == null) + session.setAttribute("WEBCL", new MultiMap()); try { out.println("ID: "+session.getId()+"
");