Further use of constants for execution variables

This commit is contained in:
Colin Adams
2013-08-14 09:22:35 +01:00
parent c93e50a7e2
commit 275c26b55b
6 changed files with 83 additions and 58 deletions

View File

@@ -28,6 +28,18 @@ create
make_with_router
feature -- Execution variables
Order_execution_variable: STRING = "ORDER"
-- Execution variable used by application
Generated_content_execution_variable: STRING = "GENERATED_CONTENT"
-- Execution variable used by application
Extracted_order_execution_variable: STRING = "EXTRACTED_ORDER"
-- Execution variable used by application
feature -- Documentation
description: READABLE_STRING_GENERAL
@@ -87,13 +99,6 @@ feature -- Access
Result.compare_objects
end
previous_location (req: WSF_REQUEST): LIST [URI]
-- Previous location(s) for resource named by `req';
do
-- precondition is never met but we need a non-void Result to satisfy the compiler in Void-safe mode:
create {LINKED_LIST [URI]} Result.make
end
age (req: WSF_REQUEST): NATURAL
-- Maximum age in seconds before response to `req` is considered stale;
-- This is used to generate a Cache-Control: max-age header.
@@ -158,7 +163,7 @@ feature -- Access
l_etag_utils: ETAG_UTILS
do
create l_etag_utils
if attached {ORDER} req.execution_variable ("ORDER") as l_order then
if attached {ORDER} req.execution_variable (Order_execution_variable) as l_order then
Result := l_etag_utils.md5_digest (l_order.out)
end
end
@@ -175,7 +180,7 @@ feature -- Measurement
content_length (req: WSF_REQUEST): NATURAL
-- Length of entity-body of the response to `req'
do
check attached {READABLE_STRING_8} req.execution_variable ("GENERATED_CONTENT") as l_response then
check attached {READABLE_STRING_8} req.execution_variable (Generated_content_execution_variable) as l_response then
-- postcondition generated_content_set_for_get_head of `ensure_content_available'
-- We only call this for GET/HEAD in this example.
Result := l_response.count.as_natural_32
@@ -201,7 +206,7 @@ feature -- Execution
check_resource_exists (req: WSF_REQUEST; a_helper: WSF_METHOD_HELPER)
-- Call `a_helper.set_resource_exists' to indicate that `req.path_translated'
-- is the name of an existing resource.
-- We also put the order into `req.execution_variable ("ORDER")' for GET or HEAD responses.
-- We also put the order into `req.execution_variable (Order_execution_variable)' for GET or HEAD responses.
local
l_id: STRING
do
@@ -216,14 +221,14 @@ feature -- Execution
if req.is_get_head_request_method then
check attached db_access.orders.item (l_id) as l_order then
-- postcondition `item_if_found' of `has_key'
req.set_execution_variable ("ORDER", l_order)
req.set_execution_variable (Order_execution_variable, l_order)
end
end
end
end
ensure then
order_saved_only_for_get_head: req.is_get_head_request_method =
attached {ORDER} req.execution_variable ("ORDER")
attached {ORDER} req.execution_variable (Order_execution_variable)
end
feature -- GET/HEAD content
@@ -234,27 +239,27 @@ feature -- GET/HEAD content
-- for a subsequent call to `content'.
-- If chunked, only the first chunk will be made available to `next_chunk'. If chunk extensions
-- are used, then this will also generate the chunk extension for the first chunk.
-- We save the text in `req.execution_variable ("GENERATED_CONTENT")'
-- We save the text in `req.execution_variable (Generated_content_execution_variable)'
-- We ignore the results of content negotiation, as there is only one possible combination.
do
check attached {ORDER} req.execution_variable ("ORDER") as l_order then
check attached {ORDER} req.execution_variable (Order_execution_variable) as l_order then
-- precondition get_or_head and postcondition order_saved_only_for_get_head of `check_resource_exists' and
if attached {JSON_VALUE} json.value (l_order) as jv then
req.set_execution_variable ("GENERATED_CONTENT", jv.representation)
req.set_execution_variable (Generated_content_execution_variable, jv.representation)
else
req.set_execution_variable ("GENERATED_CONTENT", "")
req.set_execution_variable (Generated_content_execution_variable, "")
end
end
ensure then
generated_content_set_for_get_head: req.is_get_head_request_method implies
attached {READABLE_STRING_8} req.execution_variable ("GENERATED_CONTENT")
attached {READABLE_STRING_8} req.execution_variable (Generated_content_execution_variable)
end
content (req: WSF_REQUEST): READABLE_STRING_8
-- Non-chunked entity body in response to `req';
-- We only call this for GET/HEAD in this example.
do
check attached {READABLE_STRING_8} req.execution_variable ("GENERATED_CONTENT") as l_response then
check attached {READABLE_STRING_8} req.execution_variable (Generated_content_execution_variable) as l_response then
-- postcondition generated_content_set_for_get_head of `ensure_content_available'
Result := l_response
end
@@ -307,14 +312,14 @@ feature -- DELETE
feature -- PUT/POST
is_entity_too_large (req: WSF_REQUEST): BOOLEAN
-- Is the entity stored in `req.execution_variable ("REQUEST_ENTITY")' too large for the application?
-- Is the entity stored in `req.execution_variable (Request_entity_execution_variable)' too large for the application?
do
-- No. We don't care for this example.
end
check_content_headers (req: WSF_REQUEST)
-- Check we can support all content headers on request entity.
-- Set `req.execution_variable ("CONTENT_CHECK_CODE")' to {NATURAL} zero if OK, or 415 or 501 if not.
-- Set `req.execution_variable (Content_check_code_execution_variable)' to {NATURAL} zero if OK, or 415 or 501 if not.
do
-- We don't bother for this example. Note that this is equivalent to setting zero.
end
@@ -331,7 +336,7 @@ feature -- PUT/POST
-- Create new resource in response to a POST request.
-- Implementor must set error code of 200 OK or 204 No Content or 303 See Other or 500 Server Error.
do
if attached {ORDER} req.execution_variable ("EXTRACTED_ORDER") as l_order then
if attached {ORDER} req.execution_variable (Extracted_order_execution_variable) as l_order then
save_order (l_order)
compute_response_post (req, res, l_order)
else
@@ -341,16 +346,16 @@ feature -- PUT/POST
check_conflict (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Check we can support all content headers on request entity.
-- Set `req.execution_variable ("CONFLICT_CHECK_CODE")' to {NATURAL} zero if OK, or 409 if not.
-- Set `req.execution_variable (Conflict_check_code_execution_variable)' to {NATURAL} zero if OK, or 409 if not.
-- In the latter case, write the full error response to `res'.
do
if attached {ORDER} req.execution_variable ("EXTRACTED_ORDER") as l_order then
if attached {ORDER} req.execution_variable (Extracted_order_execution_variable) as l_order then
if not is_valid_to_update (l_order) then
req.set_execution_variable ("CONFLICT_CHECK_CODE", {NATURAL} 409)
req.set_execution_variable (Conflict_check_code_execution_variable, {NATURAL} 409)
handle_resource_conflict_response (l_order.out +"%N There is conflict while trying to update the order, the order could not be update in the current state", req, res)
end
else
req.set_execution_variable ("CONFLICT_CHECK_CODE", {NATURAL} 409)
req.set_execution_variable (Conflict_check_code_execution_variable, {NATURAL} 409)
--| This ought to be a 500, as if attached should probably be check attached. But as yet I lack a proof.
handle_resource_conflict_response ("There is conflict while trying to update the order, the order could not be update in the current state", req, res)
end
@@ -358,31 +363,31 @@ feature -- PUT/POST
check_request (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Check that the request entity is a valid request.
-- The entity is available as `req.execution_variable ("REQUEST_ENTITY")'.
-- Set `req.execution_variable ("REQUEST_CHECK_CODE")' to {NATURAL} zero if OK, or 400 if not.
-- The entity is available as `req.execution_variable (Conflict_check_code_execution_variable)'.
-- Set `req.execution_variable (Request_check_code_execution_variable)' to {NATURAL} zero if OK, or 400 if not.
-- In the latter case, write the full error response to `res'.
local
l_order: detachable ORDER
l_id: STRING
do
if attached {READABLE_STRING_8} req.execution_variable ("REQUEST_ENTITY") as l_request then
if attached {READABLE_STRING_8} req.execution_variable (Request_entity_execution_variable) as l_request then
l_order := extract_order_request (l_request)
if req.is_put_request_method then
l_id := order_id_from_request (req)
if l_order /= Void and then db_access.orders.has_key (l_id) then
l_order.set_id (l_id)
req.set_execution_variable ("REQUEST_CHECK_CODE", {NATURAL} 0)
req.set_execution_variable ("EXTRACTED_ORDER", l_order)
req.set_execution_variable (Request_check_code_execution_variable, {NATURAL} 0)
req.set_execution_variable (Extracted_order_execution_variable, l_order)
else
req.set_execution_variable ("REQUEST_CHECK_CODE", {NATURAL} 400)
req.set_execution_variable (Request_check_code_execution_variable, {NATURAL} 400)
handle_bad_request_response (l_request +"%N is not a valid ORDER, maybe the order does not exist in the system", req, res)
end
else
req.set_execution_variable ("REQUEST_CHECK_CODE", {NATURAL} 0)
req.set_execution_variable ("EXTRACTED_ORDER", l_order)
req.set_execution_variable (Request_check_code_execution_variable, {NATURAL} 0)
req.set_execution_variable (Extracted_order_execution_variable, l_order)
end
else
req.set_execution_variable ("REQUEST_CHECK_CODE", {NATURAL} 400)
req.set_execution_variable (Request_check_code_execution_variable, {NATURAL} 400)
handle_bad_request_response ("Request is not a valid ORDER", req, res)
end
end
@@ -391,7 +396,7 @@ feature -- PUT/POST
-- Perform the update requested in `req'.
-- Write a response to `res' with a code of 204 or 500.
do
if attached {ORDER} req.execution_variable ("EXTRACTED_ORDER") as l_order then
if attached {ORDER} req.execution_variable (Extracted_order_execution_variable) as l_order then
update_order (l_order)
compute_response_put (req, res, l_order)
else

View File

@@ -58,7 +58,7 @@ feature -- Basic operations
l_locs := a_handler.previous_location (req)
handle_redirection_error (req, res, l_locs, {HTTP_STATUS_CODE}.found)
else
check attached {HTTP_HEADER} req.execution_variable ("NEGOTIATED_HTTP_HEADER") as h then
check attached {HTTP_HEADER} req.execution_variable (a_handler.Negotiated_http_header_execution_variable) as h then
-- postcondition header_attached of `handle_content_negotiation'
h.put_content_type_text_plain
h.put_current_date
@@ -68,7 +68,7 @@ feature -- Basic operations
end
end
else
check attached {HTTP_HEADER} req.execution_variable ("NEGOTIATED_HTTP_HEADER") as h then
check attached {HTTP_HEADER} req.execution_variable (a_handler.Negotiated_http_header_execution_variable) as h then
-- postcondition header_attached of `handle_content_negotiation'
h.put_content_type_text_plain
h.put_current_date
@@ -140,7 +140,7 @@ feature -- Basic operations
end
end
if not l_failed then
check attached {HTTP_HEADER} req.execution_variable ("NEGOTIATED_HTTP_HEADER") as h then
check attached {HTTP_HEADER} req.execution_variable (a_handler.Negotiated_http_header_execution_variable) as h then
-- postcondition header_attached of `handle_content_negotiation'
send_response (req, res, a_handler, h, False)
end

View File

@@ -22,7 +22,7 @@ feature -- Basic operations
-- Policy routines are available in `a_handler'.
do
if a_handler.allow_post_to_missing_resource (req) then
check attached {HTTP_HEADER} req.execution_variable ("NEGOTIATED_HTTP_HEADER") as h then
check attached {HTTP_HEADER} req.execution_variable (a_handler.Negotiated_http_header_execution_variable) as h then
-- postcondition header_attached of `handle_content_negotiation'
send_response (req, res, a_handler, h, True)
end

View File

@@ -43,10 +43,11 @@ feature -- Access
req_attached: req /= Void
previously_existed: resource_previously_existed (req)
moved: resource_moved_permanently (req) or resource_moved_temporarily (req)
deferred
do
create {LINKED_LIST [URI]} Result.make
ensure
previous_location_attached: Result /= Void
non_empty_list: not Result.empty
non_empty_list: not Result.is_empty
end
note

View File

@@ -24,7 +24,7 @@ feature -- Basic operations
if a_handler.treat_as_moved_permanently (req) then
handle_redirection_error (req, res, a_handler.previous_location (req), {HTTP_STATUS_CODE}.moved_permanently)
else
check attached {HTTP_HEADER} req.execution_variable ("NEGOTIATED_HTTP_HEADER") as h then
check attached {HTTP_HEADER} req.execution_variable (a_handler.Negotiated_http_header_execution_variable) as h then
-- postcondition header_attached of `handle_content_negotiation'
send_response (req, res, a_handler, h, True)
end
@@ -69,4 +69,14 @@ feature {NONE} -- Implementation
end
end
note
copyright: "2011-2013, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software
5949 Hollister Ave., Goleta, CA 93117 USA
Telephone 805-685-1006, Fax 805-685-6869
Website http://www.eiffel.com
Customer support http://support.eiffel.com
]"
end

View File

@@ -46,6 +46,15 @@ feature -- Execution variables
Request_entity_execution_variable: STRING = "REQUEST_ENTITY"
-- Execution variable set by framework
Conflict_check_code_execution_variable: STRING = "CONFLICT_CHECK_CODE"
-- Execution variable set by framework
Content_check_code_execution_variable: STRING = "CONTENT_CHECK_CODE"
-- Execution variable set by framework
Request_check_code_execution_variable: STRING = "REQUEST_CHECK_CODE"
-- Execution variable set by framework
feature -- Access
is_chunking (req: WSF_REQUEST): BOOLEAN
@@ -329,7 +338,7 @@ feature -- PUT/POST
end
is_entity_too_large (req: WSF_REQUEST): BOOLEAN
-- Is the entity stored in `req.execution_variable ("REQUEST_ENTITY")' too large for the application?
-- Is the entity stored in `req.execution_variable (Request_entity_execution_variable)' too large for the application?
require
req_attached: req /= Void
deferred
@@ -337,7 +346,7 @@ feature -- PUT/POST
check_content_headers (req: WSF_REQUEST)
-- Check we can support all content headers on request entity.
-- Set `req.execution_variable ("CONTENT_CHECK_CODE")' to {NATURAL} zero if OK, or 415 or 501 if not.
-- Set `req.execution_variable (Content_check_code_execution_variable)' to {NATURAL} zero if OK, or 415 or 501 if not.
require
req_attached: req /= Void
deferred
@@ -348,7 +357,7 @@ feature -- PUT/POST
require
req_attached: req /= Void
do
if attached {NATURAL} req.execution_variable ("CONTENT_CHECK_CODE") as l_code then
if attached {NATURAL} req.execution_variable (Content_check_code_execution_variable) as l_code then
Result := l_code
end
end
@@ -375,7 +384,7 @@ feature -- PUT/POST
check_conflict (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Check to see if updating the resource is problematic due to the current state of the resource.
-- Set `req.execution_variable ("CONFLICT_CHECK_CODE")' to {NATURAL} zero if OK, or 409 if not.
-- Set `req.execution_variable (Conflict_check_code_execution_variable)' to {NATURAL} zero if OK, or 409 if not.
-- In the latter case, write the full error response to `res'.
require
req_attached: req /= Void
@@ -388,15 +397,15 @@ feature -- PUT/POST
require
req_attached: req /= Void
do
if attached {NATURAL} req.execution_variable ("CONFLICT_CHECK_CODE") as l_code then
if attached {NATURAL} req.execution_variable (Conflict_check_code_execution_variable) as l_code then
Result := l_code
end
end
check_request (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Check that the request entity is a valid request.
-- The entity is available as `req.execution_variable ("REQUEST_ENTITY")'.
-- Set `req.execution_variable ("REQUEST_CHECK_CODE")' to {NATURAL} zero if OK, or 400 if not.
-- The entity is available as `req.execution_variable (Conflict_check_code_execution_variable)'.
-- Set `req.execution_variable (Request_check_code_execution_variable)' to {NATURAL} zero if OK, or 400 if not.
-- In the latter case, write the full error response to `res'.
require
req_attached: req /= Void
@@ -410,7 +419,7 @@ feature -- PUT/POST
require
req_attached: req /= Void
do
if attached {NATURAL} req.execution_variable ("REQUEST_CHECK_CODE") as l_code then
if attached {NATURAL} req.execution_variable (Request_check_code_execution_variable) as l_code then
Result := l_code
end
end