1 25 package org.nemesis.forum.webapp.front; 26 27 import java.text.SimpleDateFormat ; 28 import java.util.Date ; 29 30 import javax.servlet.http.Cookie ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpSession ; 34 35 import org.nemesis.forum.Authorization; 36 import org.nemesis.forum.AuthorizationFactory; 37 import org.nemesis.forum.Message; 38 import org.nemesis.forum.exception.NotFoundException; 39 import org.nemesis.forum.exception.UnauthorizedException; 40 import org.nemesis.forum.util.CookieManager; 41 import org.nemesis.forum.util.StringUtils; 42 43 46 public class Tools { 47 48 public static final String AUTH_TOKEN = "_Authorization_"; 49 50 51 public static final String AUTOLOGIN_COOKIE = "_AutoLogin_"; 52 53 54 public static final String LASTVISITED_TOKEN = "_LastVisited_"; 55 56 57 public static final String LASTVISITED_COOKIE = "_LastVisited_"; 58 59 60 61 85 public static Authorization getUserAuthorization(HttpServletRequest request, HttpServletResponse response, boolean checkCookie) { 86 HttpSession session = request.getSession(); 88 89 Authorization authToken = (Authorization) session.getAttribute(AUTH_TOKEN); 91 if (authToken != null) { 92 return authToken; 93 } 94 95 if (checkCookie) { 97 Cookie cookie = CookieManager.getCookie(request, AUTOLOGIN_COOKIE); 98 try { 99 if (cookie != null) { 100 String [] values = CookieManager.decodePasswordCookie(cookie.getValue()); 103 String username = values[0]; 104 String password = values[1]; 105 authToken = AuthorizationFactory.getAuthorization(username, password); 107 108 session.setAttribute(AUTH_TOKEN, authToken); 110 111 return authToken; 113 } 114 } catch (Exception e) { 115 CookieManager.setCookie(response, AUTOLOGIN_COOKIE, null, 0); 121 } 122 } 123 124 return null; 126 } 127 128 140 public static Authorization getUserAuthorization(HttpServletRequest request, HttpServletResponse response) { 141 return getUserAuthorization(request, response, true); 142 } 143 144 158 public static Authorization setUserAuthorization( 159 HttpServletRequest request, 160 HttpServletResponse response, 161 String username, 162 String password, 163 boolean autoLogin) 164 throws NotFoundException, UnauthorizedException { 165 HttpSession session = request.getSession(); 166 Authorization authToken = AuthorizationFactory.getAuthorization(username, password); 167 session.setAttribute(AUTH_TOKEN, authToken); 168 169 if (autoLogin) { 170 CookieManager.setCookie(response, AUTOLOGIN_COOKIE, CookieManager.encodePasswordCookie(username, password), CookieManager.MAX_COOKIE_AGE); 171 } 172 173 return authToken; 174 } 175 176 182 public static void removeUserAuthorization(HttpServletRequest request, HttpServletResponse response) { 183 HttpSession session = request.getSession(); 184 session.removeAttribute(AUTH_TOKEN); 185 186 CookieManager.setCookie(response, AUTOLOGIN_COOKIE, null,0); 187 188 } 189 190 199 public static long getLastVisited(HttpServletRequest request, HttpServletResponse response, boolean updateLastVisitedTime) { 200 HttpSession session = request.getSession(); 202 203 long now = System.currentTimeMillis(); 205 206 String lastTime = (String ) session.getAttribute(LASTVISITED_TOKEN); 208 209 if (lastTime != null) { 211 try { 212 long time = Long.parseLong(lastTime); 213 CookieManager.setCookie(response, LASTVISITED_TOKEN, Long.toString(now), CookieManager.MAX_COOKIE_AGE); 216 return time; 218 } catch (NumberFormatException e) { 219 } 221 } 222 223 long time = now; 226 lastTime = CookieManager.getCookieValue(request, LASTVISITED_TOKEN); 227 if (lastTime != null) { 228 try { 229 time = Long.parseLong(lastTime); 230 } catch (NumberFormatException e) { 231 } 232 } 233 234 236 session.setAttribute(LASTVISITED_TOKEN, Long.toString(time)); 237 CookieManager.setCookie(response, LASTVISITED_TOKEN, Long.toString(now), CookieManager.MAX_COOKIE_AGE); 238 239 return time; 240 } 241 248 public static long getLastVisited(HttpServletRequest request, HttpServletResponse response) { 249 return getLastVisited(request, response, true); 250 } 251 252 private static final long SECOND = 1000; 253 private static final long MINUTE = 60 * SECOND; 254 private static final long HOUR = 60 * MINUTE; 255 private static final long DAY = 24 * HOUR; 256 private static final long WEEK = 7 * DAY; 257 private static final String [] DAYS_OF_WEEK = 259 { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" }; 260 261 private static final SimpleDateFormat dateFormatter = 263 new SimpleDateFormat ("EEEE, MMM d 'at' h:mm a"); 264 private static final SimpleDateFormat yesterdayFormatter = 265 new SimpleDateFormat ("'Yesterday at' h:mm a"); 266 275 public static String dateToText(Date date) { 276 if (date == null) { 277 return ""; 278 } 279 280 long delta = System.currentTimeMillis() - date.getTime(); 281 282 if ((delta / HOUR) < 1) { 284 long minutes = (delta / MINUTE); 285 if (minutes == 0) { 286 return "Less than 1 min ago"; 287 } else if (minutes == 1) { 288 return "1 minute ago"; 289 } else { 290 return (minutes + " minutes ago"); 291 } 292 } 293 294 if ((delta / DAY) < 1) { 296 long hours = (delta / HOUR); 297 if (hours <= 1) { 298 return "1 hour ago"; 299 } else { 300 return (hours + " hours ago"); 301 } 302 } 303 304 if ((delta / WEEK) < 1) { 306 double days = ((double) delta / (double) DAY); 307 if (days <= 1.0) { 308 return yesterdayFormatter.format(date); 309 } else { 310 return dateFormatter.format(date); 311 } 312 } 313 314 else { 316 return dateFormatter.format(date); 317 } 318 } 319 320 321 343 public static String quoteOriginal(String body, String delimiter, int lineLength) { 344 if (body == null || body.length() == 0) { 345 return ""; 346 } 347 int length = body.length(); 348 StringBuffer buf = new StringBuffer (body.length()); 350 for (int i = 0; i < length;) { 352 String partialString = StringUtils.chopAtWord(body.substring(i), lineLength); 353 354 i += partialString.length() + 1; 355 buf.append(delimiter).append(partialString.trim()).append("\\n"); 356 } 357 return buf.toString(); 358 } 359 360 369 public static boolean isNewMessage(Message message, long lastVisited) { 370 if (message.getModifiedDate().getTime() > lastVisited) { 371 return true; 372 } else { 373 return false; 374 } 375 } 376 377 } 378 | Popular Tags |