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