1 33 34 package com.icesoft.applications.faces.auctionMonitor.beans; 35 36 import com.icesoft.applications.faces.auctionMonitor.ChatState; 37 import com.icesoft.applications.faces.auctionMonitor.Message; 38 import com.icesoft.applications.faces.auctionMonitor.MessageLog; 39 import com.icesoft.faces.async.render.OnDemandRenderer; 40 import com.icesoft.faces.async.render.RenderManager; 41 import com.icesoft.faces.async.render.Renderable; 42 import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState; 43 import com.icesoft.faces.webapp.xmlhttp.RenderingException; 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 47 import javax.faces.context.ExternalContext; 48 import javax.faces.context.FacesContext; 49 import java.net.MalformedURLException ; 50 import java.net.URL ; 51 import java.util.Map ; 52 53 57 public class UserBean implements Renderable { 58 private static final String DEFAULT_NICK = "Anonymous"; 59 private static final String MINIMIZE_IMAGE = 60 "./images/button_triangle_close.gif"; 61 private static final String MAXIMIZE_IMAGE = 62 "./images/button_triangle_open.gif"; 63 private static final int NUM_MESSAGES = 64 4; private static final int FLOOD_CAP = 66 500; 68 private static final String MESSAGE_SEND = "send"; 69 70 private static final ChatState chatState = ChatState.getInstance(); 71 private static Log log = LogFactory.getLog(UserBean.class); 72 73 private OnDemandRenderer renderer = null; 74 private PersistentFacesState persistentState = 75 PersistentFacesState.getInstance(); 76 private ExternalContext externalContext = 77 FacesContext.getCurrentInstance().getExternalContext(); 78 private String autoLoad = " "; 79 private String message = ""; 80 private String color = ChatState.DEFAULT_COLOR; 81 private String nick = DEFAULT_NICK; 82 private String buttonImage = MINIMIZE_IMAGE; private MessageLog messageLog = getMessageLog(); 84 private Message[] pageLog = new Message[NUM_MESSAGES]; 85 private long lastMessageTime = System.currentTimeMillis(); 86 private int position = 0; 87 private boolean minimized = true; private boolean inConversation = false; private boolean leaving = 91 false; 95 public boolean isMinimized() { 96 return (minimized); 97 } 98 99 public boolean getConversationStatus() { 100 return (inConversation); 101 } 102 103 public String getColor() { 104 return (color); 105 } 106 107 public String getMessage() { 108 return (message); 109 } 110 111 public String getButtonImage() { 112 return (buttonImage); 113 } 114 115 public String getNick() { 116 return (nick); 117 } 118 119 public String getNickFormatted() { 120 return (nick + ": "); 121 } 122 123 public String getAutoLoad() { 124 if (this.autoLoad.equals(" ")) { 125 this.autoLoad = "UserBean-Loaded"; 126 } 127 return (this.autoLoad); 128 } 129 130 public String getChatStatus() { 131 int numParticipants = chatState.getNumParticipants(); 132 if (numParticipants > 1) { 133 return ("Chatting as " + nick + " (" + numParticipants + 134 " users online)"); 135 } 136 return ("Chatting as " + nick + " (" + numParticipants + 137 " user online)"); 138 } 139 140 private Message getMessageAt(int index) { 141 if ((index < messageLog.size()) && (index >= 0)) { 143 return ((Message) messageLog.get(index)); 144 } 145 return (null); 146 } 147 148 private MessageLog getMessageLog() { 149 Map applicationMap = externalContext.getApplicationMap(); 151 messageLog = (MessageLog) applicationMap.get(LogBean.LOG_PATH); 152 153 if (messageLog == null) { 155 messageLog = LogBean.getInstance().storeMessageLog(); 156 } 157 158 return (messageLog); 159 } 160 161 public Message[] getPageLog() { 162 for (int i = 0; i < NUM_MESSAGES; i++) { 164 pageLog[i] = getMessageAt(position - (NUM_MESSAGES - i - 1)); 165 } 166 167 return (pageLog); 168 } 169 170 public void setMessage(String pageMessage) { 171 if (!leaving) { 172 addMessage(pageMessage); 173 } 174 } 175 176 public void setNick(String nick) { 177 nick = cleanTags(nick).trim(); 179 180 if (nick.length() <= 0) { 182 nick = DEFAULT_NICK; 183 } else if (nick.length() > 20) { 184 nick = nick.substring(0, 20); 185 } 186 187 this.nick = nick; 189 enterConversation(); 190 } 191 192 public void setColor(String color) { 193 this.color = color; 194 } 195 196 202 private String addMessage(String toAdd) { 203 if ((toAdd == null) || (toAdd.trim().length() == 0)) { 205 message = ""; 206 return MESSAGE_SEND; 207 } 208 209 if ((System.currentTimeMillis() - lastMessageTime) < FLOOD_CAP) { 211 lastMessageTime = System.currentTimeMillis(); 212 message = ""; 213 return MESSAGE_SEND; 214 } 215 216 toAdd = trimLength(toAdd); 218 toAdd = cleanTags(toAdd); 219 toAdd = urlRecognize(toAdd); 220 221 messageLog.addMessage(nick, toAdd, color); 223 lastMessageTime = System.currentTimeMillis(); 224 message = ""; 225 updateMessageLog(); 226 227 return MESSAGE_SEND; 228 } 229 230 237 private String trimLength(String trim) { 238 if (trim.length() > 150) { 239 trim = (trim.substring(0, 150) + "..."); 240 } 241 242 return trim; 243 } 244 245 252 private String cleanTags(String clean) { 253 clean = clean.replaceAll("<", "<"); 254 clean = clean.replaceAll(">", ">"); 255 clean = clean.replaceAll("&", "&"); 256 257 return (clean); 258 } 259 260 267 private String urlRecognize(String pad) { 268 int index = pad.indexOf("http://"); 269 270 if (index == -1) { 272 return (pad); 273 } 274 275 String before, after = ""; 277 278 URL checker; 280 try { 281 int spaceIndex = pad.indexOf(" ", index); 283 284 before = pad.substring(0, index); 285 if (spaceIndex != -1) { 286 after = pad.substring(spaceIndex, pad.length()); 287 } 288 if (spaceIndex != -1) { 289 pad = pad.substring(index, spaceIndex); 290 } else { 291 pad = pad.substring(index); 292 } 293 checker = new URL (pad); 294 } catch (MalformedURLException mue) { 295 return pad; 296 } 297 298 pad = checker.toString(); 300 pad = ("<A HREF=\"" + pad + "\" target=\"_blank\">" + pad + "</A>"); 301 pad = before + pad + after; 302 return (pad); 303 } 304 305 310 private int bottom() { 311 if (messageLog != null) { 312 return (messageLog.size() - 1); 313 } 314 return 0; 315 } 316 317 321 public void enterConversation() { 322 chatState.addUserChild(this); 323 buttonImage = MAXIMIZE_IMAGE; 324 minimized = false; 325 position = bottom(); 326 inConversation = true; 327 leaving = false; 328 } 329 330 334 public void leaveConversation() { 335 leaving = true; 336 inConversation = false; 337 buttonImage = MINIMIZE_IMAGE; 338 minimized = true; 339 chatState.removeUserChild(this); 340 } 341 342 348 public String moveUpMinor() { 349 if ((position - 1) - (NUM_MESSAGES - 1) >= 0) { 350 position--; 351 } 352 353 return ("moveUpMinor"); 354 } 355 356 362 public String moveDownMinor() { 363 if (messageLog.size() > (position + 1)) { 364 position++; 365 } 366 367 return ("moveDownMinor"); 368 } 369 370 375 public String moveToTop() { 376 if (position >= NUM_MESSAGES) { 377 position = (NUM_MESSAGES - 1); 378 } 379 380 return ("moveToTop"); 381 } 382 383 388 public String moveToBottom() { 389 position = bottom(); 390 391 return ("moveToBottom"); 392 } 393 394 399 public String switchMinimized() { 400 minimized = !minimized; 401 402 if (minimized) { 404 buttonImage = MINIMIZE_IMAGE; 405 } else { 406 buttonImage = MAXIMIZE_IMAGE; 407 position = bottom(); 408 } 409 reRender(); 410 411 return ("switchMinimized"); 412 } 413 414 418 private void updateMessageLog() { 419 chatState.updateAll(); 420 position = bottom(); 421 } 422 423 432 public void reRender() { 433 position = bottom(); 434 435 if (renderer != null) { 436 renderer.requestRender(); 437 } else { 438 if (log.isDebugEnabled()) { 439 log.debug("OnDemandRenderer was not available (it was null)"); 440 } 441 } 442 } 443 444 450 public void setRenderManager(RenderManager manager) { 451 if (manager != null) { 452 renderer = manager.getOnDemandRenderer(AuctionBean.RENDERER_NAME); 453 renderer.add(this); 454 } 455 } 456 457 460 public RenderManager getRenderManager() { 461 return null; 462 } 463 464 470 public PersistentFacesState getState() { 471 return persistentState; 472 } 473 474 480 public void renderingException(RenderingException renderingException) { 481 if (log.isDebugEnabled()) { 482 log.debug("Rendering exception called because of " + 483 renderingException); 484 } 485 486 if (renderer != null) { 487 renderer.remove(this); 488 renderer = null; 489 } 490 491 chatState.removeUserChild(this); 492 } 493 } 494 | Popular Tags |