Class: Soybean::Engine

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
SOAP
Defined in:
lib/soybean/engine.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

add_document_operation, add_headerhandler, add_rpc_method, add_rpc_method_as, add_rpc_method_with_namespace, add_rpc_method_with_namespace_as, add_rpc_operation, add_rpc_servant, endpoint, endpoint=, filterchain, generate_explicit_type, generate_explicit_type=, literal_mapping_registry, literal_mapping_registry=, mapping_registry, mapping_registry=, router, setup

Constructor Details

#initialize(app = nil) ⇒ Engine

Returns a new instance of Engine.



116
117
118
119
# File 'lib/soybean/engine.rb', line 116

def initialize(app = nil)
  @app = app
  setup!(self.class.service)
end

Class Method Details

.inherited(subclass) ⇒ Object



11
12
13
# File 'lib/soybean/engine.rb', line 11

def self.inherited(subclass)
  Soybean.engines << subclass
end

Instance Method Details

#call(env) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/soybean/engine.rb', line 135

def call(env)
  s, h, b = with_logging(env, logger) do
    handle(env)
  end
  return s, h, b
rescue => e
  [200, {'Allow' => 'POST',
         'Content-Type' => 'text/xml'}, [e.message,
                                           e.backtrace.join("\n")]]
end

#handle(env) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/soybean/engine.rb', line 177

def handle(env)
  # yeah, all soap calls are over POST
  if env['REQUEST_METHOD'] != 'POST'
    return 405, {
        'Allow' => 'POST',
        'Content-Type' => 'text/plain'}, ["405 - Method Not Allowed"]
  end

  conn_data = ::SOAP::StreamHandler::ConnectionData.new
  setup_request(conn_data, env)
  conn_data = self.class.router.route(conn_data)
  status, headers, body = setup_response(conn_data, env)
  [status, headers, body]
rescue
  raise # TODO -- do we 500 right here, or let the exception bubble up?
end

#parse_soapaction(soapaction) ⇒ Object



225
226
227
228
229
230
231
232
# File 'lib/soybean/engine.rb', line 225

def parse_soapaction(soapaction)
  if !soapaction.nil? and !soapaction.empty?
    if /\A"(.+)"\z/ =~ soapaction
      return $1
    end
  end
  nil
end

#setup!(service) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/soybean/engine.rb', line 121

def setup!(service)
  self.class.setup {}
  service.class::Methods.each do |definitions|
    opt = definitions.last
    if opt[:request_style] == :document
      self.class.router.add_document_operation(service, *definitions)
    else
      self.class.router.add_rpc_operation(service, *definitions)
    end
  end
  self.class.mapping_registry = Mappings::EncodedRegistry
  self.class.literal_mapping_registry = Mappings::LiteralRegistry
end

#setup_request(conn_data, env) ⇒ Object



194
195
196
197
198
199
200
201
202
# File 'lib/soybean/engine.rb', line 194

def setup_request(conn_data, env)
  # TODO: we're reading the whole input here, which kind of stinks if rack is
  # reading from the client on demand. We can't just pass in the rack input
  # object, since REXML needs an IO that responds to :eof? -- we'd need a
  # wrapper IO-like object.
  conn_data.receive_string = env['rack.input'].read
  conn_data.receive_contenttype = env['CONTENT_TYPE']
  conn_data.soapaction = parse_soapaction(env['HTTP_SOAPAction'])
end

#setup_response(conn_data, env) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/soybean/engine.rb', line 204

def setup_response(conn_data, env)
  status = 200
  headers = {}
  body = []
  headers['content-type'] = conn_data.send_contenttype
  # TODO: cookies?
  if conn_data.is_nocontent
    status = 202 # ACCEPTED
  elsif conn_data.is_fault
    # rather than sending the 500 here, let's bubble up the exception so the
    # parent application can do with it what it will. The only downside is
    # soap4r has already converted the exception into a soap response body at
    # this point, which isn't what we want at all.
    # maybe someday i'll re-parse the response or something. but not today.
    raise conn_data.send_string
  else
    body << conn_data.send_string
  end
  return status, headers, body
end

#with_logging(env, logger) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/soybean/engine.rb', line 146

def with_logging(env, logger)
  request = Rack::Request.new(env)
  input_params = request.body.read
  request.body.rewind

  logger.info "Started %s \"%s\" for %s at %s. caller %s" % [request.request_method,
                                                             URI(env['REQUEST_URI']).path,
                                                             request.ip,
                                                             Time.now.strftime("%Y-%m-%d %H:%M:%S"),
                                                             request.referer] if logger

  logger.info "  Processing by %s" % [self.class.service.class.name] if logger
  logger.debug "  Request: " if logger

  logger.debug pretty_xml(input_params, '    ') if logger

  s, h, b = yield

  logger.debug "  Response:" % [h.inspect] if logger
  logger.debug "    Headers: %s" % [h.inspect] if logger
  logger.debug "    Body: " if logger

  logger.debug pretty_xml(b.join, '    ') if logger

  return s, h, b

rescue => e
  logger.error "\tERROR: \n%s" % [e.message] if logger
  raise e
end