forked from istSOS/istsos2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_wnslib.py
executable file
·158 lines (127 loc) · 5.89 KB
/
application_wnslib.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
# -*- coding: utf-8 -*-
# ===============================================================================
#
# Authors: Massimiliano Cannata, Milan Antonovic
#
# Copyright (c) 2015 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
#
# ===============================================================================
from urllib.parse import parse_qs
from wnslib import resourceFactory
import sys
import traceback
import config
import logging, logging.config, logging.handlers
def executeWns(environ, start_response):
# Intitializing logging ----------------------------------------------------
'''formatter = logging.Formatter('%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
log_filename = path.join(config.errorlog_path,"wns.log")
handler = logging.handlers.RotatingFileHandler(filename=log_filename, maxBytes = 1024*1024, backupCount = 20)
handler.setFormatter(formatter)
logger = logging.getLogger('istsos')
if len(logger.handlers) == 0:
logger.addHandler(handler)
elif len(logger.handlers) == 1 and type(logger.handlers[0])==type(handler):
pass
else:
for h in logger.handlers:
logger.removeHandler(h)
logger.addHandler(handler)
if config.errorlog_level == "INFO":
logger.setLevel(logging.INFO)
elif config.errorlog_level == "ERROR":
logger.setLevel(logging.ERROR)
else:
logger.setLevel(logging.UNSET)'''
wsgi_response = "Hello istWNS"
wsgi_mime = 'text/plain'
wsgi_status = '200 OK'
wnsEnviron = {
"path": environ['PATH_INFO'][3:],
"method": str(environ['REQUEST_METHOD']).upper(),
"pathinfo": environ['PATH_INFO'].strip()[4:].split("/"),
"wsgi_input": environ['wsgi.input'].read(int(environ["CONTENT_LENGTH"])) if environ.get("CONTENT_LENGTH") else None,
"url_scheme": environ['wsgi.url_scheme'],
"http_host": environ['HTTP_HOST'] if environ.get('HTTP_HOST') else None,
"server_name": environ['SERVER_NAME'],
"server_port": environ['SERVER_PORT'],
"script_name": environ['SCRIPT_NAME'] if environ.get('SCRIPT_NAME', '') else None,
"parameters": parse_qs(environ['QUERY_STRING']) if environ.get('QUERY_STRING') else None,
"services_path": config.services_path,
}
if 'HTTP_AUTHORIZATION' in environ:
wnsEnviron['HTTP_AUTHORIZATION'] = environ['HTTP_AUTHORIZATION']
try:
try:
op = None
op = resourceFactory.initResource(wnsEnviron)
try:
if op.response['success']:
method = str(environ['REQUEST_METHOD']).upper()
# Data RETRIEVAL
if method == "GET":
op.executeGet()
# Data UPDATE
elif method == "POST":
op.executePost()
# Data INSERT
elif method == "PUT":
op.executePut()
# Data DELETE
elif method == "DELETE":
op.executeDelete()
else:
raise Exception("HTTP method %s not supported" % wnsEnviron["method"])
'''if 'log' in op.response:
logger.info("Executing %s on %s: %s" % (wnsEnviron["method"],
environ['PATH_INFO'], str(op.response['log'])))
if 'message' in op.response:
logger.info("Executing %s on %s: %s" % (wnsEnviron["method"],
environ['PATH_INFO'], str(op.response['message'])))'''
except Exception as exe:
print(traceback.print_exc(), file=sys.stderr)
'''logger.error("Executing %s on %s: %s" % (wnsEnviron["method"],
environ['PATH_INFO'], str(exe)))'''
op.setException(str(exe))
except Exception as exe:
print(traceback.print_exc(), file=sys.stderr)
'''logger.error("On initialization %s on %s: %s" % (wnsEnviron["method"],
environ['PATH_INFO'], str(exe)))'''
from walib import resource
op = resource.waResource(wnsEnviron)
op.setException(str(exe))
try:
wsgi_response = op.getResponse()
except Exception as exe:
print(traceback.print_exc(), file=sys.stderr)
'''logger.error("Executing %s on %s: %s" % (wnsEnviron["method"],
environ['PATH_INFO'], str(exe)))'''
op.setException("Error converting response to json")
wsgi_mime = "application/json"
except Exception as e:
print(traceback.print_exc(), file=sys.stderr)
'''logger.error("Executing %s on %s: %s" % (wnsEnviron["method"],
environ['PATH_INFO'], str(e)))'''
wsgi_response = str(e)
wsgi_mime = 'text/plain'
wsgi_status = '400 Bad Request'
wsgi_headers = [
('Content-Type', "%s; charset=utf-8" % wsgi_mime),
('Content-Length', str(len(wsgi_response)))
]
start_response(wsgi_status, wsgi_headers)
return [wsgi_response]