001: /*
002: * Enhydra Java Application Server
003: * The Initial Developer of the Original Code is Lutris Technologies Inc.
004: * Portions created by Lutris are Copyright (C) 1997-2000 Lutris Technologies
005: * Inc.
006: * All Rights Reserved.
007: *
008: * The contents of this file are subject to the Enhydra Public License Version
009: * 1.0 (the "License"); you may not use this file except in compliance with the
010: * License. You may obtain a copy of the License at
011: * http://www.enhydra.org/software/license/epl.html
012: *
013: * Software distributed under the License is distributed on an "AS IS" basis,
014: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
015: * License for the specific language governing rights and limitations under the
016: * License.
017: *
018: *
019: */
020:
021: package golfShop.presentation.xmlc.login;
022:
023: import org.enhydra.xml.xmlc.*;
024: import org.enhydra.xml.xmlc.html.*;
025: import com.lutris.appserver.server.httpPresentation.*;
026: import java.io.*;
027: import org.w3c.dom.*;
028: import org.w3c.dom.html.*;
029: import golfShop.presentation.xmlc.utilities.*;
030:
031: /**
032: * This presentation object dynamically creates an HTML page to
033: * prompt for login information.<P>
034: */
035: public class Login implements HttpPresentation {
036: /*
037: * Default name & password to allow input without entering anything.
038: * Wouldn't do this in real life :-)
039: */
040: private static final String defaultUserName = "enhydra";
041: private static final String defaultPassowrd = "lutris";
042:
043: /**
044: * State object in session.
045: */
046: private LoginState loginState;
047:
048: /**
049: * Generate DOM nodes for the login error message and add
050: * as the first children of the message paragraph element.
051: */
052: private void addErrorMsg(HTMLDocument doc, HTMLElement msgElem,
053: String errorMsg) {
054: // Resulting HTML is:
055: // <FONT SIZE=+3>! </FONT><FONT COLOR=red>errorMsg</FONT><BR>
056:
057: // Create <FONT SIZE=+3>! </FONT>
058: HTMLFontElement fontSize = (HTMLFontElement) doc
059: .createElement("font");
060: fontSize.setSize("+3");
061: fontSize.appendChild(doc.createTextNode("! "));
062:
063: // Create <FONT COLOR=red>errorMsg</FONT>
064: HTMLFontElement fontColor = (HTMLFontElement) doc
065: .createElement("font");
066: fontColor.setColor("red");
067: fontColor.appendChild(doc.createTextNode(errorMsg));
068:
069: // finally <BR>
070: HTMLBRElement br = (HTMLBRElement) doc.createElement("br");
071:
072: // Assemble, bottom-up so its before template text in message.
073: msgElem.insertBefore(br, msgElem.getFirstChild());
074: msgElem.insertBefore(fontColor, msgElem.getFirstChild());
075: msgElem.insertBefore(fontSize, msgElem.getFirstChild());
076:
077: // Indicate error reported
078: loginState.lastError = null;
079: }
080:
081: /**
082: * Displays page to prompt for a username and password.
083: */
084: private void promptPage(HttpPresentationComms comms)
085: throws HttpPresentationException {
086: LoginHTML htmlObj = (LoginHTML) comms.xmlcFactory
087: .create(LoginHTML.class);
088:
089: // If login has just failied , modify message to user.
090: if (loginState.lastError != null) {
091: addErrorMsg(htmlObj, htmlObj.getElementMessage(),
092: loginState.lastError);
093: }
094:
095: // If we have a user name, set it as the default value.
096: // otherwise, use standard default to make demo easy.
097: HTMLInputElement userName = htmlObj.getElementUsername();
098: if (loginState.userName != null) {
099: userName.setValue(loginState.userName);
100: } else {
101: userName.setValue(defaultUserName);
102: htmlObj.getElementPassword().setValue(defaultPassowrd);
103: }
104:
105: comms.response.writeDOM(htmlObj);
106: }
107:
108: /**
109: * Entry.
110: */
111: public void run(HttpPresentationComms comms)
112: throws HttpPresentationException {
113:
114: loginState = LoginState.get(comms.session);
115: promptPage(comms);
116: }
117: }
|