refactoring:
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web.Hosting / ServletWorkerRequest.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using javax.servlet.http;
5 using java.security;
6 using javax.servlet;
7
8 namespace Mainsoft.Web.Hosting
9 {
10         public sealed class ServletWorkerRequest : BaseWorkerRequest
11         {
12                 readonly HttpServlet _HttpServlet;
13                 readonly HttpServletRequest _HttpServletRequest;
14                 readonly HttpServletResponse _HttpServletResponse;
15                 OutputStreamWrapper _outputStream;
16
17                 public ServletWorkerRequest (HttpServlet servlet, HttpServletRequest req, HttpServletResponse resp)
18                         : base (req.getContextPath(), req.getServletPath (), req.getRequestURI ()) {
19
20                         _HttpServlet = servlet;
21                         _HttpServletRequest = req;
22                         _HttpServletResponse = resp;
23
24                 }
25
26                 static readonly Type typeOfHttpServletRequest = typeof (HttpServletRequest);
27                 static readonly Type typeOfHttpServletResponse = typeof (HttpServletResponse);
28                 static readonly Type typeOfHttpServlet = typeof (HttpServlet);
29                 static readonly Type typeOfHttpSession = typeof (HttpSession);
30                 static readonly Type typeOfServletContext = typeof (ServletContext);
31                 static readonly Type typeOfServletConfig = typeof (ServletConfig);
32                 public override object GetService (Type serviceType) {
33                         if (serviceType == typeOfHttpServlet ||
34                                 serviceType == typeOfServletConfig)
35                                 return _HttpServlet;
36                         if (serviceType == typeOfHttpServletRequest)
37                                 return _HttpServletRequest;
38                         if (serviceType == typeOfHttpServletResponse)
39                                 return _HttpServletResponse;
40                         if (serviceType == typeOfHttpSession)
41                                 return GetSession (false);
42                         if (serviceType == typeOfServletContext)
43                                 return _HttpServlet.getServletContext ();
44                         return base.GetService (serviceType);
45                 }
46
47                 public HttpServlet Servlet {
48                         get {
49                                 return _HttpServlet;
50                         }
51                 }
52
53                 public HttpServletRequest ServletRequest {
54                         get {
55                                 return _HttpServletRequest;
56                         }
57                 }
58
59                 public HttpServletResponse ServletResponse {
60                         get {
61                                 return _HttpServletResponse;
62                         }
63                 }
64
65                 public override string GetHttpVerbName () {
66                         return _HttpServletRequest.getMethod ();
67                 }
68
69                 public override string GetHttpVersion () {
70                         return _HttpServletRequest.getProtocol ();
71                 }
72
73                 public override string GetLocalAddress () {
74                         return _HttpServletRequest.getLocalAddr ();
75                 }
76
77                 public override int GetLocalPort () {
78                         return _HttpServletRequest.getServerPort ();
79                 }
80
81                 public override string GetPathInfo () {
82                         string pathInfo = base.GetPathInfo() ?? _HttpServletRequest.getPathInfo ();
83                         return pathInfo ?? String.Empty;
84                 }
85
86                 public override string GetQueryString () {
87                         return _HttpServletRequest.getQueryString ();
88                 }
89
90                 public override string GetRemoteAddress () {
91                         return _HttpServletRequest.getRemoteAddr ();
92                 }
93
94                 public override string GetRemoteName () {
95                         return _HttpServletRequest.getRemoteHost ();
96                 }
97
98
99                 public override int GetRemotePort () {
100                         try {
101                                 return _HttpServletRequest.getRemotePort ();
102                         }
103                         catch (Exception e) {
104                                 // if servlet API is 2.3 and below - there is no method getRemotePort 
105                                 // in ServletRequest interface... should be described as limitation.
106                                 return 0;
107                         }
108                 }
109
110                 public override string GetProtocol () {
111                         return _HttpServletRequest.getScheme ();
112                 }
113
114                 public override string GetServerName () {
115                         return _HttpServletRequest.getServerName ();
116                 }
117
118                 public override bool IsSecure () {
119                         return _HttpServletRequest.isSecure ();
120                 }
121
122                 public override void SendStatus (int statusCode, string statusDescription) {
123                         // setStatus(int, string) is deprecated
124                         _HttpServletResponse.setStatus (statusCode/*, statusDescription*/);
125                 }
126
127                 public override void SendUnknownResponseHeader (string name, string value) {
128                         if (HeadersSent ())
129                                 return;
130
131                         _HttpServletResponse.addHeader (name, value);
132                 }
133
134                 public override bool HeadersSent () {
135                         return _HttpServletResponse.isCommitted ();
136                 }
137
138                 public override void SendCalculatedContentLength (int contentLength) {
139                         _HttpServletResponse.setContentLength (contentLength);
140                 }
141
142                 public override string GetAuthType () {
143                         return _HttpServletRequest.getAuthType ();
144                 }
145
146                 protected override int getContentLength () {
147                         return _HttpServletRequest.getContentLength ();
148                 }
149
150                 protected override string getContentType () {
151                         return _HttpServletRequest.getContentType ();
152                 }
153
154                 public override string GetRemoteUser () {
155                         return _HttpServletRequest.getRemoteUser ();
156                 }
157
158                 protected override int getServerPort () {
159                         return _HttpServletRequest.getServerPort ();
160                 }
161
162                 protected override java.util.Enumeration getHeaderNames () {
163                         return _HttpServletRequest.getHeaderNames ();
164                 }
165
166                 protected override string getHeader (string name) {
167                         return _HttpServletRequest.getHeader (name);
168                 }
169
170                 protected override java.io.InputStream getInputStream () {
171                         return _HttpServletRequest.getInputStream ();
172                 }
173
174                 public override javax.servlet.ServletContext GetContext () {
175                         return _HttpServlet.getServletContext ();
176                 }
177
178                 protected override OutputStreamWrapper CreateOutputStream (bool binary) {
179                         return _outputStream ?? (_outputStream = binary ?
180                                 new OutputStreamWrapper (_HttpServletResponse.getOutputStream ()) :
181                                 new OutputStreamWrapper (_HttpServletResponse.getWriter ()));
182                 }
183
184                 public override HttpSession GetSession (bool create) {
185                         return _HttpServletRequest.getSession (create);
186                 }
187
188                 public override bool IsRequestedSessionIdValid () {
189                         return _HttpServletRequest.isRequestedSessionIdValid ();
190                 }
191                 public override string GetRequestedSessionId () {
192                         return _HttpServletRequest.getRequestedSessionId ();
193                 }
194
195                 public override bool IsUserInRole (string name) {
196                         return _HttpServletRequest.isUserInRole (name);
197                 }
198
199                 public override Principal GetUserPrincipal () {
200                         return _HttpServletRequest.getUserPrincipal ();
201                 }
202
203                 public override BaseHttpContext CreateContext (System.Web.HttpContext context) {
204                         return new ServletHttpContext (context);
205                 }
206         }
207 }