From 9333d9c5be2d3a3405becd46a968e550afb9ded3 Mon Sep 17 00:00:00 2001 From: Jocelyn Fiat Date: Thu, 4 Oct 2012 15:00:44 +0200 Subject: [PATCH] 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 Signed-off-by: Olivier Ligot --- examples/filter/license.lic | 4 +++ .../filter/src/filter/authentication_filter.e | 31 ++++++----------- .../src/filter/filter_handler_context.e | 34 +++++++++++++++++++ examples/filter/src/filter_server.e | 9 ++--- examples/filter/src/resource/user_handler.e | 29 ++++++++++------ 5 files changed, 70 insertions(+), 37 deletions(-) create mode 100644 examples/filter/license.lic create mode 100644 examples/filter/src/filter/filter_handler_context.e diff --git a/examples/filter/license.lic b/examples/filter/license.lic new file mode 100644 index 00000000..73a78070 --- /dev/null +++ b/examples/filter/license.lic @@ -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)" + diff --git a/examples/filter/src/filter/authentication_filter.e b/examples/filter/src/filter/authentication_filter.e index 949ff380..44c27d2e 100644 --- a/examples/filter/src/filter/authentication_filter.e +++ b/examples/filter/src/filter/authentication_filter.e @@ -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 diff --git a/examples/filter/src/filter/filter_handler_context.e b/examples/filter/src/filter/filter_handler_context.e new file mode 100644 index 00000000..a4f5a448 --- /dev/null +++ b/examples/filter/src/filter/filter_handler_context.e @@ -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 diff --git a/examples/filter/src/filter_server.e b/examples/filter/src/filter_server.e index 17c345cc..fff542ac 100644 --- a/examples/filter/src/filter_server.e +++ b/examples/filter/src/filter_server.e @@ -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 diff --git a/examples/filter/src/resource/user_handler.e b/examples/filter/src/resource/user_handler.e index dad66c18..e834b28f 100644 --- a/examples/filter/src/resource/user_handler.e +++ b/examples/filter/src/resource/user_handler.e @@ -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 - compute_response_get (req, res, l_user) + 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