Example #1
0
        public MobeelizerSyncResponse SendSyncAllRequest()
        {
            WebRequest request = WebRequest.Create(GetUrl("/synchronizeAll"));

            request.Method = "POST";
            SetHeaders(request, true, true);
            try
            {
                MobeelizerResponse response = new Synchronizer().GetResponse(request);
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(new MobeelizerSyncResponse((response as MobeelizerTicketResponse).Ticket));
                }
                else if (response.StatusCode == HttpStatusCode.InternalServerError)
                {
                    return(new MobeelizerSyncResponse(MobeelizerOperationError.ServerError((response as MobeelizerJsonResponse).Json)));
                }
                else
                {
                    throw new IOException("Http connection status code: " + response.StatusCode.ToString());
                }
            }
            catch (NullReferenceException)
            {
                throw new IOException("Server not respond.");
            }
        }
Example #2
0
        public MobeelizerOperationError SendRemoteNotification(string device, string group, System.Collections.Generic.IList <string> users, System.Collections.Generic.IDictionary <string, string> notification)
        {
            JObject       jobject    = new JObject();
            StringBuilder logBuilder = new StringBuilder();

            logBuilder.Append("Sent remote notification ").Append(notification).Append(" to");
            if (device != null)
            {
                jobject.Add("device", device);
                logBuilder.Append(" device: ").Append(device);
            }
            if (group != null)
            {
                jobject.Add("group", group);
                logBuilder.Append(" group: ").Append(group);
            }
            if (users != null)
            {
                jobject.Add("users", new JArray(users));
                logBuilder.Append(" users: ").Append(users);
            }
            if (device == null && group == null && users == null)
            {
                logBuilder.Append(" everyone");
            }
            JObject jnotification = new JObject();

            foreach (var notify in notification)
            {
                jnotification.Add(notify.Key, notify.Value);
            }

            jobject.Add("notification", jnotification);

            WebRequest request = WebRequest.Create(GetUrl("/push"));

            request.Method = "POST";
            using (Stream stream = new Synchronizer().GetRequestStream(request))
            {
                byte[] notificationMessage = Encoding.UTF8.GetBytes(jobject.ToString());
                stream.Write(notificationMessage, 0, notificationMessage.Length);
            }

            SetHeaders(request, true, true);
            MobeelizerResponse result = new Synchronizer().GetResponse(request);

            if (result.StatusCode == HttpStatusCode.OK)
            {
                Log.i(TAG, logBuilder.ToString());
                return(null);
            }
            else if (result.StatusCode == HttpStatusCode.InternalServerError)
            {
                return(MobeelizerOperationError.ServerError((result as MobeelizerJsonResponse).Json));
            }
            else
            {
                throw new IOException("Http connection status code: " + result.StatusCode.ToString());
            }
        }
Example #3
0
        public MobeelizerOperationError UnregisterForRemoteNotifications(string channelUri)
        {
            String     token   = this.tokenConverter.Convert(channelUri);
            WebRequest request = WebRequest.Create(GetUrl(String.Format("/unregisterPushToken?deviceToken={0}&deviceType=wp7", token)));

            request.Method = "POST";
            SetHeaders(request, true, true);
            try
            {
                MobeelizerResponse result = new Synchronizer().GetResponse(request);
                if (result.StatusCode == HttpStatusCode.OK)
                {
                    Log.i(TAG, "Unregistered for remote notifications with chanel: " + channelUri);
                    return(null);
                }
                else if (result.StatusCode == HttpStatusCode.InternalServerError)
                {
                    return(MobeelizerOperationError.ServerError((result as MobeelizerJsonResponse).Json));
                }
                else
                {
                    throw new IOException("Http connection status code: " + result.StatusCode.ToString());
                }
            }
            catch (WebException e)
            {
                throw new IOException(e.Message, e);
            }
        }
