~leonardr/lazr.restful/web-link

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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
# Copyright 2008 Canonical Ltd.  All rights reserved.

"""Base classes for HTTP resources."""

__metaclass__ = type

__all__ = [
    'BatchingResourceMixin',
    'Collection',
    'CollectionResource',
    'Entry',
    'EntryAdapterUtility',
    'EntryField',
    'EntryFieldResource',
    'EntryHTMLView',
    'EntryResource',
    'HTTPResource',
    'JSONItem',
    'ReadOnlyResource',
    'RedirectResource',
    'register_versioned_request_utility',
    'render_field_to_html',
    'ResourceJSONEncoder',
    'RESTUtilityBase',
    'ScopedCollection',
    'ServiceRootResource',
    'WADL_SCHEMA_FILE',
    ]


from datetime import datetime, date
from email.Utils import formatdate
import cgi
import copy
import os
import simplejson
import time

# Import SHA in a way compatible with both Python 2.4 and Python 2.6.
try:
    import hashlib
    sha_constructor = hashlib.sha1
except ImportError:
     import sha
     sha_constructor = sha.new

from zope.app.pagetemplate.engine import TrustedAppPT
from zope import component
from zope.component import (
    adapts,
    getAdapters,
    getAllUtilitiesRegisteredFor,
    getGlobalSiteManager,
    getMultiAdapter,
    getSiteManager,
    getUtility,
    queryAdapter,
    queryMultiAdapter,
    )
from zope.component.interfaces import ComponentLookupError
from zope.event import notify
from zope.publisher.http import init_status_codes, status_reasons
from zope.interface import (
    alsoProvides, implementer, implements, implementedBy, providedBy,
    Interface)
from zope.interface.common.sequence import IFiniteSequence
from zope.interface.interfaces import IInterface
from zope.location.interfaces import ILocation
from zope.pagetemplate.pagetemplatefile import PageTemplateFile
from zope.proxy import isProxy
from zope.publisher.interfaces import NotFound
from zope.publisher.interfaces.http import IHTTPRequest
from zope.schema import ValidationError, getFieldsInOrder
from zope.schema.interfaces import (
    ConstraintNotSatisfied, IBytes, IField, RequiredMissing)
from zope.security.interfaces import Unauthorized
from zope.security.proxy import getChecker, removeSecurityProxy
from zope.security.management import checkPermission
from zope.traversing.browser import absoluteURL, AbsoluteURL
from zope.traversing.browser.interfaces import IAbsoluteURL

from lazr.batchnavigator import BatchNavigator
from lazr.delegates import Passthrough
from lazr.enum import BaseItem
from lazr.lifecycle.event import ObjectModifiedEvent
from lazr.lifecycle.snapshot import Snapshot
from lazr.restful.interfaces import (
    IClientError,
    IClientErrorView,
    ICollectionField,
    ICollectionResource,
    IEntry,
    IEntryField,
    IEntryFieldResource,
    IEntryResource,
    IFieldHTMLRenderer,
    IFieldMarshaller,
    IHTTPResource,
    IJSONPublishable,
    IReference,
    IReferenceChoice,
    IRepresentationCache,
    IResourceDELETEOperation,
    IResourceGETOperation,
    IResourcePOSTOperation,
    IScopedCollection,
    IServiceRootResource,
    ITopLevelEntryLink,
    IUnmarshallingDoesntNeedValue,
    IWebBrowserOriginatingRequest,
    ICollection,
    IWebServiceClientRequest,
    IWebServiceConfiguration,
    IWebServiceLayer,
    IWebServiceVersion,
    LAZR_WEBSERVICE_NAME,
    )
from lazr.restful.utils import (
    extract_write_portion,
    get_current_browser_request,
    get_current_web_service_request,
    sorted_named_things,
    )


# The path to the WADL XML Schema definition.
WADL_SCHEMA_FILE = os.path.join(os.path.dirname(__file__),
                                'wadl20061109.xsd')

# Constants and levels of detail to use when unmarshalling the data.
MISSING = object()
NORMAL_DETAIL = object()
CLOSEUP_DETAIL = object()

# XXX leonardr 2009-01-29
# bug=https://bugs.edge.launchpad.net/zope3/+bug/322486:
# Add nonstandard status methods to Zope's status_reasons dictionary.
for code, reason in [(209, 'Content Returned')]:
    if not code in status_reasons:
        status_reasons[code] = reason
init_status_codes()


def decode_value(value):
    """Return a unicode value curresponding to `value`."""
    if isinstance(value, unicode):
        return value
    elif isinstance(value, str):
        return value.decode("utf-8")
    else:
        return unicode(value)


def encode_value(value):
    """Return a UTF-8 string corresponding to `value`.

    Non-unicode strings are assumed to be UTF-8 already.
    """
    if isinstance(value, unicode):
        return value.encode("utf-8")
    elif isinstance(value, str):
        return value
    else:
        return str(value)


def register_versioned_request_utility(interface, version):
    """Registers a marker interface as a utility for version lookup.

    This function registers the given interface class as the
    IWebServiceVersion utility for the given version string.
    """
    alsoProvides(interface, IWebServiceVersion)
    getSiteManager().registerUtility(
        interface, IWebServiceVersion, name=version)


class LazrPageTemplateFile(TrustedAppPT, PageTemplateFile):
    "A page template class for generating web service-related documents."
    pass


class ResourceJSONEncoder(simplejson.JSONEncoder):
    """A JSON encoder for JSON-exposable resources like entry resources.

    This class works with simplejson to encode objects as JSON if they
    implement IJSONPublishable. All EntryResource subclasses, for
    instance, should implement IJSONPublishable.
    """

    def default(self, obj):
        """Convert the given object to a simple data structure."""
        if isinstance(obj, datetime) or isinstance(obj, date):
            return obj.isoformat()
        if isProxy(obj):
            # We have a security-proxied version of a built-in
            # type. We create a new version of the type by copying the
            # proxied version's content. That way the container is not
            # security proxied (and simplejson will know what do do
            # with it), but the content will still be security
            # wrapped.
            underlying_object = removeSecurityProxy(obj)
            if isinstance(underlying_object, list):
                return list(obj)
            if isinstance(underlying_object, tuple):
                return tuple(obj)
            if isinstance(underlying_object, dict):
                return dict(obj)
        request = get_current_web_service_request()
        if queryMultiAdapter((obj, request), IEntry):
            obj = EntryResource(obj, request)

        return IJSONPublishable(obj).toDataForJSON()


class JSONItem:
    """JSONPublishable adapter for lazr.enum."""
    implements(IJSONPublishable)
    adapts(BaseItem)

    def __init__(self, context):
        self.context = context

    def toDataForJSON(self):
        """See `ISJONPublishable`"""
        return str(self.context.title)


class RedirectResource:
    """A resource that redirects to another URL."""
    implements(IHTTPResource)

    def __init__(self, url, request):
        self.url = url
        self.request = request

    def __call__(self):
        url = self.url
        self.request.response.setStatus(301)
        self.request.response.setHeader("Location", url)


