BaseStaticHttpServlet.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. pathInfo = getServletContext().getRealPath(req.getServletPath());
  55. resp.setHeader("X-Powered-By", "ASP.NET");
  56. resp.setHeader("X-AspNet-Version", "1.1.4322");
  57. ServletOutputStream hos = resp.getOutputStream();
  58. String filename = pathInfo;
  59. try
  60. {
  61. resp.setContentType(this.getServletContext().getMimeType(filename));
  62. FileStream fis = null;
  63. try {
  64. fis = new FileStream(filename,FileMode.Open,FileAccess.Read);
  65. byte[] buf = new byte[4 * 1024]; // 4K buffer
  66. int bytesRead;
  67. while ((bytesRead = fis.Read(buf,0,buf.Length)) != -1 &&
  68. bytesRead != 0) {
  69. hos.write(TypeUtils.ToSByteArray(buf), 0, bytesRead);
  70. }
  71. }
  72. finally {
  73. if (fis != null) fis.Close();
  74. }
  75. }
  76. catch (System.IO.FileNotFoundException e)
  77. {
  78. resp.setStatus(404,"Object Not Found.");
  79. HttpException myExp = new HttpException (404, "File '" + filename + "' not found.");
  80. hos.print(((HttpException) myExp).GetHtmlErrorMessage ());
  81. hos.flush();
  82. }
  83. catch(Exception e)
  84. {
  85. Console.WriteLine("ERROR in Static File Reading {0},{1}",e.GetType(), e.Message);
  86. resp.setStatus(500);
  87. HttpException myExp = new HttpException ("Exception in Reading static file", e);
  88. hos.print(((HttpException) myExp).GetHtmlErrorMessage ());
  89. hos.flush();
  90. }
  91. }
  92. override public void destroy()
  93. {
  94. base.destroy();
  95. }
  96. private string AppDir;
  97. }
  98. }
  99. namespace System.Web.GH
  100. {
  101. public class BaseStaticHttpServlet : System.Web.J2EE.BaseStaticHttpServlet
  102. {
  103. }
  104. }