-
Notifications
You must be signed in to change notification settings - Fork 1
/
JavaSerializer.java
99 lines (85 loc) · 3.32 KB
/
JavaSerializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.appng.tomcat.session.redis;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import org.apache.juli.logging.Log;
import org.appng.tomcat.session.Utils;
/**
* The default {@link Serializer} implementation
*/
public class JavaSerializer implements Serializer {
private final Log log = Utils.getLog(JavaSerializer.class);
private ClassLoader loader;
public void setClassLoader(ClassLoader loader)
throws NoSuchMethodException, SecurityException, ClassNotFoundException {
this.loader = loader;
}
public byte[] attributesHashFrom(RedisSession session) throws IOException {
Map<String, Object> attributes = new HashMap<String, Object>();
for (Enumeration<String> enumerator = session.getAttributeNames(); enumerator.hasMoreElements();) {
String key = enumerator.nextElement();
attributes.put(key, session.getAttribute(key));
}
byte[] serialized = null;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos));) {
oos.writeUnshared(attributes);
oos.flush();
bos.flush();
serialized = bos.toByteArray();
}
MessageDigest digester = null;
try {
digester = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
log.error("Unable to get MessageDigest instance for MD5");
}
return digester.digest(serialized);
}
public byte[] serializeFrom(RedisSession session, SessionSerializationMetadata metadata) throws IOException {
byte[] serialized = null;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos));) {
oos.writeObject(metadata);
session.writeObjectData(oos);
oos.flush();
serialized = bos.toByteArray();
}
return serialized;
}
public void deserializeInto(byte[] data, RedisSession session, SessionSerializationMetadata metadata)
throws IOException, ReflectiveOperationException {
ObjectInputStream ois = Utils.getObjectInputStream(loader, session.getServletContext(), data);
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(loader);
try {
SessionSerializationMetadata serializedMetadata = (SessionSerializationMetadata) ois.readObject();
metadata.copyFieldsFrom(serializedMetadata);
session.readObjectData(ois);
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
}
}