public void Execute(HttpRequest httpRequest, JsonPacket jsonRequest, SessionComponent session)
        {
            // Connect
            NetworkChannel channel = new NetworkChannel(Connection);

            // Request
            JsonBrowseRequestMessage jsonRequestMessage = JsonBrowseRequestMessage.Parse(jsonRequest.Message);

            JsonClient jsonClient = jsonRequestMessage.Client;

            if (jsonClient == null)
            {
                channel.SendNotFound();
                return;
            }

            Entity entity = ClientMap.Get(jsonClient.Id);

            if (entity == null)
            {
                channel.SendNotFound();
                return;
            }

            // Command
            TunnelComponent      tunnel  = entity.Get <TunnelComponent>();
            BrowseRequestCommand command = new BrowseRequestCommand(entity, tunnel.Connection);
            string jsonData = command.Execute(jsonRequest.Data);

            if (string.IsNullOrEmpty(jsonData))
            {
                channel.SendNotFound();
                return;
            }

            // Response
            JsonBrowseResponseMessage jsonResponseMessage = new JsonBrowseResponseMessage();
            JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage, jsonData);

            HttpResponse httpResponse = new HttpResponse()
            {
                Data = session.Encrypt(jsonResponse)
            };

            channel.Send(httpResponse);
#if DEBUG
            Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
        }
Exemple #2
0
        public List <BrowseComponent> Execute(string clientId, string fileId)
        {
            Clear();

            NetworkChannel channel = null;

            try
            {
                // Connect
                channel = new NetworkChannel(Connection);

                // Request
                JsonClient jsonClient = new JsonClient(clientId);
                JsonBrowseRequestMessage jsonRequestMessage = new JsonBrowseRequestMessage()
                {
                    Client = jsonClient
                };

                JsonFile jsonFolder = !string.IsNullOrEmpty(fileId) ? new JsonFile(fileId) : null;
                JsonBrowseRequestData jsonRequestData = new JsonBrowseRequestData()
                {
                    Folder = jsonFolder
                };
                JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, Group.Encrypt(jsonRequestData));

                HttpRequest httpRequest = new HttpRequest(Session.Id)
                {
                    Data = Session.Encrypt(jsonRequest)
                };
                channel.Send(httpRequest);

                // Response
                HttpResponse httpResponse;
                channel.Receive(out httpResponse);
                Code = httpResponse.Code;

                if (httpResponse.Ok)
                {
                    JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data));
                    JsonBrowseResponseMessage jsonResponseMessage = JsonBrowseResponseMessage.Parse(jsonResponse.Message);
                    Debug.Assert(jsonResponseMessage != null);

                    JsonBrowseResponseData jsonResponseData = JsonBrowseResponseData.Parse(Group.Decrypt(jsonResponse.Data));
                    Debug.Assert(jsonResponseData != null);

                    // Data
                    List <BrowseComponent> list = new List <BrowseComponent>();

                    // Folders
                    if (jsonResponseData.Folders != null)
                    {
                        foreach (JsonFile json in jsonResponseData.Folders)
                        {
                            BrowseComponent comp = new BrowseComponent(json.Id, json.Name)
                            {
                                ClientId = jsonClient.Id, Owner = Entity
                            };
                            list.Add(comp);
                        }
                    }

                    // Files
                    if (jsonResponseData.Files != null)
                    {
                        foreach (JsonFile json in jsonResponseData.Files)
                        {
                            BrowseComponent comp = new BrowseComponent(json.Id, json.Name, json.Size)
                            {
                                ClientId = jsonClient.Id, Owner = Entity
                            };
                            list.Add(comp);
                        }
                    }
#if DEBUG
                    Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
                    return(list);
                }
            }
            finally
            {
                if (channel != null)
                {
                    channel.Shutdown();
                }
            }

            return(null);
        }
Exemple #3
0
        public void Execute(HttpRequest httpRequest, JsonPacket jsonRequest)
        {
            Clear();

            // Connect
            NetworkChannel channel = new NetworkChannel(Connection);

            // Request
            JsonBrowseRequestMessage jsonRequestMessage = JsonBrowseRequestMessage.Parse(jsonRequest.Message);
            JsonBrowseRequestData    jsonRequestData    = JsonBrowseRequestData.Parse(Group.Decrypt(jsonRequest.Data));
            JsonFile jsonFolder = (jsonRequestData != null) ? jsonRequestData.Folder : null;

            // Data
            List <JsonFile> folders = new List <JsonFile>();
            List <JsonFile> files   = new List <JsonFile>();

            if (jsonFolder != null)
            {
                FileComponent file = FolderMap.Get(jsonFolder.Id);

                if (file == null)
                {
                    channel.SendNotFound();
                    return;
                }

                // Folders
                foreach (FileComponent f in file.Folders)
                {
                    JsonFile json = (JsonFile)f;
                    folders.Add(json);
                }

                // Files
                foreach (FileComponent f in file.Files)
                {
                    JsonFile json = (JsonFile)f;
                    files.Add(json);
                }
            }
            else
            {
                // Folders
                FolderListComponent shared = Entity.Get <FolderListComponent>();

                foreach (FileComponent f in shared)
                {
                    JsonFile json = (JsonFile)f;
                    folders.Add(json);
                }
            }

            // Response
            JsonBrowseResponseMessage jsonResponseMessage = new JsonBrowseResponseMessage();
            JsonBrowseResponseData    jsonResponseData    = new JsonBrowseResponseData()
            {
                Files = files, Folders = folders
            };
            JsonPacket jsonResponse = new JsonPacket(jsonResponseMessage, Group.Encrypt(jsonResponseData));

            HttpResponse httpResponse = new HttpResponse()
            {
                Data = Session.Encrypt(jsonResponse)
            };

            channel.Send(httpResponse);
#if DEBUG
            Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
        }