Example #4
0
        public IMobeelizerAuthenticateResponse Authenticate(string user, string password, String notificationChanelUri)
        {
            WebRequest request = WebRequest.Create(GetUrl("/authenticate?cache=" + Guid.NewGuid().ToString()));

            request.Method = "GET";
            SetHeaders(request, false, false);
            request.Headers["mas-user-name"]     = user;
            request.Headers["mas-user-password"] = password;
            try
            {
                MobeelizerResponse result = new Synchronizer().GetResponse(request);
                if (result.StatusCode == HttpStatusCode.OK)
                {
                    JObject jObject = (result as MobeelizerJsonResponse).Json;
                    return(new MobeelizerAuthenticateResponse((String)jObject["instanceGuid"], (String)jObject["role"]));
                }
                else if (result.StatusCode == HttpStatusCode.InternalServerError)
                {
                    return(new MobeelizerAuthenticateResponse(MobeelizerOperationError.ServerError((result as MobeelizerJsonResponse).Json)));
                }
                else
                {
                    throw new IOException("Http connection status code: " + result.StatusCode.ToString());
                }
            }
            catch (WebException e)
            {
                return(new MobeelizerAuthenticateResponse(MobeelizerOperationError.ConnectionError(e.Message)));
            }
            catch (JsonException e)
            {
                return(new MobeelizerAuthenticateResponse(MobeelizerOperationError.Other(e.Message)));
            }
        }
Example #5
0
        public MobeelizerGetSyncDataOperationResult GetSyncData(string ticket)
        {
            WebRequest request = WebRequest.Create(GetUrl(String.Format("/data?ticket={0}", ticket)));

            request.Method = "GET";
            SetHeaders(request, false, true);
            try
            {
                Others.File inputFile = GetInputFileStream();
                inputFile.Create();
                MobeelizerResponse response = new Synchronizer().GetResponseData(request);
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    using (Stream stream = (response as MobeelizerDataResponse).Data)
                    {
                        using (IsolatedStorageFileStream inputStream = inputFile.OpenToWrite())
                        {
                            stream.CopyTo(inputStream);
                        }
                    }
                    return(new MobeelizerGetSyncDataOperationResult(inputFile));
                }
                else if (response.StatusCode == HttpStatusCode.InternalServerError)
                {
                    return(new MobeelizerGetSyncDataOperationResult(MobeelizerOperationError.ServerError((response as MobeelizerJsonResponse).Json)));
                }
                else
                {
                    throw new IOException("Http connection status code: " + response.StatusCode.ToString());
                }
            }
            catch (WebException e)
            {
                String message;
                using (Stream stream = e.Response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        message = reader.ReadToEnd();
                    }
                }
                throw new IOException(message, e);
            }
            catch (NullReferenceException)
            {
                throw new IOException("Server not respond.");
            }
        }
Example #6
0
        public MobeelizerSyncResponse SendSyncDiffRequest(Others.File outputFile)
        {
            String boundary = DateTime.Now.Ticks.ToString();

            WebRequest request = WebRequest.Create(GetUrl("/synchronize"));

            request.Method      = "POST";
            request.ContentType = String.Format("multipart/form-data; boundary={0}", boundary);

            using (Stream requestStream = new Synchronizer().GetRequestStream(request))
            {
                String header = String.Format("--{0}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"file\";\r\nContent-Type: application/octet-stream\r\n\r\n", boundary);
                requestStream.Write(Encoding.UTF8.GetBytes(header), 0, header.Length);
                using (IsolatedStorageFileStream stream = outputFile.OpenToRead())
                {
                    int    lenght = (int)stream.Length;
                    byte[] bytes  = new byte[lenght];
                    stream.Read(bytes, 0, lenght);
                    requestStream.Write(bytes, 0, lenght);
                }
                string footer      = "\r\n--" + boundary + "--\r\n";
                byte[] footerbytes = Encoding.UTF8.GetBytes(footer);
                requestStream.Write(footerbytes, 0, footerbytes.Length);
            }

            SetHeaders(request, false, true);
            try
            {
                MobeelizerResponse response = new Synchronizer().GetResponse(request);
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(new MobeelizerSyncResponse((response as MobeelizerTicketResponse).Ticket));
                }
                else if (response.StatusCode == HttpStatusCode.InternalServerError)
                {
                    return(new MobeelizerSyncResponse(MobeelizerOperationError.ServerError((response as MobeelizerJsonResponse).Json)));
                }
                else
                {
                    throw new IOException("Http connection status code: " + response.StatusCode.ToString());
                }
            }
            catch (NullReferenceException)
            {
                throw new IOException("Server not respond.");
            }
        }