Updated filter example to demonstrate the use of context.

(note: this commit is a merged of pull request from Olivier Ligot, and changes from Jocelyn Fiat)

Signed-off-by: Jocelyn Fiat <jfiat@eiffel.com>
Signed-off-by: Olivier Ligot <oligot@gmail.com>
This commit is contained in:
Jocelyn Fiat
2012-10-04 15:00:44 +02:00
parent 55e4602faf
commit 9333d9c5be
5 changed files with 70 additions and 37 deletions

View File

@@ -0,0 +1,4 @@
${NOTE_KEYWORD}
copyright: "2011-${YEAR}, Olivier Ligot, Jocelyn Fiat and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"

View File

@@ -8,16 +8,9 @@ class
AUTHENTICATION_FILTER
inherit
WSF_FILTER_HANDLER [WSF_URI_TEMPLATE_HANDLER]
WSF_FILTER_CONTEXT_HANDLER [FILTER_HANDLER_CONTEXT, WSF_URI_TEMPLATE_CONTEXT_HANDLER [FILTER_HANDLER_CONTEXT]]
SHARED_DATABASE_API
WSF_URI_TEMPLATE_HANDLER
WSF_RESOURCE_HANDLER_HELPER
-- redefine
-- do_get
-- end
WSF_URI_TEMPLATE_CONTEXT_HANDLER [FILTER_HANDLER_CONTEXT]
SHARED_DATABASE_API
@@ -25,7 +18,7 @@ inherit
feature -- Basic operations
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
execute (ctx: FILTER_HANDLER_CONTEXT; req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute the filter
local
l_auth: HTTP_AUTHORIZATION
@@ -34,22 +27,15 @@ feature -- Basic operations
if (attached l_auth.type as l_auth_type and then l_auth_type.is_equal ("basic")) and
attached Db_access.users.item (1) as l_user and then
(attached l_auth.login as l_auth_login and then l_auth_login.is_equal (l_user.name)
and attached l_auth.password as l_auth_password and then l_auth_password.is_equal (l_user.password)) then
execute_next (req, res)
and attached l_auth.password as l_auth_password and then l_auth_password.is_equal (l_user.password))
then
ctx.set_user (l_user)
execute_next (ctx, req, res)
else
handle_unauthorized ("Unauthorized", req, res)
end
end
feature -- Filter
execute_next (req: WSF_REQUEST; res: WSF_RESPONSE)
do
if attached next as n then
n.execute (req, res)
end
end
feature {NONE} -- Implementation
handle_unauthorized (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
@@ -67,4 +53,7 @@ feature {NONE} -- Implementation
res.put_string (a_description)
end
note
copyright: "2011-2012, Olivier Ligot, Jocelyn Fiat and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,34 @@
note
description: "Summary description for {FILTER_HANDLER_CONTEXT}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
FILTER_HANDLER_CONTEXT
inherit
WSF_HANDLER_CONTEXT
create
make
feature -- Access
user: detachable USER
-- Authenticated user
feature -- Element change
set_user (a_user: USER)
-- Set `user' to `a_user'
do
user := a_user
ensure
user_set: user = a_user
end
note
copyright: "2011-2012, Olivier Ligot, Jocelyn Fiat and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -37,17 +37,14 @@ feature {NONE} -- Initialization
l_router: WSF_ROUTER
l_authentication_filter_hdl: AUTHENTICATION_FILTER
l_user_filter: USER_HANDLER
l_user_handler: WSF_URI_TEMPLATE_HANDLER
l_routing_filter: WSF_ROUTING_FILTER
do
create l_router.make (1)
create l_authentication_filter_hdl
create l_user_filter
l_authentication_filter_hdl.set_next (l_user_filter)
l_user_handler := l_authentication_filter_hdl
l_router.handle_with_request_methods ("/user/{userid}", l_user_handler, l_router.methods_get)
-- l_router.map_with_request_methods ("/user/{userid}", l_user_handler, << "GET" >>)
-- create l_routing_hdl.make_with_router (l_router)
l_router.handle_with_request_methods ("/user/{userid}", l_authentication_filter_hdl, l_router.methods_get)
create l_routing_filter.make (l_router)
l_routing_filter.set_execute_default_action (agent execute_default)
filter := l_routing_filter
@@ -99,7 +96,7 @@ feature -- Basic operations
end
note
copyright: "2011-2012, Javier Velilla and others"
copyright: "2011-2012, Olivier Ligot, Jocelyn Fiat and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software

View File

@@ -8,11 +8,11 @@ class
USER_HANDLER
inherit
WSF_FILTER_HANDLER [WSF_URI_TEMPLATE_HANDLER]
WSF_FILTER_CONTEXT_HANDLER [FILTER_HANDLER_CONTEXT, WSF_URI_TEMPLATE_CONTEXT_HANDLER [FILTER_HANDLER_CONTEXT]]
WSF_URI_TEMPLATE_HANDLER
WSF_URI_TEMPLATE_CONTEXT_HANDLER [FILTER_HANDLER_CONTEXT]
WSF_RESOURCE_HANDLER_HELPER
WSF_RESOURCE_CONTEXT_HANDLER_HELPER [FILTER_HANDLER_CONTEXT]
redefine
do_get
end
@@ -23,25 +23,34 @@ inherit
feature -- Basic operations
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
execute (ctx: FILTER_HANDLER_CONTEXT; req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute request handler
do
execute_methods (req, res)
execute_methods (ctx, req, res)
execute_next (ctx, req, res)
end
do_get (req: WSF_REQUEST; res: WSF_RESPONSE)
do_get (ctx: FILTER_HANDLER_CONTEXT; req: WSF_REQUEST; res: WSF_RESPONSE)
-- Using GET to retrieve resource information.
-- If the GET request is SUCCESS, we response with
-- 200 OK, and a representation of the user
-- If the GET request is not SUCCESS, we response with
-- 404 Resource not found
require else
authenticated_user_attached: attached ctx.user
local
id : STRING
do
if attached req.orig_path_info as orig_path then
id := get_user_id_from_path (orig_path)
if attached retrieve_user (id) as l_user then
if l_user ~ ctx.user then
compute_response_get (req, res, l_user)
elseif attached ctx.user as l_auth_user then
-- Trying to access another user that the authenticated one,
-- which is forbidden in this example...
handle_forbidden ("You try to access the user " + id.out + " while authenticating with the user " + l_auth_user.id.out, req, res)
end
else
handle_resource_not_found_response ("The following resource " + orig_path + " is not found ", req, res)
end
@@ -61,7 +70,7 @@ feature {NONE} -- Implementation
l_msg := jv.representation
h.put_content_length (l_msg.count)
if attached req.request_time as time then
h.add_header ("Date:" + time.formatted_out ("ddd,[0]dd mmm yyyy [0]hh:[0]mi:[0]ss.ff2") + " GMT")
h.put_utc_date (time)
end
res.set_status_code ({HTTP_STATUS_CODE}.ok)
res.put_header_text (h.string)
@@ -69,7 +78,7 @@ feature {NONE} -- Implementation
end
end
get_user_id_from_path (a_path: READABLE_STRING_32) : STRING
get_user_id_from_path (a_path: READABLE_STRING_32): STRING
do
Result := a_path.split ('/').at (3)
end
@@ -83,6 +92,6 @@ feature {NONE} -- Implementation
end
note
copyright: "2011-2012, Javier Velilla and others"
copyright: "2011-2012, Olivier Ligot, Jocelyn Fiat and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end