Class OpenWFE::OldRestServlet
In: lib/openwfe/orest/oldrestservlet.rb
Parent: WEBrick::HTTPServlet::AbstractServlet

A common parent class for the worklist and the engine old-style (2002) REST interface.

Methods

Constants

MUTEX = Mutex.new
CT = "Content-Type"

Public Class methods

Returns always the same servlet instance.

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 122
122:         def self.get_instance (server, *options)
123:             MUTEX.synchronize do
124:                 return @__instance__ if @__instance__
125:                 @__instance__ = self.new(server, *options)
126:             end
127:         end

[Source]

    # File lib/openwfe/orest/oldrestservlet.rb, line 58
58:         def initialize (server, params)
59: 
60:             super
61: 
62:             @auth_system = params[:AuthSystem] || {}
63: 
64:             @realm_name = get_realm_name
65: 
66:             @last_given_session_id = -1
67:             @sessions = {}
68:         end

Public Instance methods

this default implementation returns "no_realm".

[Source]

    # File lib/openwfe/orest/oldrestservlet.rb, line 73
73:         def get_realm_name
74:             "no_realm"
75:         end

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 77
 77:         def service req, res
 78: 
 79:             if req.request_method == 'POST'
 80:                 class << req
 81:                     def parse_query
 82:                         @query = WEBrick::HTTPUtils::parse_query(@query_string)
 83:                     end
 84:                 end
 85:             end
 86: 
 87:             username = authenticate req, res
 88: 
 89:             if req.query_string == nil
 90: 
 91:                 get_new_session username, req, res
 92:                 return
 93:             end
 94: 
 95:             req.attributes['username'] = username
 96: 
 97:             action = req.query["action"]
 98:             action = action.downcase if action
 99: 
100:             if action == "endworksession"
101:                 end_work_session req, res
102:                 return
103:             end
104: 
105:             action_method = "do__#{action}".intern
106: 
107:             unless self.respond_to?(action_method)
108:                 action_not_implemented action, res
109:                 return
110:             end
111: 
112:             begin
113:                 self.send action_method, req, res
114:             rescue Exception => e
115:                 reply_with_exception res, e
116:             end
117:         end

Protected Instance methods

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 203
203:             def _authenticate user, pass
204: 
205:                 if @auth_system.kind_of?(Hash)
206:                     @auth_system[user] == pass
207:                 elsif @auth_system.kind_of?(Proc)
208:                     @auth_system.call user, pass
209:                 elsif @auth_system.respond_to?(:authenticate)
210:                     @auth_system.authenticate user, pass
211:                 else
212:                     false
213:                 end
214:             end

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 240
240:             def action_not_implemented action, res
241: 
242:                 reply_with_error(
243:                     res, 404, "action '#{action}' is not implemented.")
244:             end

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 184
184:             def authenticate req, res
185: 
186:                 user = nil
187: 
188:                 WEBrick::HTTPAuth::basic_auth(req, res, @realm_name) do |u, p|
189: 
190:                     user = get_session_user req
191: 
192:                     if user
193:                         true
194:                     else
195:                         user = u
196:                         _authenticate u, p
197:                     end
198:                 end
199: 
200:                 user
201:             end

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 216
216:             def determine_agent (req)
217: 
218:                 req.addr.join "|"
219:             end

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 173
173:             def end_work_session (req, res)
174: 
175:                 sid = req.query['session']
176:                 @sessions.delete(Integer(sid)) if sid
177: 
178:                 @logger.debug "end_work_session() #{sid}"
179:                 #@logger.debug "end_work_session() sessions : #{@sessions.size}"
180: 
181:                 reply_with_xml(res, 200, REXML::Element.new('bye'))
182:             end

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 221
221:             def get_new_session username, req, res
222: 
223:                 sid = new_session_id
224: 
225:                 @sessions[sid] = [ 
226:                     username,
227:                     determine_agent(req), 
228:                     Time.now.to_i 
229:                 ]
230: 
231:                 @logger.debug "get_new_session() #{sid}"
232:                 #@logger.debug "get_new_session() sessions : #{@sessions.size}"
233: 
234:                 esess = REXML::Element.new 'session'
235:                 esess.add_attribute 'id', sid
236: 
237:                 reply_with_xml res, 200, esess
238:             end

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 259
259:             def get_session_user (req)
260: 
261:                 sid = req.query['session']
262: 
263:                 @logger.debug "get_session_user() sid : #{sid}"
264: 
265:                 return nil unless sid
266: 
267:                 s = @sessions[Integer(sid)]
268: 
269:                 return nil unless s
270: 
271:                 username, agent, last_seen = s
272: 
273:                 return username if agent == determine_agent(req)
274: 
275:                 nil
276:             end

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 246
246:             def new_session_id
247:                 MUTEX.synchronize do
248: 
249:                     id = Integer(Time.new.to_f * 100000)
250: 
251:                     id = @last_given_session_id + 1 \
252:                         if id <= @last_given_session_id
253: 
254:                     @last_given_session_id = id
255:                     id
256:                 end
257:             end

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 131
131:             def reply_with_error (res, code, error_message)
132: 
133:                 res.status = code
134:                 res[CT] = "text/plain"
135:                 res.body = error_message
136:             end

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 138
138:             def reply_with_exception (res, exception)
139: 
140:                 message = exception.message
141: 
142:                 ms = message.split
143: 
144:                 code = 500
145:                 body = message
146: 
147:                 if ms.length > 1 and ms[0].to_i != 0
148:                     code = Integer(ms[0])
149:                     body = message[4..-1]
150:                 end
151: 
152:                 message << "\n"
153:                 message << OpenWFE::exception_to_s(exception)
154:                 message << "\n"
155: 
156:                 reply_with_error(res, code, body)
157:             end

[Source]

     # File lib/openwfe/orest/oldrestservlet.rb, line 159
159:             def reply_with_xml (res, code, xml)
160: 
161:                 res.status = code
162:                 res[CT] = "application/xml"
163: 
164:                 if xml.kind_of?(REXML::Element)
165:                     doc = REXML::Document.new
166:                     doc << xml
167:                     xml = OpenWFE::xml_to_s(doc)
168:                 end
169: 
170:                 res.body = xml
171:             end

[Validate]