Created wsf_extension, and moved some classes from wsf to wsf_extension

WSF_HANDLER_HELPER
   WSF_RESOURCE_HANDLER_HELPER
   WSF_HANDLER_ROUTES_RECORDER
This commit is contained in:
Jocelyn Fiat
2012-03-16 14:12:02 +01:00
parent eb3e9f2186
commit f69ff42564
11 changed files with 48 additions and 11 deletions

View File

@@ -60,6 +60,8 @@ feature -- Access
feature {NONE} -- Implementation
string_in_array (arr: ARRAY [STRING]; s: STRING): BOOLEAN
-- Is `s' in array `arr' ?
--| using `{STRING}.same_string (..)'
local
i,n: INTEGER
do

View File

@@ -1,96 +0,0 @@
note
description: "[
Provides a few helpful feature to respond predefined message to the client
]"
date: "$Date$"
revision: "$Revision$"
class
WSF_HANDLER_HELPER
inherit
ANY
feature -- Helper
execute_content_type_not_allowed (req: WSF_REQUEST; res: WSF_RESPONSE; a_content_types: detachable ARRAY [STRING]; a_uri_formats: detachable ARRAY [STRING])
local
accept_s, uri_s: detachable STRING
i, n: INTEGER
do
if a_content_types /= Void then
create accept_s.make (10)
from
i := a_content_types.lower
n := a_content_types.upper
until
i > n
loop
accept_s.append_string (a_content_types[i])
if i < n then
accept_s.append_character (',')
accept_s.append_character (' ')
end
i := i + 1
end
else
accept_s := "*/*"
end
if a_uri_formats /= Void then
create uri_s.make (10)
from
i := a_uri_formats.lower
n := a_uri_formats.upper
until
i > n
loop
uri_s.append_string (a_uri_formats[i])
if i < n then
uri_s.append_character (',')
uri_s.append_character (' ')
end
i := i + 1
end
end
res.put_header ({HTTP_STATUS_CODE}.unsupported_media_type, << ["Content-Type", "text/plain"], ["Accept", accept_s]>>)
if accept_s /= Void then
res.put_string ("Unsupported request content-type, Accept: " + accept_s + "%N")
end
if uri_s /= Void then
res.put_string ("Unsupported request format from the URI: " + uri_s + "%N")
end
end
execute_request_method_not_allowed (req: WSF_REQUEST; res: WSF_RESPONSE; a_methods: ITERABLE [STRING])
local
s: STRING
do
create s.make (25)
across
a_methods as c
loop
if not s.is_empty then
s.append_character (',')
s.append_character (' ')
end
s.append_string (c.item)
end
res.put_header ({HTTP_STATUS_CODE}.method_not_allowed, <<
["Content-Type", {HTTP_MIME_TYPES}.text_plain],
["Allow", s]
>>)
res.put_string ("Unsupported request method, Allow: " + s + "%N")
end
note
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, 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

@@ -1,44 +0,0 @@
note
description: "[
Summary description for WSF_HANDLER_ROUTES_RECORDER.
You can inherit from this class from any WSF_HANDLER and redefine `on_handler_mapped'
to record the available routes if your handler needs it.
]"
author: ""
date: "$Date$"
revision: "$Revision$"
deferred class
WSF_HANDLER_ROUTES_RECORDER
feature {WSF_HANDLER} -- Routes access
available_routes: detachable LIST [TUPLE [resource: READABLE_STRING_8; rqst_methods: detachable ARRAY [READABLE_STRING_8]]]
-- Available routes
feature {WSF_ROUTER} -- Routes change
on_handler_mapped (a_resource: READABLE_STRING_8; a_rqst_methods: detachable ARRAY [READABLE_STRING_8])
local
l_routes: like available_routes
do
l_routes := available_routes
if l_routes = Void then
create {ARRAYED_LIST [like available_routes.item]} l_routes.make (3)
available_routes := l_routes
end
l_routes.force ([a_resource, a_rqst_methods])
end
note
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, 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

