[Gd-chatter] r11714 - in trunk/libraries/network/koala: sources/examples/koala-basics sources/koala www/demo

cgay at gwydiondylan.org cgay at gwydiondylan.org
Tue Feb 26 15:37:28 CET 2008


Author: cgay
Date: Tue Feb 26 15:37:27 2008
New Revision: 11714

Modified:
   trunk/libraries/network/koala/sources/examples/koala-basics/main.dylan
   trunk/libraries/network/koala/sources/koala/dsp-taglib.dylan
   trunk/libraries/network/koala/sources/koala/dsp.dylan
   trunk/libraries/network/koala/sources/koala/library-unix.dylan
   trunk/libraries/network/koala/sources/koala/library.dylan
   trunk/libraries/network/koala/sources/koala/pages.dylan
   trunk/libraries/network/koala/www/demo/iterator.dsp
Log:
job: koala
More changes to get koala-basics working again.
Restored respond-to-get and respond-to-post (optional, it just
  reads better).

Modified: trunk/libraries/network/koala/sources/examples/koala-basics/main.dylan
==============================================================================
--- trunk/libraries/network/koala/sources/examples/koala-basics/main.dylan	(original)
+++ trunk/libraries/network/koala/sources/examples/koala-basics/main.dylan	Tue Feb 26 15:37:27 2008
@@ -81,9 +81,11 @@
 // Slightly higher level than responders.  Gives you the convenience of not
 // having to figure out whether it's a GET, POST, HEAD, request, and the ability
 // to dispatch on your own page classes.  Just define methods for
-// respond-to that dispatch on your page class.  Note that the default methods
-// for GET and HEAD do nothing and the default method for POST calls the method
-// for GET.
+// respond-to that dispatch on your page class.
+
+// Note that you may override respond-to-get and respond-to-post instead of
+// overriding respond-to(== #"get") and respond-to(== #"post") as a convenience
+// since these are by far the most common request methods.
 
 // This defines a <hello-world-page> class which is a subclass of <page>, and a
 // variable called *hello-world-page* which is an instance of
@@ -98,12 +100,10 @@
 // Respond to a GET for <hello-world-page>.  Note the use of do-query-values to
 // find all the values passed in the URL (e.g., /hello?foo=1&bar=2).  You can
 // also use get-query-value to get a specific query value, and
-// count-query-values can be used to find out how many there are.  Note that
-// respond-to(#"post", ...) automatically calls respond-to(#"get", unless you
-// override it.
+// count-query-values can be used to find out how many there are.
 //
-define method respond-to
-    (request-method == #"get", page :: <hello-world-page>)
+define method respond-to-get
+    (page :: <hello-world-page>)
   let stream :: <stream> = output-stream(current-response());
   format(stream, "<html>\n<head><title>Hello World</title></head>\n"
                  "<body>Hello there.<p>");
@@ -115,33 +115,42 @@
   do-query-values(method (key, val)
                     format(stream, "key = %s, val = %s<br>\n", key, val);
                   end);
-  format(stream, "<p>Use your browser's Back button to return to the demo.</body></html>");
+  format(stream,
+         "<p>Use your browser's Back button to return to the "
+         "demo.</body></html>");
 end;
 
 
 //// Dylan Server Pages
 
-// Dylan Server Pages are also defined with the "define page" macro, but you
-// also specify the source: argument which is a file that contains normal
-// HTML plus DSP tags.  The default method for respond-to GET parses the DSP
-// source file and displays it.  Any HTML is output directly to the output
-// stream, and tags invoke the corresponding tag definition code.
-
-// Note that the .dsp source file doesn't have to be under the *document-root*
-// directory.
-
-// Define a class that will be used for all our example pages.  It must be a
-// subclass of <dylan-server-page> so that all the template parsing will happen.
-// If we define all our tags to be specialized on this class they can be used
-// in any example page.
+// Dylan Server Pages are also usually defined with the "define page"
+// macro, but you must also specify the source: argument which is a
+// file that contains normal content plus DSP tags.
+
+// In general you will define respond-to-get/post methods for your
+// <dylan-server-page>s that do some processing based on the query
+// values in the request and then call next-method() to process the
+// template for the page or call process-template on some other page
+// to display its output instead (e.g., an error template).
+
+// Any plain content is output directly to the output stream, and
+// tags invoke the corresponding tag definition.
+
+// Note that the .dsp source file doesn't have to be under the
+// *document-root* directory.
+
+// This defines a class that will be used for all our example pages.
+// It must be a subclass of <dylan-server-page> so that all the
+// template parsing will happen.  If we define all our tags to be
+// specialized on this class they can be used in any example page.
 //
 define class <demo-page> (<dylan-server-page>)
 end;
 
-// Define a tag library in which to put all tag definitions.  This isn't
-// strictly necessary; tag defs can go in the existing 'dsp' tag library
-// but then you run the risk of overriding built-in DSP tags or other
-// user-defined tags in the dsp taglib.
+// Define a tag library in which to put all tag definitions.  This
+// isn't strictly necessary; tag defs can go in the existing 'dsp'
+// tag library but then you run the risk of overriding built-in DSP
+// tags or other user-defined tags in the dsp taglib.
 //
 define taglib demo ()
 end;
@@ -163,12 +172,14 @@
      source: "demo/hello.dsp")
 end;
 
