DocumentUtils.java 18.9 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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemfd.docmanager.Document
 *  com.adobe.granite.workflow.WorkflowException
 *  com.adobe.granite.workflow.WorkflowSession
 *  com.adobe.granite.workflow.exec.WorkItem
 *  com.adobe.granite.workflow.exec.WorkflowData
 *  com.adobe.granite.workflow.metadata.MetaDataMap
 *  com.adobe.granite.workflow.model.WorkflowNode
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.ValueFactory
 *  javax.jcr.nodetype.NodeType
 *  org.apache.commons.io.IOUtils
 *  org.apache.commons.lang.StringUtils
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.fd.workflow.utils;

import com.adobe.aemfd.docmanager.Document;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.granite.workflow.model.WorkflowNode;
import java.io.IOException;
import java.io.InputStream;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import javax.jcr.nodetype.NodeType;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DocumentUtils {
    private static Logger logger = LoggerFactory.getLogger(DocumentUtils.class);

    public static void writeDocument(WorkflowSession workflowSession, Document doc, String filePath) throws WorkflowException {
        DocumentUtils.writeDocument((Session)workflowSession.adaptTo(Session.class), doc, filePath);
    }

    public static void writeDocument(Session _session, Document doc, String filePath) throws WorkflowException {
        InputStream is = null;
        try {
            logger.debug("write document " + filePath);
            if (StringUtils.isBlank((String)filePath)) {
                throw new WorkflowException("Output file path cannot be null");
            }
            if (_session == null) {
                throw new WorkflowException("Session cannot be null");
            }
            if (_session.nodeExists(filePath)) {
                throw new WorkflowException("Error in writing a file. File already exists");
            }
            if (doc == null) {
                throw new WorkflowException("Error in writing a file. Document is null");
            }
            String folderPath = filePath.substring(0, filePath.lastIndexOf("/"));
            String outputpath = filePath.substring(filePath.lastIndexOf("/") + 1);
            if (folderPath == null) {
                throw new WorkflowException("Output folder path cannot be null");
            }
            if (outputpath == null) {
                throw new WorkflowException("Ouput Document cannot be null");
            }
            is = doc.getInputStream();
            Node node = _session.getNode(folderPath);
            ValueFactory valueFactory = _session.getValueFactory();
            Binary contentValue = valueFactory.createBinary(is);
            Node fileNode = node.addNode(outputpath, "nt:file");
            fileNode.addMixin("mix:referenceable");
            Node resNode = fileNode.addNode("jcr:content", "nt:resource");
            resNode.setProperty("jcr:mimeType", "application/octet-stream");
            resNode.setProperty("jcr:data", contentValue);
        }
        catch (Exception e) {
            throw new WorkflowException("Exception while writing document to " + filePath + " " + e.getMessage());
        }
        finally {
            if (is != null) {
                try {
                    is.close();
                }
                catch (IOException e) {
                    logger.error("Exception while closing document input stream " + e.getMessage());
                }
            }
        }
    }

    public static void updateDocument(WorkflowSession workflowSession, Document doc, String filePath) throws RepositoryException, WorkflowException, IOException {
        DocumentUtils.updateDocument((Session)workflowSession.adaptTo(Session.class), doc, filePath);
    }

    public static void updateDocument(Session _session, Document doc, String filePath) throws RepositoryException, WorkflowException, IOException {
        block12 : {
            logger.debug("write document " + filePath);
            if (StringUtils.isBlank((String)filePath)) {
                throw new WorkflowException("Unable to update file - Output file path cannot be null");
            }
            if (_session == null) {
                throw new WorkflowException("Session cannot be null");
            }
            if (!_session.nodeExists(filePath)) {
                throw new WorkflowException("Unable to over write file - " + filePath + " File does not exist");
            }
            if (doc == null) {
                throw new WorkflowException("Error in writing a file. Document is null");
            }
            Node fileNode = _session.getNode(filePath);
            if (!fileNode.getPrimaryNodeType().getName().equals("nt:file")) {
                throw new WorkflowException("Unable to over write file - " + filePath + ". The resource is not a File");
            }
            InputStream docInputStream = null;
            try {
                docInputStream = doc.getInputStream();
                Binary opBin = _session.getValueFactory().createBinary(docInputStream);
                if (fileNode.getNode("jcr:content") != null) {
                    fileNode.getNode("jcr:content").getProperty("jcr:data").setValue(opBin);
                    break block12;
                }
                throw new WorkflowException("jcr-content node is missing at " + fileNode.toString());
            }
            catch (IOException e) {
                throw new WorkflowException("Exception in updating Document", (Throwable)e);
            }
            finally {
                if (docInputStream != null) {
                    docInputStream.close();
                }
            }
        }
    }

    public static void saveOutputDocument(WorkItem workItem, WorkflowSession workflowSession, String key, Document outDoc) throws Exception {
        MetaDataMap metaDataMap = workItem.getNode().getMetaDataMap();
        String metadata = (String)metaDataMap.get(key, String.class);
        String payload = workItem.getWorkflowData().getPayload() + "";
        logger.debug("key =" + key);
        logger.debug("metadata =" + metadata);
        logger.debug("payload =" + payload);
        DocumentUtils.createOrUpdateDocument((Session)workflowSession.adaptTo(Session.class), outDoc, payload, metadata);
    }

    public static void createOrUpdateDocument(WorkflowSession workflowSession, Document doc, String payload, String metaData) throws WorkflowException {
        DocumentUtils.createOrUpdateDocument((Session)workflowSession.adaptTo(Session.class), doc, payload, metaData);
    }

    public static void createOrUpdateDocument(Session _session, Document doc, String payload, String metaData) throws WorkflowException {
        block20 : {
            if (StringUtils.isEmpty((String)payload) || StringUtils.isEmpty((String)metaData)) {
                throw new WorkflowException("payload or metadata can't be null or empty");
            }
            if (_session == null) {
                throw new WorkflowException("Session cannot be null");
            }
            if (doc == null) {
                throw new WorkflowException("Document cannot be null");
            }
            try {
                logger.debug("metadata =" + metaData);
                logger.debug("payload =" + payload);
                int indexOfColon = metaData.indexOf(58);
                if (indexOfColon == -1) {
                    throw new WorkflowException("Invalid format of MetaData" + metaData);
                }
                String outputType = metaData.substring(0, indexOfColon);
                logger.debug("outputType = " + outputType);
                if ("RELATIVE_PLOAD".equals(outputType)) {
                    String folderPath;
                    String fileName = metaData.substring(indexOfColon + 1);
                    logger.debug("fileName = " + fileName);
                    if (StringUtils.isBlank((String)fileName)) {
                        throw new WorkflowException("File name is null or blank for RELATIVE_PLOAD, throwing an exception");
                    }
                    String string = folderPath = payload.equals("/") ? "" : payload + "/" + fileName;
                    if (_session.nodeExists(folderPath)) {
                        DocumentUtils.updateDocument(_session, doc, folderPath);
                    } else {
                        DocumentUtils.writeDocument(_session, doc, folderPath);
                    }
                    break block20;
                }
                if ("OVERWRITE".equals(outputType)) {
                    DocumentUtils.updateDocument(_session, doc, payload);
                    break block20;
                }
                if ("FOLDER_PAYLOAD".equals(outputType)) {
                    String folderPath;
                    String fileName = metaData.substring(indexOfColon + 1);
                    logger.debug("fileName = " + fileName);
                    if (fileName == null || fileName.trim().equals("")) {
                        throw new WorkflowException("File name is null or blank for FOLDER_PAYLOAD, throwing an exception");
                    }
                    String string = folderPath = payload.equals("/") ? "" : payload + "/" + fileName;
                    if (_session.nodeExists(folderPath)) {
                        DocumentUtils.updateDocument(_session, doc, folderPath);
                    } else {
                        DocumentUtils.writeDocument(_session, doc, folderPath);
                    }
                    break block20;
                }
                if ("FILE_PAYLOAD".equals(outputType)) {
                    DocumentUtils.updateDocument(_session, doc, payload);
                    break block20;
                }
                if ("ABSOLUTE_PATH".equals(outputType)) {
                    String fileName = metaData.substring(indexOfColon + 1);
                    logger.debug("fileName = " + fileName);
                    if (fileName == null || fileName.trim().equals("")) {
                        throw new WorkflowException("File name is null or blank for FOLDER_PAYLOAD, throwing an exception");
                    }
                    if (_session.nodeExists(fileName)) {
                        DocumentUtils.updateDocument(_session, doc, fileName);
                    } else {
                        DocumentUtils.writeDocument(_session, doc, fileName);
                    }
                    break block20;
                }
                throw new WorkflowException("Invalid format of MetaData");
            }
            catch (Exception e) {
                throw new WorkflowException(e.getMessage());
            }
        }
    }

    public static Document getInputDocument(WorkItem workItem, String key) throws WorkflowException {
        if (StringUtils.isBlank((String)key) || workItem == null) {
            throw new WorkflowException("Key or WorkItem can't be null");
        }
        logger.debug("key =" + key);
        MetaDataMap metaDataMap = workItem.getNode().getMetaDataMap();
        String metadata = (String)metaDataMap.get(key, String.class);
        logger.debug("metadata =" + metadata);
        if (StringUtils.isBlank((String)metadata)) {
            logger.debug("metadata is null so returning null document");
            return null;
        }
        String payload = workItem.getWorkflowData().getPayload().toString();
        logger.debug("payload =" + payload);
        return DocumentUtils.getInputDocument(payload, metadata);
    }

    public static Document getInputDocument(String payload, String metaData) throws WorkflowException {
        if (StringUtils.isBlank((String)payload) || StringUtils.isBlank((String)metaData)) {
            throw new WorkflowException("payload or metadata can't be null or empty");
        }
        logger.debug("payload =" + payload);
        logger.debug("metaData =" + metaData);
        Document inDoc = null;
        String jcrPath = DocumentUtils.getJCRPath(payload, metaData);
        if (jcrPath != null) {
            inDoc = new Document(jcrPath);
        }
        return inDoc;
    }

    private static String getJCRPath(String payload, String metaData) throws WorkflowException {
        logger.debug("payload =" + payload);
        logger.debug("metaData =" + metaData);
        if (StringUtils.isBlank((String)payload)) {
            throw new WorkflowException("payload can't be blank");
        }
        if (StringUtils.isBlank((String)metaData)) {
            throw new WorkflowException("MetaData can't be blank");
        }
        int indexOfColon = metaData.indexOf(58);
        if (indexOfColon == -1) {
            logger.debug("Invalid format of MetaData");
            throw new WorkflowException("Invalid format of MetaData");
        }
        String inputType = metaData.substring(0, indexOfColon);
        String jcrPath = null;
        if ("FOLDER_PAYLOAD".equals(inputType)) {
            String fileName = metaData.substring(indexOfColon + 1);
            logger.debug("fileName =" + fileName);
            if (fileName == null || fileName.equalsIgnoreCase("")) {
                logger.debug("File name is null or balnk, throwing an exception");
                throw new WorkflowException("File name is null or balnk for FOLDER_PAYLOAD, throwing an exception");
            }
            jcrPath = payload + "/" + fileName;
        } else if ("FILE_PAYLOAD".equals(inputType)) {
            jcrPath = payload;
        } else if ("ABSOLUTE_PATH".equals(inputType)) {
            String fileName = metaData.substring(indexOfColon + 1);
            if (fileName == null || fileName.equalsIgnoreCase("")) {
                throw new WorkflowException("File name can't be blank or null for ABSOLUTE_PATH");
            }
            jcrPath = fileName;
        } else {
            throw new WorkflowException("Invalid format of MetaData");
        }
        return jcrPath;
    }

    public static String getAbsolutePath(String payload, String metaData) throws WorkflowException {
        if (StringUtils.isBlank((String)metaData)) {
            return null;
        }
        logger.debug("metaData =" + metaData);
        int indexOfColon = metaData.indexOf(58);
        if (indexOfColon == -1) {
            throw new WorkflowException("Invalid format of MetaData");
        }
        String inputType = metaData.substring(0, indexOfColon);
        String jcrPath = null;
        if ("FOLDER_PAYLOAD".equals(inputType)) {
            String fileName = metaData.substring(indexOfColon + 1);
            logger.debug("fileName =" + fileName);
            if (fileName == null || fileName.equalsIgnoreCase("")) {
                // empty if block
            }
            jcrPath = payload + "/" + fileName;
        } else if ("FILE_PAYLOAD".equals(inputType)) {
            jcrPath = payload;
        } else if ("ABSOLUTE_PATH".equals(inputType)) {
            String fileName = metaData.substring(indexOfColon + 1);
            if (fileName == null || fileName.equalsIgnoreCase("")) {
                throw new WorkflowException("fileName is either null or empty");
            }
            jcrPath = fileName;
        } else {
            throw new WorkflowException("Invalid format of MetaData");
        }
        return jcrPath;
    }

    public static String getAbsolutePath(WorkItem workItem, WorkflowSession workflowSession, String key) throws WorkflowException {
        if (StringUtils.isBlank((String)key)) {
            throw new WorkflowException("key cannot be null or empty");
        }
        logger.debug("key =" + key);
        MetaDataMap metaDataMap = workItem.getNode().getMetaDataMap();
        String metaData = (String)metaDataMap.get(key, String.class);
        if (metaData == null || metaData.equals("")) {
            throw new WorkflowException(key + " is not found at workitem");
        }
        logger.debug("metaData =" + metaData);
        String payload = workItem.getWorkflowData().getPayload().toString();
        logger.debug("payload =" + payload);
        return DocumentUtils.getAbsolutePath(payload, metaData);
    }

    public static void createFolderPathForFile(Session session, String parentNode, String filePath) throws RepositoryException {
        Node parentPath = session.getNode(parentNode);
        DocumentUtils.createFolderPathForFile(session, parentPath, filePath);
    }

    public static void createFolderPathForFile(Session session, Node parentNode, String filePath) throws RepositoryException {
        if (filePath == null || filePath.lastIndexOf("/") == -1) {
            return;
        }
        String folderPath = filePath.substring(0, filePath.lastIndexOf("/"));
        if (parentNode.hasNode(folderPath)) {
            return;
        }
        String[] folders = folderPath.split("/");
        for (int i = 0; i < folders.length; ++i) {
            String folderName = folders[i];
            parentNode = parentNode.hasNode(folderName) ? parentNode.getNode(folderName) : parentNode.addNode(folderName, "sling:Folder");
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static byte[] readFileContentForFileNode(Node file) {
        if (file == null) {
            return null;
        }
        Node resNode = null;
        InputStream is = null;
        byte[] data = null;
        String filePath = null;
        try {
            filePath = file.getPath();
            if (!file.getPrimaryNodeType().getName().equals("nt:file")) {
                logger.error("File Node is not an nt:file. unable to read the data " + filePath);
                byte[] arrby = null;
                return arrby;
            }
            resNode = file.getNode("jcr:content");
            is = resNode.getProperty("jcr:data").getBinary().getStream();
            byte[] arrby = data = IOUtils.toByteArray((InputStream)is);
            return arrby;
        }
        catch (Exception e) {
            logger.error("exception reading file", (Object)filePath, (Object)e);
            byte[] e = null;
            return e;
        }
        finally {
            try {
                is.close();
            }
            catch (IOException e) {
                logger.error("exception while closing the stream", (Throwable)e);
            }
        }
    }

    public static void createOrUpdateDocument(Session _session, Document doc, String filePath) throws WorkflowException, RepositoryException, IOException {
        if (!_session.nodeExists(filePath)) {
            DocumentUtils.writeDocument(_session, doc, filePath);
        } else {
            DocumentUtils.updateDocument(_session, doc, filePath);
        }
    }
}