This file is indexed.

/usr/share/pyshared/lazr/restful/example/base/tests/field.txt is in python-lazr.restful 0.9.29-0ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
Field resources
***************

Each of an entry's fields has its own HTTP resource. If you only need
to change one of an entry's fields, you can send PUT or PATCH to the
field resource itself, rather than PUT/PATCH to the entry.

    >>> from lazr.restful.testing.webservice import WebServiceCaller
    >>> webservice = WebServiceCaller(domain='cookbooks.dev')

    >>> from urllib import quote
    >>> cookbook_url = quote("/cookbooks/The Joy of Cooking")
    >>> field_url = cookbook_url + "/description"

    >>> import simplejson
    >>> def set_description(description):
    ...     """Sets the description for "The Joy of Cooking"."""
    ...     return webservice(field_url, 'PATCH',
    ...                       simplejson.dumps(description)).jsonBody()

    >>> print set_description("New description")
    New description

    >>> print webservice.get(field_url)
    HTTP/1.1 200 Ok
    ...
    Content-Type: application/json
    ...
    "New description"

PATCH on a field resource works identically to PUT.

    >>> representation = simplejson.dumps('<b>Bold description</b>')
    >>> print webservice.put(field_url, 'application/json',
    ...                      representation).jsonBody()
    <b>Bold description</b>

If you get a field that contains a link to another object, you'll see
the link, rather than the actual object.

    >>> link_field_url = "/recipes/3/cookbook_link"
    >>> print webservice.get(link_field_url).jsonBody()
    http://.../cookbooks/James%20Beard%27s%20American%20Cookery

    >>> collection_url = quote(
    ...     "/cookbooks/The Joy of Cooking/recipes_collection_link")
    >>> print webservice.get(collection_url).jsonBody()
    http://.../cookbooks/The%20Joy%20of%20Cooking/recipes

Changing a field resource that contains a link works the same way as
changing a field resource that contains a scalar value.

    >>> new_value = simplejson.dumps(
    ...     webservice.get(cookbook_url).jsonBody()['self_link'])
    >>> print new_value
    "http://.../cookbooks/The%20Joy%20of%20Cooking"

    >>> print webservice(link_field_url, 'PATCH', new_value)
    HTTP/1.1 209 Content Returned
    ...
    Content-Type: application/json
    ...
    <BLANKLINE>
    "http://cookbooks.dev/.../cookbooks/The%20Joy%20of%20Cooking"

The same rules for modifying a field apply whether you're modifying
the entry as a whole or just modifying a single field.

    >>> date_field_url = cookbook_url + "/copyright_date"
    >>> print webservice.put(date_field_url, 'application/json',
    ...                      simplejson.dumps("string"))
    HTTP/1.1 400 Bad Request
    ...
    copyright_date: Value doesn't look like a date.

    >>> print webservice(collection_url, 'PATCH', new_value)
    HTTP/1.1 400 Bad Request
    ...
    recipes_collection_link: You tried to modify a collection attribute.

Field resources also support GET, for when you only need part of an
entry. You can get either a JSON or XHTML-fragment representation.

    >>> print webservice.get(field_url).jsonBody()
    <b>Bold description</b>

    >>> print webservice.get(field_url, 'application/xhtml+xml')
    HTTP/1.1 200 Ok
    ...
    Content-Type: application/xhtml+xml
    ...
    &lt;b&gt;Bold description&lt;/b&gt;

Cleanup.

    >>> ignored = set_description("Description")

Changing a field resource can move the entry
--------------------------------------------

