CIFSPrintJob.java 4.45 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.fd.jcifs.wrapper.api.JCIFSWrapper
 *  com.adobe.fd.jcifs.wrapper.api.JCIFSWrapperException
 */
package com.adobe.fd.stp.internal.print.jobs;

import com.adobe.fd.jcifs.wrapper.api.JCIFSWrapper;
import com.adobe.fd.jcifs.wrapper.api.JCIFSWrapperException;
import com.adobe.fd.stp.api.PrinterProtocol;
import com.adobe.fd.stp.api.PrinterSpec;
import com.adobe.fd.stp.internal.CryptoService;
import com.adobe.fd.stp.internal.exception.STPServiceException;
import com.adobe.fd.stp.internal.logging.STPServiceLogger;
import com.adobe.fd.stp.internal.print.jobs.IPrintJob;
import java.io.InputStream;
import java.util.UUID;
import java.util.regex.Pattern;

public class CIFSPrintJob
implements IPrintJob {
    private static STPServiceLogger logger = new STPServiceLogger(CIFSPrintJob.class);
    private static final String CIFS = PrinterProtocol.CIFS.toString();
    private static Pattern regexPattern = Pattern.compile("/+|\\\\+");
    private final String printServer;
    private final String smbURI;
    private final String domainName;
    private final String userName;
    private final String userPassword;
    JCIFSWrapper jcifsWrapper;

    public CIFSPrintJob(PrinterSpec printerSpec, JCIFSWrapper jcifsWrapper, CryptoService cryptoService) throws Exception {
        if (jcifsWrapper == null) {
            throw new STPServiceException("AEM_STP_001_001", new String[]{"jcifsWrapper"});
        }
        this.jcifsWrapper = jcifsWrapper;
        this.printServer = printerSpec.getPrintServer();
        if (this.printServer == null) {
            throw new STPServiceException("AEM_STP_001_001", new String[]{"inPrintServer"});
        }
        logger.debug("AEM_STP_001_003", new Object[]{"PrintServer=" + this.printServer});
        this.smbURI = CIFSPrintJob.getSmbUri(this.printServer);
        logger.debug("AEM_STP_001_003", new Object[]{"Smb URI=" + this.smbURI});
        this.userName = printerSpec.getUsername();
        this.domainName = printerSpec.getDomain();
        if (cryptoService != null) {
            this.userPassword = cryptoService.getPlainText(printerSpec.getPassword());
        } else {
            logger.warn("AEM_STP_001_003", new Object[]{"cryptoService is null"});
            this.userPassword = printerSpec.getPassword();
        }
        logger.debug("AEM_STP_001_003", new Object[]{"Domain Name=" + this.domainName});
        logger.debug("AEM_STP_001_003", new Object[]{"user Name=" + this.userName});
    }

    @Override
    public boolean isAccessible() {
        return this.jcifsWrapper.isAccessible(this.smbURI, this.domainName, this.userName, this.userPassword);
    }

    @Override
    public void print(InputStream inStream) throws STPServiceException {
        try {
            UUID printJobName = UUID.randomUUID();
            int total = this.jcifsWrapper.print(inStream, this.smbURI, printJobName.toString(), this.domainName, this.userName, this.userPassword);
            logger.debug("AEM_STP_001_003", new String[]{"Total Bytes sent to printer:" + total});
            if (total == -1) {
                throw new STPServiceException("AEM_STP_001_006", new String[]{this.printServer});
            }
        }
        catch (JCIFSWrapperException e) {
            logger.error("AEM_STP_001_003", new String[]{e.getMessage() + " , " + this.smbURI + " , " + this.printServer + " , " + this.domainName + " , " + this.userName});
            Throwable origException = e.getCause() != null ? e.getCause() : e;
            throw new STPServiceException(origException);
        }
        catch (Exception e) {
            logger.error("AEM_STP_001_003", new String[]{e.getMessage() + " , " + this.smbURI + " , " + this.printServer + " , " + this.domainName + " , " + this.userName});
            throw new STPServiceException("AEM_STP_001_005", new String[]{CIFS, this.printServer}, e);
        }
    }

    public static String getSmbUri(String printServerUri) {
        String[] splitArray;
        if (!printServerUri.startsWith("/") && !printServerUri.startsWith("\\")) {
            return printServerUri;
        }
        StringBuilder ret = new StringBuilder("smb:/");
        for (String uriFrag : splitArray = regexPattern.split(printServerUri)) {
            if (uriFrag.length() <= 0) continue;
            ret.append('/');
            ret.append(uriFrag);
        }
        if ("smb:/".equals(ret.toString())) {
            ret.append('/');
        }
        return ret.toString();
    }
}