class HTTPResource:
    """See `IHTTPResource`."""
    implements(IHTTPResource)

    # Some interesting media types.
    WADL_TYPE = 'application/vnd.sun.wadl+xml'
    JSON_TYPE = 'application/json'
    XHTML_TYPE = 'application/xhtml+xml'

    # This misspelling of the WADL media type was used for a while,
    # and lazr.restful still supports it to avoid breaking clients
    # that depend on it.
    DEPRECATED_WADL_TYPE = 'application/vd.sun.wadl+xml'

    # A preparsed template file for WADL representations of resources.
    WADL_TEMPLATE = LazrPageTemplateFile('templates/wadl-resource.pt')

    # All resources serve WADL and JSON representations. Only entry
    # resources serve XHTML representations.
    SUPPORTED_CONTENT_TYPES = [WADL_TYPE, DEPRECATED_WADL_TYPE, JSON_TYPE]

    def __init__(self, context, request):
        self.context = context
        self.request = request
        self.etags_by_media_type = {}

    def __call__(self):
        """See `IHTTPResource`."""
        pass

    def getRequestMethod(self, request=None):
        """Return the HTTP method of the provided (or current) request.

        This is usually the actual HTTP method, but it might be
        overridden by a value for X-HTTP-Method-Override.

        :return: The HTTP method to use.
        """
        if request == None:
            request = self.request
        override = request.headers.get('X-HTTP-Method-Override')
        if override is not None and request.method == 'POST':
            # POST is the only HTTP method for which we respect
            # X-HTTP-Method-Override.
            return override
        return request.method

    def handleConditionalGET(self):
        """Handle a possible conditional GET request.

        This method has side effects. If the resource provides a
        generated ETag, it sets this value as the "ETag" response
        header. If the "ETag" request header matches the generated
        ETag, it sets the response code to 304 ("Not Modified").

        :return: The media type to serve. If this value is None, the
            incoming ETag matched the generated ETag and there is no
            need to serve anything else.
        """
        incoming_etags = self._parseETags('If-None-Match')

        media_type = self.getPreferredSupportedContentType()
        existing_etag = self.getETag(media_type)
        if existing_etag is not None:
            self.request.response.setHeader('ETag', existing_etag)
            if existing_etag in incoming_etags:
                # The client already has this representation.
                # No need to send it again.
                self.request.response.setStatus(304) # Not Modified
                media_type = None
        return media_type

    def handleConditionalWrite(self):
        """Handle a possible conditional PUT or PATCH request.

        This method has side effects. If the write operation should
        not continue, because the incoming ETag doesn't match the
        generated ETag, it sets the response code to 412 ("Precondition
        Failed"). Whether or not the two ETags 'match' depends on the
        resource-specific implementation, but by default two ETags
        only 'match' if they are identical.

        If the PUT or PATCH request is being tunneled through POST
        with X-HTTP-Method-Override, the media type of the incoming
        representation will be obtained from X-Content-Type-Override
        instead of Content-Type, should X-C-T-O be provided.

        :return: The media type of the incoming representation. If
            this value is None, the incoming ETag didn't match the
            generated ETag and the incoming representation should be
            ignored.
        """
        media_type = self.request.headers.get('X-Content-Type-Override')
        if media_type is not None:
            if self.request.method != 'POST':
                # X-C-T-O should not be used unless the underlying
                # method is POST. Set response code 400 ("Bad
                # Request").
                self.request.response.setStatus(400)
                return None
        else:
            media_type = self.request.headers.get(
                'Content-Type', self.JSON_TYPE)

        incoming_etags = self._parseETags('If-Match')
        if len(incoming_etags) == 0:
            # This is not a conditional write.
            return media_type
        existing_etag = self.getETag(media_type)
        if self._etagMatchesForWrite(existing_etag, incoming_etags):
            # The conditional write can continue.
            return media_type
        # The resource has changed since the client requested it.
        # Don't let the write go through. Set response code 412
        # ("Precondition Failed")
        self.request.response.setStatus(412)
        return None

    def _etagMatchesForWrite(self, existing_etag, incoming_etags):
        """Perform an ETag match to see if conditional write is OK.

        By default, we match the full ETag strings against each other.
        """
        return existing_etag in incoming_etags

    def _getETagCores(self, cache=None):
        """Calculate the core ETag for a representation.

        :return: a list of strings that will be used to calculate the
            full ETag. If None is returned, no ETag at all will be
            calculated for the given resource and media type.
        """
        return None

    def getETag(self, media_type, cache=None):
        """Calculate the ETag for an entry.

        An ETag is derived from a 'core' string conveying information
        about the representation itself, plus information about the
        content type and the current Launchpad revision number. The
        resulting string is hashed, and there's your ETag.

        :arg unmarshalled_field_values: A dict mapping field names to
            unmarshalled values, obtained during some other operation
            such as the construction of a representation.
        """
        # Try to find a cached value.
        etag = self.etags_by_media_type.get(media_type)
        if etag is not None:
            return etag

        if media_type in [self.WADL_TYPE, self.DEPRECATED_WADL_TYPE]:
            # The WADL representation of a resource only changes when
            # the software itself changes. Thus, we don't need any
            # special information for its ETag core.
            etag_cores = ['']
        else:
            # For other media types, we calculate the ETag core by
            # delegating to the subclass.
            etag_cores = self._getETagCores(cache)

        if etag_cores is None:
            return None

        core_hashes = []
        for core in etag_cores:
            hash_object = sha_constructor()
            hash_object.update(core)
            core_hashes.append(hash_object)

        # Append the media type, so that web browsers won't treat
        # different representations of a resource interchangeably.
        core_hashes[-1].update("\0" + media_type)

        etag = '"%s"' % "-".join([core.hexdigest() for core in core_hashes])
        self.etags_by_media_type[media_type] = etag
        return etag

    def implementsPOST(self):
        """Returns True if this resource will respond to POST.

        Right now this means the resource has defined one or more
        custom POST operations.
        """
        adapters = list(
            getAdapters((self.context, self.request), IResourcePOSTOperation))
        return len(adapters) > 0

    def implementsDELETE(self):
        """Returns True if this resource will respond to DELETE."""
        adapters = list(
            getAdapters(
                (self.context, self.request), IResourceDELETEOperation))
        assert len(adapters) < 2, ("%s has more than one registered DELETE "
                                   "handler." % self.context.__class__)
        return len(adapters) > 0

    def toWADL(self):
        """Represent this resource as a WADL application.

        The WADL document describes the capabilities of this resource.
        """
        namespace = self.WADL_TEMPLATE.pt_getContext()
        namespace['context'] = self
        return self.WADL_TEMPLATE.pt_render(namespace)

    def getPreferredSupportedContentType(self):
        """Of the content types we serve, which would the client prefer?

        The web service supports WADL, XHTML, and JSON
        representations. If no supported media type is requested, JSON
        is the default. This method determines whether the client
        would rather have WADL, XHTML, or JSON.
        """
        content_types = self.getPreferredContentTypes()
        preferences = []
        winner = None
        for media_type in self.SUPPORTED_CONTENT_TYPES:
            try:
                pos = content_types.index(media_type)
                if winner is None or pos < winner[1]:
                    winner = (media_type, pos)
            except ValueError:
                pass
        if winner is None:
            return self.JSON_TYPE
        else:
            return winner[0]

    def getPreferredContentTypes(self):
        """Find which content types the client prefers to receive."""
        accept_header = (self.request.form.pop('ws.accept', None)
            or self.request.get('HTTP_ACCEPT'))
        return self._parseAcceptStyleHeader(accept_header)

    def _parseETags(self, header_name):
        """Extract a list of ETags from a header and parse the list.

        RFC2616 allows multiple comma-separated ETags.

        If the compensate_for_mod_compress_etag_modification value is set,
        ETags like '"foo"-gzip' and '"foo-gzip"' will be transformed into
        '"foo"'.
        """
        header = self.request.getHeader(header_name)
        if header is None:
            return []
        utility = getUtility(IWebServiceConfiguration)
        strip_gzip_from_etag = (
            utility.compensate_for_mod_compress_etag_modification)
        etags = []
        # We're kind of cheating, because entity tags can technically
        # have commas in them, but none of our tags contain commas, so
        # this will work.
        for etag in header.split(','):
            etag = etag.strip()
            if strip_gzip_from_etag:
                if etag.endswith('-gzip'):
                    etag = etag[:-5]
                elif etag.endswith('-gzip"'):
                    etag = etag[:-6] + '"'
            etags.append(etag)
        return etags

    def _parseContentDispositionHeader(self, value):
        """Parse a Content-Disposition header.

        :return: a 2-tuple (disposition-type, disposition-params).
        disposition-params is a dict mapping parameter names to values.
        """
        disposition = None
        params = {}
        if value is None:
            return (disposition, params)
        pieces = value.split(';')
        if len(pieces) > 1:
            disposition = pieces[0].strip()
        for name_value in pieces[1:]:
            name_and_value = name_value.split('=', 2)
            if len(name_and_value) == 2:
                name = name_and_value[0].strip()
                value = name_and_value[1].strip()
                # Strip quotation marks if present. RFC2183 gives
                # guidelines for when to quote these values, but it's
                # very likely that a client will quote even short
                # filenames, and unlikely that a filename will
                # actually begin and end with quotes.
                if (value[0] == '"' and value[-1] == '"'):
                    value = value[1:-1]
            else:
                name = name_and_value
                value = None
            params[name] = value
        return (disposition, params)

    def _parseAcceptStyleHeader(self, value):
        """Parse an HTTP header from the Accept-* family.

        These headers contain a list of possible values, each with an
        optional priority.

        This code is modified from Zope's
        BrowserLanguages#getPreferredLanguages.

        :return: All values, in descending order of priority.
        """
        if value is None:
            return []

        values = value.split(',')
        # In the original getPreferredLanguages there was some language
        # code normalization here, which I removed.
        values = [v for v in values if v != ""]

        accepts = []
        for index, value in enumerate(values):
            l = value.split(';', 2)

            # If not supplied, quality defaults to 1...
            quality = 1.0

            if len(l) == 2:
                q = l[1]
                if q.startswith('q='):
                    q = q.split('=', 2)[1]
                    quality = float(q)

            if quality == 1.0:
                # ... but we use 1.9 - 0.001 * position to
                # keep the ordering between all items with
                # 1.0 quality, which may include items with no quality
                # defined, and items with quality defined as 1.
                quality = 1.9 - (0.001 * index)

            accepts.append((quality, l[0].strip()))

        accepts = [acc for acc in accepts if acc[0] > 0]
        accepts.sort()
        accepts.reverse()
        return [value for quality, value in accepts]


