1 32 33 package scioworks.imap.presentation.base; 34 35 import java.util.Vector ; 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 handleDefault() 59 throws HttpPresentationException, ServerPageRedirectException; 60 61 64 protected boolean isLoginRequired() { 65 return true; 66 } 67 68 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 Object obj = getComms().sessionData.get(ImapWebSessionData.SESSION_KEY); 92 93 if(null != obj) { 95 m_ImapWebSessionData = (ImapWebSessionData) obj; 96 97 } else { 98 99 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 112 protected void checkForUserLogin() 113 throws KMSPresentationException { 114 115 try { 116 if (!m_ImapWebSessionData.isValidSession()) { 117 118 String 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 } 126 } catch (Exception 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 initSessionData(comms); 136 137 if(isLoginRequired()) { 139 checkForUserLogin(); 140 } 141 142 try { 143 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 returnHTML = null; 157 158 try { 159 if (this.getStringParameter(PARAM_imageData).equals("y")) { 160 String returnMimeType = handleDefault(); 161 comms.response.setContentType(returnMimeType); 162 return; 163 164 } else { 165 returnHTML = handleDefault(); 166 } 167 } catch (Exception 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 parm) 179 throws HttpPresentationException, NumberFormatException { 180 long res = 0; 181 try { 182 res = Long.parseLong(m_comms.request.getParameter(parm)); 183 } catch (NumberFormatException e) { 184 res = -1; 185 } 186 return res; 187 } 188 189 protected int getIntParameter(String parm) 190 throws HttpPresentationException, NumberFormatException { 191 int res = 0; 192 try { 193 res = Integer.parseInt(m_comms.request.getParameter(parm)); 194 } catch (NumberFormatException e) { 195 res = -1; 196 } 197 return res; 198 } 199 200 protected String getStringParameter(String parm) 201 throws HttpPresentationException { 202 String value = m_comms.request.getParameter(parm); 203 if ((value == null) || (value.length() == 0)) { 204 value = ""; 205 } 206 return value; 207 } 208 209 protected String [] getStringParameterArray(String parm) 210 throws HttpPresentationException { 211 return m_comms.request.getParameterValues(parm); 212 } 213 214 protected void setStringParameter(String name, String value) 215 throws HttpPresentationException { 216 217 m_comms.response.setHeader(name, value); 218 } 219 220 protected long[] getLongParameterArray(String parm) 221 throws HttpPresentationException { 222 String [] 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 showErrorPage(String errorHeader, String errorText, 231 String errorLinkText, String errorLink, String 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 showErrorPage(String errorHeader, String errorText, 251 String errorLinkText, String errorLink) { 252 return showErrorPage(errorHeader, errorText, errorLinkText, errorLink, ""); 253 } 254 255 protected String showCompletionPage(String header, String text, 256 String linkText, String 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 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 elementSet) { 288 Element currElement; 289 java.util.Iterator 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 displayList, Vector valueList, 304 String selectedItem) { 305 306 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 HTMLOptionElement o1 = (HTMLOptionElement) page.createElement("OPTION"); 314 o1.setValue((String )valueList.elementAt(i)); 315 o1.appendChild(page.createTextNode((String )displayList.elementAt(i))); 316 if (selectedItem.equals((String )valueList.elementAt(i))) { 317 o1.setAttribute("selected","selected"); 318 } 319 lookUpElement.appendChild(o1); 320 } 321 } 322 } 323 | Popular Tags |