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:     
 361:     // printer uris
 362:     Set uris = getPrinterAttributeSet(PrinterUriSupported.class);
 363:     printerUris = new ArrayList(uris.size());
 364:     Iterator it = uris.iterator();
 365:     while (it.hasNext())
 366:       {
 367:         PrinterUriSupported uri = (PrinterUriSupported) it.next();
 368:         printerUris.add( new PrinterURI(uri.getURI()));
 369:       }
 370:   }
 371: 
 372:   /**
 373:    * We always return a implementation implementing CancelablePrintJob.
 374:    * 
 375:    * @see javax.print.PrintService#createPrintJob()
 376:    */
 377:   public DocPrintJob createPrintJob()
 378:   {
 379:     return new DocPrintJobImpl(this, user, passwd);
 380:   }
 381:   
 382: 
 383:   /**
 384:    * @see javax.print.PrintService#getAttribute(java.lang.Class)
 385:    */
 386:   public PrintServiceAttribute getAttribute(Class category)
 387:   {
 388:     if (category == null)
 389:       throw new NullPointerException("category may not be null");
 390:     
 391:     if (! PrintServiceAttribute.class.isAssignableFrom(category))
 392:       throw new IllegalArgumentException(
 393:          "category must be of type PrintServiceAttribute");
 394:     
 395:     Set set = getPrinterAttributeSet(category);
 396:     if (set != null && set.size() > 0)     
 397:         return (PrintServiceAttribute) set.toArray()[0];
 398:         
 399:     return null;
 400:   }
 401: 
 402:   /**
 403:    * @see javax.print.PrintService#getAttributes()
 404:    */
 405:   public PrintServiceAttributeSet getAttributes()
 406:   {
 407:     PrintServiceAttributeSet set = new HashPrintServiceAttributeSet();
 408:     
 409:     Iterator it = printerAttr.values().iterator();
 410:     while (it.hasNext())
 411:       {        
 412:         Iterator it2 = ((Set) it.next()).iterator();
 413:         while (it2.hasNext())
 414:           {
 415:             Attribute attr = (Attribute) it2.next();
 416:             if (attr instanceof PrintServiceAttribute)
 417:               set.add(attr);
 418:           }
 419:       }
 420:     
 421:     return AttributeSetUtilities.unmodifiableView(set);
 422:   }
 423: 
 424:   /**
 425:    * @see javax.print.PrintService#getDefaultAttributeValue(java.lang.Class)
 426:    */
 427:   public Object getDefaultAttributeValue(Class category)
 428:   { 
 429:     // required attributes
 430:     if (category.equals(Fidelity.class))
 431:       return Fidelity.FIDELITY_FALSE;    
 432:     if (category.equals(JobName.class))
 433:       return JOB_NAME;
 434:     if (category.equals(RequestingUserName.class))
 435:       return REQUESTING_USER_NAME;
 436:     
 437:     // optional attributes
 438:     if (category.equals(JobPriority.class) 
 439:         && printerAttr.containsKey(JobPriorityDefault.class))
 440:       return getPrinterDefaultAttribute(JobPriorityDefault.class);
 441:     if (category.equals(JobHoldUntil.class) 
 442:         && printerAttr.containsKey(JobHoldUntilDefault.class))
 443:       return getPrinterDefaultAttribute(JobHoldUntilDefault.class);
 444:     if (category.equals(JobSheets.class) 
 445:         && printerAttr.containsKey(JobSheetsDefault.class))
 446:       return getPrinterDefaultAttribute(JobSheetsDefault .class);
 447:     if (category.equals(MultipleDocumentHandling.class) 
 448:         && printerAttr.containsKey(MultipleDocumentHandlingDefault.class))
 449:       return getPrinterDefaultAttribute(MultipleDocumentHandlingDefault.class);
 450:     if (category.equals(Copies.class) 
 451:         && printerAttr.containsKey(CopiesDefault.class))
 452:       return getPrinterDefaultAttribute(CopiesDefault.class);
 453:     if (category.equals(Finishings.class) 
 454:         && printerAttr.containsKey(FinishingsDefault.class))
 455:       return getPrinterDefaultAttribute(FinishingsDefault.class);
 456:     if (category.equals(Sides.class) 
 457:         && printerAttr.containsKey(SidesDefault.class))
 458:       return getPrinterDefaultAttribute(SidesDefault.class);
 459:     if (category.equals(NumberUp.class) 
 460:         && printerAttr.containsKey(NumberUpDefault.class))
 461:       return getPrinterDefaultAttribute(NumberUpDefault.class);
 462:     if (category.equals(OrientationRequested.class) 
 463:         && printerAttr.containsKey(OrientationRequestedDefault.class))
 464:       return getPrinterDefaultAttribute(OrientationRequestedDefault.class);
 465:     if (category.equals(Media.class) 
 466:         && printerAttr.containsKey(MediaDefault.class))
 467:       return getPrinterDefaultAttribute(MediaDefault.class);
 468:     if (category.equals(PrinterResolution.class) 
 469:         && printerAttr.containsKey(PrinterResolutionDefault.class))
 470:       return getPrinterDefaultAttribute(PrinterResolutionDefault.class);
 471:     if (category.equals(PrintQuality.class) 
 472:         && printerAttr.containsKey(PrintQualityDefault.class))
 473:       return getPrinterDefaultAttribute(PrintQualityDefault.class);
 474:     if (category.equals(Compression.class) 
 475:         && printerAttr.containsKey(CompressionSupported.class))
 476:       return Compression.NONE;
 477:     if (category.equals(PageRanges.class))
 478:       return new PageRanges(1, Integer.MAX_VALUE);
 479: 
 480:     return null; 
 481:   }
 482:   
 483:   /**
 484:    * We return the value of <code>PrinterName</code> here.
 485:    * @see javax.print.PrintService#getName()
 486:    */
 487:   public String getName()
 488:   {
 489:     return name;
 490:   }
 491: 
 492:   /**
 493:    * We currently provide no factories - just returns null.
 494:    * @see javax.print.PrintService#getServiceUIFactory()
 495:    */
 496:   public ServiceUIFactory getServiceUIFactory()
 497:   {
 498:     // SUN does not provide any service factory for
 499:     // print services (tested on linux/windows)
 500:     
 501:     // for the moment we do the same - just return null
 502:     // later on we could provide at least the about UI dialog
 503:     return null;
 504:   }
 505: 
 506:   /**
 507:    * @see javax.print.PrintService#getSupportedAttributeCategories()
 508:    */
 509:   public Class[] getSupportedAttributeCategories()
 510:   {
 511:     Set categories = new HashSet();
 512:     
 513:     // Should only be job template attributes as of section 4.2    
 514:     if (printerAttr.containsKey(JobPrioritySupported.class))
 515:       categories.add(JobPriority.class);
 516:     if (printerAttr.containsKey(JobHoldUntilSupported.class))
 517:       categories.add(JobHoldUntil.class);
 518:     if (printerAttr.containsKey(JobSheetsSupported.class))
 519:       categories.add(JobSheets.class);
 520:     if (printerAttr.containsKey(MultipleDocumentHandlingSupported.class))
 521:       categories.add(MultipleDocumentHandling.class);    
 522:     if (printerAttr.containsKey(CopiesSupported.class))
 523:       categories.add(Copies.class);
 524:     if (printerAttr.containsKey(FinishingsSupported.class))
 525:       {
 526:         // if only none finishing is supported - it does not count as supported
 527:         Set set = getPrinterAttributeSet(FinishingsSupported.class);        
 528:         if (! (set.size() == 1 && set.contains(FinishingsSupported.NONE)))          
 529:           categories.add(Finishings.class);
 530:       }
 531:     if (printerAttr.containsKey(PageRangesSupported.class))
 532:       categories.add(PageRanges.class);
 533:     if (printerAttr.containsKey(SidesSupported.class))
 534:       categories.add(Sides.class);
 535:     if (printerAttr.containsKey(NumberUpSupported.class))
 536:       categories.add(NumberUp.class);
 537:     if (printerAttr.containsKey(OrientationRequestedSupported.class))
 538:       categories.add(OrientationRequested.class);
 539:     if (printerAttr.containsKey(MediaSupported.class))
 540:       categories.add(Media.class);
 541:     if (printerAttr.containsKey(PrinterResolutionSupported.class))
 542:       categories.add(PrinterResolution.class);
 543:     if (printerAttr.containsKey(PrintQualitySupported.class))
 544:       categories.add(PrintQuality.class);
 545:       
 546:     // Chromaticity, Destination, MediaPrintableArea, 
 547:     // SheetCollate, PresentationDirection - not IPP attributes
 548:     
 549:     // attributes outside section 4.2   
 550:     if (printerAttr.containsKey(CompressionSupported.class))
 551:       categories.add(Compression.class);
 552:     if (printerAttr.containsKey(JobImpressionsSupported.class))
 553:       categories.add(JobImpressions.class);
 554:     if (printerAttr.containsKey(JobKOctetsSupported.class))
 555:       categories.add(JobKOctets.class);
 556:     if (printerAttr.containsKey(JobMediaSheetsSupported.class))
 557:       categories.add(JobMediaSheets.class);
 558:     
 559:     // always supported as required by IPP specification
 560:     categories.add(Fidelity.class);
 561:     categories.add(JobName.class);
 562:     categories.add(RequestingUserName.class);
 563: 
 564:     return (Class[]) categories.toArray(new Class[categories.size()]);
 565:   }
 566: 
 567:   /**
 568:    * Implemented by a GetPrinterAttributes request. Subclasses providing supported
 569:    * attribute values totally different may override this methods. Subclass only in
 570:    * need of handling the response differently may override the method
 571:    * <code>handleSupportedAttributeValuesResponse(IppResponse, Class)</code> only.
 572:    * 
 573:    * @see PrintService#getSupportedAttributeValues(Class, DocFlavor, AttributeSet)
 574:    * @see #handleSupportedAttributeValuesResponse(IppResponse, Class)
 575:    */
 576:   public Object getSupportedAttributeValues(Class category, DocFlavor flavor,
 577:                                             AttributeSet attributes)
 578:   {
 579:     // We currently ignore the attribute set - there is nothing in the IPP
 580:     // specification which would come closer to what we do here.
 581: 
 582:     if (category == null)
 583:       throw new NullPointerException("category may not be null");
 584: 
 585:     if (!Attribute.class.isAssignableFrom(category))
 586:       throw new IllegalArgumentException("category must be of type Attribute");
 587: 
 588:     if (flavor != null && !isDocFlavorSupported(flavor))
 589:       throw new IllegalArgumentException("flavor is not supported");
 590: 
 591:     if (!isAttributeCategorySupported(category))
 592:       return null;
 593: 
 594:     // always supported
 595:     if (category.equals(Fidelity.class))
 596:       return new Fidelity[] { Fidelity.FIDELITY_FALSE, Fidelity.FIDELITY_TRUE };
 597:     if (category.equals(JobName.class))
 598:       return JOB_NAME;
 599:     if (category.equals(RequestingUserName.class))
 600:       return REQUESTING_USER_NAME;
 601: 
 602:     // map category to category-supported
 603:     String categoryName = IppUtilities.getSupportedAttrName(category);
 604: 
 605:     IppResponse response = null;
 606:     try
 607:       {
 608:         IppRequest request = new IppRequest(printerUri.getURI(), user, passwd);
 609:         request.setOperationID(
 610:           (short) OperationsSupported.GET_PRINTER_ATTRIBUTES.getValue());
 611:         request.setOperationAttributeDefaults();
 612:         request.addOperationAttribute(new RequestedAttributes(categoryName));
 613:         request.addOperationAttribute(printerUri);
 614:         
 615:         if (flavor != null)
 616:           {
 617:             DocumentFormat f = DocumentFormat.createDocumentFormat(flavor);
 618:             request.addOperationAttribute(f);
 619:           }
 620: 
 621:         response = request.send();
 622:         
 623:         int status = response.getStatusCode();
 624:         if (! (status == IppStatusCode.SUCCESSFUL_OK
 625:              || status == IppStatusCode.SUCCESSFUL_OK_IGNORED_OR_SUBSTITUED_ATTRIBUTES
 626:              || status == IppStatusCode.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES) )
 627:           {
 628:             logger.log(Component.IPP, "Statuscode not OK - got:" + status);
 629:           }
 630:       }
 631:     catch (IOException e)
 632:       {
 633:         // method cannot throw exception - just log
 634:         logger.log(Component.IPP, "IOException", e);
 635:       }
 636:     catch (IppException e)
 637:       {
 638:         // method cannot throw exception - just log
 639:         logger.log(Component.IPP, "IPPException", e);
 640:       }
 641:     
 642:     return handleSupportedAttributeValuesResponse(response, category);
 643:   }
 644:   
 645:   /**
 646:    * Called to handle the supported attribute values response for the given
 647:    * category. This might be overridden by subclasses with different requirements
 648:    * for parsing/handling the response from the GetPrinterAttributes.
 649:    * 
 650:    * @param response the response of the GetPrinterAttributes IPP request
 651:    * @param category the category for which the supported values are requested
 652:    * @return A object indicating the supported values for the given attribute 
 653:    * category, or <code>null</code> if this print service doesn't support the 
 654:    * given attribute category at all.
 655:    * 
 656:    * @see #getSupportedAttributeValues(Class, DocFlavor, AttributeSet)
 657:    */
 658:   protected Object handleSupportedAttributeValuesResponse(IppResponse response, 
 659:     Class category)
 660:   {
 661:     List printerAtts = response.getPrinterAttributes();
 662:    
 663:     // only one will be returned
 664:     Map printerAttribute = (Map) printerAtts.get(0);
 665:     Class suppCategory = IppUtilities.getSupportedCategory(category);
 666:     Set attr = (Set) printerAttribute.get(suppCategory);
 667:     
 668:     // We sometime assume its a single instance with arbritrary value just indicating 
 669:     // support or an array which is returned. This is because I sometimes just choosed
 670:     // what sounds right to me - as I have yet to find a printer which supports every 
 671:     // special category in the SUN implementation to see what they return :-)
 672:     
 673:     //  Map whats in the JSP API
 674:     if (suppCategory.equals(JobPrioritySupported.class))
 675:       return (JobPrioritySupported) attr.toArray(new JobPrioritySupported[1])[0];
 676:     if (suppCategory.equals(JobHoldUntilSupported.class))
 677:       return new JobHoldUntil(new Date());
 678:     if (suppCategory.equals(JobSheetsSupported.class))
 679:       return JobSheetsSupported.getAssociatedAttributeArray(attr);
 680:     if (suppCategory.equals(MultipleDocumentHandlingSupported.class))
 681:       return MultipleDocumentHandlingSupported.getAssociatedAttributeArray(attr);
 682:     if (suppCategory.equals(CopiesSupported.class))
 683:       return (CopiesSupported) attr.toArray(new CopiesSupported[1])[0];
 684:     if (suppCategory.equals(FinishingsSupported.class))
 685:       return FinishingsSupported.getAssociatedAttributeArray(attr);
 686:     if (suppCategory.equals(PageRangesSupported.class))
 687:       return new PageRanges[] { new PageRanges(1, Integer.MAX_VALUE) };    
 688:     if (suppCategory.equals(OrientationRequestedSupported.class))
 689:       return OrientationRequestedSupported.getAssociatedAttributeArray(attr);
 690:     if (suppCategory.equals(MediaSupported.class))
 691:       return MediaSupported.getAssociatedAttributeArray(attr);
 692:     if (suppCategory.equals(PrinterResolutionSupported.class))
 693:       return PrinterResolutionSupported.getAssociatedAttributeArray(attr);
 694:     if (suppCategory.equals(PrintQualitySupported.class))
 695:       return PrintQualitySupported.getAssociatedAttributeArray(attr);
 696:     if (suppCategory.equals(CompressionSupported.class))
 697:       return CompressionSupported.getAssociatedAttributeArray(attr);
 698:     // Special handling as it might also be in range of integers
 699:     if (suppCategory.equals(NumberUpSupported.class))
 700:       {
 701:         NumberUpSupported[] tmp = (NumberUpSupported[]) 
 702:           attr.toArray(new NumberUpSupported[attr.size()]);
 703: 
 704:         if (attr.size() == 1) // number-up maybe in rangeofintegers
 705:           return tmp[0];
 706: 
 707:         int[][] members = new int[attr.size()][2];
 708:         for (int j = 0; j < attr.size(); j++)
 709:           {
 710:             int value = tmp[j].getMembers()[0][0];
 711:             members[j] = new int[] { value, value };
 712:           }
 713: 
 714:         NumberUpSupported supported = new NumberUpSupported(members);
 715:         return supported;
 716:       }
 717:         
 718:     return null;
 719:   }  
 720:   
 721:   /**
 722:    * @see javax.print.PrintService#getSupportedDocFlavors()
 723:    */
 724:   public DocFlavor[] getSupportedDocFlavors()
 725:   {
 726:     return (DocFlavor[]) flavors.toArray(new DocFlavor[flavors.size()]);
 727:   }
 728: 
 729:   /**
 730:    * This is done by a validate-job operation and actually implemented in 
 731:    * this generic IPP reference implementation. Subclasses which does
 732:    * not correctly support Validate-Job operation might want to override this.
 733:    *
 734:    * @see PrintService#getUnsupportedAttributes(DocFlavor, AttributeSet)
 735:    */
 736:   public AttributeSet getUnsupportedAttributes(DocFlavor flavor,
 737:                                                AttributeSet attributes)
 738:   {    
 739:     if (flavor != null && !isDocFlavorSupported(flavor))
 740:       throw new IllegalArgumentException("flavor is not supported");
 741: 
 742:     IppResponse response = null;
 743:     try
 744:       {
 745:         IppRequest request = new IppRequest(printerUri.getURI(), user, passwd);
 746:         short operationId = (short) OperationsSupported.VALIDATE_JOB.getValue();
 747:         request.setOperationID(operationId);
 748:         request.setOperationAttributeDefaults();
 749:         request.addOperationAttribute(printerUri);
 750:         request.addOperationAttribute(Fidelity.FIDELITY_TRUE);
 751:         
 752:         if (attributes != null && attributes.size() > 0)
 753:           {
 754:             request.addAndFilterJobOperationAttributes(attributes);
 755:             request.addAndFilterJobTemplateAttributes(attributes);
 756:           }
 757:         
 758:         if (flavor != null)
 759:           {
 760:             DocumentFormat f = DocumentFormat.createDocumentFormat(flavor);
 761:             request.addOperationAttribute(f);
 762:           }
 763:         
 764:         response = request.send();
 765:         
 766:         int status = response.getStatusCode();
 767:         if (! (status == IppStatusCode.SUCCESSFUL_OK
 768:              || status == IppStatusCode.SUCCESSFUL_OK_IGNORED_OR_SUBSTITUED_ATTRIBUTES
 769:              || status == IppStatusCode.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES) )
 770:           {
 771:             logger.log(Component.IPP, "Statuscode not OK - got:" + status);
 772:           }
 773:       }
 774:     catch (IOException e)
 775:       {
 776:         // method cannot throw exception - just log
 777:         logger.log(Component.IPP, "IOException", e);
 778:       }
 779:     catch (IppException e)
 780:       {
 781:         // method cannot throw exception - just log
 782:         logger.log(Component.IPP, "IPPException", e);
 783:       }
 784: 
 785:     // Validate Jobs returns only Unsupported and Operation  
 786:     List unsupportedMaps = response.getUnsupportedAttributes();    
 787:     if (unsupportedMaps.size() == 0)
 788:       return  null;
 789:     
 790:     Map unsupportedAttr = (Map) unsupportedMaps.get(0);
 791:     if (unsupportedAttr.size() == 0)
 792:       return null;
 793:     
 794:     // Convert the return map with unsupported attributes 
 795:     // into an AttribueSet instance
 796:     HashAttributeSet set = new HashAttributeSet();
 797:     Iterator it = unsupportedAttr.values().iterator();
 798:     while (it.hasNext())
 799:       {
 800:         Set unsupported = (Set) it.next();
 801:         Iterator it2 = unsupported.iterator();
 802:         while (it2.hasNext())
 803:           set.add((Attribute) it2.next());        
 804:       }
 805:     
 806:     return set;
 807:   }
 808: 
 809:   /**
 810:    * @see PrintService#isAttributeCategorySupported(Class)
 811:    */
 812:   public boolean isAttributeCategorySupported(Class category)
 813:   {
 814:     if (category == null)
 815:       throw new NullPointerException("category may not be null");
 816:     
 817:     if (! Attribute.class.isAssignableFrom(category))
 818:       throw new IllegalArgumentException("category must be of type Attribute");
 819:     
 820:     return Arrays.asList(getSupportedAttributeCategories()).contains(category);
 821:   }
 822: 
 823:   /**
 824:    * @see PrintService#isAttributeValueSupported(Attribute, DocFlavor, AttributeSet)
 825:    */
 826:   public boolean isAttributeValueSupported(Attribute attrval, DocFlavor flavor,
 827:                                            AttributeSet attributes)
 828:   {
 829:     // just redirect to getSupportedAttributeValues    
 830:     Object values = getSupportedAttributeValues(attrval.getCategory(), 
 831:                                                 flavor, attributes);    
 832:     // null means none supported
 833:     if (values == null)
 834:       return false;
 835:     
 836:     // object may be an array
 837:     if (values.getClass().isArray())
 838:       return Arrays.asList((Object[]) values).contains(attrval);
 839:     
 840:     // may be a single instance of the category (value is irrelevant)
 841:     if (values.getClass().equals(attrval.getCategory()))
 842:       return true;
 843:        
 844:     // a single instance of another class to give the bounds    
 845:     // copies
 846:     if (values.getClass().equals(CopiesSupported.class))
 847:       return ((CopiesSupported) values).contains((IntegerSyntax) attrval);    
 848:     // number up
 849:     if (values.getClass().equals(NumberUpSupported.class))
 850:       return ((NumberUpSupported) values).contains((IntegerSyntax) attrval);    
 851:     // job priority
 852:     if (values.getClass().equals(JobPrioritySupported.class))
 853:       {
 854:         JobPriority priority = (JobPriority) attrval;
 855:         JobPrioritySupported maxSupported = (JobPrioritySupported) values;
 856:         if (priority.getValue() < maxSupported.getValue())
 857:           return true;
 858:       }
 859:     
 860:     // I am unsure if these might also show up - not yet found a printer where 
 861:     // Suns implementation supports them: 
 862:     // JobImpressionsSupported, JobKOctetsSupported, JobMediaSheetsSupported
 863:     
 864:     return false;
 865:   }
 866: 
 867:  
 868:   /**
 869:    * @see javax.print.PrintService#isDocFlavorSupported(DocFlavor)
 870:    */
 871:   public boolean isDocFlavorSupported(DocFlavor flavor)
 872:   {
 873:     if (flavor == null)
 874:       throw new NullPointerException("DocFlavor may not be null.");
 875:     
 876:     return flavors.contains(flavor);
 877:   }
 878: 
 879:   
 880:   /**
 881:    * @see PrintService#addPrintServiceAttributeListener(PrintServiceAttributeListener)
 882:    */
 883:   public void addPrintServiceAttributeListener(
 884:     PrintServiceAttributeListener listener)
 885:   {
 886:     printServiceAttributeListener.add(listener);
 887:   }
 888:   
 889:   /**
 890:    * @see PrintService#removePrintServiceAttributeListener(PrintServiceAttributeListener)
 891:    */
 892:   public void removePrintServiceAttributeListener(
 893:     PrintServiceAttributeListener listener)
 894:   {
 895:     printServiceAttributeListener.remove(listener);
 896:   }
 897:   
 898:   /**
 899:    * Returns "IppPrinter: " + <code>getName()</code>
 900:    * @return The string representation.
 901:    */
 902:   public String toString()
 903:   {
 904:     return "IppPrinter: " + getName();
 905:   } 
 906:   
 907:   /**
 908:    * Returns the printer-uri of this print service.
 909:    * 
 910:    * @return The printer-uri attribute.
 911:    */
 912:   public PrinterURI getPrinterURI()
 913:   {
 914:     return printerUri;
 915:   }  
 916: }