Class: JIJI::JsonServlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/jiji/server.rb

Constant Summary collapse

@@instance =

サーブレットの唯一のインスタンス

nil
@@instance_creation_mutex =

インスタンス生成を同期化するためのMutex

Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, *options) ⇒ JsonServlet

Returns a new instance of JsonServlet.



81
82
83
84
# File 'lib/jiji/server.rb', line 81

def initialize( config, *options )
  super
  @registry = options[0]
end

Class Method Details

.get_instance(config, *options) ⇒ Object

サーブレットのインスタンスを生成するための関数



74
75
76
77
78
79
# File 'lib/jiji/server.rb', line 74

def self.get_instance(config, *options)
  @@instance_creation_mutex.synchronize {
    @@instance = self.new(config, *options) if @@instance == nil
    @@instance
  }
end

Instance Method Details

#do_GET(req, res) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/jiji/server.rb', line 86

def do_GET(req, res)
  begin
    process( req, res, req.query["request"].to_s )
  rescue Exception
    @registry.server_logger.error $!
    error =  $!.to_s + " : " + $!.backtrace.join("\n")
    res.body = "[{\"error\":\"fatal:#{error}\", \"result\":null}]"
  end
end

#do_POST(req, res) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/jiji/server.rb', line 96

def do_POST(req, res)
  begin 
    process( req, res, CGI.unescape(req.body.to_s) )
  rescue Exception
    @registry.server_logger.error $!
    error =  $!.to_s + " : " + $!.backtrace.join("\n")
    res.body = "[{\"error\":\"fatal:#{error}\", \"result\":null}]"
  end
end

#process(req, res, request) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jiji/server.rb', line 106

def process( req, res, request )
  res['Content-Type'] = "application/json"
  begin
    path = req.path
    @registry.server_logger.info "access : path=#{req.path}"
    raise "illegal path." unless path =~ /\/json\/([a-zA-Z0-9_]+)$/
    @registry.server_logger.info "access : request=#{request}"
    service = @registry["#{$1}_service".to_sym]
    raise "service not found." if service == nil
    res.body = JSONBroker::Broker.new( service ).invoke( request )
  rescue JIJI::UserError
    @registry.server_logger.warn $!
    error =  $!.to_s + " : " + $!.backtrace.join("\n")
    res.body = "[{\"error\":\"#{$!.code}:#{error}\", \"result\":null}]" 
  rescue JIJI::FatalError
    @registry.server_logger.error $!
    error =  $!.to_s + " : " + $!.backtrace.join("\n")
    res.body = "[{\"error\":\"#{$!.code}:#{error}\", \"result\":null}]"
  end
end