jdk/src/solaris/classes/sun/print/CUPSPrinter.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 542 eb75700cdf75
child 1737 90d4fe987b09
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 542
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.print;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.HttpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.print.IPPPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.print.CustomMediaSizeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.print.CustomMediaTray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.print.attribute.standard.Media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.print.attribute.standard.MediaSizeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.print.attribute.standard.MediaSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.print.attribute.standard.MediaTray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.print.attribute.standard.MediaPrintableArea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.print.attribute.Size2DSyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.print.attribute.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.print.attribute.EnumSyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.print.attribute.standard.PrinterName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class CUPSPrinter  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final double PRINTER_DPI = 72.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static boolean initialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static native String getCupsServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static native int getCupsPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static native boolean canConnect(String server, int port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static native boolean initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // These functions need to be synchronized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // CUPS does not support multi-threading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static synchronized native String[] getMedia(String printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static synchronized native float[] getPageSizes(String printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    //public static boolean useIPPMedia = false; will be used later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private MediaPrintableArea[] cupsMediaPrintables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private MediaSizeName[] cupsMediaSNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private CustomMediaSizeName[] cupsCustomMediaSNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private MediaTray[] cupsMediaTrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public  int nPageSizes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public  int nTrays = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private  String[] media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private  float[] pageSizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private String printer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static boolean libFound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static String cupsServer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static int cupsPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // load awt library to access native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            new sun.security.action.LoadLibraryAction("awt"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        libFound = initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (libFound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
           cupsServer = getCupsServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
           cupsPort = getCupsPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    CUPSPrinter (String printerName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (printerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new IllegalArgumentException("null printer name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        printer = printerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        cupsMediaSNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        cupsMediaPrintables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        cupsMediaTrays = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (!libFound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new RuntimeException("cups lib not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // get page + tray names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            media =  getMedia(printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (media == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                // either PPD file is not found or printer is unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                throw new RuntimeException("error getting PPD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            // get sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            pageSizes = getPageSizes(printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (pageSizes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                nPageSizes = pageSizes.length/6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                nTrays = media.length/2-nPageSizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                assert (nTrays >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Returns array of MediaSizeNames derived from PPD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public MediaSizeName[] getMediaSizeNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return cupsMediaSNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Returns array of Custom MediaSizeNames derived from PPD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public CustomMediaSizeName[] getCustomMediaSizeNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return cupsCustomMediaSNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Returns array of MediaPrintableArea derived from PPD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public MediaPrintableArea[] getMediaPrintableArea() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return cupsMediaPrintables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns array of MediaTrays derived from PPD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public MediaTray[] getMediaTrays() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return cupsMediaTrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Initialize media by translating PPD info to PrintService attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private void initMedia() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (pageSizes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        cupsMediaPrintables = new MediaPrintableArea[nPageSizes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        cupsMediaSNames = new MediaSizeName[nPageSizes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        cupsCustomMediaSNames = new CustomMediaSizeName[nPageSizes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        CustomMediaSizeName msn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        MediaPrintableArea mpa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        float length, width, x, y, w, h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // initialize names and printables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        for (int i=0; i<nPageSizes; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // media width and length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            width = (float)(pageSizes[i*6]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            length = (float)(pageSizes[i*6+1]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // media printable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            x = (float)(pageSizes[i*6+2]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            h = (float)(pageSizes[i*6+3]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            w = (float)(pageSizes[i*6+4]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            y = (float)(pageSizes[i*6+5]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            msn = new CustomMediaSizeName(media[i*2], media[i*2+1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                          width, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            // add to list of standard MediaSizeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if ((cupsMediaSNames[i] = msn.getStandardMedia()) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                // add custom if no matching standard media
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                cupsMediaSNames[i] = msn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                // add this new custom msn to MediaSize array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if ((width > 0.0) && (length > 0.0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    new MediaSize(width, length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                  Size2DSyntax.INCH, msn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // add to list of custom MediaSizeName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // for internal use of IPPPrintService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            cupsCustomMediaSNames[i] = msn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            mpa = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                mpa = new MediaPrintableArea(x, y, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                             MediaPrintableArea.INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (width > 0 && length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    mpa = new MediaPrintableArea(0, 0, width, length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                             MediaPrintableArea.INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            cupsMediaPrintables[i] = mpa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // initialize trays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        cupsMediaTrays = new MediaTray[nTrays];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        MediaTray mt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        for (int i=0; i<nTrays; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            mt = new CustomMediaTray(media[(nPageSizes+i)*2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                     media[(nPageSizes+i)*2+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            cupsMediaTrays[i] = mt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Get CUPS default printer using IPP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public static String getDefaultPrinter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            URL url = new URL("http", getServer(), getPort(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            final HttpURLConnection urlConnection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                IPPPrintService.getIPPConnection(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (urlConnection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                OutputStream os = (OutputStream)java.security.AccessController.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    doPrivileged(new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                return urlConnection.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                if (os == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                AttributeClass attCl[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    AttributeClass.ATTRIBUTES_CHARSET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    new AttributeClass("requested-attributes",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                       AttributeClass.TAG_KEYWORD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                       "printer-name")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                if (IPPPrintService.writeIPPRequest(os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                        IPPPrintService.OP_CUPS_GET_DEFAULT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                        attCl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    HashMap defaultMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    InputStream is = urlConnection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    HashMap[] responseMap = IPPPrintService.readIPPResponse(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                         is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    if (responseMap.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        defaultMap = responseMap[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    if (defaultMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        urlConnection.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    AttributeClass attribClass = (AttributeClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        defaultMap.get("printer-name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    if (attribClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        String nameStr = attribClass.getStringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        urlConnection.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        return nameStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                urlConnection.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Get list of all CUPS printers using IPP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public static String[] getAllPrinters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            URL url = new URL("http", getServer(), getPort(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            final HttpURLConnection urlConnection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                IPPPrintService.getIPPConnection(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (urlConnection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                OutputStream os = (OutputStream)java.security.AccessController.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    doPrivileged(new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                return urlConnection.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                if (os == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                AttributeClass attCl[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    AttributeClass.ATTRIBUTES_CHARSET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    new AttributeClass("requested-attributes",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                       AttributeClass.TAG_KEYWORD,
542
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   336
                                       "printer-uri-supported")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                if (IPPPrintService.writeIPPRequest(os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                IPPPrintService.OP_CUPS_GET_PRINTERS, attCl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    InputStream is = urlConnection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    HashMap[] responseMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        IPPPrintService.readIPPResponse(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    urlConnection.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    if (responseMap == null || responseMap.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    ArrayList printerNames = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    for (int i=0; i< responseMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        AttributeClass attribClass = (AttributeClass)
542
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   357
                            responseMap[i].get("printer-uri-supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        if (attribClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                            String nameStr = attribClass.getStringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                            printerNames.add(nameStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    return (String[])printerNames.toArray(new String[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    urlConnection.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Returns CUPS server name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public static String getServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return cupsServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * Returns CUPS port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public static int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return cupsPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Detects if CUPS is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public static boolean isCupsRunning() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        IPPPrintService.debug_println("libFound "+libFound);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (libFound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            IPPPrintService.debug_println("CUPS server "+getServer()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                          " port "+getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return canConnect(getServer(), getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
}