[Microsoft.Build] Fix expected output newline from ProcessWrapper.OutputStreamChanged...
[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                         return base.GetPathInfo () ?? String.Empty;
83                 }
84
85                 public override string GetQueryString () {
86                         return _HttpServletRequest.getQueryString ();
87                 }
88
89                 public override string GetRemoteAddress () {
90                         return _HttpServletRequest.getRemoteAddr ();
91                 }
92
93                 public override string GetRemoteName () {
94                         return _HttpServletRequest.getRemoteHost ();
95                 }
96
97
98                 public override int GetRemotePort () {
99                         try {
100                                 return _HttpServletRequest.getRemotePort ();
101                         }
102                         catch (Exception e) {
103                                 // if servlet API is 2.3 and below - there is no method getRemotePort 
104                                 // in ServletRequest interface... should be described as limitation.
105                                 return 0;
106                         }
107                 }
108
109                 public override string GetProtocol () {
110                         return _HttpServletRequest.getScheme ();
111                 }
112
113                 public override string GetServerName () {
114                         return _HttpServletRequest.getServerName ();
115                 }
116
117                 public override bool IsSecure () {
118                         return _HttpServletRequest.isSecure ();
119                 }
120
121                 public override void SendStatus (int statusCode, string statusDescription) {
122                         // setStatus(int, string) is deprecated
123                         _HttpServletResponse.setStatus (statusCode/*, statusDescription*/);
124                 }
125
126                 public override void SendUnknownResponseHeader (string name, string value) {
127                         if (HeadersSent ())
128                                 return;
129
130                         _HttpServletResponse.addHeader (name, value);
131                 }
132
133                 public override bool HeadersSent () {
134                         return _HttpServletResponse.isCommitted ();
135                 }
136
137                 public override void SendCalculatedContentLength (int contentLength) {
138                         _HttpServletResponse.setContentLength (contentLength);
139                 }
140
141                 public override string GetAuthType () {
142                         return _HttpServletRequest.getAuthType ();
143                 }
144
145                 protected override int getContentLength () {
146                         return _HttpServletRequest.getContentLength ();
147                 }
148
149                 protected override string getContentType () {
150                         return _HttpServletRequest.getContentType ();
151                 }
152
153                 public override string GetRemoteUser () {
154                         return _HttpServletRequest.getRemoteUser ();
155                 }
156
157                 protected override java.util.Enumeration getHeaderNames () {
158                         return _HttpServletRequest.getHeaderNames ();
159                 }
160
161                 protected override string getHeader (string name) {
162                         return _HttpServletRequest.getHeader (name);
163                 }
164
165                 protected override java.io.InputStream getInputStream () {
166                         return _HttpServletRequest.getInputStream ();
167                 }
168
169                 public override javax.servlet.ServletContext GetContext () {
170                         return _HttpServlet.getServletContext ();
171                 }
172
173                 protected override OutputStreamWrapper CreateOutputStream (bool binary) {
174                         return _outputStream ?? (_outputStream = binary ?
175                                 new OutputStreamWrapper (_HttpServletResponse.getOutputStream ()) :
176                                 new OutputStreamWrapper (_HttpServletResponse.getWriter ()));
177                 }
178
179                 public override HttpSession GetSession (bool create) {
180                         return _HttpServletRequest.getSession (create);
181                 }
182
183                 public override bool IsRequestedSessionIdValid () {
184                         return _HttpServletRequest.isRequestedSessionIdValid ();
185                 }
186                 public override string GetRequestedSessionId () {
187                         return _HttpServletRequest.getRequestedSessionId ();
188                 }
189
190                 public override bool IsUserInRole (string name) {
191                         return _HttpServletRequest.isUserInRole (name);
192                 }
193
194                 public override Principal GetUserPrincipal () {
195                         return _HttpServletRequest.getUserPrincipal ();
196                 }
197
198                 public override BaseHttpContext CreateContext (System.Web.HttpContext context) {
199                         return new ServletHttpContext (context);
200                 }
201         }
202 }