35 import sun.print.CustomMediaSizeName;
36 import sun.print.CustomMediaTray;
37 import javax.print.attribute.standard.Media;
38 import javax.print.attribute.standard.MediaSizeName;
39 import javax.print.attribute.standard.MediaSize;
40 import javax.print.attribute.standard.MediaTray;
41 import javax.print.attribute.standard.MediaPrintableArea;
42 import javax.print.attribute.standard.PrinterResolution;
43 import javax.print.attribute.Size2DSyntax;
44 import javax.print.attribute.Attribute;
45 import javax.print.attribute.EnumSyntax;
46 import javax.print.attribute.standard.PrinterName;
47
48
49 public class CUPSPrinter {
50 private static final String debugPrefix = "CUPSPrinter>> ";
51 private static final double PRINTER_DPI = 72.0;
52 private boolean initialized;
53 private static native String getCupsServer();
54 private static native int getCupsPort();
55 private static native boolean canConnect(String server, int port);
56 private static native boolean initIDs();
57 // These functions need to be synchronized as
58 // CUPS does not support multi-threading.
59 private static synchronized native String[] getMedia(String printer);
60 private static synchronized native float[] getPageSizes(String printer);
61 private static synchronized native void
62 getResolutions(String printer, ArrayList<Integer> resolutionList);
63 //public static boolean useIPPMedia = false; will be used later
64
65 private MediaPrintableArea[] cupsMediaPrintables;
66 private MediaSizeName[] cupsMediaSNames;
67 private CustomMediaSizeName[] cupsCustomMediaSNames;
68 private MediaTray[] cupsMediaTrays;
69
70 public int nPageSizes = 0;
71 public int nTrays = 0;
72 private String[] media;
73 private float[] pageSizes;
74 int[] resolutionsArray;
249 cupsMediaPrintables[i] = mpa;
250 }
251
252 // initialize trays
253 cupsMediaTrays = new MediaTray[nTrays];
254
255 MediaTray mt;
256 for (int i=0; i<nTrays; i++) {
257 mt = new CustomMediaTray(media[(nPageSizes+i)*2],
258 media[(nPageSizes+i)*2+1]);
259 cupsMediaTrays[i] = mt;
260 }
261
262 }
263
264 /**
265 * Get CUPS default printer using IPP.
266 * Returns 2 values - index 0 is printer name, index 1 is the uri.
267 */
268 static String[] getDefaultPrinter() {
269 try {
270 URL url = new URL("http", getServer(), getPort(), "");
271 final HttpURLConnection urlConnection =
272 IPPPrintService.getIPPConnection(url);
273
274 if (urlConnection != null) {
275 OutputStream os = java.security.AccessController.
276 doPrivileged(new java.security.PrivilegedAction<OutputStream>() {
277 public OutputStream run() {
278 try {
279 return urlConnection.getOutputStream();
280 } catch (Exception e) {
281 IPPPrintService.debug_println(debugPrefix+e);
282 }
283 return null;
284 }
285 });
286
287 if (os == null) {
288 return null;
289 }
290
291 AttributeClass attCl[] = {
292 AttributeClass.ATTRIBUTES_CHARSET,
293 AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
294 new AttributeClass("requested-attributes",
295 AttributeClass.TAG_URI,
296 "printer-uri")
297 };
298
299 if (IPPPrintService.writeIPPRequest(os,
300 IPPPrintService.OP_CUPS_GET_DEFAULT,
301 attCl)) {
302
303 HashMap<String, AttributeClass> defaultMap = null;
304 String[] printerInfo = new String[2];
305 InputStream is = urlConnection.getInputStream();
306 HashMap<String, AttributeClass>[] responseMap = IPPPrintService.readIPPResponse(
307 is);
308 is.close();
309
310 if (responseMap != null && responseMap.length > 0) {
311 defaultMap = responseMap[0];
312 } else {
313 IPPPrintService.debug_println(debugPrefix+
314 " empty response map for GET_DEFAULT.");
315 }
316
317 if (defaultMap == null) {
318 os.close();
319 urlConnection.disconnect();
320
321 /* CUPS on OS X, as initially configured, considers the
322 * default printer to be the last one used that's
323 * presently available. So if no default was
324 * reported, exec lpstat -d which has all the Apple
|
35 import sun.print.CustomMediaSizeName;
36 import sun.print.CustomMediaTray;
37 import javax.print.attribute.standard.Media;
38 import javax.print.attribute.standard.MediaSizeName;
39 import javax.print.attribute.standard.MediaSize;
40 import javax.print.attribute.standard.MediaTray;
41 import javax.print.attribute.standard.MediaPrintableArea;
42 import javax.print.attribute.standard.PrinterResolution;
43 import javax.print.attribute.Size2DSyntax;
44 import javax.print.attribute.Attribute;
45 import javax.print.attribute.EnumSyntax;
46 import javax.print.attribute.standard.PrinterName;
47
48
49 public class CUPSPrinter {
50 private static final String debugPrefix = "CUPSPrinter>> ";
51 private static final double PRINTER_DPI = 72.0;
52 private boolean initialized;
53 private static native String getCupsServer();
54 private static native int getCupsPort();
55 private static native String getCupsDefaultPrinter();
56 private static native boolean canConnect(String server, int port);
57 private static native boolean initIDs();
58 // These functions need to be synchronized as
59 // CUPS does not support multi-threading.
60 private static synchronized native String[] getMedia(String printer);
61 private static synchronized native float[] getPageSizes(String printer);
62 private static synchronized native void
63 getResolutions(String printer, ArrayList<Integer> resolutionList);
64 //public static boolean useIPPMedia = false; will be used later
65
66 private MediaPrintableArea[] cupsMediaPrintables;
67 private MediaSizeName[] cupsMediaSNames;
68 private CustomMediaSizeName[] cupsCustomMediaSNames;
69 private MediaTray[] cupsMediaTrays;
70
71 public int nPageSizes = 0;
72 public int nTrays = 0;
73 private String[] media;
74 private float[] pageSizes;
75 int[] resolutionsArray;
250 cupsMediaPrintables[i] = mpa;
251 }
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];
322 } else {
323 IPPPrintService.debug_println(debugPrefix+
324 " empty response map for GET_DEFAULT.");
325 }
326
327 if (defaultMap == null) {
328 os.close();
329 urlConnection.disconnect();
330
331 /* CUPS on OS X, as initially configured, considers the
332 * default printer to be the last one used that's
333 * presently available. So if no default was
334 * reported, exec lpstat -d which has all the Apple
|