-
Notifications
You must be signed in to change notification settings - Fork 38
RequestHandlerInterceptorsExamples
bartdeleye edited this page Jul 25, 2011
·
2 revisions
public class TransactionalRequestInterceptor : Disposable, IRequestHandlerInterceptor
{
private IUnitOfWork _uow;
public void BeforeHandlingRequest(RequestProcessingContext context)
{
if (!Transactional(context.Request)) return;
//UnitOfWork opens an NHibernate session and begins a transaction
_uow = UnitOfWork.Start();
}
public void AfterHandlingRequest(RequestProcessingContext context)
{
if (!Transactional(context.Request)) return;
if (context.Response.Exception != null) return;
_uow.Complete();
}
protected override void DisposeManagedResources()
{
if (_uow != null)
_uow.Dispose();
}
private static bool Transactional(Request request)
{
return request is TransactionalRequest;
}
}
public class AuthenticatingInterceptor : ConventionBasedInterceptor
{
private readonly IAuthenticationService _authenticationService;
public AuthenticatingInterceptor(IAuthenticationService authenticationService)
{
_authenticationService = authenticationService;
}
public override void BeforeHandlingRequest(RequestProcessingContext context)
{
var request = context.Request as AuthenticatedRequest;
if (request == null) return;
if (_authenticationService.Validate(request.UserName, request.Password)) return;
var response = CreateDefaultResponseFor(request);
response.Exception = new ExceptionInfo(new MySecurityException("username or password is not valid"));
response.ExceptionType = ExceptionType.Security;
context.MarkAsProcessed(response);
}
public override void AfterHandlingRequest(RequestProcessingContext context)
{
}
protected override void DisposeManagedResources()
{
}
}
public class TracingInterceptor : Disposable, IRequestHandlerInterceptor
{
private ILog Log = LogManager.GetLogger(typeof(TracingInterceptor));
public void BeforeHandlingRequest(RequestProcessingContext context)
{
if (!Log.IsDebugEnabled) return;
var request = context.Request;
try
{
Log.DebugFormat("received request: {0}", GetXml(request));
}
catch (Exception exc)
{
Log.DebugFormat("Error tracing request type {0}: {1}", request, exc);
}
}
public void AfterHandlingRequest(RequestProcessingContext context)
{
var response = context.Response;
if (response == null) return;
if (!Log.IsDebugEnabled) return;
try
{
Log.DebugFormat("sending response: {0}", GetXml(response));
}
catch (Exception exc)
{
Log.DebugFormat("Error tracing response type {0}: {1}", response, exc);
}
}
private string GetXml(object request)
{
var serializer = new XmlSerializer(request.GetType());
using (var writer = new StringWriter())
{
serializer.Serialize(writer, request);
return writer.ToString();
}
}
protected override void DisposeManagedResources()
{
}
}
public class ValidatingInterceptor : ConventionBasedInterceptor
{
public override void BeforeHandlingRequest(RequestProcessingContext context)
{
try
{
Validate.Object(context.Request as dynamic);
}
catch (ValidationException exc)
{
var response = CreateDefaultResponseFor(context.Request);
response.Exception = new ExceptionInfo(exc);
response.ExceptionType = ExceptionType.Unknown;
context.MarkAsProcessed(response);
}
}
public override void AfterHandlingRequest(RequestProcessingContext context)
{
}
protected override void DisposeManagedResources()
{
}
}