1 40 package org.dspace.app.webui.util; 41 42 import java.io.PrintWriter ; 43 import java.io.StringWriter ; 44 import java.sql.SQLException ; 45 import java.util.Date ; 46 import java.util.Enumeration ; 47 import java.net.URLEncoder ; 48 49 import javax.servlet.http.HttpServletRequest ; 50 import javax.servlet.http.HttpSession ; 51 52 import org.apache.log4j.Logger; 53 import org.dspace.content.Collection; 54 import org.dspace.content.Community; 55 import org.dspace.content.DCDate; 56 import org.dspace.core.ConfigurationManager; 57 import org.dspace.core.Constants; 58 import org.dspace.core.Context; 59 import org.dspace.core.Email; 60 import org.dspace.eperson.EPerson; 61 import org.dspace.eperson.AuthenticationManager; 62 63 69 public class UIUtil 70 { 71 72 private static Logger log = Logger.getLogger(UIUtil.class); 73 74 85 public static Context obtainContext(HttpServletRequest request) 86 throws SQLException 87 { 88 89 try 95 { 96 if(request.getCharacterEncoding()==null) 97 request.setCharacterEncoding(Constants.DEFAULT_ENCODING); 98 } 99 catch(Exception e) 100 { 101 log.error("Unable to set encoding to UTF-8.", e); 102 } 103 104 Context c = (Context) request.getAttribute("dspace.context"); 105 106 if (c == null) 107 { 108 c = new Context(); 110 111 HttpSession session = request.getSession(); 113 Integer userID = (Integer ) session.getAttribute( 114 "dspace.current.user.id"); 115 116 if (userID != null) 117 { 118 String remAddr = (String )session.getAttribute("dspace.current.remote.addr"); 119 if (remAddr != null && remAddr.equals(request.getRemoteAddr())) 120 { 121 EPerson e = EPerson.find(c, userID.intValue()); 122 123 Authenticate.loggedIn(c, request, e); 124 } 125 else 126 { 127 log.warn("POSSIBLE HIJACKED SESSION: request from "+ 128 request.getRemoteAddr()+" does not match original "+ 129 "session address: "+remAddr+". Authentication rejected."); 130 } 131 } 132 133 int[] groupIDs = AuthenticationManager.getSpecialGroups(c, request); 135 136 for (int i = 0; i < groupIDs.length; i++) 137 { 138 c.setSpecialGroup(groupIDs[i]); 139 log.debug("Adding Special Group id="+String.valueOf(groupIDs[i])); 140 } 141 142 c.setExtraLogInfo("session_id=" + request.getSession().getId() + ":ip_addr=" + request.getRemoteAddr()); 144 145 request.setAttribute("dspace.context", c); 147 } 148 149 return c; 150 } 151 152 162 public static Community getCommunityLocation(HttpServletRequest request) 163 { 164 return ((Community) request.getAttribute("dspace.community")); 165 } 166 167 177 public static Collection getCollectionLocation(HttpServletRequest request) 178 { 179 return ((Collection) request.getAttribute("dspace.collection")); 180 } 181 182 191 public static void storeOriginalURL(HttpServletRequest request) 192 { 193 String orig = (String ) request.getAttribute("dspace.original.url"); 194 195 if (orig == null) 196 { 197 String fullURL = request.getRequestURL().toString(); 198 199 if (request.getQueryString() != null) 200 { 201 fullURL = fullURL + "?" + request.getQueryString(); 202 } 203 204 request.setAttribute("dspace.original.url", fullURL); 205 } 206 } 207 208 216 public static String getOriginalURL(HttpServletRequest request) 217 { 218 storeOriginalURL(request); 220 221 return ((String ) request.getAttribute("dspace.original.url")); 222 } 223 224 233 public static String nonBreakSpace(String s) 234 { 235 StringBuffer newString = new StringBuffer (); 236 237 for (int i = 0; i < s.length(); i++) 238 { 239 char ch = s.charAt(i); 240 241 if (ch == ' ') 242 { 243 newString.append(" "); 244 } 245 else 246 { 247 newString.append(ch); 248 } 249 } 250 251 return newString.toString(); 252 } 253 254 266 public static String displayDate(DCDate d, boolean time, boolean localTime) 267 { 268 StringBuffer sb = new StringBuffer (); 269 270 if (d != null) 271 { 272 int year; 273 int month; 274 int day; 275 int hour; 276 int minute; 277 int second; 278 279 if (localTime) 280 { 281 year = d.getYear(); 282 month = d.getMonth(); 283 day = d.getDay(); 284 hour = d.getHour(); 285 minute = d.getMinute(); 286 second = d.getSecond(); 287 } 288 else 289 { 290 year = d.getYearGMT(); 291 month = d.getMonthGMT(); 292 day = d.getDayGMT(); 293 hour = d.getHourGMT(); 294 minute = d.getMinuteGMT(); 295 second = d.getSecondGMT(); 296 } 297 298 if (year > -1) 299 { 300 if (month > -1) 301 { 302 if (day > -1) 303 { 304 sb.append(day + "-"); 305 } 306 307 sb.append(DCDate.getMonthName(month).substring(0, 3) + "-"); 308 } 309 310 sb.append(year + " "); 311 } 312 313 if (time && (hour > -1)) 314 { 315 String hr = String.valueOf(hour); 316 317 while (hr.length() < 2) 318 { 319 hr = "0" + hr; 320 } 321 322 String mn = String.valueOf(minute); 323 324 while (mn.length() < 2) 325 { 326 mn = "0" + mn; 327 } 328 329 String sc = String.valueOf(second); 330 331 while (sc.length() < 2) 332 { 333 sc = "0" + sc; 334 } 335 336 sb.append(hr + ":" + mn + ":" + sc + " "); 337 } 338 } 339 else 340 { 341 sb.append("Unset"); 342 } 343 344 return (sb.toString()); 345 } 346 347 355 public static String getRequestLogInfo(HttpServletRequest request) 356 { 357 String report; 358 359 report = "-- URL Was: " + getOriginalURL(request) + "\n"; 360 report = report + "-- Method: " + request.getMethod() + "\n"; 361 362 report = report + "-- Parameters were:\n"; 364 365 Enumeration e = request.getParameterNames(); 366 367 while (e.hasMoreElements()) 368 { 369 String name = (String ) e.nextElement(); 370 371 if (name.equals("login_password")) 372 { 373 report = report + "-- " + name + ": *not logged*\n"; 376 } 377 else 378 { 379 report = report + "-- " + name + ": \"" 380 + request.getParameter(name) + "\"\n"; 381 } 382 } 383 384 return report; 385 } 386 387 398 public static int getIntParameter(HttpServletRequest request, String param) 399 { 400 String val = request.getParameter(param); 401 402 try 403 { 404 return Integer.parseInt(val); 405 } 406 catch (Exception e) 407 { 408 return -1; 410 } 411 } 412 413 425 public static int[] getIntParameters(HttpServletRequest request, 426 String param) 427 { 428 String [] request_values = request.getParameterValues(param); 429 430 if (request_values == null) 431 { 432 return null; 433 } 434 435 int[] return_values = new int[request_values.length]; 436 437 for (int x = 0; x < return_values.length; x++) 438 { 439 try 440 { 441 return_values[x] = Integer.parseInt(request_values[x]); 442 } 443 catch (Exception e) 444 { 445 return_values[x] = -1; 447 } 448 } 449 450 return return_values; 451 } 452 453 465 public static boolean getBoolParameter(HttpServletRequest request, 466 String param) 467 { 468 return ((request.getParameter(param) != null) && request.getParameter( 469 param).equals("true")); 470 } 471 472 485 public static String getSubmitButton(HttpServletRequest request, String def) 486 { 487 Enumeration e = request.getParameterNames(); 488 489 while (e.hasMoreElements()) 490 { 491 String parameterName = (String ) e.nextElement(); 492 493 if (parameterName.startsWith("submit")) 494 { 495 return parameterName; 496 } 497 } 498 499 return def; 500 } 501 502 520 public static void sendAlert(HttpServletRequest request, Exception exception) 521 { 522 String logInfo = UIUtil.getRequestLogInfo(request); 523 524 try 525 { 526 String recipient = ConfigurationManager 527 .getProperty("alert.recipient"); 528 529 if (recipient != null) 530 { 531 Email email = ConfigurationManager.getEmail("internal_error"); 532 533 email.addRecipient(recipient); 534 email.addArgument(ConfigurationManager 535 .getProperty("dspace.url")); 536 email.addArgument(new Date ()); 537 email.addArgument(request.getSession().getId()); 538 email.addArgument(logInfo); 539 540 String stackTrace; 541 542 if (exception != null) 543 { 544 StringWriter sw = new StringWriter (); 545 PrintWriter pw = new PrintWriter (sw); 546 exception.printStackTrace(pw); 547 pw.flush(); 548 stackTrace = sw.toString(); 549 } 550 else 551 { 552 stackTrace = "No exception"; 553 } 554 555 email.addArgument(stackTrace); 556 email.send(); 557 } 558 } 559 catch (Exception e) 560 { 561 log.warn("Unable to send email alert", e); 563 } 564 } 565 566 584 public static String encodeBitstreamName(String stringIn, String encoding) 585 throws java.io.UnsupportedEncodingException 586 { 587 StringBuffer out = new StringBuffer (); 589 590 final String [] pctEncoding = { "%00", "%01", "%02", "%03", "%04", 591 "%05", "%06", "%07", "%08", "%09", "%0a", "%0b", "%0c", "%0d", 592 "%0e", "%0f", "%10", "%11", "%12", "%13", "%14", "%15", "%16", 593 "%17", "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f", 594 "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27", "%28", 595 "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f", "%30", "%31", 596 "%32", "%33", "%34", "%35", "%36", "%37", "%38", "%39", "%3a", 597 "%3b", "%3c", "%3d", "%3e", "%3f", "%40", "%41", "%42", "%43", 598 "%44", "%45", "%46", "%47", "%48", "%49", "%4a", "%4b", "%4c", 599 "%4d", "%4e", "%4f", "%50", "%51", "%52", "%53", "%54", "%55", 600 "%56", "%57", "%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", 601 "%5f", "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67", 602 "%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f", "%70", 603 "%71", "%72", "%73", "%74", "%75", "%76", "%77", "%78", "%79", 604 "%7a", "%7b", "%7c", "%7d", "%7e", "%7f", "%80", "%81", "%82", 605 "%83", "%84", "%85", "%86", "%87", "%88", "%89", "%8a", "%8b", 606 "%8c", "%8d", "%8e", "%8f", "%90", "%91", "%92", "%93", "%94", 607 "%95", "%96", "%97", "%98", "%99", "%9a", "%9b", "%9c", "%9d", 608 "%9e", "%9f", "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", 609 "%a7", "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af", 610 "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7", "%b8", 611 "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf", "%c0", "%c1", 612 "%c2", "%c3", "%c4", "%c5", "%c6", "%c7", "%c8", "%c9", "%ca", 613 "%cb", "%cc", "%cd", "%ce", "%cf", "%d0", "%d1", "%d2", "%d3", 614 "%d4", "%d5", "%d6", "%d7", "%d8", "%d9", "%da", "%db", "%dc", 615 "%dd", "%de", "%df", "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", 616 "%e6", "%e7", "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", 617 "%ef", "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7", 618 "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff" }; 619 620 byte[] bytes = stringIn.getBytes(encoding); 621 622 for (int i = 0; i < bytes.length; i++) 623 { 624 if ((bytes[i] >= 'A' && bytes[i] <= 'Z') 626 || (bytes[i] >= 'a' && bytes[i] <= 'z') 627 || (bytes[i] >= '0' && bytes[i] <= '9') || bytes[i] == '-' 628 || bytes[i] == '.' || bytes[i] == '_' || bytes[i] == '~' 629 || bytes[i] == '/') 630 { 631 out.append((char) bytes[i]); 632 } 633 else if (bytes[i] >= 0) 634 { 635 out.append(pctEncoding[bytes[i]]); 637 } 638 else 639 { 640 out.append(pctEncoding[256 + bytes[i]]); 643 } 644 } 645 log.debug("encoded \"" + stringIn + "\" to \"" + out.toString() + "\""); 646 647 return out.toString(); 648 } 649 650 656 public static String encodeBitstreamName(String stringIn) 657 throws java.io.UnsupportedEncodingException 658 { 659 return encodeBitstreamName(stringIn, Constants.DEFAULT_ENCODING); 660 } 661 } 662 | Popular Tags |