Skip to content
This repository has been archived by the owner on Sep 22, 2020. It is now read-only.

Commit

Permalink
better example .. to be continued
Browse files Browse the repository at this point in the history
  • Loading branch information
jocelyn committed Jun 1, 2011
1 parent bf208f5 commit 7db998e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 82 deletions.
99 changes: 77 additions & 22 deletions examples/rest_server/src/app/app_test.e
Expand Up @@ -39,38 +39,93 @@ feature -- Execution

execute_application (henv: REST_ENVIRONMENT; a_format: detachable STRING; a_args: detachable STRING)
local
rep: REST_RESPONSE
rep: detachable REST_RESPONSE
s: STRING
ht: HASH_TABLE_ITERATION_CURSOR [STRING_GENERAL, STRING_GENERAL]
do
create rep.make (path)
rep.headers.put_content_type_text_html
create s.make_empty
s.append_string ("test")
if attached henv.environment_variable ("REQUEST_COUNT") as l_request_count then
s.append_string ("(request_count="+ l_request_count +")<br/>%N")
end

if a_args /= Void and then not a_args.is_empty then
s.append_string (" arguments=" + a_args)
if a_args.same_string ("crash") then
rep := Void
(create {DEVELOPER_EXCEPTION}).raise
elseif a_args.starts_with ("env") then
s.append_string ("%N%NAll variables:")
s.append (string_hash_table_string_string (henv.variables.new_cursor))
s.append_string ("<br/>script_url(%"" + henv.path_info + "%")=" + henv.script_url (henv.path_info) + "%N")
if attached henv.http_authorization_login_password as t then
s.append_string ("Check login=" + t.login + "<br/>%N")
end
if henv.authenticated and then attached henv.authenticated_login as l_login then
s.append_string ("Authenticated: login=" + l_login.as_string_8 + "<br/>%N")
create rep.make (path)
create s.make_empty
ht := henv.variables.new_cursor
if a_format = Void or else a_format.same_string ("html") then
rep.headers.put_content_type_text_html
from
ht.start
until
ht.after
loop
s.append_string ("<li><strong>" + ht.key.as_string_8 + "</strong> = " + ht.item.as_string_8 + "</li>%N")
ht.forth
end
elseif a_format.same_string ("json") then
rep.headers.put_content_type_application_json
s.append ("{ %"application%": %""+ path +"%" ")
from
ht.start
until
ht.after
loop
s.append_string (",%"" + ht.key.as_string_8 + "%": %"" + ht.item.as_string_8 + "%"%N")
ht.forth
end
s.append ("}%N")
elseif a_format.same_string ("xml") then
rep.headers.put_content_type_text_xml
s.append ("<application name=%""+ path +"%">")
from
ht.start
until
ht.after
loop
s.append_string ("<variable name=%"" + ht.key.as_string_8 + "%">" + ht.item.as_string_8 + "</variable>%N")
ht.forth
end
s.append ("</application>%N")
else
rep.headers.put_content_type_text_plain
from
ht.start
until
ht.after
loop
s.append_string (ht.key.as_string_8 + " = " + ht.item.as_string_8 + "%N")
ht.forth
end
end
rep.set_message (s)
else
end
else
s.append ("%N Try <a href=%"http://" + henv.script_absolute_url (henv.path_info + "/env") + "%">/env</a> to display all variables <br/>%N")
end
if rep = Void then
create rep.make (path)
rep.headers.put_content_type_text_html
create s.make_empty
s.append_string ("test")
if attached henv.environment_variable ("REQUEST_COUNT") as l_request_count then
s.append_string ("(request_count="+ l_request_count +")<br/>%N")
end
s.append ("%N Try <a href=%"http://" + henv.script_absolute_url (henv.path_info + "/env") + "%">/test/env</a> to display all variables <br/>%N")
s.append ("%N Try <a href=%"http://" + henv.script_absolute_url (henv.path_info + ".json/env") + "%">/test.json/env</a> to display all variables in JSON <br/>%N")
s.append ("%N Try <a href=%"http://" + henv.script_absolute_url (henv.path_info + ".json/env") + "%">/test.xml/env</a> to display all variables in XML <br/>%N")
s.append ("%N Try <a href=%"http://" + henv.script_absolute_url (henv.path_info + ".json/env") + "%">/test.html/env</a> to display all variables in HTML<br/>%N")
s.append ("%N Try <a href=%"http://" + henv.script_absolute_url (henv.path_info + "/crash") + "%">/crash</a> to demonstrate exception trace <br/>%N")

if attached henv.http_authorization_login_password as t then
s.append_string ("Check login=" + t.login + "<br/>%N")
end
if henv.authenticated and then attached henv.authenticated_login as l_login then
s.append_string ("Authenticated: login=" + l_login.as_string_8 + "<br/>%N")
end

s.append_string ("<br/>script_url(%"" + henv.path_info + "%")=" + henv.script_url (henv.path_info) + "%N")

rep.set_message (s)
end
rep.set_message (s)
rep.compute

henv.output.put_string (rep.string)
rep.recycle
end
Expand Down
8 changes: 7 additions & 1 deletion examples/rest_server/src/rest_server.e
Expand Up @@ -72,12 +72,18 @@ feature {NONE} -- Initialization

feature {NONE} -- Environment

new_environment (a_vars: HASH_TABLE [STRING, STRING]; a_input: HTTPD_SERVER_INPUT; a_output: HTTPD_SERVER_OUTPUT): REST_SERVER_ENVIRONMENT
new_environment (a_vars: HASH_TABLE [STRING, STRING]; a_input: HTTPD_SERVER_INPUT; a_output: HTTPD_SERVER_OUTPUT): REST_ENVIRONMENT
do
create Result.make (a_vars, a_input, a_output)

--| At this point, you can decided to add your own environment variable
--| this is a convenient way to share a value
Result.environment_variables.add_variable (request_count.out, "REQUEST_COUNT")

--| Precise our own authentication system
Result.authentication := create {REST_SERVER_AUTHENTICATION}

--| Note that you can also create your own REST_ENVIRONMENT with specific features
end

feature {NONE} -- Handlers
Expand Down
59 changes: 0 additions & 59 deletions examples/rest_server/src/rest_server_environment.e

This file was deleted.

0 comments on commit 7db998e

Please sign in to comment.