[738d179] | 1 | from gutenbach.server.printer import GutenbachPrinter |
---|
[b2e077a] | 2 | import gutenbach.ipp as ipp |
---|
[aef164a] | 3 | import gutenbach.ipp.constants as consts |
---|
[287d6ec] | 4 | import logging |
---|
[5e44432] | 5 | import traceback |
---|
[287d6ec] | 6 | |
---|
[d04a689] | 7 | # initialize logger |
---|
| 8 | logger = logging.getLogger(__name__) |
---|
[287d6ec] | 9 | |
---|
| 10 | def handler_for(operation): |
---|
| 11 | """A decorator method to mark a function with the operation id |
---|
| 12 | that it handles. This value will be stored in |
---|
| 13 | 'func.ipp_operation'. |
---|
| 14 | |
---|
| 15 | """ |
---|
| 16 | |
---|
| 17 | def f(func): |
---|
| 18 | func.ipp_operation = operation |
---|
| 19 | return func |
---|
| 20 | return f |
---|
| 21 | |
---|
[b2e077a] | 22 | class GutenbachRequestHandler(object): |
---|
[287d6ec] | 23 | |
---|
[b2e077a] | 24 | def __init__(self): |
---|
| 25 | self.printers = { |
---|
| 26 | "test": GutenbachPrinter(name="test") |
---|
| 27 | } |
---|
| 28 | self.default = "test" |
---|
[287d6ec] | 29 | |
---|
[ef8df33] | 30 | def handle(self, request): |
---|
[b2e077a] | 31 | # look up the handler |
---|
| 32 | handler = None |
---|
[9eeab06] | 33 | handler_name = None |
---|
[287d6ec] | 34 | for d in dir(self): |
---|
| 35 | if getattr(getattr(self, d), "ipp_operation", None) == request.operation_id: |
---|
[9eeab06] | 36 | handler_name = d |
---|
[b2e077a] | 37 | break |
---|
| 38 | # we couldn't find a handler, so default to unknown operation |
---|
[9eeab06] | 39 | if handler_name is None: |
---|
| 40 | handler_name = "unknown_operation" |
---|
[b2e077a] | 41 | # call the handler |
---|
[9eeab06] | 42 | handler = getattr(self, handler_name) |
---|
[7bd1035] | 43 | logger.info("Handling request of type '%s'" % handler_name) |
---|
[aef164a] | 44 | |
---|
| 45 | # Try to handle the request |
---|
| 46 | try: |
---|
| 47 | response = handler(request) |
---|
| 48 | |
---|
| 49 | # Handle any errors that occur. If an exception occurs that |
---|
| 50 | # is an IPP error, then we can get the error code from the |
---|
| 51 | # exception itself. |
---|
| 52 | except ipp.errors.IPPException: |
---|
| 53 | exctype, excval, exctb = sys.exc_info() |
---|
| 54 | logger.error(traceback.format_exc()) |
---|
| 55 | response = ipp.ops.make_empty_response(request) |
---|
| 56 | excval.update_response(response) |
---|
| 57 | |
---|
| 58 | # If it wasn't an IPP error, then it's our fault, so mark it |
---|
| 59 | # as an internal server error |
---|
| 60 | except Exception: |
---|
| 61 | logger.error(traceback.format_exc()) |
---|
| 62 | response = ipp.ops.make_empty_response(request) |
---|
| 63 | response.operation_id = ipp.StatusCodes.INTERNAL_ERROR |
---|
| 64 | |
---|
[ef8df33] | 65 | return response |
---|
[287d6ec] | 66 | |
---|
[ef8df33] | 67 | def unknown_operation(self, request): |
---|
[9eeab06] | 68 | logger.warning("Received unknown operation 0x%x" % request.operation_id) |
---|
[ef8df33] | 69 | response = ipp.ops.make_empty_response(request) |
---|
[aef164a] | 70 | response.operation_id = consts.StatusCodes.OPERATION_NOT_SUPPORTED |
---|
[ef8df33] | 71 | return response |
---|
[0ede474] | 72 | |
---|
[9eeab06] | 73 | ##### Printer Commands |
---|
| 74 | |
---|
[94a4825] | 75 | @handler_for(consts.Operations.PRINT_JOB) |
---|
[ef8df33] | 76 | def print_job(self, request): |
---|
[7a7a09e] | 77 | """RFC 2911: 3.2.1 Print-Job Operation |
---|
| 78 | |
---|
| 79 | This REQUIRED operation allows a client to submit a print job |
---|
| 80 | with only one document and supply the document data (rather |
---|
| 81 | than just a reference to the data). See Section 15 for the |
---|
| 82 | suggested steps for processing create operations and their |
---|
| 83 | Operation and Job Template attributes. |
---|
| 84 | |
---|
| 85 | """ |
---|
| 86 | |
---|
[94a4825] | 87 | raise NotImplementedError |
---|
[9eeab06] | 88 | |
---|
[94a4825] | 89 | @handler_for(consts.Operations.VALIDATE_JOB) |
---|
[ef8df33] | 90 | def validate_job(self, request): |
---|
[94a4825] | 91 | |
---|
| 92 | raise NotImplementedError |
---|
[9eeab06] | 93 | |
---|
[aef164a] | 94 | @handler_for(consts.Operations.GET_JOBS) |
---|
[ef8df33] | 95 | def get_jobs(self, request): |
---|
[6ed9d7a] | 96 | """RFC 2911: 3.2.6 Get-Jobs Operation |
---|
| 97 | |
---|
| 98 | This REQUIRED operation allows a client to retrieve the list |
---|
| 99 | of Job objects belonging to the target Printer object. The |
---|
| 100 | client may also supply a list of Job attribute names and/or |
---|
| 101 | attribute group names. A group of Job object attributes will |
---|
| 102 | be returned for each Job object that is returned. |
---|
| 103 | |
---|
| 104 | This operation is similar to the Get-Job-Attributes operation, |
---|
| 105 | except that this Get-Jobs operation returns attributes from |
---|
| 106 | possibly more than one object. |
---|
| 107 | |
---|
| 108 | """ |
---|
[59a1d4a] | 109 | |
---|
| 110 | # verify the request and get an attribute dictionary |
---|
[ef8df33] | 111 | req_dict = ipp.ops.verify_get_jobs_request(request) |
---|
[59a1d4a] | 112 | |
---|
| 113 | # lookup printer name |
---|
[ef8df33] | 114 | printer_name = req_dict['printer-uri'] |
---|
[5fe360e] | 115 | if printer_name not in self.printers: |
---|
| 116 | raise ipp.errors.Attributes( |
---|
| 117 | "Invalid printer uri: %s" % printer_name, |
---|
| 118 | [request.attribute_groups[0].attributes[2]]) |
---|
[6ed9d7a] | 119 | |
---|
[59a1d4a] | 120 | # get the job attributes |
---|
[ef8df33] | 121 | jobs = [job.get_job_attributes(request) for job in \ |
---|
| 122 | self.printers[printer_name].get_jobs()] |
---|
[59a1d4a] | 123 | |
---|
| 124 | # build the response |
---|
[ef8df33] | 125 | response = ipp.ops.make_get_jobs_response(jobs, request) |
---|
| 126 | return response |
---|
[287d6ec] | 127 | |
---|
[94a4825] | 128 | @handler_for(consts.Operations.PRINT_URI) |
---|
[ef8df33] | 129 | def print_uri(self, request): |
---|
[94a4825] | 130 | raise NotImplementedError |
---|
[9eeab06] | 131 | |
---|
[94a4825] | 132 | @handler_for(consts.Operations.CREATE_JOB) |
---|
[ef8df33] | 133 | def create_job(self, request): |
---|
[94a4825] | 134 | """RFC 2911: 3.2.4 Create-Job Operation |
---|
| 135 | |
---|
| 136 | This OPTIONAL operation is similar to the Print-Job operation |
---|
| 137 | (section 3.2.1) except that in the Create-Job request, a |
---|
| 138 | client does not supply document data or any reference to |
---|
| 139 | document data. Also, the client does not supply any of the |
---|
| 140 | 'document-name', 'document- format', 'compression', or |
---|
| 141 | 'document-natural-language' operation attributes. This |
---|
| 142 | operation is followed by one or more Send-Document or Send-URI |
---|
| 143 | operations. In each of those operation requests, the client |
---|
| 144 | OPTIONALLY supplies the 'document-name', 'document-format', |
---|
| 145 | and 'document-natural-language' attributes for each document |
---|
| 146 | in the multi-document Job object. |
---|
| 147 | |
---|
| 148 | If a Printer object supports the Create-Job operation, it MUST |
---|
| 149 | also support the Send-Document operation and also MAY support |
---|
| 150 | the Send-URI operation. |
---|
| 151 | |
---|
| 152 | If the Printer object supports this operation, it MUST support |
---|
| 153 | the 'multiple-operation-time-out' Printer attribute (see |
---|
| 154 | section 4.4.31). If the Printer object supports this |
---|
| 155 | operation, then it MUST support the |
---|
| 156 | 'multiple-document-jobs-supported' Printer Description |
---|
| 157 | attribute (see section 4.4.16) and indicate whether or not it |
---|
| 158 | supports multiple-document jobs. |
---|
| 159 | |
---|
| 160 | If the Printer object supports this operation and supports |
---|
| 161 | multiple documents in a job, then it MUST support the |
---|
| 162 | 'multiple-document- handling' Job Template job attribute with |
---|
| 163 | at least one value (see section 4.2.4) and the associated |
---|
| 164 | 'multiple-document-handling- default' and |
---|
| 165 | 'multiple-document-handling-supported' Job Template Printer |
---|
| 166 | attributes (see section 4.2). |
---|
| 167 | |
---|
| 168 | After the Create-Job operation has completed, the value of the |
---|
| 169 | 'job- state' attribute is similar to the 'job-state' after a |
---|
| 170 | Print-Job, even though no document-data has arrived. A Printer |
---|
| 171 | MAY set the 'job-data-insufficient' value of the job's |
---|
| 172 | 'job-state-reason' attribute to indicate that processing |
---|
| 173 | cannot begin until sufficient data has arrived and set the |
---|
| 174 | 'job-state' to either 'pending' or 'pending-held'. A |
---|
| 175 | non-spooling printer that doesn't implement the 'pending' job |
---|
| 176 | state may even set the 'job-state' to 'processing', even |
---|
| 177 | though there is not yet any data to process. See sections |
---|
| 178 | 4.3.7 and 4.3.8. |
---|
| 179 | |
---|
| 180 | """ |
---|
[9eeab06] | 181 | |
---|
[94a4825] | 182 | raise NotImplementedError |
---|
| 183 | |
---|
| 184 | @handler_for(consts.Operations.PAUSE_PRINTER) |
---|
[ef8df33] | 185 | def pause_printer(self, request): |
---|
[94a4825] | 186 | raise NotImplementedError |
---|
[9eeab06] | 187 | |
---|
[94a4825] | 188 | @handler_for(consts.Operations.RESUME_PRINTER) |
---|
[ef8df33] | 189 | def resume_printer(self, request): |
---|
[94a4825] | 190 | raise NotImplementedError |
---|
[9eeab06] | 191 | |
---|
[aef164a] | 192 | @handler_for(consts.Operations.GET_PRINTER_ATTRIBUTES) |
---|
[ef8df33] | 193 | def get_printer_attributes(self, request): |
---|
[cad7502] | 194 | """RFC 2911: 3.2.5 Get-Printer-Attributes Operation |
---|
| 195 | |
---|
| 196 | This REQUIRED operation allows a client to request the values |
---|
| 197 | of the attributes of a Printer object. |
---|
| 198 | |
---|
| 199 | In the request, the client supplies the set of Printer |
---|
| 200 | attribute names and/or attribute group names in which the |
---|
| 201 | requester is interested. In the response, the Printer object |
---|
| 202 | returns a corresponding attribute set with the appropriate |
---|
| 203 | attribute values filled in. |
---|
| 204 | |
---|
| 205 | For Printer objects, the possible names of attribute groups are: |
---|
| 206 | |
---|
| 207 | - 'job-template': the subset of the Job Template attributes |
---|
| 208 | that apply to a Printer object (the last two columns of the |
---|
| 209 | table in Section 4.2) that the implementation supports for |
---|
| 210 | Printer objects. |
---|
| 211 | |
---|
| 212 | - 'printer-description': the subset of the attributes |
---|
| 213 | specified in Section 4.4 that the implementation supports |
---|
| 214 | for Printer objects. |
---|
| 215 | |
---|
| 216 | - 'all': the special group 'all' that includes all attributes |
---|
| 217 | that the implementation supports for Printer objects. |
---|
| 218 | |
---|
| 219 | Since a client MAY request specific attributes or named |
---|
| 220 | groups, there is a potential that there is some overlap. For |
---|
| 221 | example, if a client requests, 'printer-name' and 'all', the |
---|
| 222 | client is actually requesting the 'printer-name' attribute |
---|
| 223 | twice: once by naming it explicitly, and once by inclusion in |
---|
| 224 | the 'all' group. In such cases, the Printer object NEED NOT |
---|
| 225 | return each attribute only once in the response even if it is |
---|
| 226 | requested multiple times. The client SHOULD NOT request the |
---|
| 227 | same attribute in multiple ways. |
---|
| 228 | |
---|
| 229 | It is NOT REQUIRED that a Printer object support all |
---|
| 230 | attributes belonging to a group (since some attributes are |
---|
| 231 | OPTIONAL). However, it is REQUIRED that each Printer object |
---|
| 232 | support all group names. |
---|
| 233 | |
---|
| 234 | """ |
---|
| 235 | |
---|
[59a1d4a] | 236 | # verify the request and get the attributes dictionary |
---|
[ef8df33] | 237 | req_dict = ipp.ops.verify_get_printer_attributes_request(request) |
---|
[59a1d4a] | 238 | |
---|
| 239 | # lookup the printer name |
---|
[ef8df33] | 240 | printer_name = req_dict['printer-uri'] |
---|
[5fe360e] | 241 | if printer_name not in self.printers: |
---|
| 242 | raise ipp.errors.Attributes( |
---|
| 243 | "Invalid printer uri: %s" % printer_name, |
---|
| 244 | [request.attribute_groups[0].attributes[2]]) |
---|
[59a1d4a] | 245 | |
---|
| 246 | # bulid response |
---|
[ef8df33] | 247 | response = ipp.ops.make_get_printer_attributes_response( |
---|
| 248 | self.printers[printer_name].get_printer_attributes(request), request) |
---|
| 249 | return response |
---|
[9eeab06] | 250 | |
---|
[94a4825] | 251 | @handler_for(consts.Operations.SET_PRINTER_ATTRIBUTES) |
---|
[ef8df33] | 252 | def set_printer_attributes(self, request): |
---|
[94a4825] | 253 | raise NotImplementedError |
---|
[9eeab06] | 254 | |
---|
| 255 | ##### Job Commands |
---|
| 256 | |
---|
[94a4825] | 257 | @handler_for(consts.Operations.CANCEL_JOB) |
---|
[ef8df33] | 258 | def cancel_job(self, request): |
---|
[94a4825] | 259 | raise NotImplementedError |
---|
[9eeab06] | 260 | |
---|
[94a4825] | 261 | @handler_for(consts.Operations.SEND_DOCUMENT) |
---|
[ef8df33] | 262 | def send_document(self, request): |
---|
[94a4825] | 263 | raise NotImplementedError |
---|
[9eeab06] | 264 | |
---|
[94a4825] | 265 | @handler_for(consts.Operations.SEND_URI) |
---|
[ef8df33] | 266 | def send_uri(self, request): |
---|
[94a4825] | 267 | raise NotImplementedError |
---|
[9eeab06] | 268 | |
---|
[94a4825] | 269 | @handler_for(consts.Operations.GET_JOB_ATTRIBUTES) |
---|
[ef8df33] | 270 | def get_job_attributes(self, request): |
---|
[59a1d4a] | 271 | |
---|
| 272 | # verify the request and get the attributes dictionary |
---|
[ef8df33] | 273 | req_dict = ipp.ops.verify_get_jobs_request(request) |
---|
[5fe360e] | 274 | |
---|
[59a1d4a] | 275 | # lookup the printer name |
---|
| 276 | printer_name = req_dict['printer-uri'] |
---|
[5fe360e] | 277 | if printer_name not in self.printers: |
---|
| 278 | raise ipp.errors.Attributes( |
---|
| 279 | "Invalid printer uri: %s" % printer_name, |
---|
| 280 | [request.attribute_groups[0].attributes[2]]) |
---|
[59a1d4a] | 281 | |
---|
| 282 | # lookup the job id |
---|
| 283 | job_id = req_dict['job-id'] |
---|
| 284 | try: job = self.printers[printer_name].get_job(job_id) |
---|
[5fe360e] | 285 | except InvalidJobException: |
---|
| 286 | raise ipp.errors.Attributes( |
---|
| 287 | "Invalid job id: %d" % job_id, |
---|
| 288 | [request.attribute_groups[0].attributes[2]]) # XXX: this is wrong |
---|
| 289 | |
---|
| 290 | # XXX: we need to honor the things that the request actually asks for |
---|
[59a1d4a] | 291 | # build the response |
---|
[ef8df33] | 292 | response = ipp.ops.make_get_job_attributes_response( |
---|
| 293 | job.get_job_attributes(request), request) |
---|
| 294 | return response |
---|
[9eeab06] | 295 | |
---|
[94a4825] | 296 | @handler_for(consts.Operations.SET_JOB_ATTRIBUTES) |
---|
[ef8df33] | 297 | def set_job_attributes(self, request): |
---|
[94a4825] | 298 | raise NotImplementedError |
---|
[9eeab06] | 299 | |
---|
[94a4825] | 300 | @handler_for(consts.Operations.RESTART_JOB) |
---|
[ef8df33] | 301 | def restart_job(self, request): |
---|
[94a4825] | 302 | raise NotImplementedError |
---|
[9eeab06] | 303 | |
---|
[94a4825] | 304 | @handler_for(consts.Operations.PROMOTE_JOB) |
---|
[ef8df33] | 305 | def promote_job(self, request): |
---|
[94a4825] | 306 | raise NotImplementedError |
---|
[9eeab06] | 307 | |
---|
| 308 | ##### CUPS Specific Commands |
---|
| 309 | |
---|
[94a4825] | 310 | @handler_for(consts.Operations.CUPS_GET_DOCUMENT) |
---|
[e70c020] | 311 | def cups_get_document(self, request): |
---|
[94a4825] | 312 | raise NotImplementedError |
---|
[e70c020] | 313 | |
---|
[aef164a] | 314 | @handler_for(consts.Operations.CUPS_GET_DEFAULT) |
---|
[ef8df33] | 315 | def cups_get_default(self, request): |
---|
[0ede474] | 316 | """The CUPS-Get-Default operation (0x4001) returns the default |
---|
| 317 | printer URI and attributes. |
---|
| 318 | |
---|
| 319 | (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_DEFAULT ) |
---|
| 320 | |
---|
| 321 | """ |
---|
[287d6ec] | 322 | |
---|
[59a1d4a] | 323 | # verify the request and get the attributes dictionary |
---|
[ef8df33] | 324 | req_dict = ipp.ops.verify_cups_get_default_request(request) |
---|
[59a1d4a] | 325 | # build the response |
---|
[ef8df33] | 326 | response = ipp.ops.make_get_printer_attributes_response( |
---|
| 327 | self.printers[self.default].get_printer_attributes(request), request) |
---|
| 328 | return response |
---|
[0ede474] | 329 | |
---|
[aef164a] | 330 | @handler_for(consts.Operations.CUPS_GET_PRINTERS) |
---|
[ef8df33] | 331 | def cups_get_printers(self, request): |
---|
| 332 | """The CUPS-Get-Printers operation (0x4002) returns the |
---|
| 333 | printer attributes for every printer known to the system. This |
---|
| 334 | may include printers that are not served directly by the |
---|
| 335 | server. |
---|
[0ede474] | 336 | |
---|
| 337 | (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_PRINTERS ) |
---|
| 338 | |
---|
| 339 | """ |
---|
| 340 | |
---|
[59a1d4a] | 341 | # verify the request and get the attributes dictionary |
---|
[ef8df33] | 342 | req_dict = ipp.ops.verify_cups_get_printers_request(request) |
---|
[59a1d4a] | 343 | # get the printer attributes |
---|
[ef8df33] | 344 | attrs = [self.printers[printer].get_printer_attributes(request) \ |
---|
| 345 | for printer in self.printers] |
---|
[59a1d4a] | 346 | # build the response |
---|
[ef8df33] | 347 | response = ipp.ops.make_cups_get_printers_response(attrs, request) |
---|
| 348 | return response |
---|
[287d6ec] | 349 | |
---|
[aef164a] | 350 | @handler_for(consts.Operations.CUPS_GET_CLASSES) |
---|
[ef8df33] | 351 | def cups_get_classes(self, request): |
---|
[0ede474] | 352 | """The CUPS-Get-Classes operation (0x4005) returns the printer |
---|
| 353 | attributes for every printer class known to the system. This |
---|
| 354 | may include printer classes that are not served directly by |
---|
| 355 | the server. |
---|
[ef8df33] | 356 | |
---|
[0ede474] | 357 | (Source: http://www.cups.org/documentation.php/spec-ipp.html#CUPS_GET_CLASSES ) |
---|
| 358 | |
---|
| 359 | """ |
---|
[ef8df33] | 360 | |
---|
[59a1d4a] | 361 | # verify the request and get the attributes dictionaryu |
---|
[ef8df33] | 362 | req_dict = ipp.ops.verify_cups_get_classes_request(request) |
---|
[59a1d4a] | 363 | # build the response |
---|
[ef8df33] | 364 | response = ipp.ops.make_cups_get_classes_response(request) |
---|
| 365 | return response |
---|