From abdd68863da6bf1eb257c330ba3794cce14dbdbf Mon Sep 17 00:00:00 2001 From: Jocelyn Fiat Date: Fri, 20 Jan 2012 15:35:03 +0100 Subject: [PATCH] Removed any "format" related query from router lib, this is too application specific to be there. Better handling of base_url for REQUEST_ROUTER --- .../rest/src/rest_request_handler_context.e | 110 +++++++++++++++++- .../rest_request_uri_template_router_i.e | 5 +- ...t_request_uri_template_routing_handler_i.e | 5 +- .../wsf/router/request_handler_context.e | 70 ++--------- library/server/wsf/router/request_router.e | 27 ++++- .../wsf/router/request_routing_handler.e | 24 ++++ .../wsf/router/uri/request_uri_router_i.e | 11 +- .../default/request_uri_template_router.e | 3 +- .../request_uri_template_router_i.e | 11 +- .../request_uri_template_routing_handler_i.e | 10 +- 10 files changed, 206 insertions(+), 70 deletions(-) diff --git a/draft/library/server/request/rest/src/rest_request_handler_context.e b/draft/library/server/request/rest/src/rest_request_handler_context.e index 395872e3..320150ca 100644 --- a/draft/library/server/request/rest/src/rest_request_handler_context.e +++ b/draft/library/server/request/rest/src/rest_request_handler_context.e @@ -10,7 +10,115 @@ deferred class inherit REQUEST_HANDLER_CONTEXT -note +feature -- Accept: Content-Type + + accepted_content_type: detachable READABLE_STRING_8 + do + if internal_accepted_content_type = Void then + get_accepted_content_type (Void) + end + Result := internal_accepted_content_type + end + + get_accepted_content_type (a_supported_content_types: detachable ARRAY [STRING_8]) + do + if internal_accepted_content_type = Void then + internal_accepted_content_type := request_accepted_content_type (a_supported_content_types) + end + end + +feature -- Format name + + accepted_format_name: detachable READABLE_STRING_8 + do + if internal_accepted_format_name = Void then + get_accepted_format_name (Void, Void) + end + Result := internal_accepted_format_name + end + + get_accepted_format_name (a_format_variable_name: detachable READABLE_STRING_8; a_supported_content_types: detachable ARRAY [STRING_8]) + do + if internal_accepted_format_name = Void then + internal_accepted_format_name := request_accepted_format (a_format_variable_name, a_supported_content_types) + end + end + +feature -- Format id + + accepted_format_id: INTEGER + do + if internal_accepted_format_id = 0 then + get_accepted_format_id (Void, Void) + end + Result := internal_accepted_format_id + end + + get_accepted_format_id (a_format_variable_name: detachable READABLE_STRING_8; a_supported_content_types: detachable ARRAY [STRING_8]) + do + if internal_accepted_format_id = 0 then + internal_accepted_format_id := request_accepted_format_id (a_format_variable_name, a_supported_content_types) + end + end + +feature {NONE} -- Constants + + Format_constants: HTTP_FORMAT_CONSTANTS + once + create Result + end + +feature -- Format + + request_accepted_format (a_format_variable_name: detachable READABLE_STRING_8; a_supported_content_types: 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 := request_format_from_content_type (request_accepted_content_type (a_supported_content_types)) + end + end + + request_accepted_format_id (a_format_variable_name: detachable READABLE_STRING_8; a_supported_content_types: detachable ARRAY [READABLE_STRING_8]): INTEGER + -- Format id for the request based on {HTTP_FORMAT_CONSTANTS} + do + if attached request_accepted_format (a_format_variable_name, a_supported_content_types) as l_format then + Result := Format_constants.format_id (l_format) + else + Result := 0 + end + end + +feature {NONE} -- Format/Content type implementation + + request_format_from_content_type (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 + +feature {NONE} -- Internal + + internal_accepted_content_type: like accepted_content_type + + internal_accepted_format_id: like accepted_format_id + + internal_accepted_format_name: like accepted_format_name + +;note copyright: "Copyright (c) 1984-2012, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" source: "[ diff --git a/draft/library/server/request/rest/src/uri_template/rest_request_uri_template_router_i.e b/draft/library/server/request/rest/src/uri_template/rest_request_uri_template_router_i.e index 14987a13..9f3a87e3 100644 --- a/draft/library/server/request/rest/src/uri_template/rest_request_uri_template_router_i.e +++ b/draft/library/server/request/rest/src/uri_template/rest_request_uri_template_router_i.e @@ -13,10 +13,11 @@ inherit REST_REQUEST_ROUTER [H, C] create - make + make, + make_with_base_url note - copyright: "Copyright (c) 1984-2011, Eiffel Software and others" + copyright: "Copyright (c) 1984-2012, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" source: "[ Eiffel Software diff --git a/draft/library/server/request/rest/src/uri_template/rest_request_uri_template_routing_handler_i.e b/draft/library/server/request/rest/src/uri_template/rest_request_uri_template_routing_handler_i.e index 13bca6bb..fa1e6d36 100644 --- a/draft/library/server/request/rest/src/uri_template/rest_request_uri_template_routing_handler_i.e +++ b/draft/library/server/request/rest/src/uri_template/rest_request_uri_template_routing_handler_i.e @@ -21,7 +21,8 @@ inherit end create - make + make, + make_with_base_url feature -- Status report @@ -53,7 +54,7 @@ feature {NONE} -- Routing router: REST_REQUEST_URI_TEMPLATE_ROUTER_I [H, C] ;note - copyright: "Copyright (c) 1984-2011, Eiffel Software and others" + copyright: "Copyright (c) 1984-2012, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" source: "[ Eiffel Software diff --git a/library/server/wsf/router/request_handler_context.e b/library/server/wsf/router/request_handler_context.e index cf5f0b82..2863b78c 100644 --- a/library/server/wsf/router/request_handler_context.e +++ b/library/server/wsf/router/request_handler_context.e @@ -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 diff --git a/library/server/wsf/router/request_router.e b/library/server/wsf/router/request_router.e index d83d95d4..58e91146 100644 --- a/library/server/wsf/router/request_router.e +++ b/library/server/wsf/router/request_router.e @@ -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]]] diff --git a/library/server/wsf/router/request_routing_handler.e b/library/server/wsf/router/request_routing_handler.e index d4519ef2..053db16e 100644 --- a/library/server/wsf/router/request_routing_handler.e +++ b/library/server/wsf/router/request_routing_handler.e @@ -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) diff --git a/library/server/wsf/router/uri/request_uri_router_i.e b/library/server/wsf/router/uri/request_uri_router_i.e index c7cb069e..de620233 100644 --- a/library/server/wsf/router/uri/request_uri_router_i.e +++ b/library/server/wsf/router/uri/request_uri_router_i.e @@ -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]) diff --git a/library/server/wsf/router/uri_template/default/request_uri_template_router.e b/library/server/wsf/router/uri_template/default/request_uri_template_router.e index 3fef77bf..b6718ca7 100644 --- a/library/server/wsf/router/uri_template/default/request_uri_template_router.e +++ b/library/server/wsf/router/uri_template/default/request_uri_template_router.e @@ -14,7 +14,8 @@ inherit end create - make + make, + make_with_base_url feature -- Mapping diff --git a/library/server/wsf/router/uri_template/request_uri_template_router_i.e b/library/server/wsf/router/uri_template/request_uri_template_router_i.e index 3faa32ba..046b60d1 100644 --- a/library/server/wsf/router/uri_template/request_uri_template_router_i.e +++ b/library/server/wsf/router/uri_template/request_uri_template_router_i.e @@ -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) diff --git a/library/server/wsf/router/uri_template/request_uri_template_routing_handler_i.e b/library/server/wsf/router/uri_template/request_uri_template_routing_handler_i.e index 32fd135c..02764a68 100644 --- a/library/server/wsf/router/uri_template/request_uri_template_routing_handler_i.e +++ b/library/server/wsf/router/uri_template/request_uri_template_routing_handler_i.e @@ -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]