@@ -1,313 +0,0 @@
note
description: "Work in progress Common abstraction to handle RESTfull methods"
author: ""
date: "$Date$"
revision: "$Revision$"
class
WSF_RESOURCE_HANDLER_HELPER [C -> WSF_HANDLER_CONTEXT]
feature -- Execute template
execute_methods (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute request and dispatch according to the request method
local
m: READABLE_STRING_8
do
m := req.request_method.as_upper
if m.same_string ({HTTP_REQUEST_METHODS}.method_get) then
execute_get (ctx, req, res)
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_put) then
execute_put (ctx, req, res)
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_delete) then
execute_delete (ctx, req, res)
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_post) then
execute_post (ctx, req, res)
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_trace) then
execute_trace (ctx, req, res)
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_options) then
execute_options (ctx, req, res)
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_head) then
execute_head (ctx, req, res)
elseif m.same_string ({HTTP_REQUEST_METHODS}.method_connect) then
execute_connect (ctx, req, res)
else
--| Eventually handle other methods...
execute_extension_method (ctx, req, res)
end
rescue
handle_internal_server_error ("Internal Server Error", ctx, req, res)
end
feature -- Method Post
execute_post (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
if req.is_chunked_input then
do_post (ctx, req, res)
else
if req.content_length_value > 0 then
do_post (ctx, req, res)
else
handle_bad_request_response ("Bad request, content_length empty", ctx, req, res)
end
end
end
do_post (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
handle_not_implemented ("Method POST not implemented", ctx, req, res)
end
feature-- Method Put
execute_put (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
if req.is_chunked_input then
do_put (ctx, req, res)
else
if req.content_length_value > 0 then
do_put (ctx, req, res)
else
handle_bad_request_response ("Bad request, content_length empty", ctx, req, res)
end
end
end
do_put (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
handle_not_implemented ("Method PUT not implemented", ctx, req, res)
end
feature -- Method Get
execute_get (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
do_get (ctx, req, res)
end
do_get (ctx: C;req: WSF_REQUEST; res: WSF_RESPONSE)
do
handle_not_implemented ("Method HEAD not implemented", ctx, req, res)
end
feature -- Method DELETE
execute_delete (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
do_delete (ctx, req, res)
end
do_delete (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
handle_not_implemented ("Method DELETE not implemented", ctx, req, res)
end
feature -- Method CONNECT
execute_connect (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
do_connect (ctx, req, res)
end
do_connect (ctx: C;req: WSF_REQUEST; res: WSF_RESPONSE)
do
handle_not_implemented ("Method CONNECT not implemented", ctx, req, res)
end
feature -- Method HEAD
execute_head (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
do_head (ctx, req, res)
end
do_head (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
handle_not_implemented ("Method HEAD not implemented", ctx, req, res)
end
feature -- Method OPTIONS
execute_options (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
do_options (ctx, req, res)
end
do_options (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
handle_not_implemented ("Method OPTIONS not implemented", ctx, req, res)
end
feature -- Method TRACE
execute_trace (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
do_trace (ctx, req, res)
end
do_trace (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
handle_not_implemented ("Method TRACE not implemented", ctx, req, res)
end
feature -- Method Extension Method
execute_extension_method (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
do_extension_method (ctx, req, res)
end
do_extension_method (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
do
handle_not_implemented ("Method extension-method not implemented", ctx, req, res)
end
feature -- Retrieve content from WGI_INPUT_STREAM
retrieve_data ( req : WSF_REQUEST) : STRING
-- retrieve the content from the input stream
-- handle differents transfers
do
Result := ""
if req.is_chunked_input then
if attached req.chunked_input as l_chunked_input then
Result := l_chunked_input.data.as_string_8
end
else
req.input.read_string (req.content_length_value.as_integer_32)
Result := req.input.last_string
end
end
feature -- Handle responses
-- TODO Handle Content negotiation.
-- The option : Server-driven negotiation: uses request headers to select a variant
-- More info : http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html#sec12
supported_content_types: detachable ARRAY [READABLE_STRING_8]
-- Supported content types
-- Can be redefined
do
Result := Void
end
handle_bad_request_response (a_description: STRING; ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE )
local
h : HTTP_HEADER
do
create h.make
h.put_content_type_application_json
h.put_content_length (a_description.count)
h.put_current_date
res.set_status_code ({HTTP_STATUS_CODE}.bad_request)
res.put_header_text (h.string)
res.put_string (a_description)
end
handle_precondition_fail_response (a_description: STRING; ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE )
local
h : HTTP_HEADER
do
create h.make
h.put_content_type_application_json
h.put_content_length (a_description.count)
h.put_current_date
res.set_status_code ({HTTP_STATUS_CODE}.precondition_failed)
res.put_header_text (h.string)
res.put_string (a_description)
end
handle_internal_server_error (a_description: STRING; ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE )
local
h : HTTP_HEADER
do
create h.make
h.put_content_type_application_json
h.put_content_length (a_description.count)
h.put_current_date
res.set_status_code ({HTTP_STATUS_CODE}.internal_server_error)
res.put_header_text (h.string)
res.put_string (a_description)
end
handle_not_implemented (a_description: STRING; ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE )
local
h : HTTP_HEADER
do
create h.make
h.put_content_type_application_json
h.put_content_length (a_description.count)
h.put_current_date
res.set_status_code ({HTTP_STATUS_CODE}.not_implemented)
res.put_header_text (h.string)
res.put_string (a_description)
end
handle_method_not_allowed_response (a_description: STRING; ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
local
h : HTTP_HEADER
do
create h.make
h.put_content_type_application_json
h.put_content_length (a_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 (a_description)
end
handle_resource_not_found_response (a_description: STRING; ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
local
h : HTTP_HEADER
do
create h.make
h.put_content_type_application_json
h.put_content_length (a_description.count)
h.put_current_date
res.set_status_code ({HTTP_STATUS_CODE}.not_found)
res.put_header_text (h.string)
res.put_string (a_description)
end
handle_resource_not_modified_response (a_description: STRING; ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
local
h : HTTP_HEADER
do
res.flush
create h.make
h.put_content_type_application_json
h.put_content_length (a_description.count)
h.put_current_date
res.set_status_code ({HTTP_STATUS_CODE}.not_modified)
res.put_header_text (h.string)
res.put_string (a_description)
end
handle_resource_conflict_response (a_description: STRING; ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
local
h : HTTP_HEADER
do
create h.make
h.put_content_type_application_json
h.put_content_length (a_description.count)
h.put_current_date
res.set_status_code ({HTTP_STATUS_CODE}.conflict)
res.put_header_text (h.string)
res.put_string (a_description)
end
note
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, 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