1
// Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
18 using System
.Collections
;
20 using System
.Web
.SessionState
;
26 public abstract class BaseHttpHandler
: IHttpHandler
28 private readonly IController controller
;
29 private readonly IControllerContext controllerContext
;
30 private readonly IEngineContext engineContext
;
31 private readonly bool sessionless
;
34 /// Initializes a new instance of the <see cref="BaseHttpHandler"/> class.
36 /// <param name="engineContext">The engine context.</param>
37 /// <param name="controller">The controller.</param>
38 /// <param name="controllerContext">The controller context.</param>
39 /// <param name="sessionless">if set to <c>true</c> then we wont have a session to work.</param>
40 protected BaseHttpHandler(IEngineContext engineContext
,
41 IController controller
, IControllerContext controllerContext
, bool sessionless
)
43 this.controller
= controller
;
44 this.controllerContext
= controllerContext
;
45 this.engineContext
= engineContext
;
46 this.sessionless
= sessionless
;
54 /// <param name="context"></param>
55 public void ProcessRequest(HttpContext context
)
63 public bool IsReusable
71 /// Performs the base work of MonoRail. Extracts
72 /// the information from the URL, obtain the controller
73 /// that matches this information and dispatch the execution
76 /// <param name="context">The context.</param>
77 public virtual void Process(HttpContext context
)
81 // Now we have a session
82 engineContext
.Session
= ResolveSession(context
);
85 IDictionary session
= engineContext
.Session
;
91 flash
= new Flash((Flash
) session
[Flash
.FlashKey
]);
98 engineContext
.Flash
= flash
;
100 // items added to be used by the test context
101 context
.Items
["mr.controller"] = controller
;
102 context
.Items
["mr.flash"] = engineContext
.Flash
;
103 context
.Items
["mr.propertybag"] = controllerContext
.PropertyBag
;
104 context
.Items
["mr.session"] = context
.Session
;
106 AcquireCustomSession();
110 engineContext
.Services
.ExtensionManager
.RaisePreProcessController(engineContext
);
112 controller
.Process(engineContext
, controllerContext
);
114 engineContext
.Services
.ExtensionManager
.RaisePostProcessController(engineContext
);
118 HttpResponse response
= context
.Response
;
120 if (response
.StatusCode
== 200)
122 response
.StatusCode
= 500;
125 engineContext
.LastException
= ex
;
127 engineContext
.Services
.ExtensionManager
.RaiseUnhandledError(engineContext
);
129 throw new MonoRailException("Error processing MonoRail request. Action " +
130 controllerContext
.Action
+ " on controller " + controllerContext
.Name
, ex
);
139 PersistCustomSession();
141 ReleaseController(controller
);
146 /// Resolves the session.
148 /// <param name="context">The context.</param>
149 /// <returns></returns>
150 protected virtual IDictionary
ResolveSession(HttpContext context
)
154 if (context
.Items
["AspSession"] != null)
156 // Windows and Testing
157 session
= context
.Items
["AspSession"];
162 session
= context
.Session
;
165 if (session
is HttpSessionState
)
167 return new SessionAdapter(session
as HttpSessionState
);
171 return (IDictionary
)session
;
176 /// Acquires the custom session from the custom session.
178 protected virtual void AcquireCustomSession()
180 engineContext
.Services
.ExtensionManager
.RaiseAcquireRequestState(engineContext
);
184 /// Persists the custom session to the custom session.
186 protected virtual void PersistCustomSession()
188 engineContext
.Services
.ExtensionManager
.RaiseReleaseRequestState(engineContext
);
191 private void ReleaseController(IController controller
)
193 engineContext
.Services
.ControllerFactory
.Release(controller
);
196 private void PersistFlashItems()
198 Flash currentFlash
= engineContext
.Flash
;
200 if (currentFlash
== null) return;
202 currentFlash
.Sweep();
204 if (currentFlash
.HasItemsToKeep
)
206 engineContext
.Session
[Flash
.FlashKey
] = currentFlash
;
208 else if (engineContext
.Session
.Contains(Flash
.FlashKey
))
210 engineContext
.Session
.Remove(Flash
.FlashKey
);