DistelliServiceFrameworkSinatra 1.2 → 1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/distelli/serviceframeworksinatra.rb +151 -174
- metadata +3 -3
@@ -1,213 +1,190 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require 'sinatra/base'
|
4
3
|
require 'logging'
|
5
4
|
require 'distelli/serviceinterface'
|
6
5
|
require 'distelli/servicemarshallers'
|
7
6
|
|
8
|
-
module
|
9
|
-
class CommonLogger
|
10
|
-
def call(env)
|
11
|
-
# do nothing
|
12
|
-
@app.call(env)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
7
|
+
module DistelliServiceFrameworkSinatra
|
16
8
|
|
17
|
-
|
18
|
-
|
9
|
+
$xml_marshaller = Distelli::XmlMarshaller.new
|
10
|
+
$json_marshaller = Distelli::JsonMarshaller.new
|
19
11
|
|
20
|
-
|
21
|
-
# disable :show_exceptions
|
22
|
-
# disable :dump_errors
|
23
|
-
LOGGER = Logging.logger[:root]
|
24
|
-
LOGGER.level = :debug
|
12
|
+
LOGGER = Logging.logger[:root]
|
25
13
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
14
|
+
def add_object(obj)
|
15
|
+
LOGGER.debug("Registering Model Object: #{obj.inspect.to_s}")
|
16
|
+
$xml_marshaller.add_object(obj)
|
17
|
+
$json_marshaller.add_object(obj)
|
18
|
+
end
|
30
19
|
|
31
|
-
|
32
|
-
|
20
|
+
def register_model(obj_list)
|
21
|
+
obj_list.each do |obj|
|
22
|
+
add_object(obj)
|
23
|
+
end
|
24
|
+
end
|
33
25
|
|
34
|
-
|
35
|
-
|
26
|
+
def unmarshall_request()
|
27
|
+
content_type = request[Distelli::ServiceConstants::CONTENT_TYPE_HEADER]
|
28
|
+
if content_type == Distelli::ServiceConstants::CONTENT_TYPE_JSON
|
29
|
+
return $json_marshaller.unmarshall(request.body)
|
30
|
+
elsif content_type == Distelli::ServiceConstants::CONTENT_TYPE_XML
|
31
|
+
return $xml_marshaller.unmarshall(request.body)
|
32
|
+
else
|
33
|
+
return request.body
|
36
34
|
end
|
35
|
+
end
|
37
36
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
def marshall_response(response, extra_headers=nil, http_code=nil)
|
38
|
+
response_type = get_response_type(request)
|
39
|
+
if http_code != nil
|
40
|
+
status http_code
|
42
41
|
end
|
43
42
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
headers_hash = Hash.new
|
44
|
+
headers_hash[Distelli::ServiceConstants::SERVER_HEADER] = "DistelliWS"
|
45
|
+
headers_hash[Distelli::ServiceConstants::REQUEST_ID_HEADER] = get_request_id()
|
46
|
+
|
47
|
+
if extra_headers != nil
|
48
|
+
headers_hash.update(extra_headers)
|
48
49
|
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
51
|
+
if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
|
52
|
+
headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
|
53
|
+
headers(headers_hash)
|
54
|
+
return $json_marshaller.marshall(response)
|
55
|
+
elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
|
56
|
+
headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_XML
|
57
|
+
headers(headers_hash)
|
58
|
+
return $xml_marshaller.marshall(response)
|
59
|
+
else
|
60
|
+
raise StandardError.new("Invalid Response type: "+response_type)
|
59
61
|
end
|
62
|
+
end
|
60
63
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
if extra_headers != nil
|
72
|
-
headers_hash.update(extra_headers)
|
73
|
-
end
|
74
|
-
|
75
|
-
if response_type == ServiceConstants::RESPONSE_TYPE_JSON
|
76
|
-
headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_JSON
|
77
|
-
headers(headers_hash)
|
78
|
-
return @json_marshaller.marshall(response)
|
79
|
-
elsif response_type == ServiceConstants::RESPONSE_TYPE_XML
|
80
|
-
headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_XML
|
81
|
-
headers(headers_hash)
|
82
|
-
return @xml_marshaller.marshall(response)
|
83
|
-
else
|
84
|
-
raise StandardError.new("Invalid Response type: "+response_type)
|
85
|
-
end
|
64
|
+
def marshall_error(error)
|
65
|
+
response_type = get_response_type(request)
|
66
|
+
if error.is_a?(Distelli::BaseException)
|
67
|
+
status error.http_code
|
68
|
+
else
|
69
|
+
log = Logging::Logger['DistelliFramework']
|
70
|
+
log.error("Unhandled Exception: "+error.inspect+" "+error.message+" "+error.backtrace.join(" \n"))
|
71
|
+
error = Distelli::ServerError.new("Cannot marshall error of type "+error.class.name+" Defaulting to ServerError")
|
72
|
+
status error.http_code
|
86
73
|
end
|
87
74
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
headers_hash =
|
98
|
-
headers_hash
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
headers(headers_hash)
|
108
|
-
return @xml_marshaller.marshall_error(error)
|
109
|
-
else
|
110
|
-
LOGGER.error("Invalid Response Type: "+response_type)
|
111
|
-
error = ServerError.new("Invalid response type: "+response_type)
|
112
|
-
status error.http_code
|
113
|
-
headers_hash[ServiceConstants::CONTENT_TYPE_HEADER] = ServiceConstants::CONTENT_TYPE_JSON
|
114
|
-
headers(headers_hash)
|
115
|
-
return @json_marshaller.marshall_error(error)
|
116
|
-
end
|
75
|
+
headers_hash = Hash.new
|
76
|
+
headers_hash[Distelli::ServiceConstants::SERVER_HEADER] = "DistelliWS"
|
77
|
+
headers_hash[Distelli::ServiceConstants::REQUEST_ID_HEADER] = get_request_id()
|
78
|
+
|
79
|
+
if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
|
80
|
+
headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
|
81
|
+
headers(headers_hash)
|
82
|
+
return $json_marshaller.marshall_error(error)
|
83
|
+
elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
|
84
|
+
headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
|
85
|
+
headers(headers_hash)
|
86
|
+
return $xml_marshaller.marshall_error(error)
|
87
|
+
else
|
88
|
+
LOGGER.error("Invalid Response Type: "+response_type)
|
89
|
+
error = ServerError.new("Invalid response type: "+response_type)
|
90
|
+
status error.http_code
|
91
|
+
headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
|
92
|
+
headers(headers_hash)
|
93
|
+
return $json_marshaller.marshall_error(error)
|
117
94
|
end
|
95
|
+
end
|
118
96
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
end
|
97
|
+
private
|
98
|
+
def validate_response_type(response_type)
|
99
|
+
if response_type == nil
|
100
|
+
return Distelli::ServiceConstants::RESPONSE_TYPE_JSON
|
101
|
+
end
|
102
|
+
if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
|
103
|
+
return response_type
|
104
|
+
elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
|
105
|
+
return response_type
|
106
|
+
else
|
107
|
+
LOGGER.error("Invalid response type: "+response_type+" Defaulting to "+Distelli::ServiceConstants::RESPONSE_TYPE_JSON)
|
108
|
+
return Distelli::ServiceConstants::RESPONSE_TYPE_JSON
|
132
109
|
end
|
110
|
+
end
|
133
111
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
return request_id
|
138
|
-
end
|
139
|
-
params = request.params()
|
140
|
-
request_id = params[ServiceConstants::REQUEST_ID_PARAM]
|
141
|
-
if request_id != nil
|
142
|
-
return request_id
|
143
|
-
end
|
144
|
-
|
145
|
-
# Create a new request id
|
146
|
-
request_id = SecureRandom.uuid
|
147
|
-
request[ServiceConstants::REQUEST_ID_HEADER] = request_id
|
112
|
+
def get_request_id()
|
113
|
+
request_id = request[Distelli::ServiceConstants::REQUEST_ID_HEADER]
|
114
|
+
if request_id != nil
|
148
115
|
return request_id
|
149
116
|
end
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
return validate_response_type(response_type)
|
155
|
-
end
|
156
|
-
|
157
|
-
response_type = nil
|
158
|
-
params = request.params()
|
159
|
-
if params != nil
|
160
|
-
response_type = params[ServiceConstants::RESPONSE_TYPE_PARAM]
|
161
|
-
end
|
162
|
-
return validate_response_type(response_type)
|
117
|
+
params = request.params()
|
118
|
+
request_id = params[Distelli::ServiceConstants::REQUEST_ID_PARAM]
|
119
|
+
if request_id != nil
|
120
|
+
return request_id
|
163
121
|
end
|
164
122
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
# else the operation is null
|
171
|
-
op_name = request[ServiceConstants::OPERATION_HEADER]
|
172
|
-
if op_name != nil
|
173
|
-
return op_name
|
174
|
-
end
|
175
|
-
|
176
|
-
params = request.params()
|
177
|
-
if params == nil
|
178
|
-
return nil
|
179
|
-
end
|
180
|
-
return params[ServiceConstants::OPERATION_PARAM]
|
181
|
-
end
|
123
|
+
# Create a new request id
|
124
|
+
request_id = SecureRandom.uuid
|
125
|
+
request[Distelli::ServiceConstants::REQUEST_ID_HEADER] = request_id
|
126
|
+
return request_id
|
127
|
+
end
|
182
128
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
# Marshall the error as a server error
|
188
|
-
ex = env['sinatra.error']
|
189
|
-
LOGGER.error('Unhandled Exception: '+ex.inspect+ex.backtrace.join("\n"))
|
190
|
-
return marshall_error(ex)
|
129
|
+
def get_response_type(request)
|
130
|
+
response_type = request[Distelli::ServiceConstants::RESPONSE_TYPE_HEADER]
|
131
|
+
if response_type != nil
|
132
|
+
return validate_response_type(response_type)
|
191
133
|
end
|
192
134
|
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
135
|
+
response_type = nil
|
136
|
+
params = request.params()
|
137
|
+
if params != nil
|
138
|
+
response_type = params[Distelli::ServiceConstants::RESPONSE_TYPE_PARAM]
|
197
139
|
end
|
140
|
+
return validate_response_type(response_type)
|
141
|
+
end
|
198
142
|
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
143
|
+
def get_operation(request)
|
144
|
+
# First check to see if there is the operation header.
|
145
|
+
# If there is then thats the operation
|
146
|
+
# If not then check to see if there is the operation query param
|
147
|
+
# If it is then thats the operation
|
148
|
+
# else the operation is null
|
149
|
+
op_name = request[Distelli::ServiceConstants::OPERATION_HEADER]
|
150
|
+
if op_name != nil
|
151
|
+
return op_name
|
203
152
|
end
|
204
153
|
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
error = MalformedRequest.new(request.request_method+" not supported on resource: "+request.path)
|
210
|
-
return marshall_error(error)
|
154
|
+
params = request.params()
|
155
|
+
if params == nil
|
156
|
+
return nil
|
211
157
|
end
|
158
|
+
return params[Distelli::ServiceConstants::OPERATION_PARAM]
|
212
159
|
end
|
160
|
+
|
161
|
+
######################################
|
162
|
+
# Error Handlers
|
163
|
+
######################################
|
164
|
+
# error do
|
165
|
+
# # Marshall the error as a server error
|
166
|
+
# ex = env['sinatra.error']
|
167
|
+
# LOGGER.error('Unhandled Exception: '+ex.inspect+ex.backtrace.join("\n"))
|
168
|
+
# return marshall_error(ex)
|
169
|
+
# end
|
170
|
+
|
171
|
+
# error Distelli::ClientError do
|
172
|
+
# # Marshall the Client Error Exception and set the response code
|
173
|
+
# ce = env['sinatra.error']
|
174
|
+
# marshall_error(ce)
|
175
|
+
# end
|
176
|
+
|
177
|
+
# error Distelli::ServerError do
|
178
|
+
# # Marshall the Server Error Exception and set the response code
|
179
|
+
# se = env['sinatra.error']
|
180
|
+
# return marshall_error(se)
|
181
|
+
# end
|
182
|
+
|
183
|
+
# not_found do
|
184
|
+
# # Marshall a Client Error Exception and set the 400 response code
|
185
|
+
# op_name = get_operation(request)
|
186
|
+
|
187
|
+
# error = MalformedRequest.new(request.request_method+" not supported on resource: "+request.path)
|
188
|
+
# return marshall_error(error)
|
189
|
+
# end
|
213
190
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: DistelliServiceFrameworkSinatra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.3'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sinatra
|
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
86
|
version: '0'
|
87
87
|
requirements: []
|
88
88
|
rubyforge_project:
|
89
|
-
rubygems_version: 1.8.
|
89
|
+
rubygems_version: 1.8.24
|
90
90
|
signing_key:
|
91
91
|
specification_version: 3
|
92
92
|
summary: Distelli Service Framework classes for Sinatra
|