001: /* -----------------------------------------------------------------------------
002: * Copyright (c) 2001, Low Kin Onn
003: * All rights reserved.
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: *
008: * Redistributions of source code must retain the above copyright notice, this
009: * list of conditions and the following disclaimer.
010: *
011: * Redistributions in binary form must reproduce the above copyright notice,
012: * this list of conditions and the following disclaimer in the documentation
013: * and/or other materials provided with the distribution.
014: *
015: * Neither name of the Scioworks Pte. Ltd. nor the names of its contributors
016: * may beused to endorse or promote products derived from this software without
017: * specific prior written permission.
018: *
019: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
023: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
024: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
025: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
026: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
028: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: *
030: * -----------------------------------------------------------------------------
031: */
032:
033: package scioworks.imap.presentation.base;
034:
035: import java.util.Vector;
036:
037: import com.lutris.appserver.server.httpPresentation.*;
038: import com.lutris.util.KeywordValueException;
039:
040: import org.w3c.dom.*;
041: import org.w3c.dom.html.*;
042:
043: import scioworks.imap.presentation.ImapWebSessionData;
044: import scioworks.imap.presentation.*;
045: import scioworks.imap.presentation.KMSPresentationException;
046:
047: public abstract class BasePO implements HttpPresentation,
048: BaseParameters, BaseEvents, BaseMessages, BaseURLs {
049:
050: protected static final int m_firstEntryIndex = 1;
051: protected static final int m_pageLength = 10;
052:
053: protected HttpPresentationComms m_comms = null;
054: protected ImapWebSessionData m_ImapWebSessionData = null;
055:
056: abstract public String handleDefault()
057: throws HttpPresentationException,
058: ServerPageRedirectException;
059:
060: /**
061: * Default required login, override otherwise
062: */
063: protected boolean isLoginRequired() {
064: return true;
065: }
066:
067: /**
068: * @return The saved comms objects
069: * to whichever subclass needs it
070: */
071: public HttpPresentationComms getComms() {
072: return m_comms;
073: }
074:
075: public ImapWebSessionData getImapWebSessionData() {
076: return m_ImapWebSessionData;
077: }
078:
079: public void setSessionData(ImapWebSessionData sessionData) {
080: this .m_ImapWebSessionData = sessionData;
081: }
082:
083: protected void initSessionData(HttpPresentationComms comms)
084: throws KMSPresentationException {
085:
086: this .m_comms = comms;
087: try {
088:
089: // try to get the imapWeb session data (if any)
090: Object obj = getComms().sessionData
091: .get(ImapWebSessionData.SESSION_KEY);
092:
093: // If we found the session data, save it in a private data member
094: if (null != obj) {
095: m_ImapWebSessionData = (ImapWebSessionData) obj;
096:
097: } else {
098:
099: // If no session data was found, create a new session data instance
100: this .m_ImapWebSessionData = new ImapWebSessionData(null);
101: getComms().sessionData.set(
102: ImapWebSessionData.SESSION_KEY,
103: this .m_ImapWebSessionData);
104: }
105: } catch (KeywordValueException ex) {
106: System.out
107: .println("Problem getting session data from session: "
108: + ex.getMessage());
109: }
110: }
111:
112: /**
113: * Checks the session data for a User, if not there then redirects to the login page
114: */
115: protected void checkForUserLogin() throws KMSPresentationException {
116:
117: try {
118: if (!m_ImapWebSessionData.isValidSession()) {
119:
120: String requestedPO = getComms().request.getRequestURI();
121: getComms().response.writeHTML(this .showErrorPage(
122: "Session Expired",
123: "You session has expired. Please re-login",
124: "Re-login", URL_login, "_parent"));
125: } else {
126: //System.out.println("USER ALREADY LOGGED IN WITH A SESSION");
127: }
128: } catch (Exception ex) {
129: throw new KMSPresentationException(
130: "Trouble checking for user login status", ex);
131: }
132: }
133:
134: public void run(HttpPresentationComms comms)
135: throws KMSPresentationException {
136:
137: // Initialize new or get the existing session data
138: initSessionData(comms);
139:
140: // Check if the user needs to be logged in for this request.
141: if (isLoginRequired()) {
142: checkForUserLogin();
143: }
144:
145: try {
146: // Handle the incoming event request
147: handleEvent(comms);
148: } catch (HttpPresentationException e) {
149: Logger.writeError("BasePO::run", e);
150: throw new KMSPresentationException(
151: "An error has occured. "
152: + "Please contact your administrator. Error message: "
153: + e.getMessage());
154: }
155:
156: }
157:
158: public void handleEvent(HttpPresentationComms comms)
159: throws HttpPresentationException {
160:
161: String returnHTML = null;
162:
163: try {
164: if (this .getStringParameter(PARAM_imageData).equals("y")) {
165: String returnMimeType = handleDefault();
166: comms.response.setContentType(returnMimeType);
167: return;
168:
169: } else {
170: returnHTML = handleDefault();
171: }
172: } catch (Exception e) {
173: returnHTML = this .showErrorPage("Internal Error",
174: "An error has occured. Please contact your administrator. "
175: + "Error message: " + e.getMessage(), "",
176: "");
177: Logger.writeError("BasePO::handleEvent", e);
178: }
179:
180: getComms().response.writeHTML(returnHTML);
181: }
182:
183: protected long getLongParameter(String parm)
184: throws HttpPresentationException, NumberFormatException {
185: long res = 0;
186: try {
187: res = Long.parseLong(m_comms.request.getParameter(parm));
188: } catch (NumberFormatException e) {
189: res = -1;
190: }
191: return res;
192: }
193:
194: protected int getIntParameter(String parm)
195: throws HttpPresentationException, NumberFormatException {
196: int res = 0;
197: try {
198: res = Integer.parseInt(m_comms.request.getParameter(parm));
199: } catch (NumberFormatException e) {
200: res = -1;
201: }
202: return res;
203: }
204:
205: protected String getStringParameter(String parm)
206: throws HttpPresentationException {
207: String value = m_comms.request.getParameter(parm);
208: if ((value == null) || (value.length() == 0)) {
209: value = "";
210: }
211: return value;
212: }
213:
214: protected String[] getStringParameterArray(String parm)
215: throws HttpPresentationException {
216: return m_comms.request.getParameterValues(parm);
217: }
218:
219: protected void setStringParameter(String name, String value)
220: throws HttpPresentationException {
221:
222: m_comms.response.setHeader(name, value);
223: }
224:
225: protected long[] getLongParameterArray(String parm)
226: throws HttpPresentationException {
227: String[] strArr = m_comms.request.getParameterValues(parm);
228: long[] longArr = new long[strArr.length];
229: for (int i = 0; i < strArr.length; i++) {
230: longArr[i] = Long.parseLong(strArr[i]);
231: }
232: return longArr;
233: }
234:
235: protected String showErrorPage(String errorHeader,
236: String errorText, String errorLinkText, String errorLink,
237: String target) {
238: errorPageHTML page = (errorPageHTML) m_comms.xmlcFactory
239: .create(errorPageHTML.class);
240: page.setTextErrorHeader(errorHeader);
241: page.setTextErrorText(errorText);
242: page.setTextErrorLinkText(errorLinkText);
243:
244: HTMLAnchorElement fileLink;
245: fileLink = page.getElementErrorLink();
246: if (errorLinkText.equals("") || errorLink.equals("")) {
247: this .removeElement(page, fileLink);
248: } else {
249: fileLink.setHref(errorLink);
250: if (!target.equals("")) {
251: fileLink.setTarget(target);
252: }
253: }
254: return page.toDocument();
255: }
256:
257: protected String showErrorPage(String errorHeader,
258: String errorText, String errorLinkText, String errorLink) {
259: return showErrorPage(errorHeader, errorText, errorLinkText,
260: errorLink, "");
261: }
262:
263: protected String showCompletionPage(String header, String text,
264: String linkText, String link) {
265: messagePageHTML page = (messagePageHTML) m_comms.xmlcFactory
266: .create(messagePageHTML.class);
267: page.setTextMessageHeader(header);
268: page.setTextMessageText(text);
269: page.setTextMessageLinkText(linkText);
270:
271: HTMLAnchorElement fileLink = page.getElementMessageLink();
272: if (linkText.equals("") || link.equals("")) {
273: this .removeElement(page, fileLink);
274: } else {
275: fileLink.setHref(link);
276: /*
277: if(!target.equals("")) {
278: fileLink.setTarget(target);
279: }
280: */
281: }
282: return page.toDocument();
283: }
284:
285: protected void removeElement(
286: org.enhydra.xml.xmlc.html.HTMLObjectImpl page,
287: Element currElement) {
288: if (currElement != null) {
289: Node parentNode = currElement.getParentNode();
290: parentNode.removeChild(currElement);
291: } else {
292: Logger.writeDebug("Error: unfound element "
293: + currElement.getLocalName());
294: }
295: }
296:
297: protected void removeElement(
298: org.enhydra.xml.xmlc.html.HTMLObjectImpl page,
299: java.util.HashSet elementSet) {
300: Element currElement;
301: java.util.Iterator it = elementSet.iterator();
302: Node parentNode;
303: while (it.hasNext()) {
304: currElement = (Element) it.next();
305: if (currElement != null) {
306: parentNode = currElement.getParentNode();
307: parentNode.removeChild(currElement);
308: } else {
309: Logger.writeDebug("Error: unfound element "
310: + currElement.getLocalName());
311: }
312: }
313: }
314:
315: protected void processSelectList(
316: org.enhydra.xml.xmlc.html.HTMLObjectImpl page,
317: HTMLSelectElement lookUpElement, Vector displayList,
318: Vector valueList, String selectedItem) {
319:
320: // remove the hard coded drop down elements
321: for (int i = lookUpElement.getLength() - 1; i >= 0; i--) {
322: lookUpElement.remove(i);
323: }
324:
325: for (int i = 0; i < valueList.size(); i++) {
326: // Create and append options, set to select if matches value
327: HTMLOptionElement o1 = (HTMLOptionElement) page
328: .createElement("OPTION");
329: o1.setValue((String) valueList.elementAt(i));
330: o1.appendChild(page.createTextNode((String) displayList
331: .elementAt(i)));
332: if (selectedItem.equals((String) valueList.elementAt(i))) {
333: o1.setAttribute("selected", "selected");
334: }
335: lookUpElement.appendChild(o1);
336: }
337: }
338: }
|