-
Notifications
You must be signed in to change notification settings - Fork 1
/
application_istsoslib.py
executable file
·160 lines (136 loc) · 5.77 KB
/
application_istsoslib.py
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
# -*- coding: utf-8 -*-
# =============================================================================
#
# Authors: Massimiliano Cannata, Milan Antonovic
#
# Copyright (c) 2016 IST-SUPSI (www.supsi.ch/ist)
#
# This program 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 2 of the License, or (at your
# option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# =============================================================================
import sys
from os import path
import traceback
import waconf2sos
from urlparse import parse_qs
import config
sys.path.insert(0, path.abspath(path.dirname(__file__)))
from istsoslib.filters import factory_filters as FF
from istsoslib.responders import factory_response as FR
from istsoslib.renderers import factory_render as FRe
def executeSos(environ, start_response):
try:
from istsoslib import sosDatabase
from istsoslib import sosException
try:
sosConfig = waconf2sos.istsosConfig(environ)
except sosException.SOSException as ise:
raise ise
except Exception as ex:
raise sosException.SOSException(
"NoApplicableCode", "", str(ex)
)
if not sosConfig.istsos_librarypath == "" or (
sosConfig.istsos_librarypath is None):
sys.path.insert(0, sosConfig.istsos_librarypath)
pgdb = sosDatabase.PgDB(
sosConfig.connection["user"],
sosConfig.connection["password"],
sosConfig.connection["dbname"],
sosConfig.connection["host"],
sosConfig.connection["port"]
)
req_filter = FF.sosFactoryFilter(environ, sosConfig)
# Checking authorizations
if not sosConfig.user.allowedService(sosConfig.schema):
raise sosException.SOSException(
"NoApplicableCode", "",
"You are not authorized to access the "
"'%s' instance" % sosConfig.schema)
elif req_filter.request in ['insertobservation', 'registersensor']:
# If hybrid mode enable check authorizations
if config.hybrid and (not 'HTTP_AUTHORIZATION' in environ):
raise sosException.SOSException(
"NoApplicableCode", "",
"In hybrid mode, you are not authorized to "
"execute %s requests on this server" % req_filter.request)
# Only Admin, Network Managers and Data Manager con execute
# insertobservation or registersensor
elif not sosConfig.user.isAdmin() and (
not sosConfig.user.isNetworkManager()) and (
not sosConfig.user.isDataManager()):
raise sosException.SOSException(
"NoApplicableCode", "",
"You are not authorized to execute %s "
"requests on this server" % req_filter.request)
response = FR.sosFactoryResponse(req_filter, pgdb)
render = FRe.sosFactoryRender(response, sosConfig)
try:
content_type = req_filter.responseFormat
except:
content_type = 'application/xml; charset=utf-8'
status = '200 OK'
response_headers = [
('Content-Type', content_type),
('Content-Length', str(len(render.encode('utf-8'))))
]
if str(environ['REQUEST_METHOD']).upper() == 'GET':
rect = parse_qs(environ['QUERY_STRING'])
requestObject = {}
for key in rect.keys():
requestObject[key.lower()] = rect[key][0]
if "attachment" in requestObject:
response_headers.append(
("Content-Disposition",
"attachment; filename=%s" % requestObject["attachment"])
)
start_response(status, response_headers)
return [render.encode('utf-8')]
except sosException.SOSException, e:
#print >> sys.stderr, traceback.print_exc()
response_body = e.ToXML()
status = '200 OK'
response_headers = [
('Content-Type', 'application/xml; charset=utf-8'),
('Content-Length', str(len(response_body.encode('utf-8'))))
]
start_response(status, response_headers)
return [response_body.encode('utf-8')]
except Exception, e:
print >> sys.stderr, traceback.print_exc()
othertext = traceback.format_exception(*sys.exc_info())
if sosConfig.debug:
response_body = "%s" % (
sosException.SOSException(
"NoApplicableCode",
None,
e.__class__.__name__, [e, othertext]),)
else:
response_body = "%s" % (
sosException.SOSException(
"NoApplicableCode",
None,
"istSOS internal error",
["Please activate debug level for more details"]
)
)
status = '200 OK'
response_headers = [
('Content-Type', 'application/xml; charset=utf-8'),
('Content-Length', str(len(response_body.encode('utf-8'))))
]
start_response(status, response_headers)
return [response_body.encode('utf-8')]
return []