public Task Invoke(IDictionary<string, object> environment)
        {
            if (!environment.Get<string>(OwinConstants.RequestMethodKey).EqualsIgnoreCase("GET"))
            {
                return _inner(environment);
            }

            var originalOutput = environment.Get<Stream>(OwinConstants.ResponseBodyKey);
            var recordedStream = new MemoryStream();
            environment.Set(OwinConstants.ResponseBodyKey, recordedStream);

            return _inner(environment).ContinueWith(t => {
                recordedStream.Position = 0;
                environment[OwinConstants.ResponseBodyKey] = originalOutput;

                var response = new OwinHttpResponse(environment);

                if (IsGetHtmlRequest(environment) && response.StatusCode < 500)
                {
                    injectContent(environment, recordedStream, response);
                }
                else
                {
                    response.StreamContents(recordedStream);
                }
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment">OWIN environment dictionary which stores state information about the request, response and relevant server state.</param>
        /// <returns></returns>
        public Task Invoke(IDictionary<string, object> environment)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }

            // No IPrincipal
            if (environment.Get<IPrincipal>(Constants.ServerUserKey) == null)
            {
                environment[Constants.ResponseStatusCodeKey] = 401;
                return GetCompletedTask();
            }

            // Anonymous IPrincipal
            var winPrincipal = environment.Get<IPrincipal>(Constants.ServerUserKey) as WindowsPrincipal;
            if (winPrincipal != null)
            {
                var winIdentity = winPrincipal.Identity as WindowsIdentity;
                if (winIdentity != null && winIdentity.IsAnonymous)
                {
                    environment[Constants.ResponseStatusCodeKey] = 401;
                    return GetCompletedTask();
                }
            }

            return _nextApp(environment);
        }
Esempio n. 3
0
 void IConstructable.Construct(IDictionary<string, string> param)
 {
     if (param.Get("source", null) != null)
         Source = param.Get("source", null);
     else
         Source = File.Exists(param["sourceRef"]) ? File.ReadAllText(param["sourceRef"]) : "";
 }
Esempio n. 4
0
        private void PrivateMessageCommand(IDictionary<string, object> command)
        {
            var sender = command.Get(Constants.Arguments.Character);
            if (!CharacterManager.IsOnList(sender, ListKind.Ignored))
            {
                if (ChatModel.CurrentPms.FirstByIdOrNull(sender) == null)
                    channels.AddChannel(ChannelType.PrivateMessage, sender);

                channels.AddMessage(command.Get(Constants.Arguments.Message), sender, sender);

                var temp = ChatModel.CurrentPms.FirstByIdOrNull(sender);
                if (temp == null)
                    return;

                temp.TypingStatus = TypingStatus.Clear; // webclient assumption
            }
            else
            {
                ChatConnection.SendMessage(
                    new Dictionary<string, object>
                    {
                        {Constants.Arguments.Action, Constants.Arguments.ActionNotify},
                        {Constants.Arguments.Character, sender},
                        {Constants.Arguments.Type, Constants.ClientCommands.UserIgnore}
                    });
            }
        }
Esempio n. 5
0
        private void LeaveChannelCommand(IDictionary<string, object> command)
        {
            var channelId = command.Get(Constants.Arguments.Channel);
            var characterName = command.Get(Constants.Arguments.Character);

            var channel = ChatModel.CurrentChannels.FirstByIdOrNull(channelId);
            if (ChatModel.CurrentCharacter.NameEquals(characterName))
            {
                if (channel != null)
                    channels.RemoveChannel(channelId, false, true);

                return;
            }

            if (channel == null)
                return;

            var ignoreUpdate = false;

            if (command.ContainsKey("ignoreUpdate"))
                ignoreUpdate = (bool) command["ignoreUpdate"];

            if (!channel.CharacterManager.SignOff(characterName) || ignoreUpdate) return;

            var updateArgs = new JoinLeaveEventArgs
            {
                Joined = false,
                TargetChannel = channel.Title,
                TargetChannelId = channel.Id
            };

            Events.NewCharacterUpdate(CharacterManager.Find(characterName), updateArgs);
        }
Esempio n. 6
0
        public Task Invoke(IDictionary<string, object> env)
        {
            var opaqueUpgrade = env.Get<OpaqueUpgrade>("opaque.Upgrade");
            var webSocketAccept = env.Get<WebSocketAccept>(Constants.WebSocketAcceptKey);

            if (opaqueUpgrade != null
                && webSocketAccept == null
                && IsWebSocketRequest(env))
            {
                // Add websocket support
                env[Constants.WebSocketAcceptKey] = new WebSocketAccept(
                    (options, callback) =>
                    {
                        if (callback == null)
                        {
                            throw new ArgumentNullException("callback");
                        }

                        env[Constants.ResponseStatusCodeKey] = 101;

                        SetWebSocketResponseHeaders(env, options);

                        opaqueUpgrade(options,
                            opaqueEnv =>
                            {
                                var webSocket = new WebSocketLayer(opaqueEnv);
                                return callback(webSocket.Environment)
                                    .Then(() => webSocket.CleanupAsync());
                            });
                    });
            }

            return _next(env);
        }
Esempio n. 7
0
 private void OnPivateMessageSendRequested(IDictionary<string, object> command)
 {
     channels.AddMessage(
         command.Get(Constants.Arguments.Message),
         command.Get("recipient"),
         Constants.Arguments.ThisCharacter);
     connection.SendMessage(command);
 }
 public void Map(IDictionary<string, object> document, Emitter emitter)
 {
     bool vacant = (bool)document.Get("vacant");
     string name = (string)document.Get("name");
     if (vacant && name != null)
     {
         emitter.Emit(name, vacant);
     }
 }
Esempio n. 9
0
 public RedisLiveConnection(IDictionary<string, string> settings)
 {
     if (!settings.TryGet("host", out _Host))
     {
         throw new Exception("Redis host is required");
     }
     _Port = settings.Get("port", 6379);
     _Timeout = settings.Get("timeout", 2000);
 }
Esempio n. 10
0
        public OwinRequestData(RouteData routeData, IDictionary<string, object> environment, ICurrentHttpRequest currentRequest)
        {
            AddValues(new RouteDataValues(routeData));
            AddValues(Querystring, new NamedKeyValues(HttpUtility.ParseQueryString(environment.Get<string>(OwinConstants.RequestQueryStringKey))));
            AddValues(FormPost, new NamedKeyValues(environment.Get<NameValueCollection>(OwinConstants.RequestFormKey)));

            AddValues(new CookieValueSource(new Cookies(currentRequest)));
            AddValues(new HeaderValueSource(currentRequest));
        }
Esempio n. 11
0
 private void OnLrpRequested(IDictionary<string, object> command)
 {
     channels.AddMessage(
         command.Get(Constants.Arguments.Message),
         command.Get(Constants.Arguments.Channel),
         Constants.Arguments.ThisCharacter,
         MessageType.Ad);
     connection.SendMessage(command);
 }
Esempio n. 12
0
        private void InviteCommand(IDictionary<string, object> command)
        {
            var sender = command.Get(Constants.Arguments.Sender);
            var id = command.Get(Constants.Arguments.Name);
            var title = command.Get(Constants.Arguments.Title);

            var args = new ChannelInviteEventArgs {Inviter = sender};
            Events.NewChannelUpdate(ChatModel.FindChannel(id, title), args);
        }
Esempio n. 13
0
        public static IDisposable Create(Func<IDictionary<string, object>, Task> app, IDictionary<string, object> properties)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }

            if (properties == null)
            {
                throw new ArgumentNullException("properties");
            }

            var capabilities = properties.Get<IDictionary<string, object>>(OwinKeys.ServerCapabilitiesKey)
                               ?? new Dictionary<string, object>();

            var addresses = properties.Get<IList<IDictionary<string, object>>>("host.Addresses")
                            ?? new List<IDictionary<string, object>>();

            var servers = new List<INowinServer>();
            var endpoints = new List<IPEndPoint>();
            foreach (var address in addresses)
            {
                var builder = ServerBuilder.New().SetOwinApp(app);
                int port;
                if (!int.TryParse(address.Get<string>("port"), out port)) throw new ArgumentException("port must be number from 0 to 65535");
                builder.SetPort(port);
                builder.SetOwinCapabilities(capabilities);
                var certificate = address.Get<X509Certificate>("certificate");
                if (certificate != null)
                    builder.SetCertificate(certificate);
                servers.Add(builder.Build());
                endpoints.Add(new IPEndPoint(IPAddress.Loopback,port));
            }

            var disposer = new Disposer(servers.Cast<IDisposable>().ToArray());
            try
            {
                foreach (var nowinServer in servers)
                {
                    nowinServer.Start();
                }
                // This is workaround to Windows Server 2012 issue by calling ReadLine after AcceptAsync, by making one bogus connection this problem goes away
                foreach (var ipEndPoint in endpoints)
                {
                    var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                    socket.Connect(ipEndPoint);
                    socket.Close();
                }
            }
            catch (Exception)
            {
                disposer.Dispose();
                throw;
            }
            return disposer;
        }
