|
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 |
15
| public String encodePageChanges(List changes)
|
|
68 |
| { |
|
69 |
15
| Defense.notNull(changes, "changes");
|
|
70 |
| |
|
71 |
15
| if (changes.isEmpty())
|
|
72 |
3
| return "";
|
|
73 |
| |
|
74 |
12
| try
|
|
75 |
| { |
|
76 |
12
| ByteArrayOutputStream bosPlain = new ByteArrayOutputStream();
|
|
77 |
12
| ByteArrayOutputStream bosCompressed = new ByteArrayOutputStream();
|
|
78 |
| |
|
79 |
12
| GZIPOutputStream gos = new GZIPOutputStream(bosCompressed);
|
|
80 |
| |
|
81 |
12
| TeeOutputStream tos = new TeeOutputStream(bosPlain, gos);
|
|
82 |
| |
|
83 |
12
| ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(tos));
|
|
84 |
| |
|
85 |
12
| writeChangesToStream(changes, oos);
|
|
86 |
| |
|
87 |
9
| oos.close();
|
|
88 |
| |
|
89 |
9
| boolean useCompressed = bosCompressed.size() < bosPlain.size();
|
|
90 |
| |
|
91 |
9
| byte[] data = useCompressed ? bosCompressed.toByteArray() : bosPlain.toByteArray();
|
|
92 |
| |
|
93 |
9
| byte[] encoded = Base64.encodeBase64(data);
|
|
94 |
| |
|
95 |
9
| String prefix = useCompressed ? GZIP_BYTESTREAM_PREFIX : BYTESTREAM_PREFIX;
|
|
96 |
| |
|
97 |
9
| return prefix + new String(encoded);
|
|
98 |
| } |
|
99 |
| catch (Exception ex) |
|
100 |
| { |
|
101 |
3
| throw new ApplicationRuntimeException(RecordMessages.encodeFailure(ex), ex);
|
|
102 |
| } |
|
103 |
| } |
|
104 |
| |
|
105 |
18
| public List decodePageChanges(String encoded)
|
|
106 |
| { |
|
107 |
18
| if (HiveMind.isBlank(encoded))
|
|
108 |
3
| return Collections.EMPTY_LIST;
|
|
109 |
| |
|
110 |
15
| String prefix = encoded.substring(0, 1);
|
|
111 |
| |
|
112 |
15
| if (!(prefix.equals(BYTESTREAM_PREFIX) || prefix.equals(GZIP_BYTESTREAM_PREFIX)))
|
|
113 |
3
| throw new ApplicationRuntimeException(RecordMessages.unknownPrefix(prefix));
|
|
114 |
| |
|
115 |
12
| try
|
|
116 |
| { |
|
117 |
| |
|
118 |
| |
|
119 |
12
| byte[] decoded = Base64.decodeBase64(encoded.substring(1).getBytes());
|
|
120 |
| |
|
121 |
12
| InputStream is = new ByteArrayInputStream(decoded);
|
|
122 |
| |
|
123 |
12
| if (prefix.equals(GZIP_BYTESTREAM_PREFIX))
|
|
124 |
9
| is = new GZIPInputStream(is);
|
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
9
| ObjectInputStream ois = new ResolvingObjectInputStream(_classResolver,
|
|
133 |
| new BufferedInputStream(is)); |
|
134 |
| |
|
135 |
9
| List result = readChangesFromStream(ois);
|
|
136 |
| |
|
137 |
9
| ois.close();
|
|
138 |
| |
|
139 |
9
| return result;
|
|
140 |
| } |
|
141 |
| catch (Exception ex) |
|
142 |
| { |
|
143 |
3
| throw new ApplicationRuntimeException(RecordMessages.decodeFailure(ex), ex);
|
|
144 |
| } |
|
145 |
| } |
|
146 |
| |
|
147 |
12
| private void writeChangesToStream(List changes, ObjectOutputStream oos) throws IOException
|
|
148 |
| { |
|
149 |
12
| oos.writeInt(changes.size());
|
|
150 |
| |
|
151 |
12
| Iterator i = changes.iterator();
|
|
152 |
12
| while (i.hasNext())
|
|
153 |
| { |
|
154 |
69
| PropertyChange pc = (PropertyChange) i.next();
|
|
155 |
| |
|
156 |
69
| String componentPath = pc.getComponentPath();
|
|
157 |
69
| String propertyName = pc.getPropertyName();
|
|
158 |
69
| Object value = pc.getNewValue();
|
|
159 |
| |
|
160 |
69
| oos.writeBoolean(componentPath != null);
|
|
161 |
| |
|
162 |
69
| if (componentPath != null)
|
|
163 |
33
| oos.writeUTF(componentPath);
|
|
164 |
| |
|
165 |
69
| oos.writeUTF(propertyName);
|
|
166 |
69
| oos.writeObject(value);
|
|
167 |
| } |
|
168 |
| } |
|
169 |
| |
|
170 |
9
| private List readChangesFromStream(ObjectInputStream ois) throws IOException,
|
|
171 |
| ClassNotFoundException |
|
172 |
| { |
|
173 |
9
| List result = new ArrayList();
|
|
174 |
| |
|
175 |
9
| int count = ois.readInt();
|
|
176 |
| |
|
177 |
9
| for (int i = 0; i < count; i++)
|
|
178 |
| { |
|
179 |
66
| boolean hasPath = ois.readBoolean();
|
|
180 |
66
| String componentPath = hasPath ? ois.readUTF() : null;
|
|
181 |
66
| String propertyName = ois.readUTF();
|
|
182 |
66
| Object value = ois.readObject();
|
|
183 |
| |
|
184 |
66
| PropertyChangeImpl pc = new PropertyChangeImpl(componentPath, propertyName, value);
|
|
185 |
| |
|
186 |
66
| result.add(pc);
|
|
187 |
| } |
|
188 |
| |
|
189 |
9
| return result;
|
|
190 |
| } |
|
191 |
| |
|
192 |
33
| public void setClassResolver(ClassResolver resolver)
|
|
193 |
| { |
|
194 |
33
| _classResolver = resolver;
|
|
195 |
| } |
|
196 |
| } |