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 }
256 return null;
257 }
258 });
259
260 if (os == null) {
261 return null;
262 }
263
264 AttributeClass attCl[] = {
265 AttributeClass.ATTRIBUTES_CHARSET,
266 AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
267 new AttributeClass("requested-attributes",
268 AttributeClass.TAG_URI,
269 "printer-uri")
270 };
271
272 if (IPPPrintService.writeIPPRequest(os,
273 IPPPrintService.OP_CUPS_GET_DEFAULT,
274 attCl)) {
275
276 HashMap defaultMap = null;
277 String[] printerInfo = new String[2];
278 InputStream is = urlConnection.getInputStream();
279 HashMap[] responseMap = IPPPrintService.readIPPResponse(
280 is);
281 is.close();
282
283 if (responseMap != null && responseMap.length > 0) {
284 defaultMap = responseMap[0];
285 }
286
287 if (defaultMap == null) {
288 os.close();
289 urlConnection.disconnect();
290
291 /* CUPS on OS X, as initially configured, considers the
292 * default printer to be the last one used that's
293 * presently available. So if no default was
294 * reported, exec lpstat -d which has all the Apple
295 * special behaviour for this built in.
296 */
297 if (UnixPrintServiceLookup.isMac()) {
298 printerInfo[0] = UnixPrintServiceLookup.
299 getDefaultPrinterNameSysV();
300 printerInfo[1] = null;
301 return printerInfo.clone();
302 } else {
303 return null;
304 }
305 }
306
307
308 AttributeClass attribClass = (AttributeClass)
309 defaultMap.get("printer-name");
310
311 if (attribClass != null) {
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 }
319 os.close();
320 urlConnection.disconnect();
321 return printerInfo.clone();
322 }
323 }
324 os.close();
325 urlConnection.disconnect();
326 }
327 } catch (Exception e) {
328 }
329 return null;
330 }
331
332
333 /**
|
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 /**
|