KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scioworks > imap > presentation > base > BasePO


1 /* -----------------------------------------------------------------------------
2  * Copyright (c) 2001, Low Kin Onn
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *
8  * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * Neither name of the Scioworks Pte. Ltd. nor the names of its contributors
16  * may beused to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * -----------------------------------------------------------------------------
31  */

32
33 package scioworks.imap.presentation.base;
34
35 import java.util.Vector JavaDoc;
36
37 import com.lutris.appserver.server.httpPresentation.*;
38 import com.lutris.util.KeywordValueException;
39
40 import org.w3c.dom.*;
41 import org.w3c.dom.html.*;
42
43
44
45 import scioworks.imap.presentation.ImapWebSessionData;
46 import scioworks.imap.presentation.*;
47 import scioworks.imap.presentation.KMSPresentationException;
48
49 public abstract class BasePO
50     implements HttpPresentation, BaseParameters, BaseEvents, BaseMessages, BaseURLs {
51
52   protected static final int m_firstEntryIndex = 1;
53   protected static final int m_pageLength = 10;
54
55   protected HttpPresentationComms m_comms = null;
56   protected ImapWebSessionData m_ImapWebSessionData = null;
57
58   abstract public String JavaDoc handleDefault()
59     throws HttpPresentationException, ServerPageRedirectException;
60
61   /**
62    * Default required login, override otherwise
63    */

64   protected boolean isLoginRequired() {
65     return true;
66   }
67
68   /**
69     * @return The saved comms objects
70     * to whichever subclass needs it
71     */

72   public HttpPresentationComms getComms() {
73     return m_comms;
74   }
75
76   public ImapWebSessionData getImapWebSessionData() {
77     return m_ImapWebSessionData;
78   }
79
80   public void setSessionData(ImapWebSessionData sessionData) {
81     this.m_ImapWebSessionData = sessionData;
82   }
83
84   protected void initSessionData(HttpPresentationComms comms)
85     throws KMSPresentationException {
86
87     this.m_comms = comms;
88     try {
89
90       // try to get the imapWeb session data (if any)
91
Object JavaDoc obj = getComms().sessionData.get(ImapWebSessionData.SESSION_KEY);
92
93       // If we found the session data, save it in a private data member
94
if(null != obj) {
95         m_ImapWebSessionData = (ImapWebSessionData) obj;
96
97       } else {
98
99         // If no session data was found, create a new session data instance
100
this.m_ImapWebSessionData = new ImapWebSessionData(null);
101         getComms().sessionData.set(ImapWebSessionData.SESSION_KEY, this.m_ImapWebSessionData);
102       }
103     } catch (KeywordValueException ex) {
104       System.out.println("Problem getting session data from session: " +
105       ex.getMessage());
106     }
107   }
108
109   /**
110     * Checks the session data for a User, if not there then redirects to the login page
111     */

112   protected void checkForUserLogin()
113     throws KMSPresentationException {
114
115     try {
116       if (!m_ImapWebSessionData.isValidSession()) {
117
118          String JavaDoc requestedPO = getComms().request.getRequestURI();
119          getComms().response.writeHTML(
120              this.showErrorPage("Session Expired",
121                                 "You session has expired. Please re-login",
122                                 "Re-login", URL_login, "_parent"));
123       } else {
124         //System.out.println("USER ALREADY LOGGED IN WITH A SESSION");
125
}
126     } catch (Exception JavaDoc ex) {
127       throw new KMSPresentationException("Trouble checking for user login status", ex);
128     }
129   }
130
131   public void run(HttpPresentationComms comms)
132       throws KMSPresentationException {
133
134     // Initialize new or get the existing session data
135
initSessionData(comms);
136
137     // Check if the user needs to be logged in for this request.
138
if(isLoginRequired()) {
139       checkForUserLogin();
140     }
141
142     try {
143       // Handle the incoming event request
144
handleEvent(comms);
145     } catch (HttpPresentationException e) {
146       Logger.writeError("BasePO::run", e);
147       throw new KMSPresentationException("An error has occured. "+
148         "Please contact your administrator. Error message: "+e.getMessage());
149     }
150
151   }
152
153   public void handleEvent(HttpPresentationComms comms)
154       throws HttpPresentationException {
155
156     String JavaDoc returnHTML = null;
157
158     try {
159       if (this.getStringParameter(PARAM_imageData).equals("y")) {
160         String JavaDoc returnMimeType = handleDefault();
161         comms.response.setContentType(returnMimeType);
162         return;
163
164       } else {
165         returnHTML = handleDefault();
166       }
167     } catch (Exception JavaDoc e) {
168       returnHTML = this.showErrorPage("Internal Error",
169                   "An error has occured. Please contact your administrator. "+
170                   "Error message: "+e.getMessage(),
171                   "", "");
172       Logger.writeError("BasePO::handleEvent", e);
173     }
174
175     getComms().response.writeHTML(returnHTML);
176   }
177
178   protected long getLongParameter(String JavaDoc parm)
179       throws HttpPresentationException, NumberFormatException JavaDoc {
180     long res = 0;
181     try {
182       res = Long.parseLong(m_comms.request.getParameter(parm));
183     } catch (NumberFormatException JavaDoc e) {
184       res = -1;
185     }
186     return res;
187   }
188
189   protected int getIntParameter(String JavaDoc parm)
190       throws HttpPresentationException, NumberFormatException JavaDoc {
191     int res = 0;
192     try {
193       res = Integer.parseInt(m_comms.request.getParameter(parm));
194     } catch (NumberFormatException JavaDoc e) {
195       res = -1;
196     }
197     return res;
198   }
199
200   protected String JavaDoc getStringParameter(String JavaDoc parm)
201       throws HttpPresentationException {
202     String JavaDoc value = m_comms.request.getParameter(parm);
203     if ((value == null) || (value.length() == 0)) {
204       value = "";
205     }
206     return value;
207   }
208
209   protected String JavaDoc[] getStringParameterArray(String JavaDoc parm)
210       throws HttpPresentationException {
211     return m_comms.request.getParameterValues(parm);
212   }
213
214   protected void setStringParameter(String JavaDoc name, String JavaDoc value)
215     throws HttpPresentationException {
216
217     m_comms.response.setHeader(name, value);
218   }
219
220   protected long[] getLongParameterArray(String JavaDoc parm)
221       throws HttpPresentationException {
222     String JavaDoc[] strArr = m_comms.request.getParameterValues(parm);
223     long[] longArr = new long[strArr.length];
224     for (int i=0; i<strArr.length; i++) {
225       longArr[i] = Long.parseLong(strArr[i]);
226     }
227     return longArr;
228   }
229
230   protected String JavaDoc showErrorPage(String JavaDoc errorHeader, String JavaDoc errorText,
231       String JavaDoc errorLinkText, String JavaDoc errorLink, String JavaDoc target) {
232     errorPageHTML page = (errorPageHTML)m_comms.xmlcFactory.create(errorPageHTML.class);
233     page.setTextErrorHeader(errorHeader);
234     page.setTextErrorText(errorText);
235     page.setTextErrorLinkText(errorLinkText);
236
237     HTMLAnchorElement fileLink;
238     fileLink = page.getElementErrorLink();
239     if(errorLinkText.equals("") || errorLink.equals("")) {
240       this.removeElement(page, fileLink);
241     } else {
242       fileLink.setHref(errorLink);
243       if(!target.equals("")) {
244         fileLink.setTarget(target);
245       }
246     }
247     return page.toDocument();
248   }
249
250   protected String JavaDoc showErrorPage(String JavaDoc errorHeader, String JavaDoc errorText,
251       String JavaDoc errorLinkText, String JavaDoc errorLink) {
252     return showErrorPage(errorHeader, errorText, errorLinkText, errorLink, "");
253   }
254
255   protected String JavaDoc showCompletionPage(String JavaDoc header, String JavaDoc text,
256       String JavaDoc linkText, String JavaDoc link) {
257     messagePageHTML page = (messagePageHTML)m_comms.xmlcFactory.create(messagePageHTML.class);
258     page.setTextMessageHeader(header);
259     page.setTextMessageText(text);
260     page.setTextMessageLinkText(linkText);
261
262     HTMLAnchorElement fileLink = page.getElementMessageLink();
263     if(linkText.equals("") || link.equals("")) {
264       this.removeElement(page, fileLink);
265     } else {
266       fileLink.setHref(link);
267       /*
268       if(!target.equals("")) {
269         fileLink.setTarget(target);
270       }
271       */

272     }
273     return page.toDocument();
274   }
275
276   protected void removeElement(org.enhydra.xml.xmlc.html.HTMLObjectImpl page,
277                                Element currElement) {
278     if (currElement != null) {
279       Node parentNode = currElement.getParentNode();
280       parentNode.removeChild(currElement);
281     } else {
282         Logger.writeDebug("Error: unfound element "+currElement.getLocalName());
283     }
284   }
285
286   protected void removeElement(org.enhydra.xml.xmlc.html.HTMLObjectImpl page,
287                                java.util.HashSet JavaDoc elementSet) {
288     Element currElement;
289     java.util.Iterator JavaDoc it = elementSet.iterator();
290     Node parentNode;
291     while (it.hasNext()) {
292       currElement = (Element) it.next();
293       if (currElement != null) {
294         parentNode = currElement.getParentNode();
295         parentNode.removeChild(currElement);
296       } else {
297         Logger.writeDebug("Error: unfound element "+currElement.getLocalName());
298       }
299     }
300   }
301
302   protected void processSelectList(org.enhydra.xml.xmlc.html.HTMLObjectImpl page,
303       HTMLSelectElement lookUpElement, Vector JavaDoc displayList, Vector JavaDoc valueList,
304       String JavaDoc selectedItem) {
305
306     // remove the hard coded drop down elements
307
for (int i=lookUpElement.getLength()-1; i>=0; i--) {
308       lookUpElement.remove(i);
309     }
310
311     for(int i=0; i<valueList.size(); i++) {
312       // Create and append options, set to select if matches value
313
HTMLOptionElement o1 = (HTMLOptionElement) page.createElement("OPTION");
314       o1.setValue((String JavaDoc)valueList.elementAt(i));
315       o1.appendChild(page.createTextNode((String JavaDoc)displayList.elementAt(i)));
316       if (selectedItem.equals((String JavaDoc)valueList.elementAt(i))) {
317         o1.setAttribute("selected","selected");
318       }
319       lookUpElement.appendChild(o1);
320     }
321   }
322 }
323
Popular Tags