Applied new ROUTER design to the whole EWF project.
This commit is contained in:
@@ -5,16 +5,27 @@ note
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
AUTHENTICATION_FILTER [C -> WSF_URI_TEMPLATE_HANDLER_CONTEXT]
|
||||
AUTHENTICATION_FILTER
|
||||
|
||||
inherit
|
||||
WSF_FILTER_HANDLER [C]
|
||||
WSF_FILTER_HANDLER [WSF_URI_TEMPLATE_HANDLER]
|
||||
|
||||
SHARED_DATABASE_API
|
||||
|
||||
WSF_URI_TEMPLATE_HANDLER
|
||||
|
||||
WSF_RESOURCE_HANDLER_HELPER
|
||||
-- redefine
|
||||
-- do_get
|
||||
-- end
|
||||
|
||||
SHARED_DATABASE_API
|
||||
|
||||
SHARED_EJSON
|
||||
|
||||
feature -- Basic operations
|
||||
|
||||
execute (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute the filter
|
||||
local
|
||||
l_auth: HTTP_AUTHORIZATION
|
||||
@@ -24,15 +35,24 @@ feature -- Basic operations
|
||||
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 (ctx, req, res)
|
||||
execute_next (req, res)
|
||||
else
|
||||
handle_unauthorized ("Unauthorized", ctx, req, res)
|
||||
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; ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
handle_unauthorized (a_description: STRING; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Handle forbidden.
|
||||
local
|
||||
h: HTTP_HEADER
|
||||
|
||||
@@ -10,7 +10,7 @@ class
|
||||
inherit
|
||||
ANY
|
||||
|
||||
WSF_URI_TEMPLATE_FILTERED_SERVICE
|
||||
WSF_FILTERED_SERVICE
|
||||
|
||||
WSF_HANDLER_HELPER
|
||||
|
||||
@@ -34,18 +34,21 @@ feature {NONE} -- Initialization
|
||||
create_filter
|
||||
-- Create `filter'
|
||||
local
|
||||
l_router: WSF_URI_TEMPLATE_ROUTER
|
||||
l_authentication_filter: AUTHENTICATION_FILTER [WSF_URI_TEMPLATE_HANDLER_CONTEXT]
|
||||
l_user_filter: USER_HANDLER [WSF_URI_TEMPLATE_HANDLER_CONTEXT]
|
||||
l_user_handler: WSF_HANDLER [WSF_URI_TEMPLATE_HANDLER_CONTEXT]
|
||||
l_routing_filter: WSF_ROUTING_FILTER [WSF_HANDLER [WSF_URI_TEMPLATE_HANDLER_CONTEXT], WSF_URI_TEMPLATE_HANDLER_CONTEXT]
|
||||
l_router: WSF_ROUTER
|
||||
l_authentication_filter_hdl: AUTHENTICATION_FILTER
|
||||
l_user_filter: USER_HANDLER
|
||||
l_user_handler: WSF_URI_TEMPLATE_HANDLER
|
||||
-- l_routing_hdl: WSF_URI_TEMPLATE_ROUTING_HANDLER --[WSF_HANDLER [WSF_URI_TEMPLATE_HANDLER_CONTEXT], WSF_URI_TEMPLATE_HANDLER_CONTEXT]
|
||||
l_routing_filter: WSF_ROUTING_FILTER
|
||||
do
|
||||
create l_router.make (1)
|
||||
create l_authentication_filter
|
||||
create l_authentication_filter_hdl
|
||||
create l_user_filter
|
||||
l_authentication_filter.set_next (l_user_filter)
|
||||
l_user_handler := l_authentication_filter
|
||||
l_router.map_with_request_methods ("/user/{userid}", l_user_handler, << "GET" >>)
|
||||
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)
|
||||
create l_routing_filter.make (l_router)
|
||||
l_routing_filter.set_execute_default_action (agent execute_default)
|
||||
filter := l_routing_filter
|
||||
|
||||
@@ -5,12 +5,14 @@ note
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
USER_HANDLER [C -> WSF_HANDLER_CONTEXT]
|
||||
USER_HANDLER
|
||||
|
||||
inherit
|
||||
WSF_FILTER_HANDLER [C]
|
||||
WSF_FILTER_HANDLER [WSF_URI_TEMPLATE_HANDLER]
|
||||
|
||||
WSF_RESOURCE_HANDLER_HELPER [C]
|
||||
WSF_URI_TEMPLATE_HANDLER
|
||||
|
||||
WSF_RESOURCE_HANDLER_HELPER
|
||||
redefine
|
||||
do_get
|
||||
end
|
||||
@@ -21,13 +23,13 @@ inherit
|
||||
|
||||
feature -- Basic operations
|
||||
|
||||
execute (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute request handler
|
||||
do
|
||||
execute_methods (ctx, req, res)
|
||||
execute_methods (req, res)
|
||||
end
|
||||
|
||||
do_get (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do_get (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
|
||||
@@ -39,16 +41,16 @@ feature -- Basic operations
|
||||
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 (ctx, req, res, l_user)
|
||||
compute_response_get (req, res, l_user)
|
||||
else
|
||||
handle_resource_not_found_response ("The following resource " + orig_path + " is not found ", ctx, req, res)
|
||||
handle_resource_not_found_response ("The following resource " + orig_path + " is not found ", req, res)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
compute_response_get (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE; l_user : USER)
|
||||
compute_response_get (req: WSF_REQUEST; res: WSF_RESPONSE; l_user : USER)
|
||||
local
|
||||
h: HTTP_HEADER
|
||||
l_msg : STRING
|
||||
|
||||
@@ -5,10 +5,23 @@ note
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
ORDER_HANDLER [C -> WSF_HANDLER_CONTEXT]
|
||||
ORDER_HANDLER
|
||||
inherit
|
||||
WSF_HANDLER [C]
|
||||
WSF_RESOURCE_HANDLER_HELPER [C]
|
||||
WSF_URI_HANDLER
|
||||
rename
|
||||
execute as uri_execute,
|
||||
new_mapping as new_uri_mapping
|
||||
end
|
||||
|
||||
WSF_URI_TEMPLATE_HANDLER
|
||||
rename
|
||||
execute as uri_template_execute,
|
||||
new_mapping as new_uri_template_mapping
|
||||
select
|
||||
new_uri_template_mapping
|
||||
end
|
||||
|
||||
WSF_RESOURCE_HANDLER_HELPER
|
||||
redefine
|
||||
do_get,
|
||||
do_post,
|
||||
@@ -22,10 +35,16 @@ inherit
|
||||
|
||||
feature -- execute
|
||||
|
||||
execute (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
uri_execute (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute request handler
|
||||
do
|
||||
execute_methods (ctx, req, res)
|
||||
execute_methods (req, res)
|
||||
end
|
||||
|
||||
uri_template_execute (ctx: WSF_URI_TEMPLATE_HANDLER_CONTEXT; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Execute request handler
|
||||
do
|
||||
execute_methods (req, res)
|
||||
end
|
||||
|
||||
feature -- API DOC
|
||||
@@ -34,7 +53,7 @@ feature -- API DOC
|
||||
|
||||
feature -- HTTP Methods
|
||||
|
||||
do_get (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do_get (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 order
|
||||
@@ -49,12 +68,12 @@ feature -- HTTP Methods
|
||||
id := get_order_id_from_path (orig_path)
|
||||
if attached retrieve_order (id) as l_order then
|
||||
if is_conditional_get (req, l_order) then
|
||||
handle_resource_not_modified_response ("The resource" + orig_path + "does not change", ctx, req, res)
|
||||
handle_resource_not_modified_response ("The resource" + orig_path + "does not change", req, res)
|
||||
else
|
||||
compute_response_get (ctx, req, res, l_order)
|
||||
compute_response_get (req, res, l_order)
|
||||
end
|
||||
else
|
||||
handle_resource_not_found_response ("The following resource" + orig_path + " is not found ", ctx, req, res)
|
||||
handle_resource_not_found_response ("The following resource" + orig_path + " is not found ", req, res)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -74,7 +93,7 @@ feature -- HTTP Methods
|
||||
end
|
||||
end
|
||||
|
||||
compute_response_get (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE; l_order : ORDER)
|
||||
compute_response_get (req: WSF_REQUEST; res: WSF_RESPONSE; l_order : ORDER)
|
||||
local
|
||||
h: HTTP_HEADER
|
||||
l_msg : STRING
|
||||
@@ -96,7 +115,7 @@ feature -- HTTP Methods
|
||||
end
|
||||
end
|
||||
|
||||
do_put (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do_put (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Updating a resource with PUT
|
||||
-- A successful PUT request will not create a new resource, instead it will
|
||||
-- change the state of the resource identified by the current uri.
|
||||
@@ -120,16 +139,16 @@ feature -- HTTP Methods
|
||||
if is_valid_to_update(l_order) then
|
||||
if is_conditional_put (req, l_order) then
|
||||
update_order( l_order)
|
||||
compute_response_put (ctx, req, res, l_order)
|
||||
compute_response_put (req, res, l_order)
|
||||
else
|
||||
handle_precondition_fail_response ("", ctx, req, res)
|
||||
handle_precondition_fail_response ("", req, res)
|
||||
end
|
||||
else
|
||||
--| FIXME: Here we need to define the Allow methods
|
||||
handle_resource_conflict_response (l_put +"%N There is conflict while trying to update the order, the order could not be update in the current state", ctx, req, res)
|
||||
handle_resource_conflict_response (l_put +"%N There is conflict while trying to update the order, the order could not be update in the current state", req, res)
|
||||
end
|
||||
else
|
||||
handle_bad_request_response (l_put +"%N is not a valid ORDER, maybe the order does not exist in the system", ctx, req, res)
|
||||
handle_bad_request_response (l_put +"%N is not a valid ORDER, maybe the order does not exist in the system", req, res)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -153,7 +172,7 @@ feature -- HTTP Methods
|
||||
end
|
||||
|
||||
|
||||
compute_response_put (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE; l_order : ORDER)
|
||||
compute_response_put (req: WSF_REQUEST; res: WSF_RESPONSE; l_order : ORDER)
|
||||
local
|
||||
h: HTTP_HEADER
|
||||
joc : JSON_ORDER_CONVERTER
|
||||
@@ -179,7 +198,7 @@ feature -- HTTP Methods
|
||||
end
|
||||
|
||||
|
||||
do_delete (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do_delete (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Here we use DELETE to cancel an order, if that order is in state where
|
||||
-- it can still be canceled.
|
||||
-- 200 if is ok
|
||||
@@ -194,18 +213,18 @@ feature -- HTTP Methods
|
||||
if db_access.orders.has_key (id) then
|
||||
if is_valid_to_delete (id) then
|
||||
delete_order( id)
|
||||
compute_response_delete (ctx, req, res)
|
||||
compute_response_delete (req, res)
|
||||
else
|
||||
--| FIXME: Here we need to define the Allow methods
|
||||
handle_method_not_allowed_response (orig_path + "%N There is conflict while trying to delete the order, the order could not be deleted in the current state", ctx, req, res)
|
||||
handle_method_not_allowed_response (orig_path + "%N There is conflict while trying to delete the order, the order could not be deleted in the current state", req, res)
|
||||
end
|
||||
else
|
||||
handle_resource_not_found_response (orig_path + " not found in this server", ctx, req, res)
|
||||
handle_resource_not_found_response (orig_path + " not found in this server", req, res)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
compute_response_delete (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
compute_response_delete (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
local
|
||||
h : HTTP_HEADER
|
||||
do
|
||||
@@ -218,7 +237,7 @@ feature -- HTTP Methods
|
||||
res.put_header_text (h.string)
|
||||
end
|
||||
|
||||
do_post (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
do_post (req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- Here the convention is the following.
|
||||
-- POST is used for creation and the server determines the URI
|
||||
-- of the created resource.
|
||||
@@ -233,13 +252,13 @@ feature -- HTTP Methods
|
||||
l_post := retrieve_data (req)
|
||||
if attached extract_order_request (l_post) as l_order then
|
||||
save_order (l_order)
|
||||
compute_response_post (ctx, req, res, l_order)
|
||||
compute_response_post (req, res, l_order)
|
||||
else
|
||||
handle_bad_request_response (l_post +"%N is not a valid ORDER", ctx, req, res)
|
||||
handle_bad_request_response (l_post +"%N is not a valid ORDER", req, res)
|
||||
end
|
||||
end
|
||||
|
||||
compute_response_post (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE; l_order : ORDER)
|
||||
compute_response_post (req: WSF_REQUEST; res: WSF_RESPONSE; l_order : ORDER)
|
||||
local
|
||||
h: HTTP_HEADER
|
||||
l_msg : STRING
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
note
|
||||
description : "Objects that ..."
|
||||
author : "$Author$"
|
||||
description : "REST Buck server"
|
||||
date : "$Date$"
|
||||
revision : "$Revision$"
|
||||
|
||||
@@ -30,11 +29,11 @@ feature {NONE} -- Initialization
|
||||
|
||||
setup_router
|
||||
local
|
||||
order_handler: ORDER_HANDLER [WSF_URI_TEMPLATE_HANDLER_CONTEXT]
|
||||
order_handler: ORDER_HANDLER
|
||||
do
|
||||
create order_handler
|
||||
router.map_with_request_methods ("/order", order_handler, <<"POST">>)
|
||||
router.map_with_request_methods ("/order/{orderid}", order_handler, <<"GET", "DELETE", "PUT">>)
|
||||
router.handle_with_request_methods ("/order", order_handler, router.methods_POST)
|
||||
router.handle_with_request_methods ("/order/{orderid}", order_handler, router.methods_GET + router.methods_DELETE + router.methods_PUT)
|
||||
end
|
||||
|
||||
feature -- Execution
|
||||
|
||||
@@ -10,8 +10,12 @@ class
|
||||
inherit
|
||||
ANY
|
||||
|
||||
WSF_ROUTED_SERVICE
|
||||
|
||||
WSF_URI_TEMPLATE_ROUTED_SERVICE
|
||||
|
||||
WSF_URI_ROUTED_SERVICE
|
||||
|
||||
WSF_DEFAULT_SERVICE
|
||||
|
||||
create
|
||||
@@ -34,14 +38,14 @@ feature {NONE} -- Initialization
|
||||
setup_router
|
||||
-- Setup router
|
||||
local
|
||||
www: WSF_FILE_SYSTEM_HANDLER [WSF_URI_TEMPLATE_HANDLER_CONTEXT]
|
||||
www: WSF_FILE_SYSTEM_HANDLER
|
||||
do
|
||||
router.map_agent ("/upload{?nb}", agent execute_upload)
|
||||
map_uri_template_agent ("/upload{?nb}", agent execute_upload)
|
||||
|
||||
create www.make (document_root)
|
||||
www.set_directory_index (<<"index.html">>)
|
||||
www.set_not_found_handler (agent execute_not_found)
|
||||
router.map_with_request_methods ("{/path}{?query}", www, <<"GET">>)
|
||||
router.handle_with_request_methods ("", www, router.methods_GET)
|
||||
end
|
||||
|
||||
feature -- Configuration
|
||||
@@ -59,6 +63,8 @@ feature -- Configuration
|
||||
if Result [Result.count] = Operating_environment.directory_separator then
|
||||
Result := Result.substring (1, Result.count - 1)
|
||||
end
|
||||
ensure
|
||||
not Result.ends_with (Operating_environment.directory_separator.out)
|
||||
end
|
||||
|
||||
files_root: READABLE_STRING_8
|
||||
@@ -79,7 +85,7 @@ feature -- Execution
|
||||
res.redirect_now_with_content (req.script_url ("/"), "Redirection to " + req.script_url ("/"), "text/html")
|
||||
end
|
||||
|
||||
execute_not_found (uri: READABLE_STRING_8; ctx: WSF_URI_TEMPLATE_HANDLER_CONTEXT; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
execute_not_found (uri: READABLE_STRING_8; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
-- `uri' is not found, redirect to default page
|
||||
do
|
||||
res.redirect_now_with_content (req.script_url ("/"), uri + ": not found.%NRedirection to " + req.script_url ("/"), "text/html")
|
||||
|
||||
Reference in New Issue
Block a user