Esempio n. 14
0
        private void OnTemporaryInterestedRequested(IDictionary<string, object> command)
        {
            var character = command.Get(Constants.Arguments.Character).ToLower().Trim();
            var add = command.Get(Constants.Arguments.Type) == "tempinteresting";

            characterManager.Add(
                character,
                add ? ListKind.Interested : ListKind.NotInterested,
                true);
        }
Esempio n. 15
0
        public Task Invoke(IDictionary<string, object> env)
        {
            IList<string> warnings = new List<string>();
            if (!TryValidateCall(env, warnings))
            {
                return TaskHelpers.Completed();
            }

            Stream originalStream = env.Get<Stream>("owin.ResponseBody");
            TriggerStream triggerStream = new TriggerStream(originalStream);
            env["owin.ResponseBody"] = triggerStream;

            // Validate response headers and values on first write.
            bool responseValidated = false;
            Action responseValidation = () =>
            {
                if (responseValidated)
                {
                    return;
                }

                responseValidated = true;

                if (!TryValidateResult(env, warnings))
                {
                    return;
                }

                if (warnings.Count > 0)
                {
                    var headers = env.Get<IDictionary<string, string[]>>(OwinConstants.ResponseHeaders);
                    headers["X-OwinValidatorWarning"] = warnings.ToArray();
                }
            };
            triggerStream.OnFirstWrite = responseValidation;

            try
            {
                return nextApp(env)
                    // Run response validation explicitly in case there was no response data written.
                    .Then(responseValidation)
                    .Catch(errorInfo =>
                    {
                        SetFatalResult(env, "6.1", "An asynchronous exception was thrown from the AppFunc: <br>"
                            + errorInfo.Exception.ToString().Replace(Environment.NewLine, "<br>"));
                        return errorInfo.Handled();
                    });
            }
            catch (Exception ex)
            {
                SetFatalResult(env, "6.1", "A synchronous exception was thrown from the AppFunc: <br>"
                            + ex.ToString().Replace(Environment.NewLine, "<br>"));
                return TaskHelpers.Completed();
            }
        }
Esempio n. 16
0
 public void Map(IDictionary<string, object> document, Emitter emitter)
 {
     if (Profile.DocType.Equals(document.Get("type")))
     {
         if (ignoreUserId == null || (ignoreUserId != null && !ignoreUserId.Equals(document
             .Get("user_id"))))
         {
             emitter.Emit(document.Get("name"), document);
         }
     }
 }
Esempio n. 17
0
        public WebSocketLayer(IDictionary<string, object> opaqueEnv)
        {
            _environment = opaqueEnv;
            _environment["websocket.SendAsync"] = new WebSocketSendAsync(SendAsync);
            _environment["websocket.ReceiveAsync"] = new WebSocketReceiveAsync(ReceiveAsync);
            _environment["websocket.CloseAsync"] = new WebSocketCloseAsync(CloseAsync);
            _environment["websocket.CallCancelled"] = _environment["opaque.CallCancelled"];
            _environment["websocket.Version"] = "1.0";

            _incoming = _environment.Get<Stream>("opaque.Incoming");
            _outgoing = _environment.Get<Stream>("opaque.Outgoing");
        }
Esempio n. 18
0
        private void RoomModeChangedCommand(IDictionary<string, object> command)
        {
            var channelId = command.Get(Constants.Arguments.Channel);
            var mode = command.Get(Constants.Arguments.Mode);

            var newMode = mode.ToEnum<ChannelMode>();
            var channel = ChatModel.CurrentChannels.FirstByIdOrNull(channelId);

            if (channel == null)
                return;

            channel.Mode = newMode;
            Events.NewChannelUpdate(channel, new ChannelModeUpdateEventArgs {NewMode = newMode});
        }
Esempio n. 19
0
        public Task Invoke(IDictionary<string, object> environment)
        {
            var path = environment.Get<string>(OwinConstants.RequestPath);
            if (path == null || !path.StartsWith(_path, StringComparison.OrdinalIgnoreCase))
            {
                return _next(environment);
            }

            var pathBase = environment.Get<string>(OwinConstants.RequestPathBase);
            var dispatcher = new HubDispatcher(pathBase + _path, _enableJavaScriptProxies);

            var handler = new CallHandler(_resolver, dispatcher);
            return handler.Invoke(environment);
        }
Esempio n. 20
0
        public Task Invoke(IDictionary<string, object> env)
        {
            string pathInfo = env.Get<string>(OwinConstants.RequestPath);

            if (pathInfo.StartsWith("/"))
            {
                pathInfo = pathInfo.Substring(1);
            }

            if (pathInfo.Contains(".."))
            {
                return Fail(Forbidden, "Forbidden").Invoke(env);
            }

            string path = Path.Combine(root ?? string.Empty, pathInfo);

            if (!File.Exists(path))
            {
                return Fail(NotFound, "File not found: " + pathInfo).Invoke(env);
            }

            try
            {
                return Serve(env, path);
            }
            catch (UnauthorizedAccessException)
            {
                return Fail(Forbidden, "Forbidden").Invoke(env);
            }
        }
Esempio n. 21
0
        public Task Invoke(IDictionary<string, object> env)
        {
            // The TraceSource is assumed to be the same across all requests.
            traceSource = traceSource ?? env.Get<TraceSource>("host.TraceSource");

            if (traceSource == null)
            {
                return nextApp(env);
            }

            try
            {
                TraceCall(env);
                return nextApp(env).Then(() =>
                {
                    TraceResult(env);
                })
                .Catch(errorInfo =>
                {
                    TraceException(errorInfo.Exception, "asynchronously");
                    return errorInfo.Throw();
                });
            }
            catch (Exception ex)
            {
                TraceException(ex, "synchronously");
                throw;
            }
        }
 public CausationContext(IDictionary<string, object> environment, string key, string newValue)
 {
     _environment = environment;
     _key = key;
     _oldValue = environment.Get<string>(key);
     environment[key] = newValue;
 }
Esempio n. 23
0
        private void OnPrivRequested(IDictionary<string, object> command)
        {
            var characterName = command.Get(Constants.Arguments.Character);
            if (cm.CurrentCharacter.NameEquals(characterName))
            {
                events.NewError("Hmmm... talking to yourself?");
                return;
            }

            var isExact = characterName.StartsWith("\"") && characterName.EndsWith("\"");

            ICharacter guess = null;
            if (isExact)
            {
                characterName = characterName.Substring(1, characterName.Length - 2);
            }
            else
            {
                guess = characterManager.SortedCharacters.OrderBy(x => x.Name)
                    .Where(x => !x.NameEquals(cm.CurrentCharacter.Name))
                    .FirstOrDefault(c => c.Name.StartsWith(characterName, true, null));
            }

            channels.JoinChannel(ChannelType.PrivateMessage, guess == null ? characterName : guess.Name);
        }
 public AttributesFacetHandler(string name, string indexFieldName, TermListFactory termListFactory, Term sizePayloadTerm, IDictionary<string, string> facetProps)
     : base(name, indexFieldName, sizePayloadTerm, termListFactory, new string[0])
 {
     if (facetProps.ContainsKey(SEPARATOR_PROP_NAME))
     {
         this.separator = Narrow(facetProps.Get(SEPARATOR_PROP_NAME))[0];
     }
     else
     {
         this.separator = DEFAULT_SEPARATOR;
     }
     if (facetProps.ContainsKey(MAX_FACETS_PER_KEY_PROP_NAME))
     {
         this.numFacetsPerKey = int.Parse(Narrow(facetProps.Get(MAX_FACETS_PER_KEY_PROP_NAME)));
     }
 }
