TCPPrintJob.java 3.71 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.fd.stp.internal.print.jobs;

import com.adobe.fd.stp.internal.exception.STPServiceException;
import com.adobe.fd.stp.internal.print.jobs.IPrintJob;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public abstract class TCPPrintJob
implements IPrintJob {
    private String msPrinterName = null;
    private String msServer = null;
    private int mnPortNo = -1;
    protected static final int mnSocketTimeOut = 180000;

    protected TCPPrintJob(String sPrintServerUri, String sPrinterName) {
        this.msPrinterName = sPrinterName;
        this.msServer = sPrintServerUri;
    }

    protected static String[] parseIPAdress(String sUri) {
        String[] hostAndPort = new String[2];
        int index1 = sUri.lastIndexOf(":");
        int index2 = sUri.indexOf("]:");
        int index3 = sUri.indexOf(":");
        if (index2 != -1) {
            hostAndPort[0] = sUri.substring(1, index2);
            hostAndPort[1] = sUri.substring(index2 + 2);
        } else if (sUri.charAt(0) == '[') {
            hostAndPort[0] = sUri.substring(1, sUri.length() - 1);
            hostAndPort[1] = "";
        } else if (index1 == -1) {
            hostAndPort[0] = sUri;
            hostAndPort[1] = "";
        } else if (index3 == index1) {
            hostAndPort[0] = sUri.substring(0, index1);
            hostAndPort[1] = sUri.substring(index1 + 1);
        } else {
            hostAndPort[0] = sUri;
            hostAndPort[1] = "";
        }
        return hostAndPort;
    }

    protected TCPPrintJob(String sPrintServerUri, String sPrinterName, int nDefaultPortNo) throws STPServiceException {
        this.mnPortNo = nDefaultPortNo;
        this.msPrinterName = sPrinterName;
        if (sPrintServerUri != null) {
            String[] hostAndPort = TCPPrintJob.parseIPAdress(sPrintServerUri);
            this.msServer = hostAndPort[0];
            if (hostAndPort[1].length() > 0) {
                try {
                    this.mnPortNo = Integer.parseInt(hostAndPort[1]);
                }
                catch (Exception e) {
                    throw new STPServiceException("AEM_STP_001_007", new Object[]{sPrintServerUri}, e);
                }
            }
        }
    }

    protected Socket connect(String sAccessMechanism) throws STPServiceException {
        Socket socket = null;
        if (this.msServer == null) {
            throw new STPServiceException("AEM_STP_001_001", new String[]{"printerServer"});
        }
        try {
            socket = new Socket(InetAddress.getByName(this.msServer), this.mnPortNo);
            socket.setSoTimeout(180000);
        }
        catch (Exception e) {
            throw new STPServiceException("AEM_STP_001_004", new String[]{this.msServer});
        }
        return socket;
    }

    protected void closeAll(Socket socket, InputStream in, OutputStream out) {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
            if (socket != null) {
                socket.close();
            }
        }
        catch (Exception var4_4) {
            // empty catch block
        }
    }

    protected void setServerUri(String sServerUri) {
        this.msServer = sServerUri;
    }

    protected void setPrinterName(String sPrinterName) {
        this.msPrinterName = sPrinterName;
    }

    protected int getServerPort() {
        return this.mnPortNo;
    }

    protected String getPrinterName() {
        return this.msPrinterName;
    }

    protected String getServer() {
        return this.msServer;
    }

    @Override
    public boolean isAccessible() {
        return true;
    }
}