[35f7259] | 1 | from .value import Value |
---|
| 2 | from .attribute import Attribute |
---|
| 3 | from .attributegroup import AttributeGroup |
---|
| 4 | from .request import Request |
---|
| 5 | from .constants import AttributeTags, Tags |
---|
| 6 | import exceptions as err |
---|
| 7 | |
---|
| 8 | from collections import OrderedDictionary as odict |
---|
| 9 | |
---|
| 10 | def verify_operations(request): |
---|
| 11 | """Pretty much all requests have the first attribute group for |
---|
| 12 | operation attributes, and these all have 'attributes-charset' and |
---|
| 13 | 'attributes-natural-language' as the first two attributes. This |
---|
| 14 | method just generically verifies that these attributes are there. |
---|
| 15 | |
---|
| 16 | """ |
---|
| 17 | |
---|
| 18 | # check operation attributes tag |
---|
| 19 | op_attrs = request.attribute_groups[0] |
---|
| 20 | if op_attrs.tag != AttributeTags.OPERATION: |
---|
| 21 | raise err.BadRequest( |
---|
| 22 | "Attribute group does not have OPERATION tag: 0x%x" % op_attrs.tag) |
---|
| 23 | |
---|
| 24 | # check charset attribute |
---|
| 25 | charset_attr = op_attrs.attributes[0] |
---|
| 26 | if charset_attr.name != 'attributes-charset': |
---|
| 27 | raise err.BadRequest( |
---|
| 28 | "Attribute is not attributes-charset: %s" % charset_attr.name) |
---|
| 29 | if len(charset_attr.values) != 1: |
---|
| 30 | raise err.BadRequest( |
---|
| 31 | "Too many values for attributes-charset: %d" % len(charset_attr.values)) |
---|
| 32 | |
---|
| 33 | # check charset value |
---|
| 34 | charset_value = charset_attr.values[0] |
---|
| 35 | if charset_value.tag != Tags.CHARSET: |
---|
| 36 | raise err.BadRequest( |
---|
| 37 | "Charset value does not have CHARSET tag: 0x%x" % charset_value.tag) |
---|
| 38 | if charset_value.value != 'utf-8': |
---|
| 39 | raise err.Charset( |
---|
| 40 | "Invalid charset value: %s" % charset_value.value) |
---|
| 41 | |
---|
| 42 | # check for attributes-natural-language |
---|
| 43 | natlang_attr = op_attrs.attributes[1] |
---|
| 44 | if natlang_attr.name != 'attributes-natural-language': |
---|
| 45 | raise err.BadRequest( |
---|
| 46 | "Attribute is not attributes-natural-language: %s" % natlang_attr.name) |
---|
| 47 | if len(charset_attr.values) != 1: |
---|
| 48 | raise err.BadRequest( |
---|
| 49 | "Too many values for attributes-natural-language: %s" % len(natlang_attr.values)) |
---|
| 50 | |
---|
| 51 | # check natural language value |
---|
| 52 | natlang_value = natlang_attr.values[0] |
---|
| 53 | if natlang_value.tag != Tags.NATURAL_LANGUAGE: |
---|
| 54 | raise err.BadRequest( |
---|
| 55 | "Natural language value does not have NATURAL_LANGUAGE tag: 0x%x" natlang_value.tag) |
---|
| 56 | if natlang_value.value != 'en-us': |
---|
| 57 | raise err.Attributes( |
---|
| 58 | "Invalid natural language value: %s" % natlang_value.value, [natlang_attr]) |
---|
| 59 | |
---|
| 60 | def verify_requesting_username(requser_attr): |
---|
| 61 | if requser_attr.tag != Tags.NAME_WITHOUT_LANGUAGE: |
---|
| 62 | raise err.BadRequest( |
---|
| 63 | "Requesting user name attribute tag is not NAME_WITHOUT_LANGUAGE: 0x%x" % \ |
---|
| 64 | requser_attr.tag) |
---|
| 65 | if len(requser_attr.values) != 1: |
---|
| 66 | raise err.BadRequest( |
---|
| 67 | "Requesting user name attribute has too many values: %d" % len(requser_attr.values)) |
---|
| 68 | requser_value = requser_attr.values[0] |
---|
| 69 | if requser_value.tag != Tags.NAME_WITHOUT_LANGUAGE: |
---|
| 70 | raise err.BadRequest( |
---|
| 71 | "Requesting user name value tag is not NAME_WITHOUT_LANGUAGE: 0x%x" % \ |
---|
| 72 | requser_value.tag) |
---|
| 73 | return requser_value.value |
---|
| 74 | |
---|
| 75 | def verify_get_jobs_request(request): |
---|
| 76 | """RFC 2911 3.2.6.1 Get-Jobs Request |
---|
| 77 | |
---|
| 78 | The client submits the Get-Jobs request to a Printer object. |
---|
| 79 | |
---|
| 80 | The following groups of attributes are part of the Get-Jobs |
---|
| 81 | Request: |
---|
| 82 | |
---|
| 83 | Group 1: Operation Attributes |
---|
| 84 | Natural Language and Character Set: |
---|
| 85 | The 'attributes-charset' and |
---|
| 86 | 'attributes-natural-language' attributes as described |
---|
| 87 | in section 3.1.4.1. |
---|
| 88 | Target: |
---|
| 89 | The 'printer-uri' (uri) operation attribute which is |
---|
| 90 | the target for this operation as described in section |
---|
| 91 | 3.1.5. |
---|
| 92 | Requesting User Name: |
---|
| 93 | The 'requesting-user-name' (name(MAX)) attribute |
---|
| 94 | SHOULD be supplied by the client as described in |
---|
| 95 | section 8.3. |
---|
| 96 | 'limit' (integer(1:MAX)): |
---|
| 97 | The client OPTIONALLY supplies this attribute. The |
---|
| 98 | Printer object MUST support this attribute. It is an |
---|
| 99 | integer value that determines the maximum number of |
---|
| 100 | jobs that a client will receive from the Printer even |
---|
| 101 | if 'which-jobs' or 'my-jobs' constrain which jobs are |
---|
| 102 | returned. The limit is a 'stateless limit' in that if |
---|
| 103 | the value supplied by the client is ’N’, then only the |
---|
| 104 | first ’N’ jobs are returned in the Get-Jobs Response. |
---|
| 105 | There is no mechanism to allow for the next ’M’ jobs |
---|
| 106 | after the first ’N’ jobs. If the client does not |
---|
| 107 | supply this attribute, the Printer object responds |
---|
| 108 | with all applicable jobs. |
---|
| 109 | 'requested-attributes' (1setOf type2 keyword): |
---|
| 110 | The client OPTIONALLY supplies this attribute. The |
---|
| 111 | Printer object MUST support this attribute. It is a |
---|
| 112 | set of Job attribute names and/or attribute groups |
---|
| 113 | names in whose values the requester is |
---|
| 114 | interested. This set of attributes is returned for |
---|
| 115 | each Job object that is returned. The allowed |
---|
| 116 | attribute group names are the same as those defined in |
---|
| 117 | the Get-Job-Attributes operation in section 3.3.4. If |
---|
| 118 | the client does not supply this attribute, the Printer |
---|
| 119 | MUST respond as if the client had supplied this |
---|
| 120 | attribute with two values: ’job-uri’ and ’job-id’. |
---|
| 121 | 'which-jobs' (type2 keyword): |
---|
| 122 | The client OPTIONALLY supplies this attribute. The |
---|
| 123 | Printer object MUST support this attribute. It |
---|
| 124 | indicates which Job objects MUST be returned by the |
---|
| 125 | Printer object. The values for this attribute are: |
---|
| 126 | - ’completed’: This includes any Job object whose |
---|
| 127 | state is ’completed’, ’canceled’, or ’aborted’. |
---|
| 128 | - ’not-completed’: This includes any Job object whose |
---|
| 129 | state is ’pending’, ’processing’, |
---|
| 130 | ’processing-stopped’, or ’pending-held’. |
---|
| 131 | A Printer object MUST support both values. However, if |
---|
| 132 | the implementation does not keep jobs in the |
---|
| 133 | ’completed’, ’canceled’, and ’aborted’ states, then it |
---|
| 134 | returns no jobs when the ’completed’ value is |
---|
| 135 | supplied. If a client supplies some other value, the |
---|
| 136 | Printer object MUST copy the attribute and the |
---|
| 137 | unsupported value to the Unsupported Attributes |
---|
| 138 | response group, reject the request, and return the |
---|
| 139 | ’client-error-attributes-or-values-not-supported’ |
---|
| 140 | status code. If the client does not supply this |
---|
| 141 | attribute, the Printer object MUST respond as if the |
---|
| 142 | client had supplied the attribute with a value of |
---|
| 143 | ’not-completed’. |
---|
| 144 | 'my-jobs' (boolean): |
---|
| 145 | The client OPTIONALLY supplies this attribute. The |
---|
| 146 | Printer object MUST support this attribute. It |
---|
| 147 | indicates whether jobs from all users or just the jobs |
---|
| 148 | submitted by the requesting user of this request MUST |
---|
| 149 | be considered as candidate jobs to be returned by the |
---|
| 150 | Printer object. If the client does not supply this |
---|
| 151 | attribute, the Printer object MUST respond as if the |
---|
| 152 | client had supplied the attribute with a value of |
---|
| 153 | ’false’, i.e., jobs from all users. The means for |
---|
| 154 | authenticating the requesting user and matching the |
---|
| 155 | jobs is described in section 8. |
---|
| 156 | |
---|
| 157 | """ |
---|
| 158 | |
---|
| 159 | # generic operations verification |
---|
| 160 | verify_operations(request) |
---|
| 161 | |
---|
| 162 | # requesting username |
---|
| 163 | requser = verify_requesting_username(request.attributes[2]) |
---|
| 164 | out = {'requesting-user-name': requser} |
---|
| 165 | |
---|
| 166 | # make the rest of the attributes into a dictionary |
---|
| 167 | attrs = dict([(attr.name, attr.values) for attr in request.attributes[3:]]) |
---|
| 168 | |
---|
| 169 | if 'limit' in attrs: |
---|
| 170 | out['limit'] = None # XXX |
---|
| 171 | |
---|
| 172 | if 'requested-attributes' in attrs: |
---|
| 173 | out['requested-attributes'] = None # XXX |
---|
| 174 | |
---|
| 175 | if 'which-jobs' in attrs: |
---|
| 176 | out['which-jobs'] = None # XXX |
---|
| 177 | |
---|
| 178 | if 'my-jobs' in attrs: |
---|
| 179 | out['my-jobs'] = None # XXX |
---|
| 180 | |
---|
| 181 | return out |
---|