235 |
235 |
236 } |
236 } |
237 |
237 |
238 /** |
238 /** |
239 * Get CUPS default printer using IPP. |
239 * Get CUPS default printer using IPP. |
240 */ |
240 * Returns 2 values - index 0 is printer name, index 1 is the uri. |
241 public static String getDefaultPrinter() { |
241 */ |
|
242 static String[] getDefaultPrinter() { |
242 try { |
243 try { |
243 URL url = new URL("http", getServer(), getPort(), ""); |
244 URL url = new URL("http", getServer(), getPort(), ""); |
244 final HttpURLConnection urlConnection = |
245 final HttpURLConnection urlConnection = |
245 IPPPrintService.getIPPConnection(url); |
246 IPPPrintService.getIPPConnection(url); |
246 |
247 |
262 |
263 |
263 AttributeClass attCl[] = { |
264 AttributeClass attCl[] = { |
264 AttributeClass.ATTRIBUTES_CHARSET, |
265 AttributeClass.ATTRIBUTES_CHARSET, |
265 AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE, |
266 AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE, |
266 new AttributeClass("requested-attributes", |
267 new AttributeClass("requested-attributes", |
267 AttributeClass.TAG_KEYWORD, |
268 AttributeClass.TAG_URI, |
268 "printer-name") |
269 "printer-uri") |
269 }; |
270 }; |
270 |
271 |
271 if (IPPPrintService.writeIPPRequest(os, |
272 if (IPPPrintService.writeIPPRequest(os, |
272 IPPPrintService.OP_CUPS_GET_DEFAULT, |
273 IPPPrintService.OP_CUPS_GET_DEFAULT, |
273 attCl)) { |
274 attCl)) { |
274 |
275 |
275 HashMap defaultMap = null; |
276 HashMap defaultMap = null; |
|
277 String[] printerInfo = new String[2]; |
276 InputStream is = urlConnection.getInputStream(); |
278 InputStream is = urlConnection.getInputStream(); |
277 HashMap[] responseMap = IPPPrintService.readIPPResponse( |
279 HashMap[] responseMap = IPPPrintService.readIPPResponse( |
278 is); |
280 is); |
279 is.close(); |
281 is.close(); |
280 |
282 |
291 * presently available. So if no default was |
293 * presently available. So if no default was |
292 * reported, exec lpstat -d which has all the Apple |
294 * reported, exec lpstat -d which has all the Apple |
293 * special behaviour for this built in. |
295 * special behaviour for this built in. |
294 */ |
296 */ |
295 if (UnixPrintServiceLookup.isMac()) { |
297 if (UnixPrintServiceLookup.isMac()) { |
296 return UnixPrintServiceLookup. |
298 printerInfo[0] = UnixPrintServiceLookup. |
297 getDefaultPrinterNameSysV(); |
299 getDefaultPrinterNameSysV(); |
|
300 printerInfo[1] = null; |
|
301 return (String[])printerInfo.clone(); |
298 } else { |
302 } else { |
299 return null; |
303 return null; |
300 } |
304 } |
301 } |
305 } |
302 |
306 |
|
307 |
303 AttributeClass attribClass = (AttributeClass) |
308 AttributeClass attribClass = (AttributeClass) |
304 defaultMap.get("printer-name"); |
309 defaultMap.get("printer-name"); |
305 |
310 |
306 if (attribClass != null) { |
311 if (attribClass != null) { |
307 String nameStr = attribClass.getStringValue(); |
312 printerInfo[0] = attribClass.getStringValue(); |
|
313 attribClass = (AttributeClass)defaultMap.get("device-uri"); |
|
314 if (attribClass != null) { |
|
315 printerInfo[1] = attribClass.getStringValue(); |
|
316 } else { |
|
317 printerInfo[1] = null; |
|
318 } |
308 os.close(); |
319 os.close(); |
309 urlConnection.disconnect(); |
320 urlConnection.disconnect(); |
310 return nameStr; |
321 return (String [])printerInfo.clone(); |
311 } |
322 } |
312 } |
323 } |
313 os.close(); |
324 os.close(); |
314 urlConnection.disconnect(); |
325 urlConnection.disconnect(); |
315 } |
326 } |
320 |
331 |
321 |
332 |
322 /** |
333 /** |
323 * Get list of all CUPS printers using IPP. |
334 * Get list of all CUPS printers using IPP. |
324 */ |
335 */ |
325 public static String[] getAllPrinters() { |
336 static String[] getAllPrinters() { |
326 try { |
337 try { |
327 URL url = new URL("http", getServer(), getPort(), ""); |
338 URL url = new URL("http", getServer(), getPort(), ""); |
328 |
339 |
329 final HttpURLConnection urlConnection = |
340 final HttpURLConnection urlConnection = |
330 IPPPrintService.getIPPConnection(url); |
341 IPPPrintService.getIPPConnection(url); |