FormsServiceClientUtils.java 12 KB
/*
 * 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);
    }
}