Removed any "format" related query from router lib, this is too application specific to be there.

Better handling of base_url for REQUEST_ROUTER
This commit is contained in:
Jocelyn Fiat
2012-01-20 15:35:03 +01:00
parent 83a8ad3879
commit abdd68863d
10 changed files with 206 additions and 70 deletions

View File

@@ -66,82 +66,32 @@ feature -- Url Query
result_attached: Result /= Void
end
feature {NONE} -- Constants
Format_constants: HTTP_FORMAT_CONSTANTS
once
create Result
end
feature -- Query
request_format (a_format_variable_name: detachable READABLE_STRING_8; content_type_supported: detachable ARRAY [READABLE_STRING_8]): detachable READABLE_STRING_8
-- Format id for the request based on {HTTP_FORMAT_CONSTANTS}
do
if a_format_variable_name /= Void and then attached string_parameter (a_format_variable_name) as ctx_format then
Result := ctx_format.as_string_8
else
Result := content_type_to_request_format (request_content_type (content_type_supported))
end
end
request_format_id (a_format_variable_name: detachable READABLE_STRING_8; content_type_supported: detachable ARRAY [READABLE_STRING_8]): INTEGER
-- Format id for the request based on {HTTP_FORMAT_CONSTANTS}
do
if attached request_format (a_format_variable_name, content_type_supported) as l_format then
Result := Format_constants.format_id (l_format)
else
Result := 0
end
end
content_type_to_request_format (a_content_type: detachable READABLE_STRING_8): detachable READABLE_STRING_8
-- `a_content_type' converted into a request format name
do
if a_content_type /= Void then
if a_content_type.same_string ({HTTP_MIME_TYPES}.text_json) then
Result := {HTTP_FORMAT_CONSTANTS}.json_name
elseif a_content_type.same_string ({HTTP_MIME_TYPES}.application_json) then
Result := {HTTP_FORMAT_CONSTANTS}.json_name
elseif a_content_type.same_string ({HTTP_MIME_TYPES}.text_xml) then
Result := {HTTP_FORMAT_CONSTANTS}.xml_name
elseif a_content_type.same_string ({HTTP_MIME_TYPES}.text_html) then
Result := {HTTP_FORMAT_CONSTANTS}.html_name
elseif a_content_type.same_string ({HTTP_MIME_TYPES}.text_plain) then
Result := {HTTP_FORMAT_CONSTANTS}.text_name
end
end
end
request_content_type (content_type_supported: detachable ARRAY [READABLE_STRING_8]): detachable READABLE_STRING_8
request_accepted_content_type (a_supported_content_types: detachable ARRAY [READABLE_STRING_8]): detachable READABLE_STRING_8
-- Accepted content-type for the request, among the supported content types `a_supported_content_types'
local
s: detachable READABLE_STRING_8
i,n: INTEGER
l_accept_lst: detachable ARRAYED_LIST [READABLE_STRING_8]
do
l_accept_lst := accepted_content_types (request)
if attached request.content_type as ct then
if l_accept_lst /= Void then
l_accept_lst.put_front (ct)
else
Result := ct
end
end
if Result = Void and l_accept_lst /= Void then
if
attached accepted_content_types (request) as l_accept_lst and then
not l_accept_lst.is_empty
then
from
l_accept_lst.start
until
l_accept_lst.after or Result /= Void
loop
s := l_accept_lst.item
if content_type_supported /= Void then
if a_supported_content_types /= Void then
from
i := content_type_supported.lower
n := content_type_supported.upper
i := a_supported_content_types.lower
n := a_supported_content_types.upper
until
i > n or Result /= Void
loop
if content_type_supported[i].same_string (s) then
if a_supported_content_types [i].same_string (s) then
Result := s
end
i := i + 1

View File

@@ -60,10 +60,19 @@ feature -- Base url
base_url: detachable READABLE_STRING_8
-- Common start of any route url
feature -- Element change
set_base_url (a_base_url: like base_url)
-- Set `base_url' to `a_base_url'
-- make sure no map is already added (i.e: count = 0)
require
no_handler_set: count = 0
do
base_url := a_base_url
if a_base_url = Void or else a_base_url.is_empty then
base_url := Void
else
base_url := a_base_url
end
end
feature -- Execution
@@ -100,6 +109,22 @@ feature -- Execution
result_void_implie_no_default: Result = Void implies default_handler = Void
end
feature -- status report
count: INTEGER
-- Count of maps handled by current
do
across
Current as curs
loop
if attached {REQUEST_ROUTING_HANDLER [H, C]} curs.item.handler as rh then
Result := Result + rh.count + 1 --| +1 for the handler itself
else
Result := Result + 1
end
end
end
feature -- Traversing
new_cursor: ITERATION_CURSOR [TUPLE [handler: H; resource: READABLE_STRING_8; request_methods: detachable ARRAY [READABLE_STRING_8]]]

View File

@@ -11,6 +11,30 @@ deferred class
inherit
REQUEST_HANDLER [C]
feature -- Access
count: INTEGER
-- Count of maps handled by current
do
Result := router.count
end
base_url: detachable READABLE_STRING_8
do
Result := router.base_url
end
feature -- Element change
set_base_url (a_base_url: like base_url)
-- Set `base_url' to `a_base_url'
-- make sure no map is already added (i.e: count = 0)
require
no_handler_set: count = 0
do
router.set_base_url (a_base_url)
end
feature -- Execution
execute (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)

View File

@@ -11,7 +11,8 @@ inherit
REQUEST_ROUTER [H, C]
create
make
make,
make_with_base_url
feature -- Initialization
@@ -21,6 +22,14 @@ feature -- Initialization
handlers.compare_objects
end
make_with_base_url (n: INTEGER; a_base_url: like base_url)
-- Make router allocated for at least `n' maps,
-- and use `a_base_url' as base_url
do
make (n)
set_base_url (a_base_url)
end
feature -- Registration
map_with_request_methods (p: READABLE_STRING_8; h: H; rqst_methods: detachable ARRAY [READABLE_STRING_8])

View File

@@ -14,7 +14,8 @@ inherit
end
create
make
make,
make_with_base_url
feature -- Mapping

View File

@@ -11,7 +11,8 @@ inherit
REQUEST_ROUTER [H, C]
create
make
make,
make_with_base_url
feature -- Initialization
@@ -22,6 +23,14 @@ feature -- Initialization
handlers.compare_objects
end
make_with_base_url (n: INTEGER; a_base_url: like base_url)
-- Make router allocated for at least `n' maps,
-- and use `a_base_url' as base_url
do
make (n)
set_base_url (a_base_url)
end
feature -- Registration
map_with_uri_template (uri: URI_TEMPLATE; h: H)

View File

@@ -12,7 +12,8 @@ inherit
REQUEST_ROUTING_HANDLER [H, C]
create
make
make,
make_with_base_url
feature {NONE} -- Initialization
@@ -21,6 +22,13 @@ feature {NONE} -- Initialization
create router.make (n)
end
make_with_base_url (n: INTEGER; a_base_url: like base_url)
-- Make allocated for at least `n' maps,
-- and use `a_base_url' as base_url
do
create router.make_with_base_url (n, a_base_url)
end
feature {NONE} -- Routing
router: REQUEST_URI_TEMPLATE_ROUTER_I [H, C]