KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > golfShop > presentation > xmlc > login > Login


1 /*
2  * Enhydra Java Application Server
3  * The Initial Developer of the Original Code is Lutris Technologies Inc.
4  * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
5  * Inc.
6  * All Rights Reserved.
7  *
8  * The contents of this file are subject to the Enhydra Public License Version
9  * 1.0 (the "License"); you may not use this file except in compliance with the
10  * License. You may obtain a copy of the License at
11  * http://www.enhydra.org/software/license/epl.html
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15  * License for the specific language governing rights and limitations under the
16  * License.
17  *
18  *
19  */

20
21 package golfShop.presentation.xmlc.login;
22
23 import org.enhydra.xml.xmlc.*;
24 import org.enhydra.xml.xmlc.html.*;
25 import com.lutris.appserver.server.httpPresentation.*;
26 import java.io.*;
27 import org.w3c.dom.*;
28 import org.w3c.dom.html.*;
29 import golfShop.presentation.xmlc.utilities.*;
30
31 /**
32  * This presentation object dynamically creates an HTML page to
33  * prompt for login information.<P>
34  */

35 public class Login implements HttpPresentation {
36     /*
37      * Default name & password to allow input without entering anything.
38      * Wouldn't do this in real life :-)
39      */

40     private static final String JavaDoc defaultUserName = "enhydra";
41     private static final String JavaDoc defaultPassowrd = "lutris";
42
43     /**
44      * State object in session.
45      */

46     private LoginState loginState;
47
48     /**
49      * Generate DOM nodes for the login error message and add
50      * as the first children of the message paragraph element.
51      */

52     private void addErrorMsg(HTMLDocument doc, HTMLElement msgElem,
53                              String JavaDoc errorMsg) {
54         // Resulting HTML is:
55
// <FONT SIZE=+3>!&nbsp;</FONT><FONT COLOR=red>errorMsg</FONT><BR>
56

57         // Create <FONT SIZE=+3>!&nbsp;</FONT>
58
HTMLFontElement fontSize = (HTMLFontElement)doc.createElement("font");
59         fontSize.setSize("+3");
60         fontSize.appendChild(doc.createTextNode("! "));
61
62
63         // Create <FONT COLOR=red>errorMsg</FONT>
64
HTMLFontElement fontColor = (HTMLFontElement)doc.createElement("font");
65         fontColor.setColor("red");
66         fontColor.appendChild(doc.createTextNode(errorMsg));
67
68         // finally <BR>
69
HTMLBRElement br = (HTMLBRElement)doc.createElement("br");
70
71         // Assemble, bottom-up so its before template text in message.
72
msgElem.insertBefore(br,
73                              msgElem.getFirstChild());
74         msgElem.insertBefore(fontColor,
75                              msgElem.getFirstChild());
76         msgElem.insertBefore(fontSize,
77                              msgElem.getFirstChild());
78         
79         // Indicate error reported
80
loginState.lastError = null;
81     }
82
83     /**
84      * Displays page to prompt for a username and password.
85      */

86     private void promptPage(HttpPresentationComms comms)
87             throws HttpPresentationException {
88         LoginHTML htmlObj
89             = (LoginHTML)comms.xmlcFactory.create(LoginHTML.class);
90
91         // If login has just failied , modify message to user.
92
if (loginState.lastError != null) {
93             addErrorMsg(htmlObj, htmlObj.getElementMessage(),
94                         loginState.lastError);
95         }
96
97         // If we have a user name, set it as the default value.
98
// otherwise, use standard default to make demo easy.
99
HTMLInputElement userName = htmlObj.getElementUsername();
100         if (loginState.userName != null) {
101             userName.setValue(loginState.userName);
102         } else {
103             userName.setValue(defaultUserName);
104             htmlObj.getElementPassword().setValue(defaultPassowrd);
105         }
106
107         comms.response.writeDOM(htmlObj);
108     }
109
110     /**
111      * Entry.
112      */

113     public void run(HttpPresentationComms comms)
114         throws HttpPresentationException {
115
116         loginState = LoginState.get(comms.session);
117         promptPage(comms);
118     }
119 }
120
Popular Tags