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

import com.adobe.fd.stp.api.PrinterProtocol;
import com.adobe.fd.stp.internal.exception.STPServiceException;
import com.adobe.fd.stp.internal.print.jobs.TCPPrintJob;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class CUPSPrintJob
extends TCPPrintJob {
    private static final String CUPS = PrinterProtocol.CUPS.toString();
    private static final String sRequestMethod = "POST";
    private static final String CONTENT_TYPE = "Content-type";
    private static final String APPLICATION_IPP = "application/ipp";
    private static final String ATTRIBUTES_CHARSET = "attributes-charset";
    private static final String US_ASCII = "us-ascii";
    private static final String NATURAL_LANGUAGE = "attributes-natural-language";
    private static final String EN_US = "en-us";
    private static final String PRINTER_URI = "printer-uri";

    public CUPSPrintJob(String sPrintServerUri, String sPrinterName) {
        super(sPrintServerUri, sPrinterName);
    }

    private HttpURLConnection connect() throws STPServiceException {
        HttpURLConnection connection = null;
        String sCUPSServerName = this.getServer();
        try {
            URL url = new URL(sCUPSServerName);
            connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-type", "application/ipp");
        }
        catch (Exception e) {
            throw new STPServiceException("AEM_STP_001_004", new String[]{sCUPSServerName}, e);
        }
        return connection;
    }

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

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

    @Override
    public void print(InputStream inStream) throws STPServiceException {
        block10 : {
            HttpURLConnection printer = null;
            InputStream oResponseStream = null;
            DataOutputStream out = null;
            String sPrinter = this.getServer();
            if (sPrinter == null) {
                throw new STPServiceException("AEM_STP_001_003", new Object[]{"sPrinter is null"});
            }
            try {
                printer = this.connect();
                out = new DataOutputStream(printer.getOutputStream());
                out.writeShort(257);
                out.writeShort(2);
                out.writeInt(1);
                out.writeByte(1);
                out.writeByte(71);
                out.writeShort("attributes-charset".length());
                out.write("attributes-charset".getBytes());
                out.writeShort("us-ascii".length());
                out.write("us-ascii".getBytes());
                out.writeByte(72);
                out.writeShort("attributes-natural-language".length());
                out.write("attributes-natural-language".getBytes());
                out.writeShort("en-us".length());
                out.write("en-us".getBytes());
                out.writeByte(69);
                out.writeShort("printer-uri".length());
                out.write("printer-uri".getBytes());
                out.writeShort(sPrinter.length());
                out.write(sPrinter.getBytes());
                out.writeByte(3);
                int len = 0;
                byte[] readbuf = new byte[8192];
                while ((len = inStream.read(readbuf)) > 0) {
                    out.write(readbuf, 0, len);
                }
                out.flush();
                oResponseStream = printer.getInputStream();
                len = oResponseStream.read(readbuf);
                if (len != -1) {
                    if (readbuf[2] != 0 && readbuf[3] != 0) {
                        throw new STPServiceException("AEM_STP_001_005", new String[]{CUPS, sPrinter});
                    }
                    break block10;
                }
                throw new STPServiceException("AEM_STP_001_005", new String[]{CUPS, sPrinter});
            }
            catch (STPServiceException e) {
                throw e;
            }
            catch (Exception e) {
                throw new STPServiceException("AEM_STP_001_005", new String[]{CUPS, sPrinter});
            }
            finally {
                this.close(printer, oResponseStream, out);
            }
        }
    }
}