BaseStaticHttpServlet.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  3. //
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. //
  24. using System;
  25. using System.IO;
  26. using System.Configuration;
  27. using System.Web.Configuration;
  28. using System.Threading;
  29. using javax.servlet;
  30. using javax.servlet.http;
  31. using vmw.common;
  32. namespace System.Web.J2EE
  33. {
  34. public class BaseStaticHttpServlet : HttpServlet
  35. {
  36. public BaseStaticHttpServlet()
  37. {
  38. }
  39. override public void init(ServletConfig config)
  40. {
  41. base.init(config);
  42. ServletContext context = config.getServletContext();
  43. AppDir = config.getInitParameter(IAppDomainConfig.APP_DIR_NAME);
  44. if (AppDir != null) {
  45. AppDir = AppDir.Replace('\\', '/');
  46. if (AppDir[AppDir.Length - 1] != '/')
  47. AppDir += '/';
  48. }
  49. }
  50. override protected void service(HttpServletRequest req, HttpServletResponse resp)
  51. {
  52. String pathInfo = req.getRequestURI();
  53. String contextPath = req.getContextPath();
  54. if (pathInfo.Equals(contextPath) ||
  55. ((pathInfo.Length - contextPath.Length) == 1) && pathInfo[pathInfo.Length-1] == '/' && pathInfo.StartsWith(contextPath))
  56. pathInfo = contextPath + req.getServletPath();
  57. resp.setHeader("X-Powered-By", "ASP.NET");
  58. resp.setHeader("X-AspNet-Version", "1.1.4322");
  59. ServletOutputStream hos = resp.getOutputStream();
  60. String filename = "";
  61. try
  62. {
  63. pathInfo = pathInfo.Substring(contextPath.Length);
  64. if (pathInfo.StartsWith("/") || pathInfo.StartsWith("\\"))
  65. pathInfo = pathInfo.Substring(1);
  66. filename = AppDir + pathInfo;
  67. resp.setContentType(this.getServletContext().getMimeType(filename));
  68. FileStream fis = null;
  69. try {
  70. fis = new FileStream(filename,FileMode.Open,FileAccess.Read);
  71. byte[] buf = new byte[4 * 1024]; // 4K buffer
  72. int bytesRead;
  73. while ((bytesRead = fis.Read(buf,0,buf.Length)) != -1 &&
  74. bytesRead != 0) {
  75. hos.write(TypeUtils.ToSByteArray(buf), 0, bytesRead);
  76. }
  77. }
  78. finally {
  79. if (fis != null) fis.Close();
  80. }
  81. }
  82. catch (System.IO.FileNotFoundException e)
  83. {
  84. resp.setStatus(404,"Object Not Found.");
  85. HttpException myExp = new HttpException (404, "File '" + filename + "' not found.");
  86. hos.print(((HttpException) myExp).GetHtmlErrorMessage ());
  87. hos.flush();
  88. }
  89. catch(Exception e)
  90. {
  91. Console.WriteLine("ERROR in Static File Reading {0},{1}",e.GetType(), e.Message);
  92. resp.setStatus(500);
  93. HttpException myExp = new HttpException ("Exception in Reading static file", e);
  94. hos.print(((HttpException) myExp).GetHtmlErrorMessage ());
  95. hos.flush();
  96. }
  97. }
  98. override public void destroy()
  99. {
  100. base.destroy();
  101. }
  102. private string AppDir;
  103. }
  104. }
  105. namespace System.Web.GH
  106. {
  107. public class BaseStaticHttpServlet : System.Web.J2EE.BaseStaticHttpServlet
  108. {
  109. }
  110. }