Esempio n. 25
0
        private void ChannelDescriptionCommand(IDictionary<string, object> command)
        {
            lock (chatStateLocker)
            {
                var channel = FindChannel(command);
                var description = command.Get("description");

                if (channel == null)
                {
                    RequeueCommand(command);
                    return;
                }

                var isInitializer = string.IsNullOrWhiteSpace(channel.Description);

                if (string.Equals(channel.Description, description, StringComparison.Ordinal))
                    return;

                channel.Description = WebUtility.HtmlDecode(WebUtility.HtmlDecode(description));

                if (isInitializer)
                    return;

                var args = new ChannelDescriptionChangedEventArgs();
                Events.NewChannelUpdate(channel, args);
            }
        }
Esempio n. 26
0
 private static void DetectWebSocketSupport(IDictionary<string, object> properties)
 {
     // There is no explicit API to detect server side websockets, just check for v4.5 / Win8.
     // Per request we can provide actual verification.
     if (Environment.OSVersion.Version >= new Version(6, 2))
     {
         var capabilities = properties.Get<CapabilitiesDictionary>(Constants.ServerCapabilitiesKey);
         capabilities[Constants.WebSocketVersionKey] = Constants.WebSocketVersion;
     }
     else
     {
         var loggerFactory = properties.Get<LoggerFactoryFunc>(Constants.ServerLoggerFactoryKey);
         LoggerFunc logger = LogHelper.CreateLogger(loggerFactory, typeof(OwinServerFactory));
         LogHelper.LogInfo(logger, "No WebSocket support detected, Windows 8 or later is required.");
     }
 }
Esempio n. 27
0
        public static IDisposable Create(AppFunc app, IDictionary<string, object> properties)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }
            if (properties == null)
            {
                throw new ArgumentNullException("properties");
            }

            // Retrieve the instances created in Initialize
            OwinHttpListener wrapper = properties.Get<OwinHttpListener>(typeof(OwinHttpListener).FullName)
                ?? new OwinHttpListener();
            System.Net.HttpListener listener = properties.Get<System.Net.HttpListener>(typeof(System.Net.HttpListener).FullName)
                ?? new System.Net.HttpListener();

            AddressList addresses = properties.Get<AddressList>(Constants.HostAddressesKey)
                ?? new List<IDictionary<string, object>>();

            CapabilitiesDictionary capabilities =
                properties.Get<CapabilitiesDictionary>(Constants.ServerCapabilitiesKey)
                    ?? new Dictionary<string, object>();

            var loggerFactory = properties.Get<LoggerFactoryFunc>(Constants.ServerLoggerFactoryKey);

            wrapper.Start(listener, app, addresses, capabilities, loggerFactory);
            return wrapper;
        }
Esempio n. 28
0
        private void ChannelInitializedCommand(IDictionary<string, object> command)
        {
            lock (chatStateLocker)
            {
                var channel = FindChannel(command);
                var mode = command.Get(Constants.Arguments.Mode).ToEnum<ChannelMode>();

                if (channel == null)
                {
                    RequeueCommand(command);
                    return;
                }

                channel.Mode = mode;
                var users = (JsonArray) command[Constants.Arguments.MultipleUsers];
                foreach (IDictionary<string, object> character in users)
                {
                    var name = character.Get(Constants.Arguments.Identity);

                    if (string.IsNullOrWhiteSpace(name))
                        continue;

                    channel.CharacterManager.SignOn(CharacterManager.Find(name));
                }
            }
        }
Esempio n. 29
0
        private void SetNewOwnerCommand(IDictionary<string, object> command)
        {
            var character = command.Get(Constants.Arguments.Character);
            var channelId = command.Get(Constants.Arguments.Channel);

            var channel = ChatModel.CurrentChannels.FirstByIdOrNull(channelId);

            if (channel == null) return;

            var mods = channel.CharacterManager.GetNames(ListKind.Moderator, false).ToList();
            mods[0] = character;
            channel.CharacterManager.Set(mods, ListKind.Moderator);

            var update = new ChannelUpdateModel(channel, new ChannelOwnerChangedEventArgs {NewOwner = character});
            Events.NewUpdate(update);
        }
Esempio n. 30
0
        public void Read(IDictionary<string, object> environment)
        {
            var form = new NameValueCollection();
            environment.Add(OwinConstants.RequestFormKey, form);
            var mediaType = environment.Get<string>(OwinConstants.MediaTypeKey);
            if (mediaType != MimeType.HttpFormMimetype && mediaType != MimeType.MultipartMimetype)  return;

            string formValue;
            var body = environment.Get<Stream>(OwinConstants.RequestBodyKey);
            using (var reader = new StreamReader(body))
            {
                formValue = reader.ReadToEnd();
            }

            form.Add(HttpUtility.ParseQueryString(formValue));
        }
Esempio n. 31
0
        public override Type GetPropertyTypeMap(
            IDictionary<string, object> optionalMapPropTypes,
            BeanEventTypeFactory beanEventTypeFactory)
        {
            // The simple, none-dynamic property needs a definition of the map contents else no property

            var def = optionalMapPropTypes?.Get(PropertyNameAtomic);
            if (def == null) {
                return null;
            }

            if (def is Type) {
                return (Type) def;
            }

            if (def is IDictionary<string, object>) {
                return typeof(IDictionary<string, object>);
            }

            if (def is TypeBeanOrUnderlying) {
                var eventType = ((TypeBeanOrUnderlying) def).EventType;
                return eventType.UnderlyingType;
            }

            if (def is TypeBeanOrUnderlying[]) {
                var eventType = ((TypeBeanOrUnderlying[]) def)[0].EventType;
                return TypeHelper.GetArrayType(eventType.UnderlyingType);
            }

            if (def is EventType) {
                var eventType = (EventType) def;
                return eventType.UnderlyingType;
            }

            if (def is EventType[]) {
                var eventType = (EventType[]) def;
                return TypeHelper.GetArrayType(eventType[0].UnderlyingType);
            }

            var message = "Nestable map type configuration encountered an unexpected value type of '" +
                          def.GetType() +
                          "' for property '" +
                          PropertyNameAtomic +
                          "', expected Map or Class";
            throw new PropertyAccessException(message);
        }
Esempio n. 32
0
        public override MapEventPropertyGetter GetGetterMap(
            IDictionary<string, object> optionalMapPropTypes,
            EventBeanTypedEventFactory eventBeanTypedEventFactory,
            BeanEventTypeFactory beanEventTypeFactory)
        {
            // The simple, none-dynamic property needs a definition of the map contents else no property

            var def = optionalMapPropTypes?.Get(PropertyNameAtomic);
            if (def == null) {
                return null;
            }

            if (def is EventType) {
                return new MapEventBeanPropertyGetter(PropertyNameAtomic, ((EventType) def).UnderlyingType);
            }

            return new MapPropertyGetterDefaultNoFragment(PropertyNameAtomic, eventBeanTypedEventFactory);
        }