class WebServiceBatchNavigator(BatchNavigator):
    """A batch navigator that speaks to web service clients.

    This batch navigator differs from others in the names of the query
    variables it expects. This class expects the starting point to be
    contained in the query variable "ws.start" and the size of the
    batch to be contained in the query variable "ws.size". When this
    navigator serves links, it includes query variables by those
    names.
    """

    start_variable_name = "ws.start"
    batch_variable_name = "ws.size"

    @property
    def default_batch_size(self):
        return getUtility(IWebServiceConfiguration).default_batch_size

    @property
    def max_batch_size(self):
        return getUtility(IWebServiceConfiguration).max_batch_size


class BatchingResourceMixin:

    """A mixin for resources that need to batch lists of entries."""

    # TODO: determine real need for __init__ and super() call

    def total_size_link(self, navigator):
        """Return the URL to fetch to find out the collection's total size.

        If this is None, the total size will be included inline.

        :param navigator: A BatchNavigator object for the current batch.
        """
        return None

    def get_total_size(self, entries):
        """Get the number of items in entries.

        :return: a JSON string representing the number of objects in the list
        """
        if not hasattr(entries, '__len__'):
            entries = IFiniteSequence(entries)

        return simplejson.dumps(len(entries))


    def batch(self, entries, request):
        """Prepare a batch from a (possibly huge) list of entries.

        :return: a JSON string representing a hash:

        'entries' contains a list of EntryResource objects for the
          entries that actually made it into this batch
        'total_size' contains the total size of the list.
        'total_size_link' contains a link to the total size of the list.
        'next_url', if present, contains a URL to get the next batch
         in the list.
        'prev_url', if present, contains a URL to get the previous batch
         in the list.
        'start' contains the starting index of this batch

        Only one of 'total_size' or 'total_size_link' will be present.

        Note that the JSON string will be missing its final curly
        brace. This is in case the caller wants to add some additional
        keys to the JSON hash. It's the caller's responsibility to add
        a '}' to the end of the string returned from this method.
        """
        if not hasattr(entries, '__len__'):
            entries = IFiniteSequence(entries)
        navigator = WebServiceBatchNavigator(entries, request)

        view_permission = getUtility(IWebServiceConfiguration).view_permission
        batch = { 'start' : navigator.batch.start }
        # If this is the last batch, then total() is easy to
        # calculate. Let's use it and save the client from having to
        # ask for the total size of the collection.
        if navigator.batch.nextBatch() is None:
            total_size_link = None
        else:
            total_size_link = self.total_size_link(navigator)
        if total_size_link is None:
            total_size = navigator.batch.total()
            if total_size < 0:
                # lazr.batchnavigator uses -1 for an empty list.
                # We want to use 0.
                total_size = 0
            batch['total_size'] = total_size
        else:
            batch['total_size_link'] = total_size_link
        next_url = navigator.nextBatchURL()
        if next_url != "":
            batch['next_collection_link'] = next_url
        prev_url = navigator.prevBatchURL()
        if prev_url != "":
            batch['prev_collection_link'] = prev_url
        json_string = simplejson.dumps(batch, cls=ResourceJSONEncoder)

        # String together a bunch of entry representations, possibly
        # obtained from a representation cache.
        resources = [EntryResource(entry, request)
                     for entry in navigator.batch
                     if checkPermission(view_permission, entry)]
        entry_strings = [
            resource._representation(HTTPResource.JSON_TYPE)
            for resource in resources]
        json_string = (json_string[:-1] + ', "entries": ['
                       + (", ".join(entry_strings) + ']'))
        # The caller is responsible for tacking on the final curly brace.
        return json_string


class CustomOperationResourceMixin:

    """A mixin for resources that implement a collection-entry pattern."""

    def __init__(self, context, request):
        """A basic constructor."""
        # Like all mixin classes, this class is designed to be used
        # with multiple inheritance. That requires defining __init__
        # to call the next constructor in the chain, which means using
        # super() even though this class itself has no superclass.
        super(CustomOperationResourceMixin, self).__init__(context, request)

    def handleCustomGET(self, operation_name):
        """Execute a custom search-type operation triggered through GET.

        This is used by both EntryResource and CollectionResource.

        :param operation_name: The name of the operation to invoke.
        :return: The result of the operation: either a string or an
        object that needs to be serialized to JSON.
        """
        try:
            operation = getMultiAdapter((self.context, self.request),
                                        IResourceGETOperation,
                                        name=operation_name)
        except ComponentLookupError:
            self.request.response.setStatus(400)
            return "No such operation: " + operation_name

        show = self.request.form.get('ws.show')
        if show == 'total_size':
            operation.total_size_only = True
        return operation()

    def handleCustomPOST(self, operation_name):
        """Execute a custom write-type operation triggered through POST.

        This is used by both EntryResource and CollectionResource.

        :param operation_name: The name of the operation to invoke.
        :return: The result of the operation: either a string or an
        object that needs to be serialized to JSON.
        """
        try:
            operation = getMultiAdapter((self.context, self.request),
                                        IResourcePOSTOperation,
                                        name=operation_name)
        except ComponentLookupError:
            self.request.response.setStatus(400)
            return "No such operation: " + operation_name
        return operation()

    def do_POST(self):
        """Invoke a custom operation.

        XXX leonardr 2008-04-01 bug=210265:
        The standard meaning of POST (ie. when no custom operation is
        specified) is "create a new subordinate resource."  Code
        should eventually go into CollectionResource that implements
        POST to create a new entry inside the collection.
        """
        operation_name = self.request.form.get('ws.op')
        if operation_name is None:
            self.request.response.setStatus(400)
            return "No operation name given."
        del self.request.form['ws.op']
        return self.handleCustomPOST(operation_name)

    def do_DELETE(self):
        """Invoke a destructor operation."""
        try:
            operation = getMultiAdapter((self.context, self.request),
                                        IResourceDELETEOperation)
        except ComponentLookupError:
            # This should never happen because implementsDELETE()
            # makes sure there is a registered
            # IResourceDELETEOperation.
            self.request.response.setStatus(405)
            return "DELETE not supported."
        return operation()


class FieldUnmarshallerMixin:

    """A class that needs to unmarshall field values."""

    # The representation value used when the client doesn't have
    # authorization to see the real value.
    REDACTED_VALUE = 'tag:launchpad.net:2008:redacted'

    def __init__(self, context, request):
        """A basic constructor."""
        # Like all mixin classes, this class is designed to be used
        # with multiple inheritance. That requires defining __init__
        # to call the next constructor in the chain, which means using
        # super() even though this class itself has no superclass.
        super(FieldUnmarshallerMixin, self).__init__(context, request)
        self._unmarshalled_field_cache = {}

    def _unmarshallField(self, field_name, field, detail=NORMAL_DETAIL):
        """See what a field would look like in a generic representation.

        :return: a 2-tuple (representation_name, representation_value).
        """
        cached_value = MISSING
        if detail is NORMAL_DETAIL:
            cached_value = self._unmarshalled_field_cache.get(
                field_name, MISSING)
        if cached_value is not MISSING:
            return cached_value

        field = field.bind(self.context)
        marshaller = getMultiAdapter((field, self.request), IFieldMarshaller)
        try:
            if IUnmarshallingDoesntNeedValue.providedBy(marshaller):
                value = None
            else:
                value = getattr(self.entry, field.__name__)
            if detail is NORMAL_DETAIL:
                repr_value = marshaller.unmarshall(self.entry, value)
            elif detail is CLOSEUP_DETAIL:
                repr_value = marshaller.unmarshall_to_closeup(
                    self.entry, value)
            else:
                raise AssertionError("Invalid level of detail specified")
        except Unauthorized:
            # Either the client doesn't have permission to see
            # this field, or it doesn't have permission to read
            # its current value. Rather than denying the client
            # access to the resource altogether, use our special
            # 'redacted' tag: URI for the field's value.
            repr_value = self.REDACTED_VALUE

        unmarshalled = (marshaller.representation_name, repr_value)

        if detail is NORMAL_DETAIL:
            self._unmarshalled_field_cache[field_name] = unmarshalled
        return unmarshalled

    def unmarshallFieldToHTML(self, field_name, field):
        """See what a field would look like in an HTML representation.

        This is usually similar to the value of _unmarshallField(),
        but it might contain some custom HTML weirdness.

        :return: a 2-tuple (representation_name, representation_value).
        """
        name, value = self._unmarshallField(field_name, field)
        try:
            # Try to get a renderer for this particular field.
            renderer = getMultiAdapter(
                (self.entry.context, field, self.request),
                IFieldHTMLRenderer, name=field.__name__)
        except ComponentLookupError:
            # There's no field-specific renderer. Look up an
            # IFieldHTMLRenderer for this _type_ of field.
            field = field.bind(self.entry.context)
            renderer = getMultiAdapter(
                (self.entry.context, field, self.request),
                IFieldHTMLRenderer)
        return name, renderer(value)


@component.adapter(Interface, IField, IWebServiceClientRequest)
@implementer(IFieldHTMLRenderer)
def render_field_to_html(object, field, request):
    """Turn a field's current value into an XHTML snippet.

    This is the default adapter for IFieldHTMLRenderer.
    """
    def unmarshall(value):
        return cgi.escape(unicode(value))
    return unmarshall


