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;
30 using System.Threading;
33 using javax.servlet.http;
35 using System.Diagnostics;
37 namespace Mainsoft.Web.Hosting
40 /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
42 public class BaseStaticHttpServlet : HttpServlet
44 public BaseStaticHttpServlet()
48 override public void init(ServletConfig config)
51 AppDir = config.getServletContext ().getInitParameter (IAppDomainConfig.APP_DIR_NAME);
53 AppDir = AppDir.Replace('\\', '/');
54 if (AppDir[AppDir.Length - 1] != '/')
59 override protected void service(HttpServletRequest req, HttpServletResponse resp)
61 resp.setHeader("X-Powered-By", "ASP.NET");
62 resp.setHeader("X-AspNet-Version", "1.1.4322");
64 String filename = getServletContext().getRealPath(req.getServletPath());
65 ServletOutputStream hos;
67 hos = resp.getOutputStream();
69 catch (java.lang.IllegalStateException e)
71 string mimeType = getServletContext().getMimeType(filename);
72 if (mimeType == null || mimeType.StartsWith("text")) {
73 sendFileUsingWriter(resp, filename);
81 string mimeType = this.getServletContext().getMimeType(filename);
83 mimeType = "text/plain";
85 resp.setContentType(mimeType);
87 FileStream fis = null;
89 fis = new FileStream(filename,FileMode.Open,FileAccess.Read);
90 byte[] buf = new byte[4 * 1024]; // 4K buffer
92 while ((bytesRead = fis.Read(buf,0,buf.Length)) != -1 &&
94 hos.write(TypeUtils.ToSByteArray(buf), 0, bytesRead);
98 if (fis != null) fis.Close();
101 catch (System.IO.FileNotFoundException e)
103 resp.setStatus(404,"Object Not Found.");
104 HttpException myExp = new HttpException (404, "File '" + filename + "' not found.");
105 hos.print(((HttpException) myExp).GetHtmlErrorMessage ());
110 Trace.WriteLine (String.Format ("ERROR in Static File Reading {0},{1}", e.GetType (), e.Message));
112 HttpException myExp = new HttpException ("Exception in Reading static file", e);
113 hos.print(((HttpException) myExp).GetHtmlErrorMessage ());
118 void sendFileUsingWriter(HttpServletResponse resp, string filename)
120 java.io.PrintWriter writer = resp.getWriter();
123 resp.setContentType(this.getServletContext().getMimeType(filename));
124 StreamReader fis = null;
125 char[] buf = new char[4 * 1024]; // 4K buffer
127 fis = new StreamReader(filename);
129 while ((charsRead = fis.Read(buf,0,buf.Length)) != -1 &&
131 writer.write(buf, 0, charsRead);
135 if (fis != null) fis.Close();
138 catch (System.IO.FileNotFoundException e)
140 resp.setStatus(404,"Object Not Found.");
141 HttpException myExp = new HttpException (404, "File '" + filename + "' not found.");
142 writer.print(((HttpException) myExp).GetHtmlErrorMessage ());
147 Trace.WriteLine (String.Format ("ERROR in Static File Reading {0},{1}", e.GetType (), e.Message));
149 HttpException myExp = new HttpException ("Exception in Reading static file", e);
150 writer.print(((HttpException) myExp).GetHtmlErrorMessage ());
155 override public void destroy()
160 private string AppDir;
164 namespace System.Web.J2EE
167 /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
169 public class BaseStaticHttpServlet : Mainsoft.Web.Hosting.BaseStaticHttpServlet
176 namespace System.Web.GH
179 /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
181 public class BaseStaticHttpServlet : Mainsoft.Web.Hosting.BaseStaticHttpServlet