Esempio n. 33
0
        private void UploadChanges(IList <RevisionInternal> changes, IDictionary <string, object> revsDiffResults)
        {
            // Go through the list of local changes again, selecting the ones the destination server
            // said were missing and mapping them to a JSON dictionary in the form _bulk_docs wants:
            var docsToSend = new List <object> ();
            var revsToSend = new RevisionList();
            IDictionary <string, object> revResults = null;

            foreach (var rev in changes)
            {
                // Is this revision in the server's 'missing' list?
                if (revsDiffResults != null)
                {
                    revResults = revsDiffResults.Get(rev.DocID).AsDictionary <string, object>();
                    if (revResults == null)
                    {
                        continue;
                    }

                    var revs = revResults.Get("missing").AsList <string>();
                    if (revs == null || !revs.Any(id => id.Equals(rev.RevID.ToString())))
                    {
                        RemovePending(rev);
                        continue;
                    }
                }

                IDictionary <string, object> properties = null;
                RevisionInternal             loadedRev;
                try {
                    loadedRev = LocalDatabase.LoadRevisionBody(rev);
                    if (loadedRev == null)
                    {
                        throw Misc.CreateExceptionAndLog(Log.To.Sync, StatusCode.NotFound, TAG,
                                                         "Unable to load revision body");
                    }

                    properties = new Dictionary <string, object>(rev.GetProperties());
                } catch (Exception e1) {
                    Log.To.Sync.E(TAG, String.Format("Couldn't get local contents of {0}, marking revision failed",
                                                     rev), e1);
                    RevisionFailed();
                    continue;
                }

                if (properties.GetCast <bool> ("_removed"))
                {
                    RemovePending(rev);
                    continue;
                }

                var populatedRev = TransformRevision(loadedRev);
                var backTo       = revResults?.Get("possible_ancestors")?.AsList <RevisionID>();

                try {
                    var history = LocalDatabase.GetRevisionHistory(populatedRev, backTo);
                    if (history == null)
                    {
                        throw Misc.CreateExceptionAndLog(Log.To.Sync, StatusCode.DbError, TAG,
                                                         "Unable to load revision history");
                    }

                    properties["_revisions"] = TreeRevisionID.MakeRevisionHistoryDict(history);
                    populatedRev.SetPropertyForKey("_revisions", properties["_revisions"]);
                } catch (Exception e1) {
                    Log.To.Sync.E(TAG, "Error getting revision history, marking revision failed", e1);
                    RevisionFailed();
                    continue;
                }

                // Strip any attachments already known to the target db:
                if (properties.Get("_attachments") != null)
                {
                    // Look for the latest common ancestor and stuf out older attachments:
                    var minRevPos = FindCommonAncestor(populatedRev, backTo);
                    try {
                        LocalDatabase.ExpandAttachments(populatedRev, minRevPos + 1, !_dontSendMultipart, false);
                    } catch (Exception ex) {
                        Log.To.Sync.E(TAG, "Error expanding attachments, marking revision failed", ex);
                        RevisionFailed();
                        continue;
                    }

                    properties = populatedRev.GetProperties();
                    if (!_dontSendMultipart && UploadMultipartRevision(populatedRev))
                    {
                        continue;
                    }
                }

                if (properties == null || !properties.ContainsKey("_id"))
                {
                    throw Misc.CreateExceptionAndLog(Log.To.Sync, StatusCode.BadParam, TAG,
                                                     "properties must contain a document _id");
                }

                // Add the _revisions list:
                revsToSend.Add(rev);

                //now add it to the docs to send
                docsToSend.Add(properties);
            }

            UploadBulkDocs(docsToSend, revsToSend);
        }
Esempio n. 34
0
 public override string DeploymentGetStage(string deploymentId)
 {
     return(_deploymentIdStages?.Get(deploymentId));
 }
Esempio n. 35
0
        public AttachmentInternal(string name, IDictionary <string, object> info)
            : this(name, info.GetCast <string>("content_type"))
        {
            Length        = info.GetCast <long>("length");
            EncodedLength = info.GetCast <long>("encoded_length");
            _digest       = info.GetCast <string>("digest");
            if (_digest != null)
            {
                BlobKey = new BlobKey(_digest);
            }

            string encodingString = info.GetCast <string>("encoding");

            if (encodingString != null)
            {
                if (encodingString.Equals("gzip"))
                {
                    Encoding = AttachmentEncoding.GZIP;
                }
                else
                {
                    throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.BadEncoding, TAG,
                                                     "Invalid encoding type ({0}) in ctor", encodingString);
                }
            }

            var data = info.Get("data");

            if (data != null)
            {
                // If there's inline attachment data, decode and store it:
                if (data is string)
                {
                    _data = Convert.FromBase64String((string)data);
                }
                else
                {
                    _data = data as IEnumerable <byte>;
                }

                if (_data == null)
                {
                    throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.BadEncoding, TAG,
                                                     "Invalid data type ({0}) in ctor", data.GetType().Name);
                }

                SetPossiblyEncodedLength(_data.LongCount());
            }
            else if (info.GetCast <bool>("stub", false))
            {
                // This item is just a stub; validate and skip it
                if (info.ContainsKey("revpos"))
                {
                    var revPos = info.GetCast("revpos", -1);  // PouchDB has a bug that generates "revpos":0; allow this (#627)
                    if (revPos < 0)
                    {
                        throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.BadAttachment, TAG,
                                                         "Invalid revpos ({0}) in ctor", revPos);
                    }

                    RevPos = revPos;
                }
            }
            else if (info.GetCast <bool>("follows", false))
            {
                // I can't handle this myself; my caller will look it up from the digest
                if (Digest == null)
                {
                    throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.BadAttachment, TAG,
                                                     "follows is true, but the attachment digest is null in ctor");
                }

                if (info.ContainsKey("revpos"))
                {
                    var revPos = info.GetCast("revpos", -1);  // PouchDB has a bug that generates "revpos":0; allow this (#627)
                    if (revPos < 0)
                    {
                        throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.BadAttachment, TAG,
                                                         "Invalid revpos ({0}) in ctor", revPos);
                    }

                    RevPos = revPos;
                }
            }
            else
            {
                throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.BadAttachment, TAG,
                                                 "Neither data nor stub nor follows was specified on the attachment data");
            }
        }
Esempio n. 36
0
 internal static IDictionary <string, object> CblAttachments(this IDictionary <string, object> dict)
 {
     return(dict?.Get("_attachments").AsDictionary <string, object>());
 }
Esempio n. 37
0
        public virtual PdfDictionary BuildTree()
        {
            int?[] numbers = new int?[items.Count];
            numbers = items.Keys.ToArray(numbers);
            iText.IO.Util.JavaUtil.Sort(numbers);
            if (numbers.Length <= NODE_SIZE)
            {
                PdfDictionary dic = new PdfDictionary();
                PdfArray      ar  = new PdfArray();
                for (int k = 0; k < numbers.Length; ++k)
                {
                    ar.Add(new PdfNumber((int)numbers[k]));
                    ar.Add(items.Get(numbers[k]));
                }
                dic.Put(PdfName.Nums, ar);
                return(dic);
            }
            int skip = NODE_SIZE;

            PdfDictionary[] kids = new PdfDictionary[(numbers.Length + NODE_SIZE - 1) / NODE_SIZE];
            for (int i = 0; i < kids.Length; ++i)
            {
                int           offset = i * NODE_SIZE;
                int           end    = Math.Min(offset + NODE_SIZE, numbers.Length);
                PdfDictionary dic    = new PdfDictionary();
                PdfArray      arr    = new PdfArray();
                arr.Add(new PdfNumber((int)numbers[offset]));
                arr.Add(new PdfNumber((int)numbers[end - 1]));
                dic.Put(PdfName.Limits, arr);
                arr = new PdfArray();
                for (; offset < end; ++offset)
                {
                    arr.Add(new PdfNumber((int)numbers[offset]));
                    arr.Add(items.Get(numbers[offset]));
                }
                dic.Put(PdfName.Nums, arr);
                dic.MakeIndirect(catalog.GetDocument());
                kids[i] = dic;
            }
            int top = kids.Length;

            while (true)
            {
                if (top <= NODE_SIZE)
                {
                    PdfArray arr = new PdfArray();
                    for (int k = 0; k < top; ++k)
                    {
                        arr.Add(kids[k]);
                    }
                    PdfDictionary dic = new PdfDictionary();
                    dic.Put(PdfName.Kids, arr);
                    return(dic);
                }
                skip *= NODE_SIZE;
                int tt = (numbers.Length + skip - 1) / skip;
                for (int k_1 = 0; k_1 < tt; ++k_1)
                {
                    int           offset = k_1 * NODE_SIZE;
                    int           end    = Math.Min(offset + NODE_SIZE, top);
                    PdfDictionary dic    = ((PdfDictionary) new PdfDictionary().MakeIndirect(catalog.GetDocument()));
                    PdfArray      arr    = new PdfArray();
                    arr.Add(new PdfNumber((int)numbers[k_1 * skip]));
                    arr.Add(new PdfNumber((int)numbers[Math.Min((k_1 + 1) * skip, numbers.Length) - 1]));
                    dic.Put(PdfName.Limits, arr);
                    arr = new PdfArray();
                    for (; offset < end; ++offset)
                    {
                        arr.Add(kids[offset]);
                    }
                    dic.Put(PdfName.Kids, arr);
                    kids[k_1] = dic;
                }
                top = tt;
            }
        }
 public Ref GetRef(string name)
 {
     return(advertisedRefs.Get(name));
 }
