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:
4
examples/filter/license.lic
Normal file
4
examples/filter/license.lic
Normal 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)"
|
||||||
|
|
||||||
@@ -8,16 +8,9 @@ class
|
|||||||
AUTHENTICATION_FILTER
|
AUTHENTICATION_FILTER
|
||||||
|
|
||||||
inherit
|
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_CONTEXT_HANDLER [FILTER_HANDLER_CONTEXT]
|
||||||
|
|
||||||
WSF_URI_TEMPLATE_HANDLER
|
|
||||||
|
|
||||||
WSF_RESOURCE_HANDLER_HELPER
|
|
||||||
-- redefine
|
|
||||||
-- do_get
|
|
||||||
-- end
|
|
||||||
|
|
||||||
SHARED_DATABASE_API
|
SHARED_DATABASE_API
|
||||||
|
|
||||||
@@ -25,7 +18,7 @@ inherit
|
|||||||
|
|
||||||
feature -- Basic operations
|
feature -- Basic operations
|
||||||
|
|
||||||
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
|
execute (ctx: FILTER_HANDLER_CONTEXT; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||||
-- Execute the filter
|
-- Execute the filter
|
||||||
local
|
local
|
||||||
l_auth: HTTP_AUTHORIZATION
|
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
|
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 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)
|
(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
|
and attached l_auth.password as l_auth_password and then l_auth_password.is_equal (l_user.password))
|
||||||
execute_next (req, res)
|
then
|
||||||
|
ctx.set_user (l_user)
|
||||||
|
execute_next (ctx, req, res)
|
||||||
else
|
else
|
||||||
handle_unauthorized ("Unauthorized", req, res)
|
handle_unauthorized ("Unauthorized", req, res)
|
||||||
end
|
end
|
||||||
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
|
feature {NONE} -- Implementation
|
||||||
|
|
||||||
handle_unauthorized (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
handle_unauthorized (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||||
@@ -67,4 +53,7 @@ feature {NONE} -- Implementation
|
|||||||
res.put_string (a_description)
|
res.put_string (a_description)
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
34
examples/filter/src/filter/filter_handler_context.e
Normal file
34
examples/filter/src/filter/filter_handler_context.e
Normal 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
|
||||||
@@ -37,17 +37,14 @@ feature {NONE} -- Initialization
|
|||||||
l_router: WSF_ROUTER
|
l_router: WSF_ROUTER
|
||||||
l_authentication_filter_hdl: AUTHENTICATION_FILTER
|
l_authentication_filter_hdl: AUTHENTICATION_FILTER
|
||||||
l_user_filter: USER_HANDLER
|
l_user_filter: USER_HANDLER
|
||||||
l_user_handler: WSF_URI_TEMPLATE_HANDLER
|
|
||||||
l_routing_filter: WSF_ROUTING_FILTER
|
l_routing_filter: WSF_ROUTING_FILTER
|
||||||
do
|
do
|
||||||
create l_router.make (1)
|
create l_router.make (1)
|
||||||
create l_authentication_filter_hdl
|
create l_authentication_filter_hdl
|
||||||
create l_user_filter
|
create l_user_filter
|
||||||
l_authentication_filter_hdl.set_next (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.handle_with_request_methods ("/user/{userid}", l_authentication_filter_hdl, 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)
|
|
||||||
create l_routing_filter.make (l_router)
|
create l_routing_filter.make (l_router)
|
||||||
l_routing_filter.set_execute_default_action (agent execute_default)
|
l_routing_filter.set_execute_default_action (agent execute_default)
|
||||||
filter := l_routing_filter
|
filter := l_routing_filter
|
||||||
@@ -99,7 +96,7 @@ feature -- Basic operations
|
|||||||
end
|
end
|
||||||
|
|
||||||
note
|
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)"
|
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
|
||||||
source: "[
|
source: "[
|
||||||
Eiffel Software
|
Eiffel Software
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ class
|
|||||||
USER_HANDLER
|
USER_HANDLER
|
||||||
|
|
||||||
inherit
|
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
|
redefine
|
||||||
do_get
|
do_get
|
||||||
end
|
end
|
||||||
@@ -23,25 +23,34 @@ inherit
|
|||||||
|
|
||||||
feature -- Basic operations
|
feature -- Basic operations
|
||||||
|
|
||||||
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
|
execute (ctx: FILTER_HANDLER_CONTEXT; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||||
-- Execute request handler
|
-- Execute request handler
|
||||||
do
|
do
|
||||||
execute_methods (req, res)
|
execute_methods (ctx, req, res)
|
||||||
|
execute_next (ctx, req, res)
|
||||||
end
|
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.
|
-- Using GET to retrieve resource information.
|
||||||
-- If the GET request is SUCCESS, we response with
|
-- If the GET request is SUCCESS, we response with
|
||||||
-- 200 OK, and a representation of the user
|
-- 200 OK, and a representation of the user
|
||||||
-- If the GET request is not SUCCESS, we response with
|
-- If the GET request is not SUCCESS, we response with
|
||||||
-- 404 Resource not found
|
-- 404 Resource not found
|
||||||
|
require else
|
||||||
|
authenticated_user_attached: attached ctx.user
|
||||||
local
|
local
|
||||||
id : STRING
|
id : STRING
|
||||||
do
|
do
|
||||||
if attached req.orig_path_info as orig_path then
|
if attached req.orig_path_info as orig_path then
|
||||||
id := get_user_id_from_path (orig_path)
|
id := get_user_id_from_path (orig_path)
|
||||||
if attached retrieve_user (id) as l_user then
|
if attached retrieve_user (id) as l_user then
|
||||||
|
if l_user ~ ctx.user then
|
||||||
compute_response_get (req, res, l_user)
|
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
|
else
|
||||||
handle_resource_not_found_response ("The following resource " + orig_path + " is not found ", req, res)
|
handle_resource_not_found_response ("The following resource " + orig_path + " is not found ", req, res)
|
||||||
end
|
end
|
||||||
@@ -61,7 +70,7 @@ feature {NONE} -- Implementation
|
|||||||
l_msg := jv.representation
|
l_msg := jv.representation
|
||||||
h.put_content_length (l_msg.count)
|
h.put_content_length (l_msg.count)
|
||||||
if attached req.request_time as time then
|
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
|
end
|
||||||
res.set_status_code ({HTTP_STATUS_CODE}.ok)
|
res.set_status_code ({HTTP_STATUS_CODE}.ok)
|
||||||
res.put_header_text (h.string)
|
res.put_header_text (h.string)
|
||||||
@@ -83,6 +92,6 @@ feature {NONE} -- Implementation
|
|||||||
end
|
end
|
||||||
|
|
||||||
note
|
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)"
|
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user