< prev index next >

src/java.desktop/unix/classes/sun/print/CUPSPrinter.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 252 
 253         // initialize trays
 254         cupsMediaTrays = new MediaTray[nTrays];
 255 
 256         MediaTray mt;
 257         for (int i=0; i<nTrays; i++) {
 258             mt = new CustomMediaTray(media[(nPageSizes+i)*2],
 259                                      media[(nPageSizes+i)*2+1]);
 260             cupsMediaTrays[i] = mt;
 261         }
 262 
 263     }
 264 
 265     /**
 266      * Get CUPS default printer using IPP.
 267      * Returns 2 values - index 0 is printer name, index 1 is the uri.
 268      */
 269     static String[] getDefaultPrinter() {
 270         // Try to get user/lpoptions-defined printer name from CUPS
 271         // if not user-set, then go for server default destination
 272         String printerInfo[] = new String[2];
 273         printerInfo[0] = getCupsDefaultPrinter();
 274 
 275         if (printerInfo[0] != null) {
 276             printerInfo[1] = null;
 277             return printerInfo.clone();
 278         }
 279         try {
 280             URL url = new URL("http", getServer(), getPort(), "");
 281             final HttpURLConnection urlConnection =
 282                 IPPPrintService.getIPPConnection(url);
 283 
 284             if (urlConnection != null) {
 285                 OutputStream os = java.security.AccessController.
 286                     doPrivileged(new java.security.PrivilegedAction<OutputStream>() {
 287                         public OutputStream run() {
 288                             try {
 289                                 return urlConnection.getOutputStream();
 290                             } catch (Exception e) {
 291                                IPPPrintService.debug_println(debugPrefix+e);
 292                             }
 293                             return null;
 294                         }
 295                     });
 296 
 297                 if (os == null) {
 298                     return null;
 299                 }
 300 
 301                 AttributeClass attCl[] = {
 302                     AttributeClass.ATTRIBUTES_CHARSET,
 303                     AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
 304                     new AttributeClass("requested-attributes",
 305                                        AttributeClass.TAG_URI,
 306                                        "printer-uri")
 307                 };
 308 
 309                 if (IPPPrintService.writeIPPRequest(os,
 310                                         IPPPrintService.OP_CUPS_GET_DEFAULT,
 311                                         attCl)) {
 312 
 313                     HashMap<String, AttributeClass> defaultMap = null;
 314 
 315                     InputStream is = urlConnection.getInputStream();
 316                     HashMap<String, AttributeClass>[] responseMap = IPPPrintService.readIPPResponse(
 317                                          is);
 318                     is.close();
 319 
 320                     if (responseMap != null && responseMap.length > 0) {
 321                         defaultMap = responseMap[0];


 380 
 381             final HttpURLConnection urlConnection =
 382                 IPPPrintService.getIPPConnection(url);
 383 
 384             if (urlConnection != null) {
 385                 OutputStream os = java.security.AccessController.
 386                     doPrivileged(new java.security.PrivilegedAction<OutputStream>() {
 387                         public OutputStream run() {
 388                             try {
 389                                 return urlConnection.getOutputStream();
 390                             } catch (Exception e) {
 391                             }
 392                             return null;
 393                         }
 394                     });
 395 
 396                 if (os == null) {
 397                     return null;
 398                 }
 399 
 400                 AttributeClass attCl[] = {
 401                     AttributeClass.ATTRIBUTES_CHARSET,
 402                     AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
 403                     new AttributeClass("requested-attributes",
 404                                        AttributeClass.TAG_KEYWORD,
 405                                        "printer-uri-supported")
 406                 };
 407 
 408                 if (IPPPrintService.writeIPPRequest(os,
 409                                 IPPPrintService.OP_CUPS_GET_PRINTERS, attCl)) {
 410 
 411                     InputStream is = urlConnection.getInputStream();
 412                     HashMap<String, AttributeClass>[] responseMap =
 413                         IPPPrintService.readIPPResponse(is);
 414 
 415                     is.close();
 416                     os.close();
 417                     urlConnection.disconnect();
 418 
 419                     if (responseMap == null || responseMap.length == 0) {
 420                         return null;


   1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 252 
 253         // initialize trays
 254         cupsMediaTrays = new MediaTray[nTrays];
 255 
 256         MediaTray mt;
 257         for (int i=0; i<nTrays; i++) {
 258             mt = new CustomMediaTray(media[(nPageSizes+i)*2],
 259                                      media[(nPageSizes+i)*2+1]);
 260             cupsMediaTrays[i] = mt;
 261         }
 262 
 263     }
 264 
 265     /**
 266      * Get CUPS default printer using IPP.
 267      * Returns 2 values - index 0 is printer name, index 1 is the uri.
 268      */
 269     static String[] getDefaultPrinter() {
 270         // Try to get user/lpoptions-defined printer name from CUPS
 271         // if not user-set, then go for server default destination
 272         String[] printerInfo = new String[2];
 273         printerInfo[0] = getCupsDefaultPrinter();
 274 
 275         if (printerInfo[0] != null) {
 276             printerInfo[1] = null;
 277             return printerInfo.clone();
 278         }
 279         try {
 280             URL url = new URL("http", getServer(), getPort(), "");
 281             final HttpURLConnection urlConnection =
 282                 IPPPrintService.getIPPConnection(url);
 283 
 284             if (urlConnection != null) {
 285                 OutputStream os = java.security.AccessController.
 286                     doPrivileged(new java.security.PrivilegedAction<OutputStream>() {
 287                         public OutputStream run() {
 288                             try {
 289                                 return urlConnection.getOutputStream();
 290                             } catch (Exception e) {
 291                                IPPPrintService.debug_println(debugPrefix+e);
 292                             }
 293                             return null;
 294                         }
 295                     });
 296 
 297                 if (os == null) {
 298                     return null;
 299                 }
 300 
 301                 AttributeClass[] attCl = {
 302                     AttributeClass.ATTRIBUTES_CHARSET,
 303                     AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
 304                     new AttributeClass("requested-attributes",
 305                                        AttributeClass.TAG_URI,
 306                                        "printer-uri")
 307                 };
 308 
 309                 if (IPPPrintService.writeIPPRequest(os,
 310                                         IPPPrintService.OP_CUPS_GET_DEFAULT,
 311                                         attCl)) {
 312 
 313                     HashMap<String, AttributeClass> defaultMap = null;
 314 
 315                     InputStream is = urlConnection.getInputStream();
 316                     HashMap<String, AttributeClass>[] responseMap = IPPPrintService.readIPPResponse(
 317                                          is);
 318                     is.close();
 319 
 320                     if (responseMap != null && responseMap.length > 0) {
 321                         defaultMap = responseMap[0];


 380 
 381             final HttpURLConnection urlConnection =
 382                 IPPPrintService.getIPPConnection(url);
 383 
 384             if (urlConnection != null) {
 385                 OutputStream os = java.security.AccessController.
 386                     doPrivileged(new java.security.PrivilegedAction<OutputStream>() {
 387                         public OutputStream run() {
 388                             try {
 389                                 return urlConnection.getOutputStream();
 390                             } catch (Exception e) {
 391                             }
 392                             return null;
 393                         }
 394                     });
 395 
 396                 if (os == null) {
 397                     return null;
 398                 }
 399 
 400                 AttributeClass[] attCl = {
 401                     AttributeClass.ATTRIBUTES_CHARSET,
 402                     AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
 403                     new AttributeClass("requested-attributes",
 404                                        AttributeClass.TAG_KEYWORD,
 405                                        "printer-uri-supported")
 406                 };
 407 
 408                 if (IPPPrintService.writeIPPRequest(os,
 409                                 IPPPrintService.OP_CUPS_GET_PRINTERS, attCl)) {
 410 
 411                     InputStream is = urlConnection.getInputStream();
 412                     HashMap<String, AttributeClass>[] responseMap =
 413                         IPPPrintService.readIPPResponse(is);
 414 
 415                     is.close();
 416                     os.close();
 417                     urlConnection.disconnect();
 418 
 419                     if (responseMap == null || responseMap.length == 0) {
 420                         return null;


< prev index next >