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
|
# Copyright (C) 2009-2010 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
"""Mappers from objects to absolute URLs."""
from __future__ import absolute_import, unicode_literals
__metaclass__ = type
__all__ = [
'DomainURLMapper',
'MailingListURLMapper',
]
import logging
from zope.component import getUtility
from zope.interface import implements
from zope.traversing.browser.interfaces import IAbsoluteURL
from mailman.config import config
from mailman.core.system import system
from mailman.interfaces.listmanager import IListManager
from mailman.interfaces.membership import ISubscriptionService
from mailman.rest.configuration import AdminWebServiceConfiguration
from mailman.rest.webservice import AdminWebServiceApplication
log = logging.getLogger('mailman.http')
class BasicURLMapper:
"""Base absolute URL mapper."""
implements(IAbsoluteURL)
def __init__(self, context, request):
"""Initialize with respect to a context and request."""
self.context = context
self.request = request
self.webservice_config = AdminWebServiceConfiguration()
self.version = self.webservice_config.service_version_uri_prefix
self.schema = ('https' if self.webservice_config.use_https else 'http')
self.hostname = config.webservice.hostname
self.port = int(config.webservice.port)
class FallbackURLMapper(BasicURLMapper):
"""Generic absolute url mapper."""
def __call__(self):
"""Return the semi-hard-coded URL to the service root."""
path = self._lookup(self.context)
return '{0.schema}://{0.hostname}:{0.port}/{0.version}/{1}'.format(
self, path)
def _lookup(self, ob):
"""Return the path component for the object.
:param ob: The object we're looking for.
:type ob: anything
:return: The path component.
:rtype: string
:raises KeyError: if no path component can be found.
"""
log.debug('generic url mapper lookup: %s', ob)
# Special cases.
if isinstance(ob, AdminWebServiceApplication):
return ''
urls = {
system: 'system',
getUtility(IListManager): 'lists',
getUtility(ISubscriptionService): 'members',
}
return urls[ob]
class TopLevelURLMapper(BasicURLMapper):
"""A simple mapper for top level objects."""
implements(IAbsoluteURL)
format_string = None
def __call__(self):
"""Return the hard-coded URL to the resource."""
return self.format_string.format(self)
class DomainURLMapper(TopLevelURLMapper):
"""Mapper of `IDomains` to `IAbsoluteURL`."""
format_string = (
'{0.schema}://{0.hostname}:{0.port}/{0.version}/'
'domains/{0.context.email_host}')
class MailingListURLMapper(TopLevelURLMapper):
"""Mapper of `IMailingList` to `IAbsoluteURL`."""
format_string = (
'{0.schema}://{0.hostname}:{0.port}/{0.version}/'
'lists/{0.context.fqdn_listname}')
class MemberURLMapper(TopLevelURLMapper):
"""Mapper of `IMember` to `IAbsoluteURL`."""
def __init__(self, context, request):
super(MemberURLMapper, self).__init__(context, request)
# Use a shorted version of the MemberRole string.
enum, dot, self.role = str(self.context.role).partition('.')
format_string = (
'{0.schema}://{0.hostname}:{0.port}/{0.version}/'
'lists/{0.context.mailing_list}/'
'{0.role}/{0.context.address.address}')
|