class ReadOnlyResource(HTTPResource):
    """A resource that serves a string in response to GET."""

    def __init__(self, context, request):
        """A basic constructor."""
        # This class is designed to be used with mixins. That means
        # defining __init__ to call the next constructor in the chain,
        # even though there's no other code in __init__.
        super(ReadOnlyResource, self).__init__(context, request)

    def __call__(self):
        """Handle a GET or (if implemented) POST request."""
        result = ""
        method = self.getRequestMethod()
        if method == "GET":
            result = self.do_GET()
        elif method == "POST" and self.implementsPOST():
            result = self.do_POST()
        else:
            allow_string = "GET"
            if self.implementsPOST():
                allow_string += " POST"
            self.request.response.setStatus(405)
            self.request.response.setHeader("Allow", allow_string)
        return result


class ReadWriteResource(HTTPResource):
    """A resource that responds to GET, PUT, and PATCH."""

    def __init__(self, context, request):
        """A basic constructor."""
        # This class is designed to be used with mixins. That means
        # defining __init__ to call the next constructor in the chain,
        # even though there's no other code in __init__.
        super(ReadWriteResource, self).__init__(context, request)

    def __call__(self):
        """Handle a GET, PUT, or PATCH request."""
        result = ""
        method = self.getRequestMethod()
        try:
            if method == "GET":
                result = self.do_GET()
            elif method in ["PUT", "PATCH"]:
                media_type = self.handleConditionalWrite()
                if media_type is not None:
                    stream = self.request.bodyStream
                    representation = stream.getCacheStream().read()
                    if method == "PUT":
                        result = self.do_PUT(media_type, representation)
                    else:
                        result = self.do_PATCH(media_type, representation)
            elif method == "POST" and self.implementsPOST():
                result = self.do_POST()
            elif method == "DELETE" and self.implementsDELETE():
                result = self.do_DELETE()
            else:
                allow_string = "GET PUT PATCH"
                if self.implementsPOST():
                    allow_string += " POST"
                if self.implementsDELETE():
                    allow_string += " DELETE"
                self.request.response.setStatus(405)
                self.request.response.setHeader("Allow", allow_string)
        except Exception, e:
            # If the exception was decorated in such a way to indicate that
            # the error is the client's fault (i.e., not a bug in the web
            # service), then the error should be communicated to the client
            # without the publisher framework treating the response as an
            # error.
            if IClientError.providedBy(e):
                view = getMultiAdapter((e, self.request), IClientErrorView)
                result = view()
                self.request.response.setStatus(view.status)
            else:
                raise

        return result


class EntryHTMLView:
    """An HTML view of an entry."""

    # A preparsed template file for HTML representations of the resource.
    HTML_TEMPLATE = LazrPageTemplateFile('templates/html-resource.pt')

    def __init__(self, context, request):
        """Initialize with respect to a data object and request."""
        self.context = context
        self.request = request
        self.resource = EntryResource(context, request)

    def __call__(self):
        """Send the entry data through an HTML template."""
        namespace = self.HTML_TEMPLATE.pt_getContext()
        names_and_values = self.resource.toDataStructure(
            HTTPResource.XHTML_TYPE).items()
        data = [{'name' : name, 'value': decode_value(value)}
                for name, value in names_and_values]
        namespace['context'] = sorted(data)
        return self.HTML_TEMPLATE.pt_render(namespace)


class EntryManipulatingResource(ReadWriteResource):
    """A resource that manipulates some aspect of an entry."""

    def __init__(self, context, request):
        # This class is designed to be used with mixins. That means
        # defining __init__ to call the next constructor in the chain,
        # even though there's no other code in __init__.
        super(EntryManipulatingResource, self).__init__(context, request)

    def processAsJSONDocument(self, media_type, representation):
        """Process an incoming representation as a JSON document."""
        if not media_type.startswith(self.JSON_TYPE):
            self.request.response.setStatus(415)
            return None, 'Expected a media type of %s.' % self.JSON_TYPE
        try:
            h = simplejson.loads(representation.decode('utf-8'))
        except ValueError:
            self.request.response.setStatus(400)
            return None, "Entity-body was not a well-formed JSON document."
        return h, None

    def applyChanges(self, changeset):
        """Apply a dictionary of key-value pairs as changes to an entry.

        :param changeset: A dictionary. Should come from an incoming
        representation.

        :return: If there was an error, a string error message to be
        propagated to the client.
        """
        changeset = copy.copy(changeset)
        validated_changeset = []
        errors = []

        # The self link and resource type link aren't part of the
        # schema, so they're handled separately.
        modified_read_only_attribute = (u"%s: You tried to modify a "
                                         "read-only attribute.")
        if 'self_link' in changeset:
            if changeset['self_link'] != absoluteURL(self.entry.context,
                                                     self.request):
                errors.append(modified_read_only_attribute % 'self_link')
            del changeset['self_link']

        if 'web_link' in changeset:
            browser_request = queryAdapter(
                self.request, IWebBrowserOriginatingRequest)
            if browser_request is not None:
                existing_web_link = absoluteURL(
                    self.entry.context, browser_request)
                if changeset['web_link'] != existing_web_link:
                    errors.append(modified_read_only_attribute % 'web_link')
                del changeset['web_link']

        if 'resource_type_link' in changeset:
            if changeset['resource_type_link'] != self.type_url:
                errors.append(modified_read_only_attribute %
                              'resource_type_link')
            del changeset['resource_type_link']

        if 'http_etag' in changeset:
            if changeset['http_etag'] != self.getETag(self.JSON_TYPE):
                errors.append(modified_read_only_attribute %
                              'http_etag')
            del changeset['http_etag']

        # For every field in the schema, see if there's a corresponding
        # field in the changeset.
        for name, field in get_entry_fields_in_write_order(self.entry):
            field = field.bind(self.entry.context)
            marshaller = getMultiAdapter((field, self.request),
                                         IFieldMarshaller)
            repr_name = marshaller.representation_name
            if not repr_name in changeset:
                # The client didn't try to set a value for this field.
                continue

            # Obtain the current value of the field, as it would be
            # shown in an outgoing representation. This gives us an easy
            # way to see if the client changed the value.
            try:
                current_value = marshaller.unmarshall(
                    self.entry, getattr(self.entry, name))
            except Unauthorized:
                # The client doesn't have permission to see the old
                # value. That doesn't necessarily mean they can't set
                # it to a new value, but it does mean we have to
                # assume they're changing it rather than see for sure
                # by comparing the old value to the new.
                current_value = self.REDACTED_VALUE

            # The client tried to set a value for this field. Marshall
            # it, validate it, and (if it's different from the current
            # value) move it from the client changeset to the
            # validated changeset.
            original_value = changeset.pop(repr_name)
            if original_value == current_value == self.REDACTED_VALUE:
                # The client can't see the field's current value, and
                # isn't trying to change it. Skip to the next field.
                continue

            try:
                value = marshaller.marshall_from_json_data(original_value)
            except (ValueError, ValidationError), e:
                errors.append(u"%s: %s" % (repr_name, e))
                continue

            if ICollectionField.providedBy(field):
                # This is a collection field, so the most we can do is set an
                # error message if the new value is not identical to the
                # current one.
                if value != current_value:
                    errors.append(u"%s: You tried to modify a collection "
                                   "attribute." % repr_name)
                continue

            if IBytes.providedBy(field):
                # We don't modify Bytes fields from the Entry that contains
                # them, but we may tell users how to do so if they attempt to
                # change them.
                if value != current_value:
                    if field.readonly:
                        errors.append(modified_read_only_attribute
                                      % repr_name)
                    else:
                        errors.append(
                            u"%s: To modify this field you need to send a PUT "
                             "request to its URI (%s)."
                            % (repr_name, current_value))
                continue

            # If the new value is an object, make sure it provides the correct
            # interface.
            if value is not None and IReference.providedBy(field):
                # XXX leonardr 2008-04-12 spec=api-wadl-description:
                # This should be moved into the
                # ObjectLookupFieldMarshaller, once we make it
                # possible for Vocabulary fields to specify a schema
                # class the way IReference fields can.
                if value != None and not field.schema.providedBy(value):
                    errors.append(u"%s: Your value points to the "
                                   "wrong kind of object" % repr_name)
                    continue

            # Obtain the current value of the field.  This gives us an easy
            # way to see if the client changed the value.
            current_value = getattr(self.entry, name)

            change_this_field = True
            # Read-only attributes can't be modified. It's okay to specify a
            # value for an attribute that can't be modified, but the new value
            # must be the same as the current value.  This makes it possible
            # to GET a document, modify one field, and send it back.
            if field.readonly:
                change_this_field = False
                if value != current_value:
                    errors.append(modified_read_only_attribute
                                  % repr_name)
                    continue

            if change_this_field is True and value != current_value:
                try:
                    # Do any field-specific validation.
                    field.validate(value)
                except ConstraintNotSatisfied, e:
                    # Try to get a string error message out of
                    # the exception; otherwise use a generic message
                    # instead of whatever object the raise site
                    # thought would be a good idea.
                    if (len(e.args) > 0 and
                        isinstance(e.args[0], basestring)):
                        error = e.args[0]
                    else:
                        error = "Constraint not satisfied."
                    errors.append(u"%s: %s" % (repr_name, error))
                    continue
                except RequiredMissing, e:
                    error = "Missing required value."
                    errors.append(u"%s: %s" % (repr_name, error))
                except (ValueError, ValidationError), e:
                    error = str(e)
                    if error == "":
                        error = "Validation error"
                    errors.append(u"%s: %s" % (repr_name, error))
                    continue
                validated_changeset.append((field, value))
        # If there are any fields left in the changeset, they're
        # fields that don't correspond to some field in the
        # schema. They're all errors.
        for invalid_field in changeset.keys():
            errors.append(u"%s: You tried to modify a nonexistent "
                           "attribute." % invalid_field)

        # If there were errors, display them and send a status of 400.
        if len(errors) > 0:
            self.request.response.setStatus(400)
            self.request.response.setHeader('Content-type', 'text/plain')
            return "\n".join(errors)

        # Make a snapshot of the entry to use in a notification event.
        entry_before_modification = Snapshot(
            self.entry.context, providing=providedBy(self.entry.context))

        # Store the entry's current URL so we can see if it changes.
        original_url = absoluteURL(self.entry.context, self.request)

        # Make the changes, in the same order obtained from
        # get_entry_fields_in_write_order.
        for field, value in validated_changeset:
            field.set(self.entry, value)
        # The representation has changed, and etags will need to be
        # recalculated.
        self.etags_by_media_type = {}

        # Send a notification event.
        event = ObjectModifiedEvent(
            object=self.entry.context,
            object_before_modification=entry_before_modification,
            edited_fields=[field for field, value in validated_changeset])
        notify(event)

        # The changeset contained new values for some of this object's
        # fields, and the notification event may have changed others.
        # Clear out any fields that changed.
        for name, ignored in self._unmarshalled_field_cache.items():
            force_clear = False
            try:
                old_value = getattr(entry_before_modification, name, None)
                new_value = getattr(self.entry.context, name, None)
            except Unauthorized:
                # Clear the cache, just to be safe.
                force_clear = True
            if force_clear or new_value != old_value:
                del(self._unmarshalled_field_cache[name])

        self._applyChangesPostHook()

        new_url = absoluteURL(self.entry.context, self.request)
        if new_url == original_url:
            # The resource did not move. Serve a new representation of
            # it. This might not necessarily be a representation of
            # the whole entry!
            self.request.response.setStatus(209)
            media_type = self.getPreferredSupportedContentType()
            self.request.response.setHeader('Content-type', media_type)
            return self._representation(media_type)
        else:
            # The object moved. Serve a redirect to its new location.
            # This might not necessarily be the location of the entry!
            self.request.response.setStatus(301)
            self.request.response.setHeader(
                'Location', absoluteURL(self.context, self.request))
            # RFC 2616 says the body of a 301 response, if present,
            # SHOULD be a note linking to the new object.
            return ''

    def _applyChangesPostHook(self):
        """A hook method called near the end of applyChanges.

        This method is called after the changes have been applied, but
        before the new representation is created.
        """
        pass

