private static CobaltStore CreateCobaltStore(IExchangePrincipal exchangePrincipal, Attachment attachment, WacRequest wacRequest, CachedAttachmentInfo attachmentInfo)
        {
            if (exchangePrincipal == null)
            {
                throw new ArgumentException("exchangePrincipal");
            }
            if (exchangePrincipal.MailboxInfo.IsRemote)
            {
                throw new OwaInvalidRequestException("Remote mailboxes are not supported.");
            }
            Guid        objectGuid         = exchangePrincipal.MailboxInfo.MailboxDatabase.ObjectGuid;
            bool        diagnosticsEnabled = WacConfiguration.Instance.DiagnosticsEnabled;
            MdbCache    instance           = MdbCache.GetInstance();
            string      path          = instance.GetPath(objectGuid);
            string      correlationId = HttpContext.Current.Request.Headers["X-WOPI-CorrelationID"];
            CobaltStore store         = new CobaltStore(path, objectGuid.ToString(), correlationId, diagnosticsEnabled, WacConfiguration.Instance.BlobStoreMemoryBudget);

            using (Stream contentStream = ((StreamAttachment)attachment).GetContentStream(PropertyOpenMode.ReadOnly))
            {
                store.Save(contentStream);
            }
            store.Saver.Initialize((string)wacRequest.MailboxSmtpAddress, wacRequest.ExchangeSessionId, WacConfiguration.Instance.AutoSaveInterval, delegate
            {
                using (Stream documentStream = store.GetDocumentStream())
                {
                    WacRequestHandler.ReplaceAttachmentContent(documentStream, wacRequest);
                }
                return(true);
            }, delegate(Exception exception)
            {
                store.SaveFailed(exception);
            });
            return(store);
        }
        private static CobaltStore GetCobaltStore(WacRequest wacRequest)
        {
            CachedAttachmentInfo attachmentInfo = WacRequestHandler.GetCachedAttachmentInfo(wacRequest);
            CobaltStore          cobaltStore    = attachmentInfo.CobaltStore;

            if (cobaltStore == null)
            {
                lock (attachmentInfo)
                {
                    cobaltStore = attachmentInfo.CobaltStore;
                    if (cobaltStore == null)
                    {
                        WacRequestHandler.ProcessAttachment(wacRequest, PropertyOpenMode.ReadOnly, delegate(IExchangePrincipal exchangePrincipal, Attachment attachment, Stream stream, bool contentProtected)
                        {
                            if (!WacRequestHandler.MessageIsDraft(wacRequest))
                            {
                                throw new NotSupportedException("Cell storage requests are only supported for draft items.");
                            }
                            cobaltStore = WacRequestHandler.CreateCobaltStore(exchangePrincipal, attachment, wacRequest, attachmentInfo);
                        });
                        attachmentInfo.CobaltStore = cobaltStore;
                    }
                }
            }
            return(cobaltStore);
        }
        private static void ProcessCobaltRequest(HttpContext context, WacRequest wacRequest, Action <Enum, string> logDetail)
        {
            Stream      inputStream  = context.Request.InputStream;
            Stream      outputStream = context.Response.OutputStream;
            CobaltStore cobaltStore  = WacRequestHandler.GetCobaltStore(wacRequest);

            cobaltStore.ProcessRequest(inputStream, outputStream, logDetail);
        }
 private static void ProcessGetFileRequest(HttpContext context, WacRequest wacRequest, RequestDetailsLogger logger)
 {
     WacRequestHandler.UpdateAttachment(wacRequest, logger);
     WacRequestHandler.ProcessAttachment(wacRequest, PropertyOpenMode.ReadOnly, delegate(IExchangePrincipal exchangePrincipal, Attachment attachment, Stream stream, bool contentProtected)
     {
         WacUtilities.WriteStreamBody(context.Response, stream);
     });
 }
        private static void OnCacheEntryExpired(string key, CacheItemUpdateReason reason, out object expensiveObject, out CacheDependency dependency, out DateTime absoluteExpiration, out TimeSpan slidingExpiration)
        {
            expensiveObject    = null;
            dependency         = null;
            absoluteExpiration = Cache.NoAbsoluteExpiration;
            slidingExpiration  = Cache.NoSlidingExpiration;
            CachedAttachmentInfo cachedAttachmentInfo = (CachedAttachmentInfo)HttpRuntime.Cache.Get(key);

            if (cachedAttachmentInfo.CobaltStore == null)
            {
                return;
            }
            WacRequestHandler.OnCacheEntryExpired(cachedAttachmentInfo);
        }
        private static void ProcessCheckFileRequest(HttpContext context, WacRequest wacRequest, RequestDetailsLogger logger)
        {
            WacRequestHandler.UpdateAttachment(wacRequest, logger);
            CachedAttachmentInfo cachedAttachmentInfo = WacRequestHandler.GetCachedAttachmentInfo(wacRequest);
            string attachmentStreamHash   = null;
            long   attachmentStreamLength = 0L;
            string attachmentFileName     = null;
            string attachmentExtension    = null;
            bool   attachmentIsProtected  = false;

            WacRequestHandler.ProcessAttachment(wacRequest, PropertyOpenMode.ReadOnly, delegate(IExchangePrincipal exchangePrincipal, Attachment attachment, Stream stream, bool isProtected)
            {
                attachmentStreamHash   = WacUtilities.GenerateSHA256HashForStream(stream);
                attachmentStreamLength = stream.Length;
                attachmentFileName     = attachment.FileName;
                attachmentExtension    = attachment.FileExtension;
                attachmentIsProtected  = isProtected;
            });
            WacFileRep           wacFileRep           = wacRequest.WacFileRep;
            string               downloadUrl          = WacRequestHandler.GetDownloadUrl(context.Request, wacFileRep, wacRequest);
            WacCheckFileResponse wacCheckFileResponse = new WacCheckFileResponse(attachmentFileName, attachmentStreamLength, attachmentStreamHash, downloadUrl, cachedAttachmentInfo.MailboxSmtpAddress, cachedAttachmentInfo.LogonSmtpAddress, cachedAttachmentInfo.LogonDisplayName, cachedAttachmentInfo.LogonPuid, attachmentIsProtected, wacFileRep.DirectFileAccessEnabled, wacFileRep.WacExternalServicesEnabled, wacFileRep.OMEXEnabled);

            if (wacFileRep.IsEdit)
            {
                wacCheckFileResponse.UserCanWrite    = true;
                wacCheckFileResponse.ReadOnly        = false;
                wacCheckFileResponse.SupportsUpdate  = true;
                wacCheckFileResponse.SupportsLocks   = true;
                wacCheckFileResponse.SupportsCobalt  = true;
                wacCheckFileResponse.SupportsFolders = true;
            }
            DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(WacCheckFileResponse));

            using (MemoryStream memoryStream = new MemoryStream())
            {
                dataContractJsonSerializer.WriteObject(memoryStream, wacCheckFileResponse);
                memoryStream.Position = 0L;
                context.Response.OutputStream.Write(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
            }
        }
        private static void ProcessAttachment(WacRequest wacRequest, PropertyOpenMode openMode, WacUtilities.AttachmentProcessor attachmentProcessor)
        {
            ADSessionSettings adSessionSettings;
            ExchangePrincipal exchangePrincipal = WacUtilities.GetExchangePrincipal(wacRequest, out adSessionSettings, wacRequest.WacFileRep.IsArchive);

            WacRequestHandler.ProcessUsingBudget(wacRequest.WacFileRep.LogonSid, delegate
            {
                string ewsAttachmentId  = wacRequest.EwsAttachmentId;
                CultureInfo cultureInfo = CultureInfo.GetCultureInfo(wacRequest.CultureName);
                string clientInfoString = "Client=OWA;Action=WAC";
                if (exchangePrincipal.RecipientTypeDetails.HasFlag(RecipientTypeDetails.PublicFolder) || exchangePrincipal.RecipientTypeDetails.HasFlag(RecipientTypeDetails.PublicFolderMailbox))
                {
                    using (PublicFolderSession publicFolderSession = PublicFolderSession.OpenAsAdmin(null, exchangePrincipal, null, cultureInfo, clientInfoString, null))
                    {
                        WacUtilities.ProcessAttachment(publicFolderSession, ewsAttachmentId, exchangePrincipal, openMode, attachmentProcessor);
                        return;
                    }
                }
                using (MailboxSession mailboxSession = MailboxSession.OpenAsSystemService(exchangePrincipal, cultureInfo, clientInfoString))
                {
                    WacUtilities.ProcessAttachment(mailboxSession, ewsAttachmentId, exchangePrincipal, openMode, attachmentProcessor);
                }
            }, adSessionSettings);
        }
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            RequestDetailsLogger logger = OwaApplication.GetRequestDetailsLogger;
            string text = context.Request.QueryString["UserEmail"];

            logger.ActivityScope.SetProperty(OwaServerLogger.LoggerData.PrimarySmtpAddress, text);
            WacUtilities.SetEventId(logger, "WAC.BadRequest");
            context.Response.TrySkipIisCustomErrors = true;
            try
            {
                if (WacActiveMonitoringHandler.IsWacActiveMonitoringRequest(context.Request, context.Response))
                {
                    WacUtilities.SetEventId(logger, "WAC.ActiveMonitoring");
                }
                else
                {
                    WacRequest wacRequest = WacRequest.ParseWacRequest(text, context.Request);
                    WacRequestHandler.SetCommonResponseHeaders(wacRequest, context.Response);
                    string text2 = wacRequest.GetElapsedTime().TotalHours.ToString("0.00");
                    logger.ActivityScope.SetProperty(WacRequestHandlerMetadata.SessionElapsedTime, text2);
                    if (wacRequest.IsExpired())
                    {
                        throw new OwaInvalidRequestException(string.Concat(new object[]
                        {
                            "Can't process ",
                            wacRequest.RequestType,
                            " request because the Url has expired. Hours since start: ",
                            text2
                        }));
                    }
                    logger.Set(WacRequestHandlerMetadata.ExchangeSessionId, wacRequest.ExchangeSessionId);
                    switch (wacRequest.RequestType)
                    {
                    case WacRequestType.CheckFile:
                        WacUtilities.SetEventId(logger, "WAC.CheckFile");
                        WacRequestHandler.ProcessCheckFileRequest(context, wacRequest, logger);
                        goto IL_28B;

                    case WacRequestType.GetFile:
                        WacUtilities.SetEventId(logger, "WAC.GetFile");
                        WacRequestHandler.ProcessGetFileRequest(context, wacRequest, logger);
                        goto IL_28B;

                    case WacRequestType.Lock:
                        WacUtilities.SetEventId(logger, "WAC.Lock");
                        WacRequestHandler.IncrementLockCount(wacRequest);
                        goto IL_28B;

                    case WacRequestType.UnLock:
                        WacUtilities.SetEventId(logger, "WAC.Unlock");
                        WacRequestHandler.DecrementLockCount(wacRequest);
                        goto IL_28B;

                    case WacRequestType.RefreshLock:
                        WacUtilities.SetEventId(logger, "WAC.RefreshLock");
                        goto IL_28B;

                    case WacRequestType.PutFile:
                        WacUtilities.SetEventId(logger, "WAC.PutFile");
                        WacRequestHandler.ReplaceAttachmentContent(context.Request.InputStream, wacRequest);
                        goto IL_28B;

                    case WacRequestType.Cobalt:
                        WacUtilities.SetEventId(logger, "WAC.Cobalt");
                        WacRequestHandler.ProcessCobaltRequest(context, wacRequest, delegate(Enum key, string value)
                        {
                            logger.ActivityScope.SetProperty(key, value);
                        });
                        goto IL_28B;

                    case WacRequestType.DeleteFile:
                        WacUtilities.SetEventId(logger, "WAC.DeleteFile");
                        throw new OwaInvalidRequestException("Exchange does not support WAC's DeleteFile operation.");
                    }
                    WacUtilities.SetEventId(logger, "WAC.Unknown");
                    throw new OwaInvalidRequestException("Invalid request type");
                    IL_28B :;
                }
            }
            catch (Exception ex)
            {
                logger.ActivityScope.SetProperty(ServiceCommonMetadata.GenericErrors, ex.ToString());
                logger.ActivityScope.SetProperty(WacRequestHandlerMetadata.RequestUrl, context.Request.Url.ToString());
                logger.ActivityScope.SetProperty(WacRequestHandlerMetadata.UserAgent, context.Request.UserAgent);
                context.Response.Headers["X-WOPI-ServerError"] = ex.ToString();
                if (ex is OwaInvalidRequestException || ex is OwaOperationNotSupportedException || ex is OverBudgetException)
                {
                    context.Response.StatusCode = 404;
                }
                else
                {
                    context.Response.StatusCode = 500;
                }
            }
            finally
            {
                logger.ActivityScope.SetProperty(WacRequestHandlerMetadata.WopiServerName, context.Request.Headers["X-WOPI-ServerVersion"]);
                logger.ActivityScope.SetProperty(WacRequestHandlerMetadata.WopiClientVersion, context.Request.Headers["X-WOPI-InterfaceVersion"]);
                logger.ActivityScope.SetProperty(WacRequestHandlerMetadata.WopiCorrelationId, context.Request.Headers["X-WOPI-CorrelationID"]);
            }
        }
        private static void DecrementLockCount(WacRequest wacRequest)
        {
            CachedAttachmentInfo cachedAttachmentInfo = WacRequestHandler.GetCachedAttachmentInfo(wacRequest);

            cachedAttachmentInfo.DecrementLockCount();
        }