src/java.desktop/unix/classes/sun/print/CUPSPrinter.java
author mdoerr
Tue, 13 Feb 2018 17:38:03 +0100
changeset 48965 be873d8c0114
parent 47216 71c04702a3d5
child 52248 2e330da7cbf4
permissions -rw-r--r--
8196786: [PPC64+s390] ConstantDynamic support Reviewed-by: psandoz, dsamersoff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21224
diff changeset
     2
 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1737
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1737
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1737
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1737
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1737
diff changeset
    23
 * questions.
2
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;
25774
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
    42
import javax.print.attribute.standard.PrinterResolution;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.print.attribute.Size2DSyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.print.attribute.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.print.attribute.EnumSyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.print.attribute.standard.PrinterName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class CUPSPrinter  {
1737
90d4fe987b09 6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents: 715
diff changeset
    50
    private static final String debugPrefix = "CUPSPrinter>> ";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final double PRINTER_DPI = 72.0;
1737
90d4fe987b09 6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents: 715
diff changeset
    52
    private boolean initialized;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static native String getCupsServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static native int getCupsPort();
36470
d5a6c81c5254 8058316: lookupDefaultPrintService returns null on Solaris 11
psadhukhan
parents: 27282
diff changeset
    55
    private static native String getCupsDefaultPrinter();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static native boolean canConnect(String server, int port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static native boolean initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // These functions need to be synchronized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // CUPS does not support multi-threading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static synchronized native String[] getMedia(String printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static synchronized native float[] getPageSizes(String printer);
25774
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
    62
    private static synchronized native void
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
    63
        getResolutions(String printer, ArrayList<Integer> resolutionList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    //public static boolean useIPPMedia = false; will be used later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private MediaPrintableArea[] cupsMediaPrintables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private MediaSizeName[] cupsMediaSNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private CustomMediaSizeName[] cupsCustomMediaSNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private MediaTray[] cupsMediaTrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public  int nPageSizes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public  int nTrays = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private  String[] media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private  float[] pageSizes;
25774
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
    75
    int[]   resolutionsArray;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private String printer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static boolean libFound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static String cupsServer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static int cupsPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // load awt library to access native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        java.security.AccessController.doPrivileged(
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    85
            new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    86
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    87
                    System.loadLibrary("awt");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    88
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    89
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    90
            });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        libFound = initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (libFound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
           cupsServer = getCupsServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
           cupsPort = getCupsPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    CUPSPrinter (String printerName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (printerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new IllegalArgumentException("null printer name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        printer = printerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        cupsMediaSNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        cupsMediaPrintables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        cupsMediaTrays = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (!libFound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new RuntimeException("cups lib not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // get page + tray names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            media =  getMedia(printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (media == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                // either PPD file is not found or printer is unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                throw new RuntimeException("error getting PPD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // get sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            pageSizes = getPageSizes(printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (pageSizes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                nPageSizes = pageSizes.length/6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                nTrays = media.length/2-nPageSizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                assert (nTrays >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
25774
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
   127
            ArrayList<Integer> resolutionList = new ArrayList<>();
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
   128
            getResolutions(printer, resolutionList);
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
   129
            resolutionsArray = new int[resolutionList.size()];
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
   130
            for (int i=0; i < resolutionList.size(); i++) {
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
   131
                resolutionsArray[i] = resolutionList.get(i);
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
   132
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Returns array of MediaSizeNames derived from PPD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
27082
7ab1aa5e8713 8035162: Service printing service
prr
parents: 25859
diff changeset
   140
    MediaSizeName[] getMediaSizeNames() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return cupsMediaSNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Returns array of Custom MediaSizeNames derived from PPD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
27082
7ab1aa5e8713 8035162: Service printing service
prr
parents: 25859
diff changeset
   149
    CustomMediaSizeName[] getCustomMediaSizeNames() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return cupsCustomMediaSNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
25555
b70a66b5b942 7067052: Default printer media is ignored
jgodinez
parents: 25140
diff changeset
   154
    public int getDefaultMediaIndex() {
b70a66b5b942 7067052: Default printer media is ignored
jgodinez
parents: 25140
diff changeset
   155
        return ((pageSizes.length >1) ? (int)(pageSizes[pageSizes.length -1]) : 0);
b70a66b5b942 7067052: Default printer media is ignored
jgodinez
parents: 25140
diff changeset
   156
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Returns array of MediaPrintableArea derived from PPD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
27082
7ab1aa5e8713 8035162: Service printing service
prr
parents: 25859
diff changeset
   161
    MediaPrintableArea[] getMediaPrintableArea() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return cupsMediaPrintables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Returns array of MediaTrays derived from PPD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
27082
7ab1aa5e8713 8035162: Service printing service
prr
parents: 25859
diff changeset
   169
    MediaTray[] getMediaTrays() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return cupsMediaTrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
25774
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
   174
    /**
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
   175
     * return the raw packed array of supported printer resolutions.
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
   176
     */
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
   177
    int[] getRawResolutions() {
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
   178
        return resolutionsArray;
21b78da4b2df 8048328: CUPS Printing does not report supported printer resolutions.
prr
parents: 25555
diff changeset
   179
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Initialize media by translating PPD info to PrintService attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
1737
90d4fe987b09 6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents: 715
diff changeset
   184
    private synchronized void initMedia() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (pageSizes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        cupsMediaPrintables = new MediaPrintableArea[nPageSizes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        cupsMediaSNames = new MediaSizeName[nPageSizes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        cupsCustomMediaSNames = new CustomMediaSizeName[nPageSizes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        CustomMediaSizeName msn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        MediaPrintableArea mpa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        float length, width, x, y, w, h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // initialize names and printables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        for (int i=0; i<nPageSizes; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // media width and length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            width = (float)(pageSizes[i*6]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            length = (float)(pageSizes[i*6+1]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            // media printable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            x = (float)(pageSizes[i*6+2]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            h = (float)(pageSizes[i*6+3]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            w = (float)(pageSizes[i*6+4]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            y = (float)(pageSizes[i*6+5]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            msn = new CustomMediaSizeName(media[i*2], media[i*2+1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                          width, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            // add to list of standard MediaSizeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if ((cupsMediaSNames[i] = msn.getStandardMedia()) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                // add custom if no matching standard media
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                cupsMediaSNames[i] = msn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                // add this new custom msn to MediaSize array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if ((width > 0.0) && (length > 0.0)) {
25555
b70a66b5b942 7067052: Default printer media is ignored
jgodinez
parents: 25140
diff changeset
   224
                    try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    new MediaSize(width, length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                  Size2DSyntax.INCH, msn);
25555
b70a66b5b942 7067052: Default printer media is ignored
jgodinez
parents: 25140
diff changeset
   227
                    } catch (IllegalArgumentException e) {
b70a66b5b942 7067052: Default printer media is ignored
jgodinez
parents: 25140
diff changeset
   228
                        /* PDF printer in Linux for Ledger paper causes
b70a66b5b942 7067052: Default printer media is ignored
jgodinez
parents: 25140
diff changeset
   229
                        "IllegalArgumentException: X dimension > Y dimension".
b70a66b5b942 7067052: Default printer media is ignored
jgodinez
parents: 25140
diff changeset
   230
                        We rotate based on IPP spec. */
b70a66b5b942 7067052: Default printer media is ignored
jgodinez
parents: 25140
diff changeset
   231
                        new MediaSize(length, width, Size2DSyntax.INCH, msn);
b70a66b5b942 7067052: Default printer media is ignored
jgodinez
parents: 25140
diff changeset
   232
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            // add to list of custom MediaSizeName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // for internal use of IPPPrintService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            cupsCustomMediaSNames[i] = msn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            mpa = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                mpa = new MediaPrintableArea(x, y, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                             MediaPrintableArea.INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                if (width > 0 && length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    mpa = new MediaPrintableArea(0, 0, width, length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                             MediaPrintableArea.INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            cupsMediaPrintables[i] = mpa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // initialize trays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        cupsMediaTrays = new MediaTray[nTrays];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        MediaTray mt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        for (int i=0; i<nTrays; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            mt = new CustomMediaTray(media[(nPageSizes+i)*2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                     media[(nPageSizes+i)*2+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            cupsMediaTrays[i] = mt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Get CUPS default printer using IPP.
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   267
     * Returns 2 values - index 0 is printer name, index 1 is the uri.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   269
    static String[] getDefaultPrinter() {
36470
d5a6c81c5254 8058316: lookupDefaultPrintService returns null on Solaris 11
psadhukhan
parents: 27282
diff changeset
   270
        // Try to get user/lpoptions-defined printer name from CUPS
d5a6c81c5254 8058316: lookupDefaultPrintService returns null on Solaris 11
psadhukhan
parents: 27282
diff changeset
   271
        // if not user-set, then go for server default destination
d5a6c81c5254 8058316: lookupDefaultPrintService returns null on Solaris 11
psadhukhan
parents: 27282
diff changeset
   272
        String printerInfo[] = new String[2];
d5a6c81c5254 8058316: lookupDefaultPrintService returns null on Solaris 11
psadhukhan
parents: 27282
diff changeset
   273
        printerInfo[0] = getCupsDefaultPrinter();
d5a6c81c5254 8058316: lookupDefaultPrintService returns null on Solaris 11
psadhukhan
parents: 27282
diff changeset
   274
d5a6c81c5254 8058316: lookupDefaultPrintService returns null on Solaris 11
psadhukhan
parents: 27282
diff changeset
   275
        if (printerInfo[0] != null) {
d5a6c81c5254 8058316: lookupDefaultPrintService returns null on Solaris 11
psadhukhan
parents: 27282
diff changeset
   276
            printerInfo[1] = null;
d5a6c81c5254 8058316: lookupDefaultPrintService returns null on Solaris 11
psadhukhan
parents: 27282
diff changeset
   277
            return printerInfo.clone();
d5a6c81c5254 8058316: lookupDefaultPrintService returns null on Solaris 11
psadhukhan
parents: 27282
diff changeset
   278
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            URL url = new URL("http", getServer(), getPort(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            final HttpURLConnection urlConnection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                IPPPrintService.getIPPConnection(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (urlConnection != null) {
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   285
                OutputStream os = java.security.AccessController.
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   286
                    doPrivileged(new java.security.PrivilegedAction<OutputStream>() {
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   287
                        public OutputStream run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                return urlConnection.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                            } catch (Exception e) {
23287
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents: 22584
diff changeset
   291
                               IPPPrintService.debug_println(debugPrefix+e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                if (os == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                AttributeClass attCl[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    AttributeClass.ATTRIBUTES_CHARSET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    new AttributeClass("requested-attributes",
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   305
                                       AttributeClass.TAG_URI,
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   306
                                       "printer-uri")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                if (IPPPrintService.writeIPPRequest(os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                        IPPPrintService.OP_CUPS_GET_DEFAULT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                        attCl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   313
                    HashMap<String, AttributeClass> defaultMap = null;
36470
d5a6c81c5254 8058316: lookupDefaultPrintService returns null on Solaris 11
psadhukhan
parents: 27282
diff changeset
   314
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    InputStream is = urlConnection.getInputStream();
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   316
                    HashMap<String, AttributeClass>[] responseMap = IPPPrintService.readIPPResponse(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                         is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   320
                    if (responseMap != null && responseMap.length > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        defaultMap = responseMap[0];
23287
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents: 22584
diff changeset
   322
                    } else {
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents: 22584
diff changeset
   323
                       IPPPrintService.debug_println(debugPrefix+
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents: 22584
diff changeset
   324
                           " empty response map for GET_DEFAULT.");
2
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 (defaultMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        urlConnection.disconnect();
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   330
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   331
                        /* CUPS on OS X, as initially configured, considers the
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   332
                         * default printer to be the last one used that's
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   333
                         * presently available. So if no default was
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   334
                         * reported, exec lpstat -d which has all the Apple
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   335
                         * special behaviour for this built in.
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   336
                         */
27269
1ef2879dc7ad 8055705: Rename UnixPrintServiceLookup and Win32PrintServiceLookup as a platform neutral class name
prr
parents: 25859
diff changeset
   337
                         if (PrintServiceLookupProvider.isMac()) {
1ef2879dc7ad 8055705: Rename UnixPrintServiceLookup and Win32PrintServiceLookup as a platform neutral class name
prr
parents: 25859
diff changeset
   338
                             printerInfo[0] = PrintServiceLookupProvider.
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   339
                                                   getDefaultPrinterNameSysV();
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   340
                             printerInfo[1] = null;
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21224
diff changeset
   341
                             return printerInfo.clone();
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   342
                         } else {
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   343
                             return null;
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   344
                         }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   347
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   348
                    AttributeClass attribClass = defaultMap.get("printer-name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    if (attribClass != null) {
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   351
                        printerInfo[0] = attribClass.getStringValue();
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   352
                        attribClass = defaultMap.get("printer-uri-supported");
23287
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents: 22584
diff changeset
   353
                        IPPPrintService.debug_println(debugPrefix+
c0f8cdafef56 8032693: javax.print.PrintService does not find any CUPS-Printers on Linux
prr
parents: 22584
diff changeset
   354
                          "printer-uri-supported="+attribClass);
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   355
                        if (attribClass != null) {
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   356
                            printerInfo[1] = attribClass.getStringValue();
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   357
                        } else {
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   358
                            printerInfo[1] = null;
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   359
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        urlConnection.disconnect();
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21224
diff changeset
   362
                        return printerInfo.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                urlConnection.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Get list of all CUPS printers using IPP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   377
    static String[] getAllPrinters() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            URL url = new URL("http", getServer(), getPort(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            final HttpURLConnection urlConnection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                IPPPrintService.getIPPConnection(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (urlConnection != null) {
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   385
                OutputStream os = java.security.AccessController.
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   386
                    doPrivileged(new java.security.PrivilegedAction<OutputStream>() {
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   387
                        public OutputStream run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                return urlConnection.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if (os == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                AttributeClass attCl[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    AttributeClass.ATTRIBUTES_CHARSET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    new AttributeClass("requested-attributes",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                       AttributeClass.TAG_KEYWORD,
542
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   405
                                       "printer-uri-supported")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                if (IPPPrintService.writeIPPRequest(os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                IPPPrintService.OP_CUPS_GET_PRINTERS, attCl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    InputStream is = urlConnection.getInputStream();
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   412
                    HashMap<String, AttributeClass>[] responseMap =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        IPPPrintService.readIPPResponse(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    urlConnection.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    if (responseMap == null || responseMap.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   423
                    ArrayList<String> printerNames = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    for (int i=0; i< responseMap.length; i++) {
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   425
                        AttributeClass attribClass =
542
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   426
                            responseMap[i].get("printer-uri-supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        if (attribClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                            String nameStr = attribClass.getStringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                            printerNames.add(nameStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    }
25140
80e863984492 8042870: Fix raw and unchecked warnings in sun.print
darcy
parents: 23287
diff changeset
   433
                    return printerNames.toArray(new String[] {});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    urlConnection.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Returns CUPS server name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public static String getServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        return cupsServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * Returns CUPS port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public static int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return cupsPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Detects if CUPS is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public static boolean isCupsRunning() {
1737
90d4fe987b09 6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents: 715
diff changeset
   464
        IPPPrintService.debug_println(debugPrefix+"libFound "+libFound);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (libFound) {
1737
90d4fe987b09 6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents: 715
diff changeset
   466
            IPPPrintService.debug_println(debugPrefix+"CUPS server "+getServer()+
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                                          " port "+getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            return canConnect(getServer(), getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
}