Esempio n. 39
0
 public static String UnicodeToName(int num)
 {
     return(unicode2names.Get(num));
 }
Esempio n. 40
0
 /// <summary>
 ///     NOTE: Code-generation-invoked method, method name and parameter order matters
 /// </summary>
 /// <param name="map">map</param>
 /// <param name="key">key</param>
 /// <returns>value</returns>
 public static object GetAvroMappedValueWNullCheck(
     IDictionary<string, object> map,
     string key)
 {
     return map?.Get(key);
 }
Esempio n. 41
0
        /// <exception cref="System.IO.IOException"/>
        private void OutputDss(PdfDictionary dss, PdfDictionary vrim, PdfArray ocsps, PdfArray crls, PdfArray certs
                               )
        {
            PdfCatalog catalog = document.GetCatalog();

            catalog.AddDeveloperExtension(PdfDeveloperExtension.ESIC_1_7_EXTENSIONLEVEL5);
            catalog.SetModified();
            foreach (PdfName vkey in validated.Keys)
            {
                PdfArray      ocsp = new PdfArray();
                PdfArray      crl  = new PdfArray();
                PdfArray      cert = new PdfArray();
                PdfDictionary vri  = new PdfDictionary();
                foreach (byte[] b in validated.Get(vkey).crls)
                {
                    PdfStream ps = new PdfStream(b);
                    ps.SetCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION);
                    ps.MakeIndirect(document);
                    crl.Add(ps);
                    crls.Add(ps);
                }
                foreach (byte[] b_1 in validated.Get(vkey).ocsps)
                {
                    PdfStream ps = new PdfStream(b_1);
                    ps.SetCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION);
                    ocsp.Add(ps);
                    ocsps.Add(ps);
                }
                foreach (byte[] b_2 in validated.Get(vkey).certs)
                {
                    PdfStream ps = new PdfStream(b_2);
                    ps.SetCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION);
                    ps.MakeIndirect(document);
                    cert.Add(ps);
                    certs.Add(ps);
                }
                if (ocsp.Size() > 0)
                {
                    ocsp.MakeIndirect(document);
                    vri.Put(PdfName.OCSP, ocsp);
                }
                if (crl.Size() > 0)
                {
                    crl.MakeIndirect(document);
                    vri.Put(PdfName.CRL, crl);
                }
                if (cert.Size() > 0)
                {
                    cert.MakeIndirect(document);
                    vri.Put(PdfName.Cert, cert);
                }
                vri.MakeIndirect(document);
                vrim.Put(vkey, vri);
            }
            vrim.MakeIndirect(document);
            dss.Put(PdfName.VRI, vrim);
            if (ocsps.Size() > 0)
            {
                ocsps.MakeIndirect(document);
                dss.Put(PdfName.OCSPs, ocsps);
            }
            if (crls.Size() > 0)
            {
                crls.MakeIndirect(document);
                dss.Put(PdfName.CRLs, crls);
            }
            if (certs.Size() > 0)
            {
                certs.MakeIndirect(document);
                dss.Put(PdfName.Certs, certs);
            }
            dss.MakeIndirect(document);
            catalog.Put(PdfName.DSS, dss);
        }
 /// =========================================================================================================================================================
 /// <summary>
 /// Gets a value from the given dictionary
 /// </summary>
 /// <typeparam name="K">The type parameter for the keys of the dictionary</typeparam>
 /// <typeparam name="V">The type parameter for the values of the dictionary</typeparam>
 /// <param name="dictionary">The dictionary to get the value from</param>
 /// <param name="key">The key of the value that should be returned from the dictionary</param>
 /// <returns>The value of the given key or the default value of the type parameter for the values</returns>
 /// =========================================================================================================================================================
 public static V Get <K, V>(this IDictionary <K, V> dictionary, K key)
 {
     return(dictionary.Get(key, default(V)));
 }
Esempio n. 43
0
 public static ExchangeDeclaration GetExchangeDeclaration(this IDictionary <string, object> ctx)
 {
     return(ctx.Get <ExchangeDeclaration>(RetryKey.ExchangeDeclaration));
 }
Esempio n. 44
0
 public static QueueDeclaration GetQueueDeclaration(this IDictionary <string, object> ctx)
 {
     return(ctx.Get <QueueDeclaration>(RetryKey.QueueDeclaration));
 }
        public AttachmentInternal(string name, IDictionary <string, object> info)
            : this(name, info.GetCast <string>("content_type"))
        {
            Length        = info.GetCast <long>("length");
            EncodedLength = info.GetCast <long>("encoded_length");
            _digest       = info.GetCast <string>("digest");
            if (_digest != null)
            {
                BlobKey = new BlobKey(_digest);
            }

            string encodingString = info.GetCast <string>("encoding");

            if (encodingString != null)
            {
                if (encodingString.Equals("gzip"))
                {
                    Encoding = AttachmentEncoding.GZIP;
                }
                else
                {
                    throw new CouchbaseLiteException(StatusCode.BadEncoding);
                }
            }

            var data = info.Get("data");

            if (data != null)
            {
                // If there's inline attachment data, decode and store it:
                if (data is string)
                {
                    _data = Convert.FromBase64String((string)data);
                }
                else
                {
                    _data = data as IEnumerable <byte>;
                }

                if (_data == null)
                {
                    throw new CouchbaseLiteException(StatusCode.BadEncoding);
                }

                SetPossiblyEncodedLength(_data.LongCount());
            }
            else if (info.GetCast <bool>("stub", false))
            {
                // This item is just a stub; validate and skip it
                if (info.ContainsKey("revpos"))
                {
                    var revPos = info.GetCast <int>("revpos");
                    if (revPos <= 0)
                    {
                        throw new CouchbaseLiteException(StatusCode.BadAttachment);
                    }

                    RevPos = revPos;
                }
            }
            else if (info.GetCast <bool>("follows", false))
            {
                // I can't handle this myself; my caller will look it up from the digest
                if (Digest == null)
                {
                    throw new CouchbaseLiteException(StatusCode.BadAttachment);
                }

                if (info.ContainsKey("revpos"))
                {
                    var revPos = info.GetCast <int>("revpos");
                    if (revPos <= 0)
                    {
                        throw new CouchbaseLiteException(StatusCode.BadAttachment);
                    }

                    RevPos = revPos;
                }
            }
            else
            {
                throw new CouchbaseLiteException(StatusCode.BadAttachment);
            }
        }
Esempio n. 46
0
 public static string GetRoutingKey(this IDictionary <string, object> ctx)
 {
     return(ctx.Get <string>(RetryKey.RoutingKey));
 }
Esempio n. 47
0
        private void OnForceChannelCloseRequested(IDictionary <string, object> command)
        {
            var channelName = command.Get(Constants.Arguments.Channel);

            channels.RemoveChannel(channelName, true);
        }
Esempio n. 48
0
 public static IChannelFactory GetChannelFactory(this IDictionary <string, object> ctx)
 {
     return(ctx.Get <IChannelFactory>(RetryKey.ChannelFactory));
 }
Esempio n. 49
0
 private void OnCloseRequested(IDictionary <string, object> command)
 {
     channels.RemoveChannel(command.Get(Constants.Arguments.Channel));
 }
Esempio n. 50
0
 public static ITopologyProvider GetTopologyProvider(this IDictionary <string, object> ctx)
 {
     return(ctx.Get <ITopologyProvider>(RetryKey.TopologyProvider));
 }
Esempio n. 51
0
        public void TestEmptyListFront()
        {
            var testWindow = new LinkedList <TimeWindowPair>();

            var list1 = new LinkedList <EventBean>();

            AddToWindow(testWindow, 10L, list1);

            var list2 = new LinkedList <EventBean>();

            list2.AddLast(_events.Get("a"));
            AddToWindow(testWindow, 15L, list2);

            var list3 = new LinkedList <EventBean>();

            list3.AddLast(_events.Get("c"));
            list3.AddLast(_events.Get("d"));
            AddToWindow(testWindow, 20L, list3);

            var list4 = new LinkedList <EventBean>();

            list4.AddLast(_events.Get("e"));
            AddToWindow(testWindow, 40L, list4);

            var it = new TimeWindowEnumerator(testWindow);

            EPAssertionUtil.AssertEqualsExactOrder(
                new[]
            {
                _events.Get("a"),
                _events.Get("c"),
                _events.Get("d"),
                _events.Get("e")
            }, it);
        }
