Merge pull request #26 from oligot/no-context

Use execution_variable instead of context
This commit is contained in:
Jocelyn Fiat
2013-01-23 02:00:38 -08:00
3 changed files with 30 additions and 40 deletions

View File

@@ -8,9 +8,9 @@ class
AUTHENTICATION_FILTER AUTHENTICATION_FILTER
inherit inherit
WSF_FILTER_CONTEXT_HANDLER [FILTER_HANDLER_CONTEXT] WSF_FILTER
WSF_URI_TEMPLATE_CONTEXT_HANDLER [FILTER_HANDLER_CONTEXT] WSF_URI_TEMPLATE_HANDLER
SHARED_DATABASE_API SHARED_DATABASE_API
@@ -18,7 +18,7 @@ inherit
feature -- Basic operations feature -- Basic operations
execute (ctx: FILTER_HANDLER_CONTEXT; req: WSF_REQUEST; res: WSF_RESPONSE) execute (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute the filter -- Execute the filter
local local
l_auth: HTTP_AUTHORIZATION l_auth: HTTP_AUTHORIZATION
@@ -31,8 +31,8 @@ feature -- Basic operations
attached l_auth.password as l_auth_password and then attached l_auth.password as l_auth_password and then
l_auth_password.same_string (l_user.password) l_auth_password.same_string (l_user.password)
then then
ctx.set_user (l_user) req.set_execution_variable ("user", l_user)
execute_next (ctx, req, res) execute_next (req, res)
else else
handle_unauthorized ("Unauthorized", req, res) handle_unauthorized ("Unauthorized", req, res)
end end
@@ -56,6 +56,6 @@ feature {NONE} -- Implementation
end end
note note
copyright: "2011-2012, Olivier Ligot, Jocelyn Fiat and others" copyright: "2011-2013, 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

View File

@@ -34,18 +34,17 @@ feature {NONE} -- Initialization
create_filter create_filter
-- Create `filter' -- Create `filter'
local local
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_routing_filter: WSF_ROUTING_FILTER l_routing_filter: WSF_ROUTING_FILTER
do do
create l_router.make (1) create 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_router.handle_with_request_methods ("/user/{userid}", l_authentication_filter_hdl, l_router.methods_get) router.handle_with_request_methods ("/user/{userid}", l_authentication_filter_hdl, router.methods_get)
create l_routing_filter.make (l_router) create l_routing_filter.make (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
end end
@@ -73,30 +72,21 @@ feature -- Basic operations
end end
execute_default (req: WSF_REQUEST; res: WSF_RESPONSE) execute_default (req: WSF_REQUEST; res: WSF_RESPONSE)
-- I'm using this method to handle the method not allowed response
-- in the case that the given uri does not have a corresponding http method
-- to handle it.
local local
h : HTTP_HEADER l_message: WSF_DEFAULT_ROUTER_RESPONSE
l_description : STRING
l_api_doc : STRING
do do
if req.content_length_value > 0 then create l_message.make_with_router (req, router)
req.input.read_string (req.content_length_value.as_integer_32) l_message.set_documentation_included (True)
end res.send (l_message)
create h.make
h.put_content_type_text_plain
l_api_doc := "%NPlease check the API%NURI:/user/{userid} METHOD: GET%N"
l_description := req.request_method + req.request_uri + " is not allowed" + "%N" + l_api_doc
h.put_content_length (l_description.count)
h.put_current_date
res.set_status_code ({HTTP_STATUS_CODE}.method_not_allowed)
res.put_header_text (h.string)
res.put_string (l_description)
end end
feature {NONE} -- Implementation
router: WSF_ROUTER;
-- Router
note note
copyright: "2011-2012, Olivier Ligot, Jocelyn Fiat and others" copyright: "2011-2013, 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

View File

@@ -8,11 +8,11 @@ class
USER_HANDLER USER_HANDLER
inherit inherit
WSF_FILTER_CONTEXT_HANDLER [FILTER_HANDLER_CONTEXT] WSF_FILTER
WSF_URI_TEMPLATE_CONTEXT_HANDLER [FILTER_HANDLER_CONTEXT] WSF_URI_TEMPLATE_HANDLER
WSF_RESOURCE_CONTEXT_HANDLER_HELPER [FILTER_HANDLER_CONTEXT] WSF_RESOURCE_HANDLER_HELPER
redefine redefine
do_get do_get
end end
@@ -23,30 +23,30 @@ inherit
feature -- Basic operations feature -- Basic operations
execute (ctx: FILTER_HANDLER_CONTEXT; req: WSF_REQUEST; res: WSF_RESPONSE) execute (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute request handler -- Execute request handler
do do
execute_methods (ctx, req, res) execute_methods (req, res)
execute_next (ctx, req, res) execute_next (req, res)
end end
do_get (ctx: FILTER_HANDLER_CONTEXT; req: WSF_REQUEST; res: WSF_RESPONSE) do_get (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 require else
authenticated_user_attached: attached ctx.user authenticated_user_attached: attached {USER} req.execution_variable ("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 if l_user ~ req.execution_variable ("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 elseif attached {USER} req.execution_variable ("user") as l_auth_user then
-- Trying to access another user that the authenticated one, -- Trying to access another user that the authenticated one,
-- which is forbidden in this example... -- 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) handle_forbidden ("You try to access the user " + id.out + " while authenticating with the user " + l_auth_user.id.out, req, res)
@@ -92,6 +92,6 @@ feature {NONE} -- Implementation
end end
note note
copyright: "2011-2012, Olivier Ligot, Jocelyn Fiat and others" copyright: "2011-2013, 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