class EntryFieldResource(FieldUnmarshallerMixin, EntryManipulatingResource):
    """An individual field of an entry."""
    implements(IEntryFieldResource, IJSONPublishable)

    SUPPORTED_CONTENT_TYPES = [HTTPResource.JSON_TYPE,
                               HTTPResource.XHTML_TYPE]

    def __init__(self, context, request):
        """Initialize with respect to a context and request."""
        super(EntryFieldResource, self).__init__(context, request)
        self.entry = self.context.entry

    def do_GET(self):
        """Create a representation of a single field."""
        media_type = self.handleConditionalGET()
        if media_type is None:
            # The conditional GET succeeded. Serve nothing.
            return ""
        else:
            self.request.response.setHeader('Content-Type', media_type)
            return self._representation(media_type)

    def do_PUT(self, media_type, representation):
        """Overwrite the field's existing value with a new value."""
        value, error = self.processAsJSONDocument(media_type, representation)
        if value is None:
            return value, error
        changeset = {self.context.name : value}
        return self.applyChanges(changeset)

    do_PATCH = do_PUT

    def _getETagCores(self, cache=None):
        """Calculate the ETag for an entry field.

        The core of the ETag is the field value itself.

        :arg cache: is ignored.
        """
        value = self._unmarshallField(
            self.context.name, self.context.field)[1]

        # Append the revision number, because the algorithm for
        # generating the representation might itself change across
        # versions.
        revno = getUtility(IWebServiceConfiguration).code_revision
        return [core.encode('utf-8') for core in [revno, unicode(value)]]

    def _representation(self, media_type):
        """Create a representation of the field value."""
        if media_type == self.JSON_TYPE:
            name, value = self._unmarshallField(
                self.context.name, self.context.field, CLOSEUP_DETAIL)
            return simplejson.dumps(value)
        elif media_type == self.XHTML_TYPE:
            name, value = self.unmarshallFieldToHTML(
                self.context.name, self.context.field)
            return encode_value(value)
        else:
            raise AssertionError(
                    "No representation implementation for media type %s"
                    % media_type)


class EntryField:
    """A schema field bound by name to a particular entry."""
    implements(IEntryField, ILocation)

    def __init__(self, entry, field, name):
        """Initialize with respect to a named field of an entry."""
        self.entry = entry
        self.field = field.bind(entry)
        self.name = name

        # ILocation implementation
        self.__parent__ = self.entry.context
        self.__name__ = self.name


class EntryFieldURL(AbsoluteURL):
    """An IAbsoluteURL adapter for EntryField objects."""
    component.adapts(EntryField, IHTTPRequest)
    implements(IAbsoluteURL)

    def __init__(self, entryfield, request):
        self.context = entryfield
        self.request = request

def make_entry_etag_cores(field_details):
    """Given the details of an entry's fields, calculate its ETag cores.

    :arg field_details: A list of field names and relevant field information,
    in particular whether or not the field is writable and its current value.
    """
    unwritable_values = []
    writable_values = []
    for name, details in field_details:
        if details['writable']:
            # The client can write to this value.
            bucket = writable_values
        else:
            # The client can't write to this value (it might be read-only or
            # it might just be non-web-service writable.
            bucket = unwritable_values
        bucket.append(decode_value(details['value']))

    unwritable = "\0".join(unwritable_values).encode("utf-8")
    writable = "\0".join(writable_values).encode("utf-8")
    return [unwritable, writable]



