Module: DistelliServiceFrameworkRails

Defined in:
lib/distelli/serviceframeworkrails.rb

Instance Method Summary collapse

Instance Method Details

#get_content_typeObject



56
57
58
59
60
# File 'lib/distelli/serviceframeworkrails.rb', line 56

def get_content_type()
  header_name = Distelli::ServiceConstants::CONTENT_TYPE_HEADER.upcase().gsub("-","_")
  content_type = request.headers[header_name]
  return content_type
end

#get_operationObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/distelli/serviceframeworkrails.rb', line 42

def get_operation()
  header_name = Distelli::ServiceConstants::OPERATION_HEADER.upcase().gsub("-","_")
  op_name = request.headers[header_name]
  if op_name != nil
    return op_name
  end

  params = request.parameters
  if params == nil
    return nil
  end
  return params[Distelli::ServiceConstants::OPERATION_PARAM]
end

#get_request_idObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/distelli/serviceframeworkrails.rb', line 9

def get_request_id()
  header_name = Distelli::ServiceConstants::REQUEST_ID_HEADER.upcase().gsub("-", "_")
  request_id = request.headers[header_name]
  if request_id != nil
    return request_id
  end
  params = request.parameters
  request_id = params[Distelli::ServiceConstants::REQUEST_ID_PARAM]
  if request_id != nil
    return request_id
  end

  # Create a new request id
  request_id = SecureRandom.uuid
  request.headers[header_name] = request_id
  return request_id
end

#get_response_typeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/distelli/serviceframeworkrails.rb', line 27

def get_response_type()
  header_name = Distelli::ServiceConstants::RESPONSE_TYPE_HEADER.upcase().gsub("-","_")
  response_type = request.headers[header_name]
  if response_type != nil
    return validate_response_type(response_type)
  end
  
  response_type = nil
  params = request.parameters
  if params != nil
    response_type = params[Distelli::ServiceConstants::RESPONSE_TYPE_PARAM]
  end
  return validate_response_type(response_type)
end

#marshall_error(error) ⇒ Object



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
# File 'lib/distelli/serviceframeworkrails.rb', line 115

def marshall_error(error)
  response_type = get_response_type()
  if error.is_a?(Distelli::BaseException)
    response.status = error.http_code
  else
    log = Logging::Logger['DistelliFramework']
    log.error("Unhandled Exception: "+error.message+" "+error.backtrace.join("    \n"))
    error = Distelli::ServerError.new("Cannot marshall error of type "+error.class.name+" Defaulting to ServerError")
    response.status = error.http_code
  end
  
  headers_hash = Hash.new
  headers_hash[Distelli::ServiceConstants::SERVER_HEADER] = "DistelliWS"
  headers_hash[Distelli::ServiceConstants::REQUEST_ID_HEADER] = get_request_id()

  if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
    headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
    response.headers.update(headers_hash)
    self.response_body = $json_marshaller.marshall_error(error)
  elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
    headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
    response.headers.update(headers_hash)
    self.response_body = $xml_marshaller.marshall_error(error)
  else
    log = Logging::Logger['DistelliFramework']
    log.error("Invalid Response Type: "+response_type)
    error = ServerError.new("Invalid response type: "+response_type)
    response.status = error.http_code
    headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
    response.headers.update(headers_hash)
    self.response_body = $json_marshaller.marshall_error(error)
  end
end

#marshall_response(svc_response, extra_headers = nil, http_code = nil) ⇒ Object



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
# File 'lib/distelli/serviceframeworkrails.rb', line 88

def marshall_response(svc_response, extra_headers=nil, http_code=nil)
  response_type = get_response_type()
  if http_code != nil
    response.status = http_code
  end
  
  headers_hash = Hash.new
  headers_hash[Distelli::ServiceConstants::SERVER_HEADER] = "DistelliWS"
  headers_hash[Distelli::ServiceConstants::REQUEST_ID_HEADER] = get_request_id()

  if extra_headers != nil
    headers_hash.update(extra_headers)
  end

  if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
    headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_JSON
    response.headers.update(headers_hash)
    self.response_body = $json_marshaller.marshall(svc_response)
  elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
    headers_hash[Distelli::ServiceConstants::CONTENT_TYPE_HEADER] = Distelli::ServiceConstants::CONTENT_TYPE_XML
    response.headers.update(headers_hash)
    self.response_body = $xml_marshaller.marshall(svc_response)
  else
    raise StandardError.new("Invalid Response type: "+response_type)
  end
end

#unmarshall_requestObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/distelli/serviceframeworkrails.rb', line 77

def unmarshall_request()
  content_type = get_content_type()
  if content_type == Distelli::ServiceConstants::CONTENT_TYPE_JSON
    return $json_marshaller.unmarshall(request.body)
  elsif content_type == Distelli::ServiceConstants::CONTENT_TYPE_XML
    return $xml_marshaller.unmarshall(request.body)
  else
    return request.body
  end
end

#validate_response_type(response_type) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/distelli/serviceframeworkrails.rb', line 62

def validate_response_type(response_type)
  if response_type == nil
    return Distelli::ServiceConstants::RESPONSE_TYPE_JSON
  end
  if response_type == Distelli::ServiceConstants::RESPONSE_TYPE_JSON
    return response_type
  elsif response_type == Distelli::ServiceConstants::RESPONSE_TYPE_XML
    return response_type
  else
    log = Logging::Logger['DistelliFramework']
    log.error("Invalid response type: "+response_type+" Defaulting to "+ServiceConstants::RESPONSE_TYPE_JSON)
    return Distelli::ServiceConstants::RESPONSE_TYPE_JSON
  end
end