FormsServiceClientUtils.java
12 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.idp.Document
* com.adobe.idp.DocumentManagerClient
*/
package com.adobe.livecycle.formsservice.utils;
import com.adobe.idp.Document;
import com.adobe.idp.DocumentManagerClient;
import com.adobe.livecycle.formsservice.client.FormPreference;
import com.adobe.livecycle.formsservice.client.OutputType;
import com.adobe.livecycle.formsservice.exception.RenderFormException;
import com.adobe.livecycle.formsservice.sharedutils.Base64;
import com.adobe.livecycle.formsservice.sharedutils.GZip;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.StringTokenizer;
public final class FormsServiceClientUtils {
public static final int DOCUMENT_ACTIVE = 0;
public static final int DOCUMENT_PASSIVE = 1;
public static final int DOCUMENT_DISPOSED = 2;
private FormsServiceClientUtils() {
}
public static boolean isEmptyOrNull(String val) {
return val == null || val.trim().length() == 0;
}
public static byte[] serializeDoc(Document d) throws IOException {
if (d == null) {
return null;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject((Object)d);
return bos.toByteArray();
}
public static String zipAndEncode(byte[] in) throws IOException {
byte[] enc;
String res = null;
if (in != null && (enc = Base64.encode(GZip.gzip(in))) != null) {
res = FormsServiceClientUtils.URLEncode(new String(enc, "UTF8"));
}
return res;
}
public static byte[] combineDocsWith(byte[] data, ArrayList documents) throws IOException {
byte[] result = data;
if (documents != null && documents.size() > 0) {
String encData = null;
if (data != null) {
encData = FormsServiceClientUtils.zipAndEncode(data);
}
result = FormsServiceClientUtils.doCombine(encData, documents);
}
return result;
}
private static byte[] doCombine(String encData, ArrayList documents) throws IOException {
if (documents.size() == 0) {
throw new IOException("Missing Documents");
}
StringBuffer ents = new StringBuffer("<xdpData>");
if (encData != null) {
ents.append("<data>");
ents.append(encData);
ents.append("</data>");
}
ents.append("<documents>");
for (int i = 0; i < documents.size(); ++i) {
Document doc = (Document)documents.get(i);
String docName = (String)((Object)doc.getAttribute("name"));
if (doc == null) continue;
if (FormsServiceClientUtils.getDocumentState(doc) == 0) {
doc.setMaxInlineSize(0);
}
byte[] serDoc = FormsServiceClientUtils.serializeDoc(doc);
ents.append("<doc>");
ents.append("<displayName>" + docName + "</displayName>");
ents.append("<serialized>");
ents.append(FormsServiceClientUtils.zipAndEncode(serDoc));
ents.append("</serialized>");
ents.append("</doc>");
}
ents.append("</documents>");
ents.append("</xdpData>");
return ents.toString().getBytes("UTF8");
}
public static String URLEncode(String s) {
String sEnc;
try {
sEnc = URLEncoder.encode(s, "UTF8");
}
catch (Exception ex) {
sEnc = URLEncoder.encode(s);
}
return sEnc.replaceAll("\\+", "%20");
}
public static byte[] URLDecode(String sURLEncoded) {
byte[] cDecoded = new byte[sURLEncoded.length() * 2];
int j = 0;
int i = 0;
while (i < sURLEncoded.length()) {
char cByte = sURLEncoded.charAt(i);
if (cByte == '+') {
cDecoded[j++] = 32;
++i;
continue;
}
if (cByte == '%') {
int nFirst = sURLEncoded.charAt(i + 1);
int nSecond = sURLEncoded.charAt(i + 2);
if (nFirst >= 97 && nFirst <= 122) {
nFirst -= 32;
}
if (nSecond >= 97 && nSecond <= 122) {
nSecond -= 32;
}
nFirst = nFirst >= 48 && nFirst <= 57 ? (nFirst -= 48) : 10 + (nFirst - 65);
nSecond = nSecond >= 48 && nSecond <= 57 ? (nSecond -= 48) : 10 + (nSecond - 65);
int nDecoded = nFirst * 16 + nSecond;
cDecoded[j++] = (byte)nDecoded;
i += 3;
continue;
}
cDecoded[j++] = (byte)cByte;
++i;
}
return FormsServiceClientUtils.resizeByteArray(cDecoded, j);
}
public static byte[] resizeByteArray(byte[] oldArray, int newSize) {
int oldSize = oldArray.length;
byte[] newArray = new byte[newSize];
int preserveLength = Math.min(oldSize, newSize);
if (preserveLength > 0) {
System.arraycopy(oldArray, 0, newArray, 0, preserveLength);
}
return newArray;
}
public static String getParameter(String queryString, String parameter) {
return FormsServiceClientUtils.getParameter(queryString, parameter, "");
}
public static String getParameter(String queryString, String parameter, String sEncoding) {
String sParameter = null;
if (queryString == null || queryString.length() == 0) {
return null;
}
if (parameter == null || parameter.length() == 0) {
return null;
}
StringTokenizer toks = new StringTokenizer(queryString, "&");
while (toks.hasMoreTokens()) {
String tok_nm;
String tok = toks.nextToken();
int sep = tok.indexOf("=");
if (sep <= 0 || !(tok_nm = tok.substring(0, sep).trim()).equalsIgnoreCase(parameter)) continue;
String sTok = tok.substring(sep + 1);
if (sTok.indexOf(37) < 0) {
sParameter = sTok;
continue;
}
byte[] cDecoded = FormsServiceClientUtils.URLDecode(sTok);
if (sEncoding == null || sEncoding.length() == 0) {
sParameter = new String(cDecoded);
continue;
}
try {
sParameter = new String(cDecoded, sEncoding);
}
catch (Exception oException) {
sParameter = sTok;
}
}
return sParameter;
}
public static String mergeParameters(String queryStringBase, String queryStringOver) {
if (queryStringBase.length() == 0) {
return queryStringOver;
}
StringTokenizer inToks = new StringTokenizer(queryStringOver, "&");
String merged = queryStringBase;
while (inToks.hasMoreTokens()) {
String tok = inToks.nextToken();
int tokEqual = tok.indexOf("=");
if (tokEqual < 0) continue;
String tokKey = tok.substring(0, tokEqual);
String paramValue = FormsServiceClientUtils.getParameter(queryStringBase, tokKey);
if (paramValue != null) {
String tokKeyLwr = tokKey.toLowerCase() + "=";
String mergedLwr = merged.toLowerCase();
int index = mergedLwr.indexOf(tokKeyLwr);
int repStart = index + tokEqual + 1;
int repEnd = repStart + paramValue.length();
merged = merged.substring(0, repStart) + tok.substring(tokEqual + 1) + merged.substring(repEnd);
continue;
}
merged = merged + "&" + tok;
}
return merged;
}
public static int findArraySegmentLimited(byte[] origArray, int start, byte[] findSegment, int origArrayLimit) {
int origArrayLen;
int i = start;
int n = origArrayLen = origArrayLimit <= 0 ? origArray.length : origArrayLimit;
if (origArrayLen > origArray.length) {
origArrayLen = origArray.length;
}
while (i + findSegment.length <= origArrayLen) {
int j;
for (j = 0; j < findSegment.length && findSegment[j] == origArray[i + j]; ++j) {
}
if (j == findSegment.length) {
return i;
}
++i;
}
return -1;
}
public static int findArraySegment(byte[] origArray, int start, byte[] findSegment) {
return FormsServiceClientUtils.findArraySegmentLimited(origArray, start, findSegment, 0);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Loose catch block
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
* Lifted jumps to return sites
*/
public static byte[] getBytes(Document oDoc) {
byte[] result = new byte[]{};
if (oDoc == null) {
return result;
}
InputStream oInStream = oDoc.getInputStream();
if (oInStream == null) {
return result;
}
BufferedInputStream oStream = new BufferedInputStream(oInStream);
ByteArrayOutputStream oBAOS = new ByteArrayOutputStream();
int nRead = 0;
int nBlkSize = 32768;
byte[] buffer = new byte[nBlkSize];
while ((nRead = oStream.read(buffer, 0, nBlkSize)) != -1) {
oBAOS.write(buffer, 0, nRead);
}
result = oBAOS.toByteArray();
try {
if (oStream != null) {
oStream.close();
}
if (oBAOS != null) {
oBAOS.close();
}
if (oInStream == null) return result;
oInStream.close();
return result;
}
catch (IOException e) {
return result;
}
catch (IOException e) {
try {
if (oStream != null) {
oStream.close();
}
if (oBAOS != null) {
oBAOS.close();
}
if (oInStream == null) return result;
oInStream.close();
return result;
}
catch (IOException e) {
return result;
}
catch (Throwable throwable) {
try {
if (oStream != null) {
oStream.close();
}
if (oBAOS != null) {
oBAOS.close();
}
if (oInStream == null) throw throwable;
oInStream.close();
throw throwable;
}
catch (IOException e) {
// empty catch block
}
throw throwable;
}
}
}
public static FormPreference convertStringToFormPreference(String sFormPreference) throws RenderFormException {
for (FormPreference formPreference : FormPreference.values()) {
if (!formPreference.name().equalsIgnoreCase(sFormPreference)) continue;
return formPreference;
}
throw new RenderFormException("ALC-FRM-001-023", new String[]{sFormPreference}, 3, true);
}
public static OutputType convertIntToOutputType(int outputTypeValue) throws RenderFormException {
for (OutputType outputType : OutputType.values()) {
if (outputType.getValue() != outputTypeValue) continue;
return outputType;
}
throw new RenderFormException("ALC-FRM-001-012", new String[]{"outputtype", Integer.toString(outputTypeValue)}, 3, true);
}
public static int getDocumentState(Document d) {
return DocumentManagerClient.getDocumentState((Document)d);
}
}