Added non-generic registration interface to IKernel and IWindsor to accommodate dynam...
[castle.git] / MonoRail / Castle.MonoRail.Framework / BaseHttpHandler.cs
blob588a90eddc1373b7a61ca92d0cfb5c75f2eda080
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.Collections;
19 using System.Web;
20 using System.Web.SessionState;
21 using Adapters;
23 /// <summary>
24 /// Pendent
25 /// </summary>
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;
33 /// <summary>
34 /// Initializes a new instance of the <see cref="BaseHttpHandler"/> class.
35 /// </summary>
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;
49 #region IHttpHandler
51 /// <summary>
52 /// Pendent
53 /// </summary>
54 /// <param name="context"></param>
55 public void ProcessRequest(HttpContext context)
57 Process(context);
60 /// <summary>
61 /// Pendent
62 /// </summary>
63 public bool IsReusable
65 get { return false; }
68 #endregion
70 /// <summary>
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
74 /// to it.
75 /// </summary>
76 /// <param name="context">The context.</param>
77 public virtual void Process(HttpContext context)
79 if (!sessionless)
81 // Now we have a session
82 engineContext.Session = ResolveSession(context);
85 IDictionary session = engineContext.Session;
87 Flash flash;
89 if (session != null)
91 flash = new Flash((Flash) session[Flash.FlashKey]);
93 else
95 flash = new Flash();
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);
116 catch(Exception ex)
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);
132 finally
134 if (!sessionless)
136 PersistFlashItems();
139 PersistCustomSession();
141 ReleaseController(controller);
145 /// <summary>
146 /// Resolves the session.
147 /// </summary>
148 /// <param name="context">The context.</param>
149 /// <returns></returns>
150 protected virtual IDictionary ResolveSession(HttpContext context)
152 object session;
154 if (context.Items["AspSession"] != null)
156 // Windows and Testing
157 session = context.Items["AspSession"];
159 else
161 // Mono
162 session = context.Session;
165 if (session is HttpSessionState)
167 return new SessionAdapter(session as HttpSessionState);
169 else
171 return (IDictionary)session;
175 /// <summary>
176 /// Acquires the custom session from the custom session.
177 /// </summary>
178 protected virtual void AcquireCustomSession()
180 engineContext.Services.ExtensionManager.RaiseAcquireRequestState(engineContext);
183 /// <summary>
184 /// Persists the custom session to the custom session.
185 /// </summary>
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);