229 MediaTray mt; 230 for (int i=0; i<nTrays; i++) { 231 mt = new CustomMediaTray(media[(nPageSizes+i)*2], 232 media[(nPageSizes+i)*2+1]); 233 cupsMediaTrays[i] = mt; 234 } 235 236 } 237 238 /** 239 * Get CUPS default printer using IPP. 240 * Returns 2 values - index 0 is printer name, index 1 is the uri. 241 */ 242 static String[] getDefaultPrinter() { 243 try { 244 URL url = new URL("http", getServer(), getPort(), ""); 245 final HttpURLConnection urlConnection = 246 IPPPrintService.getIPPConnection(url); 247 248 if (urlConnection != null) { 249 OutputStream os = (OutputStream)java.security.AccessController. 250 doPrivileged(new java.security.PrivilegedAction() { 251 public Object run() { 252 try { 253 return urlConnection.getOutputStream(); 254 } catch (Exception e) { 255 IPPPrintService.debug_println(debugPrefix+e); 256 } 257 return null; 258 } 259 }); 260 261 if (os == null) { 262 return null; 263 } 264 265 AttributeClass attCl[] = { 266 AttributeClass.ATTRIBUTES_CHARSET, 267 AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE, 268 new AttributeClass("requested-attributes", 269 AttributeClass.TAG_URI, 270 "printer-uri") 271 }; 272 273 if (IPPPrintService.writeIPPRequest(os, 274 IPPPrintService.OP_CUPS_GET_DEFAULT, 275 attCl)) { 276 277 HashMap defaultMap = null; 278 String[] printerInfo = new String[2]; 279 InputStream is = urlConnection.getInputStream(); 280 HashMap[] responseMap = IPPPrintService.readIPPResponse( 281 is); 282 is.close(); 283 284 if (responseMap != null && responseMap.length > 0) { 285 defaultMap = responseMap[0]; 286 } else { 287 IPPPrintService.debug_println(debugPrefix+ 288 " empty response map for GET_DEFAULT."); 289 } 290 291 if (defaultMap == null) { 292 os.close(); 293 urlConnection.disconnect(); 294 295 /* CUPS on OS X, as initially configured, considers the 296 * default printer to be the last one used that's 297 * presently available. So if no default was 298 * reported, exec lpstat -d which has all the Apple 299 * special behaviour for this built in. 300 */ 301 if (UnixPrintServiceLookup.isMac()) { 302 printerInfo[0] = UnixPrintServiceLookup. 303 getDefaultPrinterNameSysV(); 304 printerInfo[1] = null; 305 return printerInfo.clone(); 306 } else { 307 return null; 308 } 309 } 310 311 312 AttributeClass attribClass = (AttributeClass) 313 defaultMap.get("printer-name"); 314 315 if (attribClass != null) { 316 printerInfo[0] = attribClass.getStringValue(); 317 attribClass = (AttributeClass) 318 defaultMap.get("printer-uri-supported"); 319 IPPPrintService.debug_println(debugPrefix+ 320 "printer-uri-supported="+attribClass); 321 if (attribClass != null) { 322 printerInfo[1] = attribClass.getStringValue(); 323 } else { 324 printerInfo[1] = null; 325 } 326 os.close(); 327 urlConnection.disconnect(); 328 return printerInfo.clone(); 329 } 330 } 331 os.close(); 332 urlConnection.disconnect(); 333 } 334 } catch (Exception e) { 335 } 336 return null; 337 } 338 339 340 /** 341 * Get list of all CUPS printers using IPP. 342 */ 343 static String[] getAllPrinters() { 344 try { 345 URL url = new URL("http", getServer(), getPort(), ""); 346 347 final HttpURLConnection urlConnection = 348 IPPPrintService.getIPPConnection(url); 349 350 if (urlConnection != null) { 351 OutputStream os = (OutputStream)java.security.AccessController. 352 doPrivileged(new java.security.PrivilegedAction() { 353 public Object run() { 354 try { 355 return urlConnection.getOutputStream(); 356 } catch (Exception e) { 357 } 358 return null; 359 } 360 }); 361 362 if (os == null) { 363 return null; 364 } 365 366 AttributeClass attCl[] = { 367 AttributeClass.ATTRIBUTES_CHARSET, 368 AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE, 369 new AttributeClass("requested-attributes", 370 AttributeClass.TAG_KEYWORD, 371 "printer-uri-supported") 372 }; 373 374 if (IPPPrintService.writeIPPRequest(os, 375 IPPPrintService.OP_CUPS_GET_PRINTERS, attCl)) { 376 377 InputStream is = urlConnection.getInputStream(); 378 HashMap[] responseMap = 379 IPPPrintService.readIPPResponse(is); 380 381 is.close(); 382 os.close(); 383 urlConnection.disconnect(); 384 385 if (responseMap == null || responseMap.length == 0) { 386 return null; 387 } 388 389 ArrayList printerNames = new ArrayList(); 390 for (int i=0; i< responseMap.length; i++) { 391 AttributeClass attribClass = (AttributeClass) 392 responseMap[i].get("printer-uri-supported"); 393 394 if (attribClass != null) { 395 String nameStr = attribClass.getStringValue(); 396 printerNames.add(nameStr); 397 } 398 } 399 return (String[])printerNames.toArray(new String[] {}); 400 } else { 401 os.close(); 402 urlConnection.disconnect(); 403 } 404 } 405 406 } catch (Exception e) { 407 } 408 return null; 409 410 } 411 412 /** 413 * Returns CUPS server name. 414 */ 415 public static String getServer() { 416 return cupsServer; 417 } 418 419 /** | 229 MediaTray mt; 230 for (int i=0; i<nTrays; i++) { 231 mt = new CustomMediaTray(media[(nPageSizes+i)*2], 232 media[(nPageSizes+i)*2+1]); 233 cupsMediaTrays[i] = mt; 234 } 235 236 } 237 238 /** 239 * Get CUPS default printer using IPP. 240 * Returns 2 values - index 0 is printer name, index 1 is the uri. 241 */ 242 static String[] getDefaultPrinter() { 243 try { 244 URL url = new URL("http", getServer(), getPort(), ""); 245 final HttpURLConnection urlConnection = 246 IPPPrintService.getIPPConnection(url); 247 248 if (urlConnection != null) { 249 OutputStream os = java.security.AccessController. 250 doPrivileged(new java.security.PrivilegedAction<OutputStream>() { 251 public OutputStream run() { 252 try { 253 return urlConnection.getOutputStream(); 254 } catch (Exception e) { 255 IPPPrintService.debug_println(debugPrefix+e); 256 } 257 return null; 258 } 259 }); 260 261 if (os == null) { 262 return null; 263 } 264 265 AttributeClass attCl[] = { 266 AttributeClass.ATTRIBUTES_CHARSET, 267 AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE, 268 new AttributeClass("requested-attributes", 269 AttributeClass.TAG_URI, 270 "printer-uri") 271 }; 272 273 if (IPPPrintService.writeIPPRequest(os, 274 IPPPrintService.OP_CUPS_GET_DEFAULT, 275 attCl)) { 276 277 HashMap<String, AttributeClass> defaultMap = null; 278 String[] printerInfo = new String[2]; 279 InputStream is = urlConnection.getInputStream(); 280 HashMap<String, AttributeClass>[] responseMap = IPPPrintService.readIPPResponse( 281 is); 282 is.close(); 283 284 if (responseMap != null && responseMap.length > 0) { 285 defaultMap = responseMap[0]; 286 } else { 287 IPPPrintService.debug_println(debugPrefix+ 288 " empty response map for GET_DEFAULT."); 289 } 290 291 if (defaultMap == null) { 292 os.close(); 293 urlConnection.disconnect(); 294 295 /* CUPS on OS X, as initially configured, considers the 296 * default printer to be the last one used that's 297 * presently available. So if no default was 298 * reported, exec lpstat -d which has all the Apple 299 * special behaviour for this built in. 300 */ 301 if (UnixPrintServiceLookup.isMac()) { 302 printerInfo[0] = UnixPrintServiceLookup. 303 getDefaultPrinterNameSysV(); 304 printerInfo[1] = null; 305 return printerInfo.clone(); 306 } else { 307 return null; 308 } 309 } 310 311 312 AttributeClass attribClass = defaultMap.get("printer-name"); 313 314 if (attribClass != null) { 315 printerInfo[0] = attribClass.getStringValue(); 316 attribClass = defaultMap.get("printer-uri-supported"); 317 IPPPrintService.debug_println(debugPrefix+ 318 "printer-uri-supported="+attribClass); 319 if (attribClass != null) { 320 printerInfo[1] = attribClass.getStringValue(); 321 } else { 322 printerInfo[1] = null; 323 } 324 os.close(); 325 urlConnection.disconnect(); 326 return printerInfo.clone(); 327 } 328 } 329 os.close(); 330 urlConnection.disconnect(); 331 } 332 } catch (Exception e) { 333 } 334 return null; 335 } 336 337 338 /** 339 * Get list of all CUPS printers using IPP. 340 */ 341 static String[] getAllPrinters() { 342 try { 343 URL url = new URL("http", getServer(), getPort(), ""); 344 345 final HttpURLConnection urlConnection = 346 IPPPrintService.getIPPConnection(url); 347 348 if (urlConnection != null) { 349 OutputStream os = java.security.AccessController. 350 doPrivileged(new java.security.PrivilegedAction<OutputStream>() { 351 public OutputStream run() { 352 try { 353 return urlConnection.getOutputStream(); 354 } catch (Exception e) { 355 } 356 return null; 357 } 358 }); 359 360 if (os == null) { 361 return null; 362 } 363 364 AttributeClass attCl[] = { 365 AttributeClass.ATTRIBUTES_CHARSET, 366 AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE, 367 new AttributeClass("requested-attributes", 368 AttributeClass.TAG_KEYWORD, 369 "printer-uri-supported") 370 }; 371 372 if (IPPPrintService.writeIPPRequest(os, 373 IPPPrintService.OP_CUPS_GET_PRINTERS, attCl)) { 374 375 InputStream is = urlConnection.getInputStream(); 376 HashMap<String, AttributeClass>[] responseMap = 377 IPPPrintService.readIPPResponse(is); 378 379 is.close(); 380 os.close(); 381 urlConnection.disconnect(); 382 383 if (responseMap == null || responseMap.length == 0) { 384 return null; 385 } 386 387 ArrayList<String> printerNames = new ArrayList<>(); 388 for (int i=0; i< responseMap.length; i++) { 389 AttributeClass attribClass = 390 responseMap[i].get("printer-uri-supported"); 391 392 if (attribClass != null) { 393 String nameStr = attribClass.getStringValue(); 394 printerNames.add(nameStr); 395 } 396 } 397 return printerNames.toArray(new String[] {}); 398 } else { 399 os.close(); 400 urlConnection.disconnect(); 401 } 402 } 403 404 } catch (Exception e) { 405 } 406 return null; 407 408 } 409 410 /** 411 * Returns CUPS server name. 412 */ 413 public static String getServer() { 414 return cupsServer; 415 } 416 417 /** |