class EntryResource(CustomOperationResourceMixin,
                    FieldUnmarshallerMixin, EntryManipulatingResource):
    """An individual object, published to the web."""
    implements(IEntryResource, IJSONPublishable)

    SUPPORTED_CONTENT_TYPES = [HTTPResource.WADL_TYPE,
                               HTTPResource.DEPRECATED_WADL_TYPE,
                               HTTPResource.XHTML_TYPE,
                               HTTPResource.JSON_TYPE]

    def __init__(self, context, request):
        """Associate this resource with a specific object and request."""
        super(EntryResource, self).__init__(context, request)
        self.entry = getMultiAdapter((context, request), IEntry)

    def handleCustomPOST(self, operation_name):
        """See `CustomOperationResourceMixin`."""
        value = super(EntryResource, self).handleCustomPOST(operation_name)
        # We don't know what the custom operation might have done.
        # Remove this object from the representation cache, just to be
        # safe.
        cache = self._representation_cache
        if cache is not None:
            cache.delete(self.context)
        return value

    def _getETagCores(self, unmarshalled_field_values=None):
        """Calculate the ETag for an entry.

        :arg unmarshalled_field_values: A dict mapping field names to
        unmarshalled values, obtained during some other operation such
        as the construction of a representation.
        """
        if unmarshalled_field_values is None:
            unmarshalled_field_values = {}

        field_details = []
        for name, field in getFieldsInOrder(self.entry.schema):
            details = {}
            # Add in any values provided by the caller.
            # The value of the field is either passed in, or extracted.
            details['value'] = unmarshalled_field_values.get(
                name, self._unmarshallField(name, field)[1])

            # The client can write to this field.
            details['writable'] = self.isModifiableField(field, True)

            field_details.append((name, details))

        return make_entry_etag_cores(field_details)

    def _etagMatchesForWrite(self, existing_etag, incoming_etags):
        """Make sure no other client has modified this resource.

        For a conditional read to succeed, the entire ETag must
        match. But for a conditional write to succeed, only the second
        half of the ETag must match. This prevents spurious 412 errors
        on conditional writes where the only fields that changed are
        read-only fields that can't possibly cause a conflict.
        """
        incoming_write_portions = map(extract_write_portion, incoming_etags)
        existing_write_portion = extract_write_portion(existing_etag)
        return existing_write_portion in incoming_write_portions


    def toDataForJSON(self):
        """Turn the object into a simple data structure."""
        return self.toDataStructure(self.JSON_TYPE)

    def toDataStructure(self, media_type):
        """Turn the object into a simple data structure.

        In this case, a dictionary containing all fields defined by
        the resource interface.

        The values in the dictionary may differ depending on the value
        of media_type.
        """
        data = {}
        data['self_link'] = absoluteURL(self.context, self.request)
        if self.adapter_utility.publish_web_link:
            # Objects in the web service correspond to pages on some website.
            # Provide the link to the corresponding page on the website.
            browser_request = IWebBrowserOriginatingRequest(self.request)
            data['web_link'] = absoluteURL(self.context, browser_request)
        data['resource_type_link'] = self.type_url
        unmarshalled_field_values = {}
        for name, field in getFieldsInOrder(self.entry.schema):
            if media_type == self.JSON_TYPE:
                repr_name, repr_value = self._unmarshallField(name, field)
            elif media_type == self.XHTML_TYPE:
                repr_name, repr_value = self.unmarshallFieldToHTML(
                    name, field)
            else:
                raise AssertionError(
                        "Cannot create data structure for media type %s"
                        % media_type)
            data[repr_name] = repr_value
            unmarshalled_field_values[name] = repr_value

        etag = self.getETag(media_type, unmarshalled_field_values)
        data['http_etag'] = etag
        return data

    def toXHTML(self):
        """Represent this resource as an XHTML document."""
        view = getMultiAdapter(
            (self.context, self.request),
            name="lazr.restful.EntryResource")
        return view()

    def processAsJSONHash(self, media_type, representation):
        """Process an incoming representation as a JSON hash.

        :param media_type: The specified media type of the incoming
        representation.

        :representation: The incoming representation:

        :return: A tuple (dictionary, error). 'dictionary' is a Python
        dictionary corresponding to the incoming JSON hash. 'error' is
        an error message if the incoming representation could not be
        processed. If there is an error, this method will set an
        appropriate HTTP response code.
        """

        value, error = self.processAsJSONDocument(media_type, representation)
        if value is None:
            return value, error
        if not isinstance(value, dict):
            self.request.response.setStatus(400)
            return None, 'Expected a JSON hash.'
        return value, None

    def do_GET(self):
        """Render an appropriate representation of the entry."""
        # Handle a custom operation, probably a search.
        operation_name = self.request.form.pop('ws.op', None)
        if operation_name is not None:
            result = self.handleCustomGET(operation_name)
            if isinstance(result, basestring):
                # The custom operation took care of everything and
                # just needs this string served to the client.
                return result
        else:
            # No custom operation was specified. Implement a standard
            # GET, which serves a JSON or WADL representation of the
            # entry.
            media_type = self.handleConditionalGET()
            if media_type is None:
                # The conditional GET succeeded. Serve nothing.
                return ""
            else:
                self.request.response.setHeader('Content-Type', media_type)
                return self._representation(media_type)

    def do_PUT(self, media_type, representation):
        """Modify the entry's state to match the given representation.

        A PUT is just like a PATCH, except the given representation
        must be a complete representation of the entry.
        """
        changeset, error = self.processAsJSONHash(media_type, representation)
        if error is not None:
            return error

        # Make sure the representation includes values for all
        # writable attributes.
        #
        # Get the fields ordered by schema order so that we always
        # evaluate them in the same order. This is needed to predict
        # errors when testing.
        for name, field in getFieldsInOrder(self.entry.schema):
            if not self.isModifiableField(field, True):
                continue
            field = field.bind(self.context)
            marshaller = getMultiAdapter((field, self.request),
                                         IFieldMarshaller)
            repr_name = marshaller.representation_name
            if (changeset.get(repr_name) is None
                and getattr(self.entry, name) is not None):
                # This entry has a value for the attribute, but the
                # entity-body of the PUT request didn't make any assertion
                # about the attribute. The resource's behavior under HTTP
                # is undefined; we choose to send an error.
                self.request.response.setStatus(400)
                return ("You didn't specify a value for the attribute '%s'."
                        % repr_name)

        return self.applyChanges(changeset)


    def do_PATCH(self, media_type, representation):
        """Apply a JSON patch to the entry."""
        changeset, error = self.processAsJSONHash(media_type, representation)
        if error is not None:
            return error
        return self.applyChanges(changeset)

    def _applyChangesPostHook(self):
        """See `EntryManipulatingResource`."""
        # Also remove the modified object from the representation
        # cache. This ensures a 209 status code will be accompanied
        # with a brand new representation.
        cache = self._representation_cache
        if cache is not None:
            cache.delete(self.context)


    @property
    def type_url(self):
        """The URL to the resource type for this resource."""
        return "%s#%s" % (
            absoluteURL(self.request.publication.getApplication(
                    self.request), self.request),
            self.adapter_utility.singular_type)

    @property
    def adapter_utility(self):
        """An EntryAdapterUtility for this resource."""
        return EntryAdapterUtility(self.entry.__class__)


    @property
    def redacted_fields(self):
        """Names the fields the current user doesn't have permission to see."""
        failures = []
        orig_interfaces = self.entry._orig_interfaces
        for name, field in getFieldsInOrder(self.entry.schema):
            try:
                # Can we view the field's value? We check the
                # permission directly using the Zope permission
                # checker, because doing it indirectly by fetching the
                # value may have very slow side effects such as
                # database hits.
                try:
                    tagged_values = field.getTaggedValue(
                        'lazr.restful.exported')
                    original_name = tagged_values['original_name']
                except KeyError, e:
                    # This field has no tagged values, or is missing
                    # the 'original_name' value. Its entry class was
                    # probably created by hand rather than by tagging
                    # an interface. In that case, it's the
                    # programmer's responsibility to set
                    # 'original_name' if the web service field name
                    # differs from the underlying interface's field
                    # name. Since 'original_name' is not present, assume the
                    # names are the same.
                    original_name = name
                context = orig_interfaces[name](self.context)
                try:
                    checker = getChecker(context)
                except TypeError:
                    # This is more expensive than using a Zope checker, but
                    # there is no checker, so either there is no permission
                    # control on this object, or permission control is
                    # implemented some other way. Also note that we use
                    # getattr() on self.entry rather than self.context because
                    # some of the fields in entry.schema will be provided by
                    # adapters rather than directly by self.context.
                    getattr(self.entry, name)
                else:
                    checker.check(context, original_name)
            except Unauthorized:
                # This is an expensive operation that will make this
                # request more expensive still, but it happens
                # relatively rarely.
                repr_name, repr_value = self._unmarshallField(name, field)
                failures.append(repr_name)
        return failures

    def isModifiableField(self, field, is_external_client):
        """Returns true if this field's value can be changed.

        Collection fields, and fields that are not part of the web
        service interface, are never modifiable. Read-only fields are
        not modifiable by external clients.

        :param is_external_client: Whether the code trying to modify
        the field is an external client. Read-only fields cannot be
        directly modified from external clients, but they might change
        as side effects of other changes.
        """
        if (ICollectionField.providedBy(field)
            or field.__name__.startswith('_')):
            return False
        if field.readonly:
            return not is_external_client
        return True

    def _representation(self, media_type):
        """Return a representation of this entry, of the given media type."""

        if media_type in [self.WADL_TYPE, self.DEPRECATED_WADL_TYPE]:
            return self.toWADL().encode("utf-8")
        elif media_type == self.JSON_TYPE:
            cache = self._representation_cache
            if cache is None:
                representation = None
            else:
                representation = cache.get(
                    self.context, self.JSON_TYPE, self.request.version)

            redacted_fields = self.redacted_fields
            if representation is None:
                # Either there is no active cache, or the representation
                # wasn't in the cache.
                representation = simplejson.dumps(self, cls=ResourceJSONEncoder)
                # If there's an active cache, and this representation
                # doesn't contain any redactions, store it in the
                # cache.
                if cache is not None and len(redacted_fields) == 0:
                    cache.set(self.context, self.JSON_TYPE,
                              self.request.version, representation)
            else:
                # We have a representation, but we might not be able
                # to use it as-is.

                # First, check to see if the user can see this
                # representation at all. If absoluteURL raises
                # Unauthorized, they can't see it--they shouldn't even
                # know its URL.
                absoluteURL(self.context, self.request)

                if len(redacted_fields) != 0:
                    # The user can see parts of this representation
                    # but not other parts. We need to deserialize it,
                    # redact certain fields, and reserialize
                    # it. Hopefully this is faster than generating the
                    # representation from scratch!
                    json = simplejson.loads(representation)
                    for field in redacted_fields:
                        json[field] = self.REDACTED_VALUE
                    # There's no need to use the ResourceJSONEncoder,
                    # because we loaded the cached representation
                    # using the standard decoder.
                    representation = simplejson.dumps(json)
            return representation
        elif media_type == self.XHTML_TYPE:
            return self.toXHTML().encode("utf-8")
        else:
            raise AssertionError(
                    "No representation implementation for media type %s"
                    % media_type)

    @property
    def _representation_cache(self):
        """Return the representation cache, or None if missing/disabled."""
        try:
            cache = getUtility(IRepresentationCache)
        except ComponentLookupError:
            # There's no representation cache.
            return None
        config = getUtility(IWebServiceConfiguration)
        if config.enable_server_side_representation_cache:
            return cache
        return None


