1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.record; |
16 |
| |
17 |
| import java.io.BufferedInputStream; |
18 |
| import java.io.BufferedOutputStream; |
19 |
| import java.io.ByteArrayInputStream; |
20 |
| import java.io.ByteArrayOutputStream; |
21 |
| import java.io.IOException; |
22 |
| import java.io.InputStream; |
23 |
| import java.io.ObjectInputStream; |
24 |
| import java.io.ObjectOutputStream; |
25 |
| import java.util.ArrayList; |
26 |
| import java.util.Collections; |
27 |
| import java.util.Iterator; |
28 |
| import java.util.List; |
29 |
| import java.util.zip.GZIPInputStream; |
30 |
| import java.util.zip.GZIPOutputStream; |
31 |
| |
32 |
| import org.apache.commons.codec.binary.Base64; |
33 |
| import org.apache.hivemind.ApplicationRuntimeException; |
34 |
| import org.apache.hivemind.ClassResolver; |
35 |
| import org.apache.hivemind.HiveMind; |
36 |
| import org.apache.hivemind.util.Defense; |
37 |
| import org.apache.tapestry.util.io.ResolvingObjectInputStream; |
38 |
| import org.apache.tapestry.util.io.TeeOutputStream; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| public class PersistentPropertyDataEncoderImpl implements PersistentPropertyDataEncoder |
52 |
| { |
53 |
| private ClassResolver _classResolver; |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| public static final String BYTESTREAM_PREFIX = "B"; |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| public static final String GZIP_BYTESTREAM_PREFIX = "Z"; |
66 |
| |
67 |
10
| public String encodePageChanges(List changes)
|
68 |
| { |
69 |
10
| Defense.notNull(changes, "changes");
|
70 |
| |
71 |
10
| if (changes.isEmpty())
|
72 |
2
| return "";
|
73 |
| |
74 |
8
| try
|
75 |
| { |
76 |
8
| ByteArrayOutputStream bosPlain = new ByteArrayOutputStream();
|
77 |
8
| ByteArrayOutputStream bosCompressed = new ByteArrayOutputStream();
|
78 |
| |
79 |
8
| GZIPOutputStream gos = new GZIPOutputStream(bosCompressed);
|
80 |
| |
81 |
8
| TeeOutputStream tos = new TeeOutputStream(bosPlain, gos);
|
82 |
| |
83 |
8
| ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(tos));
|
84 |
| |
85 |
8
| writeChangesToStream(changes, oos);
|
86 |
| |
87 |
6
| oos.close();
|
88 |
| |
89 |
6
| boolean useCompressed = bosCompressed.size() < bosPlain.size();
|
90 |
| |
91 |
6
| byte[] data = useCompressed ? bosCompressed.toByteArray() : bosPlain.toByteArray();
|
92 |
| |
93 |
6
| byte[] encoded = Base64.encodeBase64(data);
|
94 |
| |
95 |
6
| String prefix = useCompressed ? GZIP_BYTESTREAM_PREFIX : BYTESTREAM_PREFIX;
|
96 |
| |
97 |
6
| return prefix + new String(encoded);
|
98 |
| } |
99 |
| catch (Exception ex) |
100 |
| { |
101 |
2
| throw new ApplicationRuntimeException(RecordMessages.encodeFailure(ex), ex);
|
102 |
| } |
103 |
| } |
104 |
| |
105 |
12
| public List decodePageChanges(String encoded)
|
106 |
| { |
107 |
12
| if (HiveMind.isBlank(encoded))
|
108 |
2
| return Collections.EMPTY_LIST;
|
109 |
| |
110 |
10
| String prefix = encoded.substring(0, 1);
|
111 |
| |
112 |
10
| if (!(prefix.equals(BYTESTREAM_PREFIX) || prefix.equals(GZIP_BYTESTREAM_PREFIX)))
|
113 |
2
| throw new ApplicationRuntimeException(RecordMessages.unknownPrefix(prefix));
|
114 |
| |
115 |
8
| try
|
116 |
| { |
117 |
| |
118 |
| |
119 |
8
| byte[] decoded = Base64.decodeBase64(encoded.substring(1).getBytes());
|
120 |
| |
121 |
8
| InputStream is = new ByteArrayInputStream(decoded);
|
122 |
| |
123 |
8
| if (prefix.equals(GZIP_BYTESTREAM_PREFIX))
|
124 |
6
| is = new GZIPInputStream(is);
|
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
6
| ObjectInputStream ois = new ResolvingObjectInputStream(_classResolver,
|
133 |
| new BufferedInputStream(is)); |
134 |
| |
135 |
6
| List result = readChangesFromStream(ois);
|
136 |
| |
137 |
6
| ois.close();
|
138 |
| |
139 |
6
| return result;
|
140 |
| } |
141 |
| catch (Exception ex) |
142 |
| { |
143 |
2
| throw new ApplicationRuntimeException(RecordMessages.decodeFailure(ex), ex);
|
144 |
| } |
145 |
| } |
146 |
| |
147 |
8
| private void writeChangesToStream(List changes, ObjectOutputStream oos) throws IOException
|
148 |
| { |
149 |
8
| oos.writeInt(changes.size());
|
150 |
| |
151 |
8
| Iterator i = changes.iterator();
|
152 |
8
| while (i.hasNext())
|
153 |
| { |
154 |
46
| PropertyChange pc = (PropertyChange) i.next();
|
155 |
| |
156 |
46
| String componentPath = pc.getComponentPath();
|
157 |
46
| String propertyName = pc.getPropertyName();
|
158 |
46
| Object value = pc.getNewValue();
|
159 |
| |
160 |
46
| oos.writeBoolean(componentPath != null);
|
161 |
| |
162 |
46
| if (componentPath != null)
|
163 |
22
| oos.writeUTF(componentPath);
|
164 |
| |
165 |
46
| oos.writeUTF(propertyName);
|
166 |
46
| oos.writeObject(value);
|
167 |
| } |
168 |
| } |
169 |
| |
170 |
6
| private List readChangesFromStream(ObjectInputStream ois) throws IOException,
|
171 |
| ClassNotFoundException |
172 |
| { |
173 |
6
| List result = new ArrayList();
|
174 |
| |
175 |
6
| int count = ois.readInt();
|
176 |
| |
177 |
6
| for (int i = 0; i < count; i++)
|
178 |
| { |
179 |
44
| boolean hasPath = ois.readBoolean();
|
180 |
44
| String componentPath = hasPath ? ois.readUTF() : null;
|
181 |
44
| String propertyName = ois.readUTF();
|
182 |
44
| Object value = ois.readObject();
|
183 |
| |
184 |
44
| PropertyChangeImpl pc = new PropertyChangeImpl(componentPath, propertyName, value);
|
185 |
| |
186 |
44
| result.add(pc);
|
187 |
| } |
188 |
| |
189 |
6
| return result;
|
190 |
| } |
191 |
| |
192 |
22
| public void setClassResolver(ClassResolver resolver)
|
193 |
| { |
194 |
22
| _classResolver = resolver;
|
195 |
| } |
196 |
| } |