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