class CollectionResource(BatchingResourceMixin,
                         CustomOperationResourceMixin,
                         ReadOnlyResource):
    """A resource that serves a list of entry resources."""
    implements(ICollectionResource)

    def __init__(self, context, request):
        """Associate this resource with a specific object and request."""
        super(CollectionResource, self).__init__(context, request)
        if ICollection.providedBy(context):
            self.collection = context
        else:
            self.collection = getMultiAdapter((context, request), ICollection)

    def do_GET(self):
        """Fetch a collection and render it as JSON."""
        # Handle a custom operation, probably a search.
        operation_name = self.request.form.pop('ws.op', None)
        if operation_name is not None:
            result = self.handleCustomGET(operation_name)
            if isinstance(result, str) or isinstance(result, unicode):
                # The custom operation took care of everything and
                # just needs this string served to the client.
                return result
        else:
            # No custom operation was specified. Implement a standard
            # GET, which serves a JSON or WADL representation of the
            # collection.
            entries = self.collection.find()
            if entries is None:
                raise NotFound(self, self.collection_name)

            media_type = self.getPreferredSupportedContentType()
            if media_type in [self.DEPRECATED_WADL_TYPE, self.WADL_TYPE]:
                result = self.toWADL().encode("utf-8")
                self.request.response.setHeader('Content-Type', media_type)
                return result

            result = self.batch(entries)

        self.request.response.setHeader('Content-type', self.JSON_TYPE)
        return result

    def batch(self, entries=None, request=None):
        """Return a JSON representation of a batch of entries.

        :param entries: (Optional) A precomputed list of entries to batch.
        :param request: (Optional) The current request.
        """
        if entries is None:
            entries = self.collection.find()
        if request is None:
            request = self.request
        result = super(CollectionResource, self).batch(entries, request)
        result += (
            ', "resource_type_link" : ' + simplejson.dumps(self.type_url)
            + '}')
        return result

    @property
    def type_url(self):
        "The URL to the resource type for the object."

        if IScopedCollection.providedBy(self.collection):
            # Scoped collection. The type URL depends on what type of
            # entry the collection holds.
            schema = self.context.relationship.value_type.schema
            adapter = EntryAdapterUtility.forSchemaInterface(
                schema, self.request)
            return adapter.entry_page_type_link
        else:
            # Top-level collection.
            schema = self.collection.entry_schema
            adapter = EntryAdapterUtility.forEntryInterface(
                schema, self.request)
            return adapter.collection_type_link


class ServiceRootResource(HTTPResource):
    """A resource that responds to GET by describing the service."""
    implements(IServiceRootResource, IJSONPublishable)

    # A preparsed template file for WADL representations of the root.
    WADL_TEMPLATE = LazrPageTemplateFile('templates/wadl-root.pt')

    def __init__(self):
        """Initialize the resource.

        The service root constructor is different from other
        HTTPResource constructors because Zope initializes the object
        with no request or context, and then passes the request in
        when it calls the service root object.
        """
        # We're not calling the superclass constructor because
        # it assumes it's being called in the context of a particular
        # request.
        # pylint:disable-msg=W0231
        self.etags_by_media_type = {}

    @property
    def request(self):
        """Fetch the current web service request."""
        return get_current_web_service_request()

    def _getETagCores(self, cache=None):
        """Calculate an ETag for a representation of this resource.

        The service root resource changes only when the software
        itself changes.
        """
        revno = getUtility(IWebServiceConfiguration).code_revision
        return [revno.encode('utf-8')]

    def __call__(self, REQUEST=None):
        """Handle a GET request."""
        method = self.getRequestMethod(REQUEST)
        if method == "GET":
            result = self.do_GET()
        else:
            REQUEST.response.setStatus(405)
            REQUEST.response.setHeader("Allow", "GET")
            result = ""
        return result

    def setCachingHeaders(self):
        "How long should the client cache this service root?"
        user_agent = self.request.getHeader('User-Agent', '')
        if user_agent.startswith('Python-httplib2'):
            # XXX leonardr 20100412
            # bug=http://code.google.com/p/httplib2/issues/detail?id=97
            #
            # A client with a User-Agent of "Python/httplib2" (such as
            # old versions of lazr.restfulclient) gives inconsistent
            # results when a resource is served with both ETag and
            # Cache-Control. We check for that User-Agent and omit the
            # Cache-Control headers if it makes a request.
            return
        config = getUtility(IWebServiceConfiguration)
        caching_policy = config.caching_policy
        if self.request.version == config.active_versions[-1]:
            max_age = caching_policy[-1]
        else:
            max_age = caching_policy[0]
        if max_age > 0:
            self.request.response.setHeader(
                'Cache-Control', 'max-age=%d' % max_age)
            # Also set the Date header so that client-side caches will
            # have something to work from.
            self.request.response.setHeader('Date', formatdate(time.time()))

    def do_GET(self):
        """Describe the capabilities of the web service."""
        media_type = self.handleConditionalGET()
        self.setCachingHeaders()
        if media_type is None:
            # The conditional GET succeeded. Serve nothing.
            return ""
        elif media_type in [self.WADL_TYPE, self.DEPRECATED_WADL_TYPE]:
            result = self.toWADL().encode("utf-8")
        elif media_type == self.JSON_TYPE:
            # Serve a JSON map containing links to all the top-level
            # resources.
            result = simplejson.dumps(self, cls=ResourceJSONEncoder)

        self.request.response.setHeader('Content-Type', media_type)
        return result

    def toWADL(self):
        # Find all resource types.
        site_manager = getGlobalSiteManager()
        entry_classes = []
        collection_classes = []
        singular_names = {}
        plural_names = {}

        # Determine the name of the earliest version. We'll be using this later.
        config = getUtility(IWebServiceConfiguration)
        earliest_version = config.active_versions[0]

        for registration in sorted(site_manager.registeredAdapters()):
            provided = registration.provided
            if IInterface.providedBy(provided):
                if (provided.isOrExtends(IEntry)
                    and IEntry.implementedBy(registration.factory)):
                    # The implementedBy check is necessary because
                    # some IEntry adapters aren't classes with
                    # schemas; they're functions. We can ignore these
                    # functions because their return value will be one
                    # of the classes with schemas, which we do describe.

                    # Make sure we have a registration relevant to
                    # this version. A given entry may have one
                    # registration for every web service version.
                    schema, version_marker = registration.required

                    if (version_marker is IWebServiceClientRequest
                        and self.request.version != earliest_version):
                        # We are generating WADL for some version
                        # other than the earliest version, and this is
                        # a registration for the earliest version. We
                        # can ignore it.
                        #
                        # We need this special test because the normal
                        # test (below) is useless when
                        # version_marker is
                        # IWebServiceClientRequest. Since all request
                        # objects provide IWebServiceClientRequest
                        # directly, it will always show up in
                        # providedBy(self.request).
                        continue
                    if not version_marker in providedBy(self.request):
                        continue

                    # Make sure that no other entry class is using this
                    # class's singular or plural names.
                    adapter = EntryAdapterUtility.forSchemaInterface(
                        schema, self.request)

                    singular = adapter.singular_type
                    assert not singular_names.has_key(singular), (
                        "Both %s and %s expose the singular name '%s'."
                        % (singular_names[singular].__name__,
                           schema.__name__, singular))
                    singular_names[singular] = schema

                    plural = adapter.plural_type
                    assert not plural_names.has_key(plural), (
                        "Both %s and %s expose the plural name '%s'."
                        % (plural_names[plural].__name__,
                           schema.__name__, plural))
                    plural_names[plural] = schema

                    entry_classes.append(registration.factory)
                elif (provided.isOrExtends(ICollection)
                      and ICollection.implementedBy(registration.factory)
                      and not IScopedCollection.implementedBy(
                        registration.factory)):
                    # See comment above re: implementedBy check.
                    # We omit IScopedCollection because those are handled
                    # by the entry classes.
                    collection_classes.append(registration.factory)

        namespace = self.WADL_TEMPLATE.pt_getContext()
        namespace['service'] = self
        namespace['request'] = self.request
        namespace['entries'] = sorted_named_things(entry_classes)
        namespace['collections'] = sorted_named_things(collection_classes)
        return self.WADL_TEMPLATE.pt_render(namespace)

    def toDataForJSON(self):
        """Return a map of links to top-level collection resources.

        A top-level resource is one that adapts a utility.  Currently
        top-level entry resources (should there be any) are not
        represented.
        """
        type_url = "%s#%s" % (
            absoluteURL(
                self.request.publication.getApplication(self.request),
                self.request),
            "service-root")
        data_for_json = {'resource_type_link' : type_url}
        publications = self.getTopLevelPublications()
        for link_name, publication in publications.items():
            data_for_json[link_name] = absoluteURL(publication,
                                                   self.request)
        return data_for_json

    def getTopLevelPublications(self):
        """Return a mapping of top-level link names to published objects."""
        top_level_resources = {}
        site_manager = getGlobalSiteManager()
        # First, collect the top-level collections.
        for registration in site_manager.registeredAdapters():
            provided = registration.provided
            if IInterface.providedBy(provided):
                # XXX sinzui 2008-09-29 bug=276079:
                # Top-level collections need a marker interface
                # so that so top-level utilities are explicit.
                if (provided.isOrExtends(ICollection)
                     and ICollection.implementedBy(registration.factory)):
                    try:
                        utility = getUtility(registration.required[0])
                    except ComponentLookupError:
                        # It's not a top-level resource.
                        continue
                    entry_schema = registration.factory.entry_schema
                    if isinstance(entry_schema, property):
                        # It's not a top-level resource.
                        continue
                    adapter = EntryAdapterUtility.forEntryInterface(
                        entry_schema, self.request)
                    link_name = ("%s_collection_link" % adapter.plural_type)
                    top_level_resources[link_name] = utility
        # Now, collect the top-level entries.
        for utility in getAllUtilitiesRegisteredFor(ITopLevelEntryLink):
            link_name = ("%s_link" % utility.link_name)
            top_level_resources[link_name] = utility

        return top_level_resources

    @property
    def type_url(self):
        "The URL to the resource type for this resource."
        adapter = self.adapter_utility

        return "%s#%s" % (
            absoluteURL(self.request.publication.getApplication(
                    self.request), self.request),
            adapter.singular_type)