Esempio n. 52
0
 public virtual TaggingHintKey GetParentHint(TaggingHintKey hintKey)
 {
     return(parentHints.Get(hintKey));
 }
Esempio n. 53
0
        private static void RecursiveAdd(
            CodegenMethod methodNode,
            IDictionary<string, Type> currentSymbols,
            CodegenClassMethods classMethods,
            CodegenClassProperties classProperties,
            bool isStatic)
        {
            ISet<string> namesPassed = GetNamesPassed(methodNode);
            methodNode.DeepParameters = namesPassed;

            IList<CodegenNamedParam> paramset = new List<CodegenNamedParam>(
                namesPassed.Count + methodNode.LocalParams.Count);

            // add local params
            foreach (var named in methodNode.LocalParams) {
                paramset.Add(named);
            }

            // add pass-thru for those methods that do not have their own scope
            if (methodNode.OptionalSymbolProvider == null) {
                foreach (var nameX in namesPassed) {
                    var symbolType = currentSymbols.Get(nameX);
                    if (symbolType == null) {
                        throw new IllegalStateException(
                            "Failed to find named parameter '" +
                            nameX +
                            "' for method from " +
                            methodNode.AdditionalDebugInfo);
                    }

                    paramset.Add(new CodegenNamedParam(symbolType, nameX));
                }
            }
            else {
                currentSymbols = new Dictionary<string, Type>();
                methodNode.OptionalSymbolProvider.Provide(currentSymbols);
            }

            var name = "M" + classMethods.PrivateMethods.Count;
            var footprint = new CodegenMethodFootprint(
                methodNode.ReturnType,
                methodNode.ReturnTypeName,
                paramset,
                methodNode.AdditionalDebugInfo);
            var method = new CodegenMethodWGraph(
                name,
                footprint,
                methodNode.Block,
                false,
                methodNode.IsOverride,
                isStatic);

            methodNode.AssignedMethod = method;
            classMethods.PrivateMethods.Add(method);

            foreach (var child in methodNode.Children) {
                RecursiveAdd(
                    child,
                    currentSymbols,
                    classMethods,
                    classProperties,
                    isStatic);
            }
        }
Esempio n. 54
0
 public static IPipeContext GetPipeContext(this IDictionary <string, object> ctx)
 {
     return(ctx.Get <IPipeContext>(RetryKey.PipeContext));
 }
Esempio n. 55
0
        /* (non-Javadoc)
         * @see com.itextpdf.html2pdf.css.apply.ICssApplier#apply(com.itextpdf.html2pdf.attach.ProcessorContext, com.itextpdf.html2pdf.html.node.IStylesContainer, com.itextpdf.html2pdf.attach.ITagWorker)
         */
        public virtual void Apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker tagWorker
                                  )
        {
            IDictionary <String, String> cssProps  = stylesContainer.GetStyles();
            IPropertyContainer           container = tagWorker.GetElementResult();

            if (container != null)
            {
                if (container is Div)
                {
                    if (!cssProps.ContainsKey(CssConstants.WIDTH) || CssConstants.AUTO.Equals(cssProps.Get(CssConstants.WIDTH)
                                                                                              ))
                    {
                        ((Div)container).SetWidth(DEFAULT_CONTENT_WIDTH_PT);
                    }
                    if (!cssProps.ContainsKey(CssConstants.HEIGHT) || CssConstants.AUTO.Equals(cssProps.Get(CssConstants.HEIGHT
                                                                                                            )))
                    {
                        ((Div)container).SetHeight(DEFAULT_CONTENT_HEIGHT_PT);
                    }
                }
                WidthHeightApplierUtil.ApplyWidthHeight(cssProps, context, container);
                BackgroundApplierUtil.ApplyBackground(cssProps, context, container);
            }
        }
