1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
|
//------------------------------------------------------------------------------
// <copyright file="TemplatedMailWebEventProvider .cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Management {
using System.Configuration;
using System.Configuration.Provider;
using System.Collections.Specialized;
using System.Web.Util;
using System.Net.Mail;
using System.Globalization;
using System.Web.Configuration;
using System.Text;
using System.IO;
using System.Runtime.Remoting.Messaging;
using System.Security.Permissions;
using System.Threading;
public sealed class TemplatedMailWebEventProvider : MailWebEventProvider, IInternalWebEventProvider {
int _nonBufferNotificationSequence = 0;
string _templateUrl;
bool _detailedTemplateErrors = false;
class TemplatedMailErrorFormatterGenerator : ErrorFormatterGenerator {
int _eventsRemaining;
bool _showDetails;
bool _errorFormatterCalled;
internal TemplatedMailErrorFormatterGenerator(int eventsRemaining, bool showDetails) {
_eventsRemaining = eventsRemaining;
_showDetails = showDetails;
}
internal bool ErrorFormatterCalled {
get { return _errorFormatterCalled; }
}
internal override ErrorFormatter GetErrorFormatter(Exception e) {
Exception inner = e.InnerException;
_errorFormatterCalled = true;
while (inner != null) {
if (inner is HttpCompileException) {
return new TemplatedMailCompileErrorFormatter((HttpCompileException)inner, _eventsRemaining, _showDetails);
}
else {
inner = inner.InnerException;
}
}
return new TemplatedMailRuntimeErrorFormatter(e, _eventsRemaining, _showDetails);
}
}
internal TemplatedMailWebEventProvider() { }
public override void Initialize(string name, NameValueCollection config)
{
Debug.Trace("TemplatedMailWebEventProvider", "Initializing: name=" + name);
ProviderUtil.GetAndRemoveStringAttribute(config, "template", name, ref _templateUrl);
if (_templateUrl == null) {
throw new ConfigurationErrorsException(SR.GetString(SR.Provider_missing_attribute, "template", name));
}
_templateUrl = _templateUrl.Trim();
if (_templateUrl.Length == 0) {
throw new ConfigurationErrorsException(SR.GetString(SR.Invalid_provider_attribute, "template", name, _templateUrl));
}
if (!UrlPath.IsRelativeUrl(_templateUrl)) {
throw new ConfigurationErrorsException(SR.GetString(SR.Invalid_mail_template_provider_attribute,
"template", name, _templateUrl));
}
_templateUrl = UrlPath.Combine(HttpRuntime.AppDomainAppVirtualPathString, _templateUrl);
// VSWhidbey 440081: Guard against templates outside the AppDomain path
if (!HttpRuntime.IsPathWithinAppRoot(_templateUrl)) {
throw new ConfigurationErrorsException(SR.GetString(SR.Invalid_mail_template_provider_attribute,
"template", name, _templateUrl));
}
ProviderUtil.GetAndRemoveBooleanAttribute(config, "detailedTemplateErrors", name, ref _detailedTemplateErrors);
base.Initialize(name, config);
}
void GenerateMessageBody(
MailMessage msg,
WebBaseEventCollection events,
DateTime lastNotificationUtc,
int discardedSinceLastNotification,
int eventsInBuffer,
int notificationSequence,
EventNotificationType notificationType,
int eventsInNotification,
int eventsRemaining,
int messagesInNotification,
int eventsLostDueToMessageLimit,
int messageSequence,
out bool fatalError) {
StringWriter writer = new StringWriter(CultureInfo.InstalledUICulture);
MailEventNotificationInfo info = new MailEventNotificationInfo(
msg,
events,
lastNotificationUtc,
discardedSinceLastNotification,
eventsInBuffer,
notificationSequence,
notificationType,
eventsInNotification,
eventsRemaining,
messagesInNotification,
eventsLostDueToMessageLimit,
messageSequence);
CallContext.SetData(CurrentEventsName, info);
try {
TemplatedMailErrorFormatterGenerator gen = new TemplatedMailErrorFormatterGenerator(events.Count + eventsRemaining, _detailedTemplateErrors);
HttpServerUtility.ExecuteLocalRequestAndCaptureResponse(_templateUrl, writer, gen);
fatalError = gen.ErrorFormatterCalled;
if (fatalError) {
msg.Subject = HttpUtility.HtmlEncode(SR.GetString(SR.WebEvent_event_email_subject_template_error,
notificationSequence.ToString(CultureInfo.InstalledUICulture),
messageSequence.ToString(CultureInfo.InstalledUICulture),
SubjectPrefix));
}
msg.Body = writer.ToString();
msg.IsBodyHtml = true;
}
finally {
CallContext.FreeNamedDataSlot(CurrentEventsName);
}
}
internal override void SendMessage(WebBaseEvent eventRaised) {
WebBaseEventCollection events = new WebBaseEventCollection(eventRaised);
bool templateError;
SendMessageInternal(events, // events
DateTime.MinValue, // lastNotificationUtc
0, // discardedSinceLastNotification
0, // eventsInBuffer
Interlocked.Increment(ref _nonBufferNotificationSequence), // notificationSequence
EventNotificationType.Unbuffered, // notificationType
1, // eventsInNotification
0, // eventsRemaining
1, // messagesInNotification
0, // eventsLostDueToMessageLimit
MessageSequenceBase, // messageSequence
out templateError); // templateError
}
internal override void SendMessage(WebBaseEventCollection events,
WebEventBufferFlushInfo flushInfo,
int eventsInNotification,
int eventsRemaining,
int messagesInNotification,
int eventsLostDueToMessageLimit,
int messageSequence,
int eventsSent,
out bool fatalError) {
SendMessageInternal(events,
flushInfo.LastNotificationUtc,
flushInfo.EventsDiscardedSinceLastNotification,
flushInfo.EventsInBuffer,
flushInfo.NotificationSequence,
flushInfo.NotificationType,
eventsInNotification,
eventsRemaining,
messagesInNotification,
eventsLostDueToMessageLimit,
messageSequence,
out fatalError);
}
void SendMessageInternal(WebBaseEventCollection events,
DateTime lastNotificationUtc,
int discardedSinceLastNotification,
int eventsInBuffer,
int notificationSequence,
EventNotificationType notificationType,
int eventsInNotification,
int eventsRemaining,
int messagesInNotification,
int eventsLostDueToMessageLimit,
int messageSequence,
out bool fatalError) {
using (MailMessage msg = GetMessage()) {
msg.Subject = GenerateSubject(notificationSequence, messageSequence, events, events.Count);
GenerateMessageBody(
msg,
events,
lastNotificationUtc,
discardedSinceLastNotification,
eventsInBuffer,
notificationSequence,
notificationType,
eventsInNotification,
eventsRemaining,
messagesInNotification,
eventsLostDueToMessageLimit,
messageSequence,
out fatalError);
SendMail(msg);
}
}
internal const string CurrentEventsName = "_TWCurEvt";
public static MailEventNotificationInfo CurrentNotification {
get {
return (MailEventNotificationInfo)CallContext.GetData(CurrentEventsName);
}
}
}
public sealed class MailEventNotificationInfo {
WebBaseEventCollection _events;
DateTime _lastNotificationUtc;
int _discardedSinceLastNotification;
int _eventsInBuffer;
int _notificationSequence;
EventNotificationType _notificationType;
int _eventsInNotification;
int _eventsRemaining;
int _messagesInNotification;
int _eventsLostDueToMessageLimit;
int _messageSequence;
MailMessage _msg;
internal MailEventNotificationInfo(
MailMessage msg,
WebBaseEventCollection events,
DateTime lastNotificationUtc,
int discardedSinceLastNotification,
int eventsInBuffer,
int notificationSequence,
EventNotificationType notificationType,
int eventsInNotification,
int eventsRemaining,
int messagesInNotification,
int eventsLostDueToMessageLimit,
int messageSequence) {
_events = events;
_lastNotificationUtc = lastNotificationUtc;
_discardedSinceLastNotification = discardedSinceLastNotification;
_eventsInBuffer = eventsInBuffer;
_notificationSequence = notificationSequence;
_notificationType = notificationType;
_eventsInNotification = eventsInNotification;
_eventsRemaining = eventsRemaining ;
_messagesInNotification = messagesInNotification;
_eventsLostDueToMessageLimit = eventsLostDueToMessageLimit;
_messageSequence = messageSequence;
_msg = msg;
}
public WebBaseEventCollection Events {
get { return _events; }
}
public EventNotificationType NotificationType {
get { return _notificationType; }
}
public int EventsInNotification {
get { return _eventsInNotification; }
}
public int EventsRemaining {
get { return _eventsRemaining; }
}
public int MessagesInNotification {
get { return _messagesInNotification; }
}
public int EventsInBuffer {
get { return _eventsInBuffer; }
}
public int EventsDiscardedByBuffer {
get { return _discardedSinceLastNotification; }
}
public int EventsDiscardedDueToMessageLimit {
get { return _eventsLostDueToMessageLimit; }
}
public int NotificationSequence {
get { return _notificationSequence; }
}
public int MessageSequence {
get { return _messageSequence; }
}
public DateTime LastNotificationUtc {
get { return _lastNotificationUtc; }
}
public MailMessage Message {
get { return _msg; }
}
}
internal class TemplatedMailCompileErrorFormatter : DynamicCompileErrorFormatter {
int _eventsRemaining;
bool _showDetails;
internal TemplatedMailCompileErrorFormatter(HttpCompileException e, int eventsRemaining,
bool showDetails) :
base(e) {
_eventsRemaining = eventsRemaining;
_showDetails = showDetails;
_hideDetailedCompilerOutput = true;
_dontShowVersion = true;
}
protected override string ErrorTitle {
get {
return SR.GetString(SR.MailWebEventProvider_template_compile_error,
_eventsRemaining.ToString(CultureInfo.InstalledUICulture));
}
}
protected override string Description {
get {
if (_showDetails) {
return base.Description;
}
else {
return SR.GetString(SR.MailWebEventProvider_template_error_no_details);
}
}
}
protected override string MiscSectionTitle {
get {
if (_showDetails) {
return base.MiscSectionTitle;
}
else {
return null;
}
}
}
protected override string MiscSectionContent {
get {
if (_showDetails) {
return base.MiscSectionContent;
}
else {
return null;
}
}
}
}
internal class TemplatedMailRuntimeErrorFormatter : UnhandledErrorFormatter {
int _eventsRemaining;
bool _showDetails;
internal TemplatedMailRuntimeErrorFormatter(Exception e, int eventsRemaining,
bool showDetails) :
base(e) {
_eventsRemaining = eventsRemaining;
_showDetails = showDetails;
_dontShowVersion = true;
}
protected override string ErrorTitle {
get {
if (HttpException.GetHttpCodeForException(Exception) == 404) {
return SR.GetString(SR.MailWebEventProvider_template_file_not_found_error,
_eventsRemaining.ToString(CultureInfo.InstalledUICulture));
}
else {
return SR.GetString(SR.MailWebEventProvider_template_runtime_error,
_eventsRemaining.ToString(CultureInfo.InstalledUICulture));
}
}
}
protected override string ColoredSquareTitle {
get { return null;}
}
protected override string ColoredSquareContent {
get { return null; }
}
protected override string Description {
get {
if (_showDetails) {
return base.Description;
}
else {
return SR.GetString(SR.MailWebEventProvider_template_error_no_details);
}
}
}
protected override string MiscSectionTitle {
get {
if (_showDetails) {
return base.MiscSectionTitle;
}
else {
return null;
}
}
}
protected override string MiscSectionContent {
get {
if (_showDetails) {
return base.MiscSectionContent;
}
else {
return null;
}
}
}
protected override string ColoredSquare2Title {
get {
if (_showDetails) {
return base.ColoredSquare2Title;
}
else {
return null;
}
}
}
protected override string ColoredSquare2Content {
get {
if (_showDetails) {
return base.ColoredSquare2Content;
}
else {
return null;
}
}
}
}
}
|