-// Defines a tag that looks like <demo:hello/> in the DSP source file.  i.e.,
-// it has no body.
+// Defines a tag that looks like <demo:hello/> in the DSP source
+// file.  i.e., it has no body.  Note that the "output" function is
+// defined by the "define tag" macro and could be defined as
+// curry(format, output-stream(current-response))
 define tag hello in demo
     (page :: <demo-page>)
     ()
-  format(output-stream(current-response()), "Hello, world!");
+  output("Hello, world!");
 end;
 
 define page args-page (<demo-page>)
@@ -207,8 +218,8 @@
      source: "demo/logout.dsp")
 end;
 
-define method respond-to
-    (request-method == #"get", page :: <example-logout-page>)
+define method respond-to-get
+    (page :: <example-logout-page>)
   let session = get-session(current-request());
   remove-attribute(session, #"username");
   remove-attribute(session, #"password");
@@ -222,8 +233,8 @@
 end;
 
 // ...so handle the POST by storing the form values in the session.
-define method respond-to
-    (request-method == #"post", page :: <example-welcome-page>)
+define method respond-to-post
+    (page :: <example-welcome-page>)
   let username = get-query-value("username");
   let password = get-query-value("password");
   let username-supplied? = username & username ~= "";
@@ -232,16 +243,17 @@
     let session = get-session(current-request());
     set-attribute(session, #"username", username);
     set-attribute(session, #"password", password);
-    next-method();  // process the DSP template for the welcome page.
+    // process the DSP template for the welcome page.
+    next-method();
   else
     note-form-error("You must supply <b>both</b> a username and password.");
     // ---*** TODO: Calling respond-to(#"get", ...) probably isn't quite right.
     // If we're redirecting to another page should the query/form values
     // be cleared first?  Probably want to call process-page instead,
     // but with the existing request?
-    respond-to(#"get", *example-login-page*);
+    respond-to-get(*example-login-page*);
   end;
-end;
+end method respond-to-post;
 
 // Note this tag is defined on <demo-page> so it can be accessed from any
 // page in this example web application.

Modified: trunk/libraries/network/koala/sources/koala/dsp-taglib.dylan
==============================================================================
--- trunk/libraries/network/koala/sources/koala/dsp-taglib.dylan	(original)
+++ trunk/libraries/network/koala/sources/koala/dsp-taglib.dylan	Tue Feb 26 15:37:27 2008
@@ -301,3 +301,4 @@
     write(out, "</div>\n");
   end;
 end;
+

Modified: trunk/libraries/network/koala/sources/koala/dsp.dylan
==============================================================================
--- trunk/libraries/network/koala/sources/koala/dsp.dylan	(original)
+++ trunk/libraries/network/koala/sources/koala/dsp.dylan	Tue Feb 26 15:37:27 2008
@@ -7,7 +7,9 @@
 
 // Users of this library may respond to HTTP requests in two ways:
 // (1) Use "define responder" to register a response function for a
-//     given URL.  The function will be passed a <request> and a <response>.
+//     given URL.  The function can access the active <request> object
+//     via current-request().  It can access the active <response> object
+//     via current-response().
 //
 // (2) Define a subclass of <page> and implement the method respond-to.
 //     Use "define page", specifying <page> as a superclass to register a
@@ -15,7 +17,8 @@
 //
 // (3) Use "define page", specifying <dylan-server-page> as a superclass
 //     and define any "tags" you need with "define tag".  Create a .dsp
-//     file that calls the tags with <dsp:my-tag .../>
+//     file that calls the tags with <dsp:my-tag .../>.  Tags can generate
+//     output via output(format-string, #rest format-args).
 //
 // See .../koala/sources/examples/koala-basics/ for example DSP usage.
 
@@ -45,7 +48,8 @@
 // Gives the user a place to store values that will have a lifetime
 // equal to the duration of the page processing (i.e., during process-page).  The
 // name is stolen from JSP's PageContext class, but it's not intended to serve the
-// same purpose.
+// same purpose.  Use set-attribute(page-context, key, val) to store attributes
+// for the page and get-attribute(page-context, key) to retrieve them.
 
 define class <page-context> (<attributes-mixin>)
 end;
@@ -76,29 +80,58 @@
 define open primary class <page> (<object>)
 end;
 
-// The protocol every page needs to support.
 // This is the method registered as the response function for all <page>s.
 // See register-page.
 define method process-page (page :: <page>)
-  let page-context = make(<page-context>);
-  dynamic-bind (*page-context* = page-context)
+  dynamic-bind (*page-context* = make(<page-context>))
     respond-to(current-request().request-method, page);
   end;
 end process-page;
 
+// The protocol every page needs to support.
 define open generic respond-to 
     (request-method :: <symbol>, page :: <page>);
 
+// Default
 define method respond-to 
     (request-method :: <symbol>, page :: <page>)
-  if (member?(request-method, #(#"get", #"post")))
-    process-template(page);
-  else  
-    unsupported-request-method-error();
-  end if;
+  unsupported-request-method-error();
 end;
 
-// Applications should call this to register a page for a particular URL.
+define method respond-to
+    (request-method == #"GET", page :: <page>)
+  respond-to-get(page)
+end method respond-to;
+
+define method respond-to
+    (request-method == #"POST", page :: <page>)
+  respond-to-post(page)
+end method respond-to;
+
+// These are by far the most common cases and it's more succinct
+// and readable than respond-to.
+//
+define open generic respond-to-get (page :: <page>);
+define open generic respond-to-post (page :: <page>);
+
+define method respond-to-get
+    (page :: <page>)
+  unsupported-request-method-error()
+end method respond-to-get;
+
+// This is a common case and it's more succinct and readable than respond-to.
+define method respond-to-post
+    (page :: <page>)
+  respond-to-get(page);
+end method respond-to-post;
+
+define method respond-to-get
+    (page :: <dylan-server-page>)
+  process-template(page);
+end method respond-to-get;
+
+// Applications should call this (usually via the "define page" macro) to
+// register a page for a particular URL.
 define function register-page
     (url :: <string>, page :: <page>, #key replace?)
  => (responder :: <function>)
@@ -107,15 +140,18 @@
     *page-to-url-map*[page] := url;
     responder
   end
-end;
+end function register-page;
 
 define function url-to-page
     (url :: <string>)
   block (return)
     for (uri keyed-by page in *page-to-url-map*)
       if (uri = url)
-        return(page); end if; end for; end block;
-end;
+        return(page);
+      end if;
+    end for;
+  end block;
+end function url-to-page;
 
 // ---TODO: Test this and export it.
 // Register URLs for all files matching the given pathname spec as instances
@@ -1255,3 +1291,4 @@
   register-page(url, make(<dylan-server-page>,
                           source: document-location(url)))
 end;
+

Modified: trunk/libraries/network/koala/sources/koala/library-unix.dylan
==============================================================================
--- trunk/libraries/network/koala/sources/koala/library-unix.dylan	(original)
+++ trunk/libraries/network/koala/sources/koala/library-unix.dylan	Tue Feb 26 15:37:27 2008
@@ -346,6 +346,8 @@
     register-page,               // Register a page for a given URL
     url-to-page,
     respond-to,                  // Implement this for your page to handle a request
+    respond-to-get,              // Convenience.
+    respond-to-post,             // Convenience.
 
     page-source,
     page-source-setter,

Modified: trunk/libraries/network/koala/sources/koala/library.dylan
==============================================================================
--- trunk/libraries/network/koala/sources/koala/library.dylan	(original)
+++ trunk/libraries/network/koala/sources/koala/library.dylan	Tue Feb 26 15:37:27 2008
@@ -346,6 +346,8 @@
     register-page,               // Register a page for a given URL
     url-to-page,
     respond-to,                  // Implement this for your page to handle a request
+    respond-to-get,              // Convenience.
+    respond-to-post,             // Convenience.
 
     page-source,
     page-source-setter,

Modified: trunk/libraries/network/koala/sources/koala/pages.dylan
==============================================================================
--- trunk/libraries/network/koala/sources/koala/pages.dylan	(original)
+++ trunk/libraries/network/koala/sources/koala/pages.dylan	Tue Feb 26 15:37:27 2008
@@ -43,10 +43,8 @@
 // in the session and is redirected to the login page.  If there is no 'id'
 // query parameter, the record to edit is already in the session.
 //
-define method respond-to-get (page :: <edit-record-page>,
-                              request :: <request>,
-                              response :: <response>)
-  let session = get-session(request);
+define method respond-to-get (page :: <edit-record-page>)
+  let session = get-session(current-request());
   let record-id = get-query-value("id");
   let record-type = get-query-value("type");
   let record = select (record-id by \=)
@@ -66,9 +64,12 @@
                         format-arguments: list(record-id, record-type));
   set-attribute(session, $edit-record-key, record);
   dynamic-bind (*record* = record)
-    respond-to-get-edit-record(page, request, response, record);
+    respond-to-get-edit-record(page,
+                               current-request(),
+                               current-response(),
+                               record);
   end;
-end;
+end method respond-to-get;
 
 define open generic respond-to-get-edit-record
     (page :: <object>,
@@ -107,11 +108,11 @@
   bind (origin = get-query-value("origin"),
         // It may be common to forget to use a leading / since it's not required...
         page = origin & (url-to-page(origin) | url-to-page(concatenate("/", origin))))
-    respond-to-get(page | default, request, response);
+    respond-to-get(page | default);
   end;
 end;
 
-define method respond-to (request-method == #"post", page :: <edit-record-page>)
+define method respond-to-post (page :: <edit-record-page>)
   let request :: <request> = current-request();
   let record :: <database-record> = get-edit-record(request);
   let slots = slot-descriptors(object-class(record));

Modified: trunk/libraries/network/koala/www/demo/iterator.dsp
==============================================================================
--- trunk/libraries/network/koala/www/demo/iterator.dsp	(original)
+++ trunk/libraries/network/koala/www/demo/iterator.dsp	Tue Feb 26 15:37:27 2008
@@ -10,7 +10,7 @@
   <%dsp:include location="header.dsp"/>
   <%dsp:include location="body-wrapper-start.dsp"/>
 
-  <demo:show-errors/>
+  <dsp:show-form-notes/>
 
   <h2>Iterator</h2>
 



More information about the chatter mailing list