class Entry:
    """An individual entry."""
    implements(IEntry)

    def __init__(self, context, request):
        """Associate the entry with some database model object."""
        self.context = context
        self.request = request


class Collection:
    """A collection of entries."""
    implements(ICollection)

    def __init__(self, context, request):
        """Associate the entry with some database model object."""
        self.context = context
        self.request = request


class ScopedCollection:
    """A collection associated with some parent object."""
    implements(IScopedCollection)
    adapts(Interface, Interface, IWebServiceLayer)

    def __init__(self, context, collection, request):
        """Initialize the scoped collection.

        :param context: The object to which the collection is scoped.
        :param collection: The scoped collection.
        """
        self.context = context
        self.collection = collection
        self.request = request
        # Unknown at this time. Should be set by our call-site.
        self.relationship = None

    @property
    def entry_schema(self):
        """The schema for the entries in this collection."""
        # We are given a model schema (IFoo). Look up the
        # corresponding entry schema (IFooEntry).
        model_schema = self.relationship.value_type.schema
        request_interface = getUtility(
            IWebServiceVersion,
            name=self.request.version)
        return getGlobalSiteManager().adapters.lookup(
            (model_schema, request_interface), IEntry).schema

    def find(self):
        """See `ICollection`."""
        return self.collection


class RESTUtilityBase:

    def _service_root_url(self):
        """Return the URL to the service root."""
        request = get_current_web_service_request()
        return absoluteURL(request.publication.getApplication(request),
                           request)


class UnknownEntryAdapter(Exception):
    """Exception that signals no adaper could be found for an interface."""

    def __init__(self, adapted_interface_name, version):
        self.adapted_interface_name = adapted_interface_name
        self.version = version
        self.whence = ''

    def __str__(self):
        message = ("No IEntry adapter found for %s (web service version: %s)."
            % (self.adapted_interface_name, self.version))
        if self.whence:
            message += '  ' + self.whence
        return message


class EntryAdapterUtility(RESTUtilityBase):
    """Useful information about an entry's presence in the web service.

    This includes the links to entry's WADL resource type, and the
    resource type for a page of these entries.
    """

    @classmethod
    def forSchemaInterface(cls, entry_interface, request):
        """Create an entry adapter utility, given a schema interface.

        A schema interface is one that can be annotated to produce a
        subclass of IEntry.
        """
        request_interface = getUtility(
            IWebServiceVersion, name=request.version)
        entry_class = getGlobalSiteManager().adapters.lookup(
            (entry_interface, request_interface), IEntry)
        if entry_class is None:
            raise UnknownEntryAdapter(entry_interface.__name__, request.version)
        return EntryAdapterUtility(entry_class)

    @classmethod
    def forEntryInterface(cls, entry_interface, request):
        """Create an entry adapter utility, given a subclass of IEntry."""
        registrations = getGlobalSiteManager().registeredAdapters()
        # There should be one IEntry subclass registered for every
        # version of the web service. We'll go through the appropriate
        # IEntry registrations looking for one associated with the
        # same IWebServiceVersion interface we find on the 'request'
        # object.
        entry_classes = [
            registration.factory for registration in registrations
            if (IInterface.providedBy(registration.provided)
                and registration.provided.isOrExtends(IEntry)
                and entry_interface.implementedBy(registration.factory)
                and registration.required[1].providedBy(request))]
        assert not len(entry_classes) > 1, (
            "%s provides more than one IEntry subclass for version %s." %
            (entry_interface.__name__, request.version))
        assert not len(entry_classes) < 1, (
            "%s does not provide any IEntry subclass for version %s." %
            (entry_interface.__name__, request.version))
        return EntryAdapterUtility(entry_classes[0])

    def __init__(self, entry_class):
        """Initialize with a class that implements IEntry."""
        self.entry_class = entry_class

    @property
    def entry_interface(self):
        """The IEntry subclass implemented by this entry type."""
        interfaces = implementedBy(self.entry_class)
        entry_ifaces = [interface for interface in interfaces
                        if interface.extends(IEntry)]
        assert len(entry_ifaces) == 1, (
            "There must be one and only one IEntry implementation "
            "for %s, found %s" % (
                self.entry_class,
                ", ".join(interface.__name__ for interface in entry_ifaces)))
        return entry_ifaces[0]

    def _get_tagged_value(self, tag):
        """Return a tagged value from the entry interface."""
        return self.entry_interface.getTaggedValue(LAZR_WEBSERVICE_NAME)[tag]

    @property
    def publish_web_link(self):
        """Return true if this entry should have a web_link."""
        # If we can't adapt a web service request to a website
        # request, we shouldn't publish a web_link for *any* entry.
        web_service_request = get_current_web_service_request()
        website_request = queryAdapter(
            web_service_request, IWebBrowserOriginatingRequest)
        return website_request is not None and self._get_tagged_value(
            'publish_web_link')

    @property
    def singular_type(self):
        """Return the singular name for this object type."""
        return self._get_tagged_value('singular')

    @property
    def plural_type(self):
        """Return the plural name for this object type."""
        return self._get_tagged_value('plural')

    @property
    def type_link(self):
        """The URL to the type definition for this kind of entry."""
        return "%s#%s" % (
            self._service_root_url(), self.singular_type)

    @property
    def collection_type_link(self):
        """The definition of a top-level collection of this kind of object."""
        return "%s#%s" % (
            self._service_root_url(), self.plural_type)

    @property
    def entry_page_type(self):
        """The definition of a collection of this kind of object."""
        return "%s-page-resource" % self.singular_type

    @property
    def entry_page_type_link(self):
        "The URL to the definition of a collection of this kind of object."
        return "%s#%s" % (
            self._service_root_url(), self.entry_page_type)

    @property
    def entry_page_representation_id(self):
        "The name of the description of a colleciton of this kind of object."
        return "%s-page" % self.singular_type

    @property
    def entry_page_representation_link(self):
        "The URL to the description of a collection of this kind of object."
        return "%s#%s" % (
            self._service_root_url(),
            self.entry_page_representation_id)

    @property
    def full_representation_link(self):
        """The URL to the description of the object's full representation."""
        return "%s#%s-full" % (
            self._service_root_url(), self.singular_type)


def get_entry_fields_in_write_order(entry):
    """Return the entry's fields in the order they should be written to.

    The ordering is intended to 1) be deterministic for a given schema
    and 2) minimize the chance of conflicts. Fields that are just
    fields come before fields (believed to be) controlled by
    mutators. Within each group, fields are returned in the order they
    appear in the schema.

    :param entry: An object that provides IEntry.
    :return: A list of 2-tuples (field name, field object)
    """
    non_mutator_fields = []
    mutator_fields = []
    field_implementations = entry.__class__.__dict__
    for name, field in getFieldsInOrder(entry.schema):
        if name.startswith('_'):
            # This field is not part of the web service interface.
            continue

        # If this field is secretly a subclass of lazr.delegates
        # Passthrough (but not a direct instance of Passthrough), put
        # it at the end -- it's probably controlled by a mutator.
        implementation = field_implementations[name]
        if (issubclass(implementation.__class__, Passthrough)
            and not implementation.__class__ is Passthrough):
            mutator_fields.append((name, field))
        else:
            non_mutator_fields.append((name, field))
    return non_mutator_fields + mutator_fields