1 21 22 package org.opensubsystems.core.www; 23 24 import java.io.BufferedInputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.IOException ; 28 import java.io.UnsupportedEncodingException ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.net.URLEncoder ; 32 import java.util.Enumeration ; 33 import java.util.Properties ; 34 import java.util.logging.Logger ; 35 36 import javax.servlet.ServletConfig ; 37 import javax.servlet.ServletContext ; 38 import javax.servlet.ServletException ; 39 import javax.servlet.ServletOutputStream ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 43 import org.opensubsystems.core.util.Config; 44 import org.opensubsystems.core.util.Log; 45 import org.opensubsystems.core.util.MimeTypeConstants; 46 47 55 public final class WebUtils 56 { 57 59 64 public static final String WEBSERVER_PORT = "oss.webserver.port"; 65 66 71 public static final String WEBSERVER_PORT_SECURE = "oss.webserver.port.secure"; 72 73 78 public static final String WEBUTILS_WEBFILE_BUFFER_SIZE = "oss.webserver.servebuffer.size"; 79 80 82 87 public static final int WEBFILE_BUFFER_DEFAULT_SIZE = 40960; 88 89 91 94 private static Logger s_logger = Log.getInstance(WebUtils.class); 95 96 101 protected static int s_iWebFileBufferSize; 102 103 109 protected static int s_iConfiguredWebPort; 110 111 114 protected static int s_iWebPort; 115 116 122 protected static int s_iConfiguredWebPortSecure; 123 124 127 protected static int s_iWebPortSecure; 128 129 131 134 static 135 { 136 Properties prpSettings; 137 138 prpSettings = Config.getInstance().getPropertiesSafely(); 139 140 s_iWebFileBufferSize = Config.getIntPropertyInRange( 142 prpSettings, 143 WEBUTILS_WEBFILE_BUFFER_SIZE, 144 WEBFILE_BUFFER_DEFAULT_SIZE, 145 "Size of a buffer to serve files ", 146 4096, 148 100000000); 151 152 s_iConfiguredWebPortSecure = Config.getIntPropertyInRange( 155 prpSettings, 156 WEBSERVER_PORT_SECURE, 157 WebCommonConstants.HTTP_SECURE_PORT_DEFAULT, 158 "Web server port", 159 WebCommonConstants.HTTP_PORT_MIN, 160 WebCommonConstants.HTTP_PORT_MAX); 161 s_iWebPortSecure = s_iConfiguredWebPortSecure; 162 163 164 s_iConfiguredWebPort = Config.getIntPropertyInRange( 167 prpSettings, 168 WEBSERVER_PORT, 169 WebCommonConstants.HTTP_PORT_DEFAULT, 170 "Web server port", 171 WebCommonConstants.HTTP_PORT_MIN, 172 WebCommonConstants.HTTP_PORT_MAX); 173 s_iWebPort = s_iConfiguredWebPort; 174 } 175 176 179 private WebUtils( 180 ) 181 { 182 } 184 185 187 192 public static int getConfiguredPort( 193 ) 194 { 195 return s_iConfiguredWebPort; 196 } 197 198 203 public static int getActualPort( 204 ) 205 { 206 return s_iWebPort; 207 } 208 209 214 public static int getConfiguredSSLPort( 215 ) 216 { 217 return s_iConfiguredWebPortSecure; 218 } 219 220 225 public static int getActualSSLPort( 226 ) 227 { 228 return s_iWebPortSecure; 229 } 230 231 238 public static void serveFile( 239 HttpServletResponse hsrpResponse, 240 String strRealPath 241 ) throws IOException 242 { 243 247 ServletOutputStream sosOut = hsrpResponse.getOutputStream(); 248 byte[] arBuffer = new byte[s_iWebFileBufferSize]; 249 File flImage = new File (strRealPath); 250 FileInputStream fisReader = new FileInputStream (flImage); 251 252 String strExt = strRealPath.substring(strRealPath.lastIndexOf(".") + 1, 254 strRealPath.length()); 255 hsrpResponse.setContentType(MimeTypeConstants.getMimeType(strExt)); 257 hsrpResponse.setBufferSize(WEBFILE_BUFFER_DEFAULT_SIZE); 258 hsrpResponse.setContentLength((int)flImage.length()); 259 260 BufferedInputStream bisReader = new BufferedInputStream (fisReader, 265 s_iWebFileBufferSize); 266 int iRead; 267 268 try 269 { 270 while (true) 271 { 272 iRead = bisReader.read(arBuffer); 273 if (iRead != -1) 274 { 275 sosOut.write(arBuffer, 0, iRead); 276 } 277 else 278 { 279 break; 280 } 281 } 282 } 283 finally 284 { 285 try 286 { 287 fisReader.close(); 288 } 289 finally 290 { 291 sosOut.close(); 292 } 293 } 294 } 295 296 303 public static String getFullRequestURL( 304 HttpServletRequest hsrqRequest 305 ) 306 { 307 String strQueryString; 308 StringBuffer sbURL; 309 310 strQueryString = hsrqRequest.getQueryString(); 311 if (strQueryString == null) 312 { 313 sbURL = new StringBuffer (); 314 } 315 else 316 { 317 sbURL = new StringBuffer ("?"); 318 sbURL.append(strQueryString); 319 } 320 321 return hsrqRequest.getRequestURL().append(sbURL).toString(); 322 } 323 324 331 public static String getRequestURLWithoutQuery( 332 HttpServletRequest hsrqRequest 333 ) 334 { 335 return hsrqRequest.getRequestURL().toString(); 336 } 337 338 345 public static String getFullRequestPath( 346 HttpServletRequest hsrqRequest 347 ) 348 { 349 StringBuffer sbPath; 350 String strPath; 351 String strExtraPath; 352 353 strPath = hsrqRequest.getServletPath(); 354 if (strPath == null) 355 { 356 strPath = ""; 357 } 358 strExtraPath = hsrqRequest.getPathInfo(); 359 if (strExtraPath != null) 360 { 361 sbPath = new StringBuffer (strPath); 362 sbPath.append(strExtraPath); 363 strPath = sbPath.toString(); 364 } 365 366 return strPath; 367 } 368 369 374 public static void adjust( 375 HttpServletRequest hsrqRequest 376 ) 377 { 378 if (hsrqRequest.isSecure()) 379 { 380 s_iWebPortSecure = hsrqRequest.getServerPort(); 381 } 382 else 383 { 384 s_iWebPort = hsrqRequest.getServerPort(); 385 } 386 } 387 388 398 public static String toggleSecure( 399 HttpServletRequest hsrqRequest, 400 String strURL, 401 boolean bIsSecure 402 ) 403 { 404 URL uURL = null; 405 URL uSwitchedURL = null; 406 407 String strUrlReturn = strURL; 408 409 try 411 { 412 uURL = new URL (strURL); 414 strUrlReturn = uURL.toExternalForm(); 417 418 if (bIsSecure) 423 { 424 if (uURL.getProtocol().equals(WebCommonConstants.PROTOCOL_HTTP)) 428 { 429 uSwitchedURL = new URL (WebCommonConstants.PROTOCOL_HTTPS, 431 uURL.getHost(), 432 s_iWebPortSecure, 433 uURL.getFile()); 434 strUrlReturn = uSwitchedURL.toExternalForm(); 437 } 438 } 439 else 440 { 441 if (uURL.getProtocol().equals(WebCommonConstants.PROTOCOL_HTTPS)) 445 { 446 uSwitchedURL = new URL (WebCommonConstants.PROTOCOL_HTTP, 448 uURL.getHost(), 449 s_iWebPort, 450 uURL.getFile()); 451 strUrlReturn = uSwitchedURL.toExternalForm(); 454 } 455 } 456 } 457 catch (MalformedURLException excMURLE) 458 { 459 s_logger.warning("Error occured while constructing URL."); 461 } 462 463 return strUrlReturn; 464 } 465 466 474 public static String debug( 475 HttpServletRequest hsrqRequest 476 ) 477 { 478 Enumeration enumNames; 479 String strParam; 480 String [] arParams; 481 int iIndex; 482 StringBuffer sbfReturn = new StringBuffer (); 483 484 sbfReturn.append("HttpServletRequest=["); 485 sbfReturn.append("FullURL="); 486 sbfReturn.append(getFullRequestURL(hsrqRequest)); 487 sbfReturn.append(";"); 488 for (enumNames = hsrqRequest.getParameterNames(); 489 enumNames.hasMoreElements();) 490 { 491 strParam = (String )enumNames.nextElement(); 492 arParams = hsrqRequest.getParameterValues(strParam); 493 sbfReturn.append("Param="); 494 sbfReturn.append(strParam); 495 sbfReturn.append(" values="); 496 for (iIndex = 0; iIndex < arParams.length; iIndex++) 497 { 498 sbfReturn.append(arParams[iIndex]); 499 if (iIndex < (arParams.length - 1)) 500 { 501 sbfReturn.append(";"); 502 } 503 } 504 if (enumNames.hasMoreElements()) 505 { 506 sbfReturn.append(";"); 507 } 508 } 509 sbfReturn.append("]"); 510 511 return sbfReturn.toString(); 512 } 513 514 522 public static String debug( 523 ServletConfig scConfig 524 ) 525 { 526 Enumeration enumNames; 527 String strParam; 528 StringBuffer sbfReturn = new StringBuffer (); 529 530 sbfReturn.append("ServletConfig=[ServletName="); 531 sbfReturn.append(scConfig.getServletName()); 532 sbfReturn.append(";"); 533 for (enumNames = scConfig.getInitParameterNames(); 534 enumNames.hasMoreElements();) 535 { 536 strParam = (String )enumNames.nextElement(); 537 sbfReturn.append("Param="); 538 sbfReturn.append(strParam); 539 sbfReturn.append(" value="); 540 sbfReturn.append(scConfig.getInitParameter(strParam)); 541 if (enumNames.hasMoreElements()) 542 { 543 sbfReturn.append(";"); 544 } 545 } 546 sbfReturn.append("]"); 547 548 return sbfReturn.toString(); 549 } 550 551 559 public static String debug( 560 ServletContext scContext 561 ) 562 { 563 Enumeration enumNames; 564 String strParam; 565 StringBuffer sbfReturn = new StringBuffer (); 566 567 sbfReturn.append("ServletContext=[ServletContextName="); 568 sbfReturn.append(scContext.getServletContextName()); 569 sbfReturn.append(";"); 570 for (enumNames = scContext.getInitParameterNames(); 571 enumNames.hasMoreElements();) 572 { 573 strParam = (String )enumNames.nextElement(); 574 sbfReturn.append("Param="); 575 sbfReturn.append(strParam); 576 sbfReturn.append(" value="); 577 sbfReturn.append(scContext.getInitParameter(strParam)); 578 if (enumNames.hasMoreElements()) 579 { 580 sbfReturn.append(";"); 581 } 582 } 583 sbfReturn.append("]"); 584 585 return sbfReturn.toString(); 586 } 587 588 596 public static void forwardToOrigin( 597 HttpServletRequest hsrqRequest, 598 HttpServletResponse hsrpResponse 599 ) throws IOException , 600 ServletException 601 { 602 hsrpResponse.sendRedirect(getFullRequestURL(hsrqRequest)); 603 } 604 605 613 public static boolean isMainIndexPage( 614 HttpServletRequest hsrqRequest 615 ) 616 { 617 String strPath; 618 int iFirstIndex = -1; 619 int iLastIndex = -1; 620 621 strPath = getFullRequestPath(hsrqRequest); 622 623 iFirstIndex = strPath.indexOf(WebCommonConstants.URL_SEPARATOR_CHAR); 626 if (iFirstIndex == 0) 627 { 628 iLastIndex = strPath.lastIndexOf(WebCommonConstants.URL_SEPARATOR_CHAR); 629 } 630 631 return (iFirstIndex == iLastIndex) 634 && ((iFirstIndex == -1) 636 || (strPath.endsWith(WebCommonConstants.URL_SEPARATOR) 638 || strPath.endsWith(WebCommonConstants.DEFAULT_DIRECTORY_WEB_PAGE))); 640 } 641 642 650 public static boolean isIndexPage( 651 HttpServletRequest hsrqRequest 652 ) 653 { 654 String strPath; 655 656 strPath = getFullRequestPath(hsrqRequest); 657 658 return strPath.endsWith(WebCommonConstants.URL_SEPARATOR) 659 || strPath.endsWith(WebCommonConstants.DEFAULT_DIRECTORY_WEB_PAGE) 660 || (!strPath.endsWith(WebCommonConstants.URL_SEPARATOR) 662 && (strPath.indexOf(WebCommonConstants.EXTENSION_SEPARATOR) == -1)); 663 } 664 665 671 public static boolean isStaticWebPage( 672 HttpServletRequest hsrqRequest 673 ) 674 { 675 String strPath; 676 677 strPath = getFullRequestPath(hsrqRequest); 678 679 return strPath.endsWith(WebCommonConstants.WEB_PAGE_EXTENSION); 680 } 681 682 696 public static String readProperty( 697 ServletConfig scConfig, 698 String strPropertyName, 699 String strDefaultValue, 700 boolean bEmptyAllowed 701 ) 702 { 703 String strPropertyValue; 704 Properties prpSettings; 705 706 prpSettings = Config.getInstance().getPropertiesSafely(); 708 strPropertyValue = prpSettings.getProperty(strPropertyName, null); 709 710 if ((strPropertyValue == null) 711 || ((strPropertyValue.length() == 0) && (!bEmptyAllowed))) 712 { 713 strPropertyValue = scConfig.getInitParameter(strPropertyName); 715 716 if ((strPropertyValue == null) 717 || ((strPropertyValue.length() == 0) && (!bEmptyAllowed))) 718 { 719 strPropertyValue = scConfig.getServletContext().getInitParameter( 721 strPropertyName); 722 if ((strPropertyValue == null) 723 || ((strPropertyValue.length() == 0) && (!bEmptyAllowed))) 724 { 725 strPropertyValue = strDefaultValue; 727 } 728 } 729 } 730 731 return strPropertyValue; 732 } 733 734 747 public static String readProperty( 748 ServletContext scContext, 749 String strPropertyName, 750 String strDefaultValue, 751 boolean bEmptyAllowed 752 ) 753 { 754 String strPropertyValue; 755 Properties prpSettings; 756 757 prpSettings = Config.getInstance().getPropertiesSafely(); 759 strPropertyValue = prpSettings.getProperty(strPropertyName, null); 760 761 if ((strPropertyValue == null) 762 || ((strPropertyValue.length() == 0) && (!bEmptyAllowed))) 763 { 764 strPropertyValue = scContext.getInitParameter(strPropertyName); 766 if ((strPropertyValue == null) 767 || ((strPropertyValue.length() == 0) && (!bEmptyAllowed))) 768 { 769 strPropertyValue = strDefaultValue; 771 } 772 } 773 774 return strPropertyValue; 775 } 776 777 790 public static String encode( 791 String strInput 792 ) throws UnsupportedEncodingException 793 { 794 String strEncoded = URLEncoder.encode(strInput, "UTF-8"); 795 return strEncoded.replaceAll("\\+", "%20"); 796 } 797 } 798 | Popular Tags |