Source for gnu.javax.print.ipp.IppPrintService

   1: /* IppPrintService.java -- 
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.javax.print.ipp;
  40: 
  41: import gnu.classpath.SystemProperties;
  42: import gnu.classpath.debug.Component;
  43: import gnu.classpath.debug.SystemLogger;
  44: import gnu.javax.print.ipp.attribute.DefaultValueAttribute;
  45: import gnu.javax.print.ipp.attribute.RequestedAttributes;
  46: import gnu.javax.print.ipp.attribute.defaults.CopiesDefault;
  47: import gnu.javax.print.ipp.attribute.defaults.FinishingsDefault;
  48: import gnu.javax.print.ipp.attribute.defaults.JobHoldUntilDefault;
  49: import gnu.javax.print.ipp.attribute.defaults.JobPriorityDefault;
  50: import gnu.javax.print.ipp.attribute.defaults.JobSheetsDefault;
  51: import gnu.javax.print.ipp.attribute.defaults.MediaDefault;
  52: import gnu.javax.print.ipp.attribute.defaults.MultipleDocumentHandlingDefault;
  53: import gnu.javax.print.ipp.attribute.defaults.NumberUpDefault;
  54: import gnu.javax.print.ipp.attribute.defaults.OrientationRequestedDefault;
  55: import gnu.javax.print.ipp.attribute.defaults.PrintQualityDefault;
  56: import gnu.javax.print.ipp.attribute.defaults.PrinterResolutionDefault;
  57: import gnu.javax.print.ipp.attribute.defaults.SidesDefault;
  58: import gnu.javax.print.ipp.attribute.printer.DocumentFormat;
  59: import gnu.javax.print.ipp.attribute.supported.CompressionSupported;
  60: import gnu.javax.print.ipp.attribute.supported.DocumentFormatSupported;
  61: import gnu.javax.print.ipp.attribute.supported.FinishingsSupported;
  62: import gnu.javax.print.ipp.attribute.supported.JobHoldUntilSupported;
  63: import gnu.javax.print.ipp.attribute.supported.JobSheetsSupported;
  64: import gnu.javax.print.ipp.attribute.supported.MediaSupported;
  65: import gnu.javax.print.ipp.attribute.supported.MultipleDocumentHandlingSupported;
  66: import gnu.javax.print.ipp.attribute.supported.OperationsSupported;
  67: import gnu.javax.print.ipp.attribute.supported.OrientationRequestedSupported;
  68: import gnu.javax.print.ipp.attribute.supported.PageRangesSupported;
  69: import gnu.javax.print.ipp.attribute.supported.PrintQualitySupported;
  70: import gnu.javax.print.ipp.attribute.supported.PrinterResolutionSupported;
  71: import gnu.javax.print.ipp.attribute.supported.PrinterUriSupported;
  72: import gnu.javax.print.ipp.attribute.supported.SidesSupported;
  73: 
  74: import java.io.IOException;
  75: import java.lang.reflect.Field;
  76: import java.net.URI;
  77: import java.util.ArrayList;
  78: import java.util.Arrays;
  79: import java.util.Date;
  80: import java.util.HashSet;
  81: import java.util.Iterator;
  82: import java.util.List;
  83: import java.util.Map;
  84: import java.util.Set;
  85: import java.util.logging.Logger;
  86: 
  87: import javax.print.DocFlavor;
  88: import javax.print.DocPrintJob;
  89: import javax.print.PrintService;
  90: import javax.print.ServiceUIFactory;
  91: import javax.print.attribute.Attribute;
  92: import javax.print.attribute.AttributeSet;
  93: import javax.print.attribute.AttributeSetUtilities;
  94: import javax.print.attribute.HashAttributeSet;
  95: import javax.print.attribute.HashPrintServiceAttributeSet;
  96: import javax.print.attribute.IntegerSyntax;
  97: import javax.print.attribute.PrintServiceAttribute;
  98: import javax.print.attribute.PrintServiceAttributeSet;
  99: import javax.print.attribute.standard.Compression;
 100: import javax.print.attribute.standard.Copies;
 101: import javax.print.attribute.standard.CopiesSupported;
 102: import javax.print.attribute.standard.Fidelity;
 103: import javax.print.attribute.standard.Finishings;
 104: import javax.print.attribute.standard.JobHoldUntil;
 105: import javax.print.attribute.standard.JobImpressions;
 106: import javax.print.attribute.standard.JobImpressionsSupported;
 107: import javax.print.attribute.standard.JobKOctets;
 108: import javax.print.attribute.standard.JobKOctetsSupported;
 109: import javax.print.attribute.standard.JobMediaSheets;
 110: import javax.print.attribute.standard.JobMediaSheetsSupported;
 111: import javax.print.attribute.standard.JobName;
 112: import javax.print.attribute.standard.JobPriority;
 113: import javax.print.attribute.standard.JobPrioritySupported;
 114: import javax.print.attribute.standard.JobSheets;
 115: import javax.print.attribute.standard.Media;
 116: import javax.print.attribute.standard.MultipleDocumentHandling;
 117: import javax.print.attribute.standard.NumberUp;
 118: import javax.print.attribute.standard.NumberUpSupported;
 119: import javax.print.attribute.standard.OrientationRequested;
 120: import javax.print.attribute.standard.PageRanges;
 121: import javax.print.attribute.standard.PrintQuality;
 122: import javax.print.attribute.standard.PrinterName;
 123: import javax.print.attribute.standard.PrinterResolution;
 124: import javax.print.attribute.standard.PrinterURI;
 125: import javax.print.attribute.standard.RequestingUserName;
 126: import javax.print.attribute.standard.Sides;
 127: import javax.print.event.PrintServiceAttributeListener;
 128: 
 129: 
 130: /**
 131:  * Implementation of the PrintService interface
 132:  * for IPP based printers.
 133:  * 
 134:  * @author Wolfgang Baer (WBaer@gmx.de)
 135:  */
 136: public class IppPrintService implements PrintService
 137: {
 138:   /** 
 139:    * A Map with sets of attributes.
 140:    * key: A attribute category
 141:    * value: A set with values
 142:    * 
 143:    * IPP may return sets of attributes e.g. for supported
 144:    * compression methods so we need to map to sets here.
 145:    */
 146:   private Map printerAttr;
 147:     
 148:   /** The set of listeners.*/
 149:   private HashSet printServiceAttributeListener;
 150:   
 151:   /** The username. */
 152:   private transient String user;
 153:   
 154:   /** The password of the user. */
 155:   private transient String passwd;
 156:   
 157:   /** The name of this print service. */
 158:   private String name;
 159:   
 160:   /** The list of supported document flavors. */
 161:   private List flavors;
 162:   
 163:   /** The standard printer URI. */
 164:   private PrinterURI printerUri;
 165:   
 166:   /** The list of all supported printer URIs. */
 167:   private ArrayList printerUris;
 168:     
 169:   /**
 170:    * Logger for tracing - enable by passing
 171:    * -Dgnu.classpath.debug.components=ipp to the vm.
 172:    */
 173:   static final Logger logger = SystemLogger.SYSTEM;
 174:   
 175:   /** 
 176:    * requesting-user-name defaults to the executing user.
 177:    */
 178:   public static final RequestingUserName REQUESTING_USER_NAME;
 179:   
 180:   /** 
 181:    * job-name defaults to "Java Printing".
 182:    */
 183:   public static final JobName JOB_NAME;
 184:   
 185:   static
 186:   {
 187:     JOB_NAME = new JobName("Java Printing", null);
 188:     REQUESTING_USER_NAME = new RequestingUserName(
 189:       SystemProperties.getProperty("user.name", ""), null);    
 190:   }
 191:   
 192:   // TODO Implement service listener notification and change detection.
 193:   
 194:   /**
 195:    * Creates a <code>IppPrintService</code> object.
 196:    * 
 197:    * @param uri the URI of the IPP printer.
 198:    * @param username the user of this print service.
 199:    * @param password the password of the user.
 200:    * 
 201:    * @throws IppException if an error during connection occurs.
 202:    */
 203:   public IppPrintService(URI uri, String username, String password) 
 204:     throws IppException
 205:   {
 206:     printerUri = new PrinterURI(uri);
 207:     user = username;
 208:     passwd = password;
 209:     
 210:     printServiceAttributeListener = new HashSet();
 211:         
 212:     printerAttr = getPrinterAttributes();
 213:     processResponse();
 214:   }
 215:   
 216:   /**
 217:    * Fetches all printer attributes from the IPP printer.
 218:    * 
 219:    * @return The Map with the printer attributes.
 220:    * @throws IppException if an error occurs.
 221:    */
 222:   private Map getPrinterAttributes() throws IppException
 223:   {
 224:     IppResponse response = null;
 225:     
 226:     try
 227:       {
 228:         IppRequest request = new IppRequest(printerUri.getURI(), user, passwd);    
 229:         
 230:         int operation = OperationsSupported.GET_PRINTER_ATTRIBUTES.getValue();
 231:         request.setOperationID((short) operation);        
 232:         request.setOperationAttributeDefaults();  
 233:         request.addOperationAttribute(printerUri);
 234:         
 235:         response = request.send();
 236:       }   
 237:     catch (IOException e)
 238:       {
 239:         throw new IppException("IOException in IPP request/response.", e);
 240:       }   
 241:      
 242:     return (Map) response.getPrinterAttributes().get(0);   
 243:   }
 244:   
 245:   /**
 246:    * Extracts the set of attribute values for a given 
 247:    * attribute category from the printer attributes map.
 248:    * 
 249:    * @param attributeClass the category
 250:    * @return The set of attributes of the category.
 251:    */
 252:   private Set getPrinterAttributeSet(Class attributeClass)
 253:   {
 254:     return (Set) printerAttr.get(attributeClass);
 255:   }
 256:   
 257:   /**
 258:    * Extracts the default attribute value for the given 
 259:    * default attribute category from the printer attributes map.
 260:    * 
 261:    * @param attributeClass the category
 262:    * @return The default attribute.
 263:    * 
 264:    * @throws ClassCastException if attributClass is not an
 265:    * instance of <code>DefaultValueAttribute</code>.
 266:    */
 267:   private Attribute getPrinterDefaultAttribute(Class attributeClass)
 268:   {
 269:     Set set = (Set) printerAttr.get(attributeClass);
 270:     return ((DefaultValueAttribute) set.toArray()[0]).getAssociatedAttribute();
 271:   }
 272:   
 273:   /**
 274:    * Processes the response, sorts and splits the attributes.
 275:    */
 276:   private void processResponse()
 277:   {
 278:     // printer name
 279:     PrinterName[] tmp = (PrinterName[]) getPrinterAttributeSet(
 280:                          PrinterName.class).toArray(new PrinterName[1]);
 281:     name = tmp[0].getValue();
 282:     
 283:     // supported flavors
 284:     // TODO Check if charsets-supported are charsets that are actually supported
 285:     // for text doc flavors as cups doesn't send charset parameters
 286:     
 287:     // utf-8 is supported at least - so we go with this only for now
 288:     flavors = new ArrayList();
 289:     Set flavorAttributes = getPrinterAttributeSet(DocumentFormatSupported.class);
 290:     if (flavorAttributes != null)
 291:       {
 292:         for (Iterator it = flavorAttributes.iterator(); it.hasNext();)
 293:           {
 294:             String mimeType = ((DocumentFormatSupported) it.next()).getValue();
 295:                         
 296:             if (mimeType.equals("text/plain"))
 297:               {
 298:                 flavors.add(DocFlavor.CHAR_ARRAY.TEXT_PLAIN);
 299:                 flavors.add(DocFlavor.READER.TEXT_PLAIN);
 300:                 flavors.add(DocFlavor.STRING.TEXT_PLAIN);
 301:                 
 302:                 // add utf-8
 303:                 mimeType = mimeType + "; charset=utf-8";
 304:               }
 305:             else if (mimeType.equals("text/html"))
 306:               {
 307:                 flavors.add(DocFlavor.CHAR_ARRAY.TEXT_HTML);
 308:                 flavors.add(DocFlavor.READER.TEXT_HTML);
 309:                 flavors.add(DocFlavor.STRING.TEXT_HTML);
 310:                 
 311:                 // add utf-8
 312:                 mimeType = mimeType + "; charset=utf-8";
 313:               }
 314:             
 315:             // Process the predefined DocFlavors and if mimetype is
 316:             // equal put them into the flavors array - otherwise
 317:             // just build them as binarie class representation.
 318:             boolean changed = false;
 319:             try
 320:               {
 321:                 Class[] clazzes = new Class[] { DocFlavor.BYTE_ARRAY.class,
 322:                                                 DocFlavor.INPUT_STREAM.class,
 323:                                                 DocFlavor.URL.class };                
 324:                  
 325:                 for (int j = 0; j < clazzes.length; j++)
 326:                   {
 327:                     Field[] fields = clazzes[j].getDeclaredFields();
 328:                     for (int i = 0; i < fields.length; i++)
 329:                       {
 330:                         if (fields[i].getType().equals(clazzes[j]))
 331:                           {
 332:                             DocFlavor flavor = (DocFlavor) fields[i].get(null);
 333:                             if (flavor.getMimeType().equals(mimeType))
 334:                               changed = flavors.add(flavor);
 335:                           }
 336:                       }
 337:                   }
 338:                 if (!changed) // not in predefined constants of DocFlavor
 339:                   {                    
 340:                     // everything should be supported as binary stuff
 341:                     flavors.add(new DocFlavor(mimeType, "[B"));
 342:                     flavors.add(new DocFlavor(mimeType, "java.io.InputStream"));
 343:                     flavors.add(new DocFlavor(mimeType, "java.net.URL"));
 344:                   }
 345:               }
 346:             catch (SecurityException e)
 347:               {
 348:                 // should not happen
 349:               }
 350:             catch (IllegalArgumentException e)
 351:               {
 352:                 // should not happen
 353:               }
 354:             catch (IllegalAccessException e)
 355:               {
 356:                 // should not happen, all fields are public
 357:               }
 358:           }
 359: 
 360:     if (this.getClass()
 361:         .isAssignableFrom(gnu.javax.print.CupsPrintService.class))
 362:       {
 363: //         CUPS always provides filters to convert from Postscript.
 364: //          This logic looks odd, but it's what OpenJDK does.
 365:         flavors.add(DocFlavor.SERVICE_FORMATTED.PAGEABLE);
 366:         flavors.add(DocFlavor.SERVICE_FORMATTED.PRINTABLE);
 367:       }
 368:       }
 369: 
 370:     // printer uris
 371:     Set uris = getPrinterAttributeSet(PrinterUriSupported.class);
 372:     printerUris = new ArrayList(uris.size());
 373:     Iterator it = uris.iterator();
 374:     while (it.hasNext())
 375:       {
 376:         PrinterUriSupported uri = (PrinterUriSupported) it.next();
 377:         printerUris.add( new PrinterURI(uri.getURI()));
 378:       }
 379:   }
 380: 
 381:   /**
 382:    * We always return a implementation implementing CancelablePrintJob.
 383:    * 
 384:    * @see javax.print.PrintService#createPrintJob()
 385:    */
 386:   public DocPrintJob createPrintJob()
 387:   {
 388:     return new DocPrintJobImpl(this, user, passwd);
 389:   }
 390:   
 391: 
 392:   /**
 393:    * @see javax.print.PrintService#getAttribute(java.lang.Class)
 394:    */
 395:   public PrintServiceAttribute getAttribute(Class category)
 396:   {
 397:     if (category == null)
 398:       throw new NullPointerException("category may not be null");
 399:     
 400:     if (! PrintServiceAttribute.class.isAssignableFrom(category))
 401:       throw new IllegalArgumentException(
 402:          "category must be of type PrintServiceAttribute");
 403:     
 404:     Set set = getPrinterAttributeSet(category);
 405:     if (set != null && set.size() > 0)     
 406:         return (PrintServiceAttribute) set.toArray()[0];
 407:         
 408:     return null;
 409:   }
 410: 
 411:   /**
 412:    * @see javax.print.PrintService#getAttributes()
 413:    */
 414:   public PrintServiceAttributeSet getAttributes()
 415:   {
 416:     PrintServiceAttributeSet set = new HashPrintServiceAttributeSet();
 417:     
 418:     Iterator it = printerAttr.values().iterator();
 419:     while (it.hasNext())
 420:       {        
 421:         Iterator it2 = ((Set) it.next()).iterator();
 422:         while (it2.hasNext())
 423:           {
 424:             Attribute attr = (Attribute) it2.next();
 425:             if (attr instanceof PrintServiceAttribute)
 426:               set.add(attr);
 427:           }
 428:       }
 429:     
 430:     return AttributeSetUtilities.unmodifiableView(set);
 431:   }
 432: 
 433:   /**
 434:    * @see javax.print.PrintService#getDefaultAttributeValue(java.lang.Class)
 435:    */
 436:   public Object getDefaultAttributeValue(Class category)
 437:   { 
 438:     // required attributes
 439:     if (category.equals(Fidelity.class))
 440:       return Fidelity.FIDELITY_FALSE;    
 441:     if (category.equals(JobName.class))
 442:       return JOB_NAME;
 443:     if (category.equals(RequestingUserName.class))
 444:       return REQUESTING_USER_NAME;
 445:     
 446:     // optional attributes
 447:     if (category.equals(JobPriority.class) 
 448:         && printerAttr.containsKey(JobPriorityDefault.class))
 449:       return getPrinterDefaultAttribute(JobPriorityDefault.class);
 450:     if (category.equals(JobHoldUntil.class) 
 451:         && printerAttr.containsKey(JobHoldUntilDefault.class))
 452:       return getPrinterDefaultAttribute(JobHoldUntilDefault.class);
 453:     if (category.equals(JobSheets.class) 
 454:         && printerAttr.containsKey(JobSheetsDefault.class))
 455:       return getPrinterDefaultAttribute(JobSheetsDefault .class);
 456:     if (category.equals(MultipleDocumentHandling.class) 
 457:         && printerAttr.containsKey(MultipleDocumentHandlingDefault.class))
 458:       return getPrinterDefaultAttribute(MultipleDocumentHandlingDefault.class);
 459:     if (category.equals(Copies.class) 
 460:         && printerAttr.containsKey(CopiesDefault.class))
 461:       return getPrinterDefaultAttribute(CopiesDefault.class);
 462:     if (category.equals(Finishings.class) 
 463:         && printerAttr.containsKey(FinishingsDefault.class))
 464:       return getPrinterDefaultAttribute(FinishingsDefault.class);
 465:     if (category.equals(Sides.class) 
 466:         && printerAttr.containsKey(SidesDefault.class))
 467:       return getPrinterDefaultAttribute(SidesDefault.class);
 468:     if (category.equals(NumberUp.class) 
 469:         && printerAttr.containsKey(NumberUpDefault.class))
 470:       return getPrinterDefaultAttribute(NumberUpDefault.class);
 471:     if (category.equals(OrientationRequested.class) 
 472:         && printerAttr.containsKey(OrientationRequestedDefault.class))
 473:       return getPrinterDefaultAttribute(OrientationRequestedDefault.class);
 474:     if (category.equals(Media.class) 
 475:         && printerAttr.containsKey(MediaDefault.class))
 476:       return getPrinterDefaultAttribute(MediaDefault.class);
 477:     if (category.equals(PrinterResolution.class) 
 478:         && printerAttr.containsKey(PrinterResolutionDefault.class))
 479:       return getPrinterDefaultAttribute(PrinterResolutionDefault.class);
 480:     if (category.equals(PrintQuality.class) 
 481:         && printerAttr.containsKey(PrintQualityDefault.class))
 482:       return getPrinterDefaultAttribute(PrintQualityDefault.class);
 483:     if (category.equals(Compression.class) 
 484:         && printerAttr.containsKey(CompressionSupported.class))
 485:       return Compression.NONE;
 486:     if (category.equals(PageRanges.class))
 487:       return new PageRanges(1, Integer.MAX_VALUE);
 488: 
 489:     return null; 
 490:   }
 491:   
 492:   /**
 493:    * We return the value of <code>PrinterName</code> here.
 494:    * @see javax.print.PrintService#getName()
 495:    */
 496:   public String getName()
 497:   {
 498:     return name;
 499:   }
 500: 
 501:   /**
 502:    * We currently provide no factories - just returns null.
 503:    * @see javax.print.PrintService#getServiceUIFactory()
 504:    */
 505:   public ServiceUIFactory getServiceUIFactory()
 506:   {
 507:     // SUN does not provide any service factory for
 508:     // print services (tested on linux/windows)
 509:     
 510:     // for the moment we do the same - just return null
 511:     // later on we could provide at least the about UI dialog
 512:     return null;
 513:   }
 514: 
 515:   /**
 516:    * @see javax.print.PrintService#getSupportedAttributeCategories()
 517:    */
 518:   public Class[] getSupportedAttributeCategories()
 519:   {
 520:     Set categories = new HashSet();
 521:     
 522:     // Should only be job template attributes as of section 4.2    
 523:     if (printerAttr.containsKey(JobPrioritySupported.class))
 524:       categories.add(JobPriority.class);
 525:     if (printerAttr.containsKey(JobHoldUntilSupported.class))
 526:       categories.add(JobHoldUntil.class);
 527:     if (printerAttr.containsKey(JobSheetsSupported.class))
 528:       categories.add(JobSheets.class);
 529:     if (printerAttr.containsKey(MultipleDocumentHandlingSupported.class))
 530:       categories.add(MultipleDocumentHandling.class);    
 531:     if (printerAttr.containsKey(CopiesSupported.class))
 532:       categories.add(Copies.class);
 533:     if (printerAttr.containsKey(FinishingsSupported.class))
 534:       {
 535:         // if only none finishing is supported - it does not count as supported
 536:         Set set = getPrinterAttributeSet(FinishingsSupported.class);        
 537:         if (! (set.size() == 1 && set.contains(FinishingsSupported.NONE)))          
 538:           categories.add(Finishings.class);
 539:       }
 540:     if (printerAttr.containsKey(PageRangesSupported.class))
 541:       categories.add(PageRanges.class);
 542:     if (printerAttr.containsKey(SidesSupported.class))
 543:       categories.add(Sides.class);
 544:     if (printerAttr.containsKey(NumberUpSupported.class))
 545:       categories.add(NumberUp.class);
 546:     if (printerAttr.containsKey(OrientationRequestedSupported.class))
 547:       categories.add(OrientationRequested.class);
 548:     if (printerAttr.containsKey(MediaSupported.class))
 549:       categories.add(Media.class);
 550:     if (printerAttr.containsKey(PrinterResolutionSupported.class))
 551:       categories.add(PrinterResolution.class);
 552:     if (printerAttr.containsKey(PrintQualitySupported.class))
 553:       categories.add(PrintQuality.class);
 554:       
 555:     // Chromaticity, Destination, MediaPrintableArea, 
 556:     // SheetCollate, PresentationDirection - not IPP attributes
 557:     
 558:     // attributes outside section 4.2   
 559:     if (printerAttr.containsKey(CompressionSupported.class))
 560:       categories.add(Compression.class);
 561:     if (printerAttr.containsKey(JobImpressionsSupported.class))
 562:       categories.add(JobImpressions.class);
 563:     if (printerAttr.containsKey(JobKOctetsSupported.class))
 564:       categories.add(JobKOctets.class);
 565:     if (printerAttr.containsKey(JobMediaSheetsSupported.class))
 566:       categories.add(JobMediaSheets.class);
 567:     
 568:     // always supported as required by IPP specification
 569:     categories.add(Fidelity.class);
 570:     categories.add(JobName.class);
 571:     categories.add(RequestingUserName.class);
 572: 
 573:     return (Class[]) categories.toArray(new Class[categories.size()]);
 574:   }
 575: 
 576:   /**
 577:    * Implemented by a GetPrinterAttributes request. Subclasses providing supported
 578:    * attribute values totally different may override this methods. Subclass only in
 579:    * need of handling the response differently may override the method
 580:    * <code>handleSupportedAttributeValuesResponse(IppResponse, Class)</code> only.
 581:    * 
 582:    * @see PrintService#getSupportedAttributeValues(Class, DocFlavor, AttributeSet)
 583:    * @see #handleSupportedAttributeValuesResponse(IppResponse, Class)
 584:    */
 585:   public Object getSupportedAttributeValues(Class category, DocFlavor flavor,
 586:                                             AttributeSet attributes)
 587:   {
 588:     // We currently ignore the attribute set - there is nothing in the IPP
 589:     // specification which would come closer to what we do here.
 590: 
 591:     if (category == null)
 592:       throw new NullPointerException("category may not be null");
 593: 
 594:     if (!Attribute.class.isAssignableFrom(category))
 595:       throw new IllegalArgumentException("category must be of type Attribute");
 596: 
 597:     if (flavor != null && !isDocFlavorSupported(flavor))
 598:       throw new IllegalArgumentException("flavor is not supported");
 599: 
 600:     if (!isAttributeCategorySupported(category))
 601:       return null;
 602: 
 603:     // always supported
 604:     if (category.equals(Fidelity.class))
 605:       return new Fidelity[] { Fidelity.FIDELITY_FALSE, Fidelity.FIDELITY_TRUE };
 606:     if (category.equals(JobName.class))
 607:       return JOB_NAME;
 608:     if (category.equals(RequestingUserName.class))
 609:       return REQUESTING_USER_NAME;
 610: 
 611:     // map category to category-supported
 612:     String categoryName = IppUtilities.getSupportedAttrName(category);
 613: 
 614:     IppResponse response = null;
 615:     try
 616:       {
 617:         IppRequest request = new IppRequest(printerUri.getURI(), user, passwd);
 618:         request.setOperationID(
 619:           (short) OperationsSupported.GET_PRINTER_ATTRIBUTES.getValue());
 620:         request.setOperationAttributeDefaults();
 621:         request.addOperationAttribute(new RequestedAttributes(categoryName));
 622:         request.addOperationAttribute(printerUri);
 623:         
 624:         if (flavor != null)
 625:           {
 626:             DocumentFormat f = DocumentFormat.createDocumentFormat(flavor);
 627:             request.addOperationAttribute(f);
 628:           }
 629: 
 630:         response = request.send();
 631:         
 632:         int status = response.getStatusCode();
 633:         if (! (status == IppStatusCode.SUCCESSFUL_OK
 634:              || status == IppStatusCode.SUCCESSFUL_OK_IGNORED_OR_SUBSTITUED_ATTRIBUTES
 635:              || status == IppStatusCode.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES) )
 636:           {
 637:             logger.log(Component.IPP, "Statuscode not OK - got:" + status);
 638:           }
 639:       }
 640:     catch (IOException e)
 641:       {
 642:         // method cannot throw exception - just log
 643:         logger.log(Component.IPP, "IOException", e);
 644:       }
 645:     catch (IppException e)
 646:       {
 647:         // method cannot throw exception - just log
 648:         logger.log(Component.IPP, "IPPException", e);
 649:       }
 650:     
 651:     return handleSupportedAttributeValuesResponse(response, category);
 652:   }
 653:   
 654:   /**
 655:    * Called to handle the supported attribute values response for the given
 656:    * category. This might be overridden by subclasses with different requirements
 657:    * for parsing/handling the response from the GetPrinterAttributes.
 658:    * 
 659:    * @param response the response of the GetPrinterAttributes IPP request
 660:    * @param category the category for which the supported values are requested
 661:    * @return A object indicating the supported values for the given attribute 
 662:    * category, or <code>null</code> if this print service doesn't support the 
 663:    * given attribute category at all.
 664:    * 
 665:    * @see #getSupportedAttributeValues(Class, DocFlavor, AttributeSet)
 666:    */
 667:   protected Object handleSupportedAttributeValuesResponse(IppResponse response, 
 668:     Class category)
 669:   {
 670:     List printerAtts = response.getPrinterAttributes();
 671:    
 672:     // only one will be returned
 673:     Map printerAttribute = (Map) printerAtts.get(0);
 674:     Class suppCategory = IppUtilities.getSupportedCategory(category);
 675:     Set attr = (Set) printerAttribute.get(suppCategory);
 676:     
 677:     // We sometime assume its a single instance with arbritrary value just indicating 
 678:     // support or an array which is returned. This is because I sometimes just choosed
 679:     // what sounds right to me - as I have yet to find a printer which supports every 
 680:     // special category in the SUN implementation to see what they return :-)
 681:     
 682:     //  Map whats in the JSP API
 683:     if (suppCategory.equals(JobPrioritySupported.class))
 684:       return (JobPrioritySupported) attr.toArray(new JobPrioritySupported[1])[0];
 685:     if (suppCategory.equals(JobHoldUntilSupported.class))
 686:       return new JobHoldUntil(new Date());
 687:     if (suppCategory.equals(JobSheetsSupported.class))
 688:       return JobSheetsSupported.getAssociatedAttributeArray(attr);
 689:     if (suppCategory.equals(MultipleDocumentHandlingSupported.class))
 690:       return MultipleDocumentHandlingSupported.getAssociatedAttributeArray(attr);
 691:     if (suppCategory.equals(CopiesSupported.class))
 692:       return (CopiesSupported) attr.toArray(new CopiesSupported[1])[0];
 693:     if (suppCategory.equals(FinishingsSupported.class))
 694:       return FinishingsSupported.getAssociatedAttributeArray(attr);
 695:     if (suppCategory.equals(PageRangesSupported.class))
 696:       return new PageRanges[] { new PageRanges(1, Integer.MAX_VALUE) };    
 697:     if (suppCategory.equals(OrientationRequestedSupported.class))
 698:       return OrientationRequestedSupported.getAssociatedAttributeArray(attr);
 699:     if (suppCategory.equals(MediaSupported.class))
 700:       return MediaSupported.getAssociatedAttributeArray(attr);
 701:     if (suppCategory.equals(PrinterResolutionSupported.class))
 702:       return PrinterResolutionSupported.getAssociatedAttributeArray(attr);
 703:     if (suppCategory.equals(PrintQualitySupported.class))
 704:       return PrintQualitySupported.getAssociatedAttributeArray(attr);
 705:     if (suppCategory.equals(CompressionSupported.class))
 706:       return CompressionSupported.getAssociatedAttributeArray(attr);
 707:     // Special handling as it might also be in range of integers
 708:     if (suppCategory.equals(NumberUpSupported.class))
 709:       {
 710:         NumberUpSupported[] tmp = (NumberUpSupported[]) 
 711:           attr.toArray(new NumberUpSupported[attr.size()]);
 712: 
 713:         if (attr.size() == 1) // number-up maybe in rangeofintegers
 714:           return tmp[0];
 715: 
 716:         int[][] members = new int[attr.size()][2];
 717:         for (int j = 0; j < attr.size(); j++)
 718:           {
 719:             int value = tmp[j].getMembers()[0][0];
 720:             members[j] = new int[] { value, value };
 721:           }
 722: 
 723:         NumberUpSupported supported = new NumberUpSupported(members);
 724:         return supported;
 725:       }
 726:         
 727:     return null;
 728:   }  
 729:   
 730:   /**
 731:    * @see javax.print.PrintService#getSupportedDocFlavors()
 732:    */
 733:   public DocFlavor[] getSupportedDocFlavors()
 734:   {
 735:     return (DocFlavor[]) flavors.toArray(new DocFlavor[flavors.size()]);
 736:   }
 737: 
 738:   /**
 739:    * This is done by a validate-job operation and actually implemented in 
 740:    * this generic IPP reference implementation. Subclasses which does
 741:    * not correctly support Validate-Job operation might want to override this.
 742:    *
 743:    * @see PrintService#getUnsupportedAttributes(DocFlavor, AttributeSet)
 744:    */
 745:   public AttributeSet getUnsupportedAttributes(DocFlavor flavor,
 746:                                                AttributeSet attributes)
 747:   {    
 748:     if (flavor != null && !isDocFlavorSupported(flavor))
 749:       throw new IllegalArgumentException("flavor is not supported");
 750: 
 751:     IppResponse response = null;
 752:     try
 753:       {
 754:         IppRequest request = new IppRequest(printerUri.getURI(), user, passwd);
 755:         short operationId = (short) OperationsSupported.VALIDATE_JOB.getValue();
 756:         request.setOperationID(operationId);
 757:         request.setOperationAttributeDefaults();
 758:         request.addOperationAttribute(printerUri);
 759:         request.addOperationAttribute(Fidelity.FIDELITY_TRUE);
 760:         
 761:         if (attributes != null && attributes.size() > 0)
 762:           {
 763:             request.addAndFilterJobOperationAttributes(attributes);
 764:             request.addAndFilterJobTemplateAttributes(attributes);
 765:           }
 766:         
 767:         if (flavor != null)
 768:           {
 769:             DocumentFormat f = DocumentFormat.createDocumentFormat(flavor);
 770:             request.addOperationAttribute(f);
 771:           }
 772:         
 773:         response = request.send();
 774:         
 775:         int status = response.getStatusCode();
 776:         if (! (status == IppStatusCode.SUCCESSFUL_OK
 777:              || status == IppStatusCode.SUCCESSFUL_OK_IGNORED_OR_SUBSTITUED_ATTRIBUTES
 778:              || status == IppStatusCode.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES) )
 779:           {
 780:             logger.log(Component.IPP, "Statuscode not OK - got:" + status);
 781:           }
 782:       }
 783:     catch (IOException e)
 784:       {
 785:         // method cannot throw exception - just log
 786:         logger.log(Component.IPP, "IOException", e);
 787:       }
 788:     catch (IppException e)
 789:       {
 790:         // method cannot throw exception - just log
 791:         logger.log(Component.IPP, "IPPException", e);
 792:       }
 793: 
 794:     // Validate Jobs returns only Unsupported and Operation  
 795:     List unsupportedMaps = response.getUnsupportedAttributes();    
 796:     if (unsupportedMaps.size() == 0)
 797:       return  null;
 798:     
 799:     Map unsupportedAttr = (Map) unsupportedMaps.get(0);
 800:     if (unsupportedAttr.size() == 0)
 801:       return null;
 802:     
 803:     // Convert the return map with unsupported attributes 
 804:     // into an AttribueSet instance
 805:     HashAttributeSet set = new HashAttributeSet();
 806:     Iterator it = unsupportedAttr.values().iterator();
 807:     while (it.hasNext())
 808:       {
 809:         Set unsupported = (Set) it.next();
 810:         Iterator it2 = unsupported.iterator();
 811:         while (it2.hasNext())
 812:           set.add((Attribute) it2.next());        
 813:       }
 814:     
 815:     return set;
 816:   }
 817: 
 818:   /**
 819:    * @see PrintService#isAttributeCategorySupported(Class)
 820:    */
 821:   public boolean isAttributeCategorySupported(Class category)
 822:   {
 823:     if (category == null)
 824:       throw new NullPointerException("category may not be null");
 825:     
 826:     if (! Attribute.class.isAssignableFrom(category))
 827:       throw new IllegalArgumentException("category must be of type Attribute");
 828:     
 829:     return Arrays.asList(getSupportedAttributeCategories()).contains(category);
 830:   }
 831: 
 832:   /**
 833:    * @see PrintService#isAttributeValueSupported(Attribute, DocFlavor, AttributeSet)
 834:    */
 835:   public boolean isAttributeValueSupported(Attribute attrval, DocFlavor flavor,
 836:                                            AttributeSet attributes)
 837:   {
 838:     // just redirect to getSupportedAttributeValues    
 839:     Object values = getSupportedAttributeValues(attrval.getCategory(), 
 840:                                                 flavor, attributes);    
 841:     // null means none supported
 842:     if (values == null)
 843:       return false;
 844:     
 845:     // object may be an array
 846:     if (values.getClass().isArray())
 847:       return Arrays.asList((Object[]) values).contains(attrval);
 848:     
 849:     // may be a single instance of the category (value is irrelevant)
 850:     if (values.getClass().equals(attrval.getCategory()))
 851:       return true;
 852:        
 853:     // a single instance of another class to give the bounds    
 854:     // copies
 855:     if (values.getClass().equals(CopiesSupported.class))
 856:       return ((CopiesSupported) values).contains((IntegerSyntax) attrval);    
 857:     // number up
 858:     if (values.getClass().equals(NumberUpSupported.class))
 859:       return ((NumberUpSupported) values).contains((IntegerSyntax) attrval);    
 860:     // job priority
 861:     if (values.getClass().equals(JobPrioritySupported.class))
 862:       {
 863:         JobPriority priority = (JobPriority) attrval;
 864:         JobPrioritySupported maxSupported = (JobPrioritySupported) values;
 865:         if (priority.getValue() < maxSupported.getValue())
 866:           return true;
 867:       }
 868:     
 869:     // I am unsure if these might also show up - not yet found a printer where 
 870:     // Suns implementation supports them: 
 871:     // JobImpressionsSupported, JobKOctetsSupported, JobMediaSheetsSupported
 872:     
 873:     return false;
 874:   }
 875: 
 876:  
 877:   /**
 878:    * @see javax.print.PrintService#isDocFlavorSupported(DocFlavor)
 879:    */
 880:   public boolean isDocFlavorSupported(DocFlavor flavor)
 881:   {
 882:     if (flavor == null)
 883:       throw new NullPointerException("DocFlavor may not be null.");
 884:     
 885:     return flavors.contains(flavor);
 886:   }
 887: 
 888:   
 889:   /**
 890:    * @see PrintService#addPrintServiceAttributeListener(PrintServiceAttributeListener)
 891:    */
 892:   public void addPrintServiceAttributeListener(
 893:     PrintServiceAttributeListener listener)
 894:   {
 895:     printServiceAttributeListener.add(listener);
 896:   }
 897:   
 898:   /**
 899:    * @see PrintService#removePrintServiceAttributeListener(PrintServiceAttributeListener)
 900:    */
 901:   public void removePrintServiceAttributeListener(
 902:     PrintServiceAttributeListener listener)
 903:   {
 904:     printServiceAttributeListener.remove(listener);
 905:   }
 906:   
 907:   /**
 908:    * Returns "IppPrinter: " + <code>getName()</code>
 909:    * @return The string representation.
 910:    */
 911:   public String toString()
 912:   {
 913:     return "IppPrinter: " + getName();
 914:   } 
 915:   
 916:   /**
 917:    * Returns the printer-uri of this print service.
 918:    * 
 919:    * @return The printer-uri attribute.
 920:    */
 921:   public PrinterURI getPrinterURI()
 922:   {
 923:     return printerUri;
 924:   }  
 925: }