If you modify a field that the entry uses as part of its URL (such as
a cookbook's name), the field's URL will change. You'll be redirected
to the new field URL.

    >>> name_url = cookbook_url + "/name"
    >>> representation = simplejson.dumps("The Joy of Cooking Extreme")
    >>> print webservice.put(name_url, 'application/json',
    ...                      representation)
    HTTP/1.1 301 Moved Permanently
    ...
    Location: http://.../cookbooks/The%20Joy%20of%20Cooking%20Extreme/name
    ...

Note that the entry's URL has also changed.

    >>> print webservice.get(cookbook_url)
    HTTP/1.1 404 Not Found
    ...

    >>> new_cookbook_url = quote("/cookbooks/The Joy of Cooking Extreme")
    >>> print webservice.get(new_cookbook_url)
    HTTP/1.1 200 Ok
    ...

Cleanup.

    >>> representation = simplejson.dumps("The Joy of Cooking")
    >>> new_name_url = new_cookbook_url + "/name"
    >>> print webservice.put(new_name_url, 'application/json',
    ...                      representation)
    HTTP/1.1 301 Moved Permanently
    ...
    Location: http://.../cookbooks/The%20Joy%20of%20Cooking/name
    ...

Field resources can give more detail than entry resources
=========================================================

An entry resource, and the field resource for one of the entry's
fields, will display the same basic information. But the entry field
can give a lot more detail.

For instance, here's the representation of a cookbook's 'cuisine' field
within the cookbook entry itself.

    >>> cookbook = webservice.get(cookbook_url).jsonBody()
    >>> print cookbook['cuisine']
    General

Here's the representation of the resource for the same 'cuisine' field.

    >>> for cuisine in sorted(
    ...     webservice.get(cookbook_url + '/cuisine').jsonBody()):
    ...     print(sorted(cuisine.items()))
    [(u'title', u'American'), (u'token', u'AMERICAN')]
    ...
    [(u'selected', True), (u'title', u'General'), (u'token', u'GENERAL')]

The detailed representation includes information about the other
values the 'status' field can take. This information is also published
in the WADL file, but that's not easily accessible to some clients,
especially JavaScript clients.

XHTML representations don't work this way. Complex XHTML
representations require custom code; see "Custom representations"
below. By default, the XHTML representation of a field is a simple
HTML-escaped string, similar to what's seen in the JSON representation
of the entry.

    >>> print webservice.get(cookbook_url + '/cuisine',
    ...                      'application/xhtml+xml')
    HTTP/1.1 200 Ok
    ...
    General

=================
Supported methods
=================

Field resources support GET, PUT, and PATCH.

    >>> for method in ['HEAD', 'POST', 'DELETE', 'OPTIONS']:
    ...     print webservice(field_url, method)
    HTTP/1.1 405 Method Not Allowed...
    Allow: GET PUT PATCH
    ...
    HTTP/1.1 405 Method Not Allowed...
    Allow: GET PUT PATCH
    ...
    HTTP/1.1 405 Method Not Allowed...
    Allow: GET PUT PATCH
    ...
    HTTP/1.1 405 Method Not Allowed...
    Allow: GET PUT PATCH
    ...

===============
Conditional GET
===============

Field resources have ETags independent of their parent entries. They
respond to conditional GET.

    >>> response = webservice.get(cookbook_url)
    >>> cookbook_etag = response.getheader('ETag')

    >>> response = webservice.get(field_url)
    >>> etag = response.getheader('ETag')

    >>> cookbook_etag == etag
    False

    >>> print webservice.get(field_url, headers={'If-None-Match': etag})
    HTTP/1.1 304 Not Modified
    ...

    >>> ignored = set_description("new description")
    >>> print webservice.get(field_url,
    ...                      headers={'If-None-Match': etag})
    HTTP/1.1 200 Ok
    ...

=================
Conditional write
=================

Every field supports conditional PUT and PATCH, just like the entries
do.

    >>> response = webservice.get(field_url)
    >>> cookbook_etag = response.getheader('ETag')

The first attempt to modify the field succeeds, because the ETag
provided in If-Match is the one we just got from a GET request.

    >>> representation = simplejson.dumps("New description")
    >>> print webservice.put(field_url, 'application/json',
    ...                      representation,
    ...                      headers={'If-Match': cookbook_etag})
    HTTP/1.1 209 Content Returned
    ...

But when the field is modified, the ETag changes. Any subsequent
requests that use that ETag in If-Match will fail.

    >>> print webservice.put(field_url, 'application/json',
    ...                      representation,
    ...                      headers={'If-Match': cookbook_etag})
    HTTP/1.1 412 Precondition Failed
    ...

    >>> ignored = set_description("Description")

============================
Custom XHTML representations
============================

Every entry has an XHTML representation. The default representation is
a simple text node.

  >>> print webservice.get(field_url, 'application/xhtml+xml')
  HTTP/1.1 200 Ok
  ...
  Description

But it's possible to define a custom HTML renderer for a particular
object and field type. Here's a simple renderer that bolds whatever
value it's given.

  >>> from zope import component
  >>> from zope.interface import implementer
  >>> from zope.schema.interfaces import ITextLine
  >>> from lazr.restful.interfaces import (
  ...     IFieldHTMLRenderer, IWebServiceClientRequest)
  >>> from lazr.restful.example.base.interfaces import ICookbook

  >>> @component.adapter(ICookbook, ITextLine, IWebServiceClientRequest)
  ... @implementer(IFieldHTMLRenderer)
  ... def dummy_renderer(context, field, request):
  ...     """Create a simple renderer that bolds the original string."""
  ...     def render(value):
  ...         return "<b>%s</b>" % value.encode("utf-8")
  ...     return render

  >>> print webservice.get(cookbook_url +'/name', 'application/xhtml+xml')
  HTTP/1.1 200 Ok
  ...
  The Joy of Cooking

Register the renderer as the IFieldHTMLRenderer adapter for an
ITextLine field of an IPerson entry...

  >>> from zope.component import getGlobalSiteManager
  >>> manager = getGlobalSiteManager()
  >>> manager.registerAdapter(dummy_renderer)

...and the XHTML representation of an ICookbook's description will be the
result of calling a dummy_renderer object.

  >>> print webservice.get(field_url, 'application/xhtml+xml')
  HTTP/1.1 200 Ok
  ...
  <b>Description</b>

In fact, that adapter will be used for every ITextLine field of an
ICookbook.

  >>> print webservice.get(cookbook_url +'/name', 'application/xhtml+xml')
  HTTP/1.1 200 Ok
  ...
  <b>The Joy of Cooking</b>

The adapter will not be used for ITextLine fields of other interfaces:

  >>> dish_field_url = quote('/dishes/Roast chicken/name')
  >>> print webservice.get(dish_field_url, 'application/xhtml+xml')
  HTTP/1.1 200 Ok
  ...
  Roast chicken

It will not be used for non-text fields of ICookbook.

  >>> print webservice.get(cookbook_url + '/copyright_date',
  ...                      'application/xhtml+xml')
  HTTP/1.1 200 Ok
  ...
  1995-01-01

Before we continue, here's some cleanup code to remove the custom
renderer we just defined.

  >>> ignored = getGlobalSiteManager().unregisterAdapter(dummy_renderer)

Compare the HTML generated by the custom renderer, to the XHTML
generated now that the default adapter is back in place.

  >>> ignored = set_description("<b>Bold description</b>")

  >>> print webservice.get(field_url, 'application/xhtml+xml')
  HTTP/1.1 200 Ok
  ...
  &lt;b&gt;Bold description&lt;/b&gt;

  >>> ignored = set_description("Description")

The default renderer escapes HTML tags because it thinks they might
contain XSS attacks. If you define a custom adapter, you can generate
XHTML without worrying about the tags being escaped. The downside is
that you're responsible for escaping user-entered HTML tags yourself
to avoid XSS attacks.

Defining a custom representation for a single field
===================================================

It's also possible to define a custom HTML representation of one
particular field, by registering a view on the field. This code
creates a custom renderer for ICookbook.description, by registering
a view on ICookbook called "description".

  >>> manager.registerAdapter(dummy_renderer, name='description')

  >>> print webservice.get(field_url, 'application/xhtml+xml')
  HTTP/1.1 200 Ok
  ...
  <b>Description</b>

Unlike what happened when we registered a renderer for
ICookbook/ITextLine, other ITextLine fields of ICookbook are not
affected.

  >>> print webservice.get(cookbook_url + '/name', 'application/xhtml+xml')
  HTTP/1.1 200 Ok
  ...
  The Joy of Cooking

The XHTML representation of an entry incorporates any custom XHTML
representations of that entry's fields.

  >>> print webservice.get(cookbook_url, 'application/xhtml+xml')
  HTTP/1.1 200 Ok
  ...
  <dt>description</dt>
  <dd><b>Description</b></dd>
  ...

Before we continue, here's some code to unregister the view.

  >>> ignored = getGlobalSiteManager().unregisterAdapter(
  ...      dummy_renderer, name='description')

  >>> print webservice.get(field_url, 'application/xhtml+xml')
  HTTP/1.1 200 Ok
  ...
  Description