Esempio n. 56
0
        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="viewChain">views</param>
        /// <param name="matchRecognizeSpec">specification</param>
        /// <param name="agentInstanceContext">The agent instance context.</param>
        /// <param name="isUnbound">true for unbound stream</param>
        /// <param name="annotations">annotations</param>
        /// <exception cref="ExprValidationException">
        /// Variable ' + defineItem.Identifier + ' has already been defined
        /// or
        /// An aggregate function may not appear in a DEFINE clause
        /// or
        /// Failed to validate condition expression for variable ' + defineItem.Identifier + ':  + ex.Message
        /// or
        /// Aggregation functions in the measure-clause must only refer to properties of exactly one group variable returning multiple events
        /// or
        /// Aggregation functions in the measure-clause must refer to one or more properties of exactly one group variable returning multiple events
        /// or
        /// The measures clause requires that each expression utilizes the AS keyword to assign a column name
        /// </exception>
        /// <throws>ExprValidationException if validation fails</throws>
        public EventRowRegexNFAViewFactory(
            ViewFactoryChain viewChain,
            MatchRecognizeSpec matchRecognizeSpec,
            AgentInstanceContext agentInstanceContext,
            bool isUnbound,
            Attribute[] annotations,
            ConfigurationEngineDefaults.MatchRecognize matchRecognizeConfig)
        {
            var parentViewType = viewChain.EventType;

            _matchRecognizeSpec   = matchRecognizeSpec;
            _isUnbound            = isUnbound;
            _isIterateOnly        = HintEnum.ITERATE_ONLY.GetHint(annotations) != null;
            _matchRecognizeConfig = matchRecognizeConfig;

            var statementContext = agentInstanceContext.StatementContext;

            // Expand repeats and permutations
            _expandedPatternNode = RegexPatternExpandUtil.Expand(matchRecognizeSpec.Pattern);

            // Determine single-row and multiple-row variables
            _variablesSingle = new LinkedHashSet <string>();
            ISet <string> variablesMultiple = new LinkedHashSet <string>();

            EventRowRegexHelper.RecursiveInspectVariables(_expandedPatternNode, false, _variablesSingle, variablesMultiple);

            // each variable gets associated with a stream number (multiple-row variables as well to hold the current event for the expression).
            var streamNum = 0;

            _variableStreams = new LinkedHashMap <string, Pair <int, bool> >();
            foreach (var variableSingle in _variablesSingle)
            {
                _variableStreams.Put(variableSingle, new Pair <int, bool>(streamNum, false));
                streamNum++;
            }
            foreach (var variableMultiple in variablesMultiple)
            {
                _variableStreams.Put(variableMultiple, new Pair <int, bool>(streamNum, true));
                streamNum++;
            }

            // mapping of stream to variable
            _streamVariables = new SortedDictionary <int, string>();
            foreach (var entry in _variableStreams)
            {
                _streamVariables.Put(entry.Value.First, entry.Key);
            }

            // determine visibility rules
            var visibility = EventRowRegexHelper.DetermineVisibility(_expandedPatternNode);

            // assemble all single-row variables for expression validation
            var allStreamNames = new string[_variableStreams.Count];
            var allTypes       = new EventType[_variableStreams.Count];

            streamNum = 0;
            foreach (var variableSingle in _variablesSingle)
            {
                allStreamNames[streamNum] = variableSingle;
                allTypes[streamNum]       = parentViewType;
                streamNum++;
            }
            foreach (var variableMultiple in variablesMultiple)
            {
                allStreamNames[streamNum] = variableMultiple;
                allTypes[streamNum]       = parentViewType;
                streamNum++;
            }

            // determine type service for use with DEFINE
            // validate each DEFINE clause expression
            ISet <string>             definedVariables = new HashSet <string>();
            IList <ExprAggregateNode> aggregateNodes   = new List <ExprAggregateNode>();
            var exprEvaluatorContext = new ExprEvaluatorContextStatement(statementContext, false);

            _isExprRequiresMultimatchState = new bool[_variableStreams.Count];

            for (var defineIndex = 0; defineIndex < matchRecognizeSpec.Defines.Count; defineIndex++)
            {
                var defineItem = matchRecognizeSpec.Defines[defineIndex];
                if (definedVariables.Contains(defineItem.Identifier))
                {
                    throw new ExprValidationException("Variable '" + defineItem.Identifier + "' has already been defined");
                }
                definedVariables.Add(defineItem.Identifier);

                // stream-type visibilities handled here
                var typeServiceDefines = EventRowRegexNFAViewFactoryHelper.BuildDefineStreamTypeServiceDefine(statementContext, _variableStreams, defineItem, visibility, parentViewType);

                var exprNodeResult    = HandlePreviousFunctions(defineItem.Expression);
                var validationContext = new ExprValidationContext(
                    typeServiceDefines,
                    statementContext.MethodResolutionService, null,
                    statementContext.SchedulingService,
                    statementContext.VariableService,
                    statementContext.TableService, exprEvaluatorContext,
                    statementContext.EventAdapterService,
                    statementContext.StatementName,
                    statementContext.StatementId,
                    statementContext.Annotations,
                    statementContext.ContextDescriptor,
                    statementContext.ScriptingService,
                    true, false, true, false, null, false);

                ExprNode validated;
                try {
                    // validate
                    validated = ExprNodeUtility.GetValidatedSubtree(ExprNodeOrigin.MATCHRECOGDEFINE, exprNodeResult, validationContext);

                    // check aggregates
                    defineItem.Expression = validated;
                    ExprAggregateNodeUtil.GetAggregatesBottomUp(validated, aggregateNodes);
                    if (!aggregateNodes.IsEmpty())
                    {
                        throw new ExprValidationException("An aggregate function may not appear in a DEFINE clause");
                    }
                }
                catch (ExprValidationException ex) {
                    throw new ExprValidationException("Failed to validate condition expression for variable '" + defineItem.Identifier + "': " + ex.Message, ex);
                }

                // determine access to event properties from multi-matches
                var visitor = new ExprNodeIdentifierCollectVisitor();
                validated.Accept(visitor);
                var streamsRequired = visitor.StreamsRequired;
                foreach (var streamRequired in streamsRequired)
                {
                    if (streamRequired >= _variableStreams.Count)
                    {
                        var streamNumIdent = _variableStreams.Get(defineItem.Identifier).First;
                        _isExprRequiresMultimatchState[streamNumIdent] = true;
                        break;
                    }
                }
            }
            _isDefineAsksMultimatches  = CollectionUtil.IsAnySet(_isExprRequiresMultimatchState);
            _defineMultimatchEventBean = _isDefineAsksMultimatches ? EventRowRegexNFAViewFactoryHelper.GetDefineMultimatchBean(statementContext, _variableStreams, parentViewType) : null;

            // assign "prev" node indexes
            // Since an expression such as "prior(2, price), prior(8, price)" translates into {2, 8} the relative index is {0, 1}.
            // Map the expression-supplied index to a relative index
            var countPrev = 0;

            foreach (var entry in _callbacksPerIndex)
            {
                foreach (var callback in entry.Value)
                {
                    callback.AssignedIndex = countPrev;
                }
                countPrev++;
            }

            // determine type service for use with MEASURE
            IDictionary <string, object> measureTypeDef = new LinkedHashMap <string, object>();

            foreach (var variableSingle in _variablesSingle)
            {
                measureTypeDef.Put(variableSingle, parentViewType);
            }
            foreach (var variableMultiple in variablesMultiple)
            {
                measureTypeDef.Put(variableMultiple, new EventType[] { parentViewType });
            }
            var outputEventTypeName = statementContext.StatementId + "_rowrecog";

            _compositeEventType = (ObjectArrayEventType)statementContext.EventAdapterService.CreateAnonymousObjectArrayType(outputEventTypeName, measureTypeDef);
            StreamTypeService typeServiceMeasure = new StreamTypeServiceImpl(_compositeEventType, "MATCH_RECOGNIZE", true, statementContext.EngineURI);

            // find MEASURE clause aggregations
            var measureReferencesMultivar = false;
            IList <ExprAggregateNode> measureAggregateExprNodes = new List <ExprAggregateNode>();

            foreach (var measureItem in matchRecognizeSpec.Measures)
            {
                ExprAggregateNodeUtil.GetAggregatesBottomUp(measureItem.Expr, measureAggregateExprNodes);
            }
            if (!measureAggregateExprNodes.IsEmpty())
            {
                var isIStreamOnly = new bool[allStreamNames.Length];
                CompatExtensions.Fill(isIStreamOnly, true);
                var typeServiceAggregateMeasure  = new StreamTypeServiceImpl(allTypes, allStreamNames, isIStreamOnly, statementContext.EngineURI, false);
                var measureExprAggNodesPerStream = new Dictionary <int, IList <ExprAggregateNode> >();

                foreach (var aggregateNode in measureAggregateExprNodes)
                {
                    // validate absence of group-by
                    aggregateNode.ValidatePositionals();
                    if (aggregateNode.OptionalLocalGroupBy != null)
                    {
                        throw new ExprValidationException("Match-recognize does not allow aggregation functions to specify a group-by");
                    }

                    // validate node and params
                    var count   = 0;
                    var visitor = new ExprNodeIdentifierVisitor(true);

                    var validationContext = new ExprValidationContext(
                        typeServiceAggregateMeasure,
                        statementContext.MethodResolutionService, null,
                        statementContext.SchedulingService,
                        statementContext.VariableService,
                        statementContext.TableService,
                        exprEvaluatorContext,
                        statementContext.EventAdapterService,
                        statementContext.StatementName,
                        statementContext.StatementId,
                        statementContext.Annotations,
                        statementContext.ContextDescriptor,
                        statementContext.ScriptingService,
                        false, false, true, false, null, false);
                    foreach (var child in aggregateNode.ChildNodes)
                    {
                        var validated = ExprNodeUtility.GetValidatedSubtree(ExprNodeOrigin.MATCHRECOGMEASURE, child, validationContext);
                        validated.Accept(visitor);
                        aggregateNode.SetChildNode(count++, new ExprNodeValidated(validated));
                    }
                    validationContext = new ExprValidationContext(
                        typeServiceMeasure, statementContext.MethodResolutionService, null,
                        statementContext.SchedulingService,
                        statementContext.VariableService,
                        statementContext.TableService,
                        exprEvaluatorContext,
                        statementContext.EventAdapterService,
                        statementContext.StatementName,
                        statementContext.StatementId,
                        statementContext.Annotations,
                        statementContext.ContextDescriptor,
                        statementContext.ScriptingService,
                        false, false, true, false, null, false);
                    aggregateNode.Validate(validationContext);

                    // verify properties used within the aggregation
                    var aggregatedStreams = new HashSet <int>();
                    foreach (var pair in visitor.ExprProperties)
                    {
                        aggregatedStreams.Add(pair.First);
                    }

                    int?multipleVarStream = null;
                    foreach (int streamNumAggregated in aggregatedStreams)
                    {
                        var variable = _streamVariables.Get(streamNumAggregated);
                        if (variablesMultiple.Contains(variable))
                        {
                            measureReferencesMultivar = true;
                            if (multipleVarStream == null)
                            {
                                multipleVarStream = streamNumAggregated;
                                continue;
                            }
                            throw new ExprValidationException("Aggregation functions in the measure-clause must only refer to properties of exactly one group variable returning multiple events");
                        }
                    }

                    if (multipleVarStream == null)
                    {
                        throw new ExprValidationException("Aggregation functions in the measure-clause must refer to one or more properties of exactly one group variable returning multiple events");
                    }

                    var aggNodesForStream = measureExprAggNodesPerStream.Get(multipleVarStream.Value);
                    if (aggNodesForStream == null)
                    {
                        aggNodesForStream = new List <ExprAggregateNode>();
                        measureExprAggNodesPerStream.Put(multipleVarStream.Value, aggNodesForStream);
                    }
                    aggNodesForStream.Add(aggregateNode);
                }

                var factoryDesc = AggregationServiceFactoryFactory.GetServiceMatchRecognize(_streamVariables.Count, measureExprAggNodesPerStream, typeServiceAggregateMeasure.EventTypes);
                _aggregationService     = factoryDesc.AggregationServiceFactory.MakeService(agentInstanceContext);
                _aggregationExpressions = factoryDesc.Expressions;
            }
            else
            {
                _aggregationService     = null;
                _aggregationExpressions = Collections.GetEmptyList <AggregationServiceAggExpressionDesc>();
            }

            // validate each MEASURE clause expression
            IDictionary <string, object> rowTypeDef = new LinkedHashMap <string, object>();
            var streamRefVisitor = new ExprNodeStreamUseCollectVisitor();

            foreach (var measureItem in matchRecognizeSpec.Measures)
            {
                if (measureItem.Name == null)
                {
                    throw new ExprValidationException("The measures clause requires that each expression utilizes the AS keyword to assign a column name");
                }
                var validated = ValidateMeasureClause(measureItem.Expr, typeServiceMeasure, variablesMultiple, _variablesSingle, statementContext);
                measureItem.Expr = validated;
                rowTypeDef.Put(measureItem.Name, validated.ExprEvaluator.ReturnType);
                validated.Accept(streamRefVisitor);
            }

            // Determine if any of the multi-var streams are referenced in the measures (non-aggregated only)
            foreach (var @ref in streamRefVisitor.Referenced)
            {
                var rootPropName = @ref.RootPropertyNameIfAny;
                if (rootPropName != null)
                {
                    if (variablesMultiple.Contains(rootPropName))
                    {
                        measureReferencesMultivar = true;
                        break;
                    }
                }

                var streamRequired = @ref.StreamReferencedIfAny;
                if (streamRequired != null)
                {
                    var streamVariable = _streamVariables.Get(streamRequired.Value);
                    if (streamVariable != null)
                    {
                        var def = _variableStreams.Get(streamVariable);
                        if (def != null && def.Second)
                        {
                            measureReferencesMultivar = true;
                            break;
                        }
                    }
                }
            }
            _isCollectMultimatches = measureReferencesMultivar || _isDefineAsksMultimatches;

            // create rowevent type
            var rowEventTypeName = statementContext.StatementId + "_rowrecogrow";

            _rowEventType = statementContext.EventAdapterService.CreateAnonymousMapType(rowEventTypeName, rowTypeDef);

            // validate partition-by expressions, if any
            if (!matchRecognizeSpec.PartitionByExpressions.IsEmpty())
            {
                var typeServicePartition = new StreamTypeServiceImpl(parentViewType, "MATCH_RECOGNIZE_PARTITION", true, statementContext.EngineURI);
                var validated            = new List <ExprNode>();
                var validationContext    = new ExprValidationContext(
                    typeServicePartition, statementContext.MethodResolutionService, null,
                    statementContext.SchedulingService, statementContext.VariableService, statementContext.TableService,
                    exprEvaluatorContext, statementContext.EventAdapterService, statementContext.StatementName,
                    statementContext.StatementId, statementContext.Annotations, statementContext.ContextDescriptor,
                    statementContext.ScriptingService,
                    false, false, true, false, null, false);
                foreach (var partitionExpr in matchRecognizeSpec.PartitionByExpressions)
                {
                    validated.Add(ExprNodeUtility.GetValidatedSubtree(ExprNodeOrigin.MATCHRECOGPARTITION, partitionExpr, validationContext));
                }
                matchRecognizeSpec.PartitionByExpressions = validated;
            }

            // validate interval if present
            if (matchRecognizeSpec.Interval != null)
            {
                var validationContext =
                    new ExprValidationContext(
                        new StreamTypeServiceImpl(statementContext.EngineURI, false),
                        statementContext.MethodResolutionService, null, statementContext.SchedulingService,
                        statementContext.VariableService, statementContext.TableService, exprEvaluatorContext,
                        statementContext.EventAdapterService, statementContext.StatementName, statementContext.StatementId,
                        statementContext.Annotations, statementContext.ContextDescriptor, statementContext.ScriptingService,
                        false, false, true, false, null, false);
                matchRecognizeSpec.Interval.Validate(validationContext);
            }
        }
        private MediaFolderInfo InternalCopyFolder(TreeNode <MediaFolderNode> sourceNode, string destPath, DuplicateEntryHandling dupeEntryHandling, IList <DuplicateFileInfo> dupeFiles)
        {
            // Get dest node
            var destNode = _folderService.GetNodeByPath(destPath);

            // Dupe handling
            if (destNode != null && dupeEntryHandling == DuplicateEntryHandling.ThrowError)
            {
                throw _exceptionFactory.DuplicateFolder(sourceNode.Value.Path, destNode.Value);
            }

            var doDupeCheck = destNode != null;

            // Create dest folder
            if (destNode == null)
            {
                destNode = CreateFolder(destPath);
            }

            var ctx = _fileRepo.Context;

            // INFO: we gonna change file name during the files loop later.
            var destPathData = new MediaPathData(destNode, "placeholder.txt");

            // Get all source files in one go
            var files = _searcher.SearchFiles(
                new MediaSearchQuery {
                FolderId = sourceNode.Value.Id
            },
                MediaLoadFlags.AsNoTracking | MediaLoadFlags.WithTags).Load();

            IDictionary <string, MediaFile> destFiles = null;
            HashSet <string> destNames = null;

            if (doDupeCheck)
            {
                // Get all files in destination folder for faster dupe selection
                destFiles = _searcher.SearchFiles(new MediaSearchQuery {
                    FolderId = destNode.Value.Id
                }, MediaLoadFlags.None).ToDictionarySafe(x => x.Name);

                // Make a HashSet from all file names in the destination folder for faster unique file name lookups
                destNames = new HashSet <string>(destFiles.Keys, StringComparer.CurrentCultureIgnoreCase);
            }

            // Holds source and copy together, 'cause we perform a two-pass copy (file first, then data)
            var tuples = new List <(MediaFile, MediaFile)>(500);

            // Copy files batched
            foreach (var batch in files.Slice(500))
            {
                foreach (var file in batch)
                {
                    destPathData.FileName = file.Name;

                    // >>> Do copy
                    var copy = InternalCopyFile(
                        file,
                        destPathData,
                        false /* copyData */,
                        dupeEntryHandling,
                        () => destFiles?.Get(file.Name),
                        UniqueFileNameChecker,
                        out var isDupe);

                    if (copy != null)
                    {
                        if (isDupe)
                        {
                            dupeFiles.Add(new DuplicateFileInfo
                            {
                                SourceFile      = ConvertMediaFile(file, sourceNode.Value),
                                DestinationFile = ConvertMediaFile(copy, destNode.Value),
                                UniquePath      = destPathData.FullPath
                            });
                        }
                        if (!isDupe || dupeEntryHandling != DuplicateEntryHandling.Skip)
                        {
                            // When dupe: add to processing queue only if file was NOT skipped
                            tuples.Add((file, copy));
                        }
                    }
                }

                // Save batch to DB (1st pass)
                ctx.SaveChanges();

                // Now copy file data
                foreach (var op in tuples)
                {
                    InternalCopyFileData(op.Item1, op.Item2);
                }

                // Save batch to DB (2nd pass)
                ctx.SaveChanges();

                ctx.DetachEntities <MediaFolder>();
                ctx.DetachEntities <MediaFile>();
                tuples.Clear();
            }

            // Copy folders
            foreach (var node in sourceNode.Children)
            {
                destPath = destNode.Value.Path + "/" + node.Value.Name;
                InternalCopyFolder(node, destPath, dupeEntryHandling, dupeFiles);
            }

            return(new MediaFolderInfo(destNode));

            void UniqueFileNameChecker(MediaPathData pathData)
            {
                if (destNames != null && _helper.CheckUniqueFileName(pathData.FileTitle, pathData.Extension, destNames, out var uniqueName))
                {
                    pathData.FileName = uniqueName;
                }
            }
        }
 public object GetMap(IDictionary<string, object> map)
 {
     // If the map does not contain the key, this is allowed and represented as null
     var wrapper = (EventBean[]) map.Get(propertyName);
     return BaseNestableEventUtil.GetBNArrayPropertyUnderlying(wrapper, index);
 }
Esempio n. 59
0
 public static string GetExchangeName(this IDictionary <string, object> ctx)
 {
     return(ctx.Get <string>(RetryKey.ExchangeName));
 }
Esempio n. 60
0
 /// <summary>Gets a shorthand resolver.</summary>
 /// <param name="shorthandProperty">the property</param>
 /// <returns>the shorthand resolver</returns>
 public static IShorthandResolver GetShorthandResolver(String shorthandProperty)
 {
     return(shorthandResolvers.Get(shorthandProperty));
 }