private Dictionary <string, string> ConvertJsonRequestToDictionary(HttpListenerRequest request)
        {
            string json;
            Dictionary <string, string> clientRequest;

            // try to retrieve JSON object from body of the request
            // if error occurs return null
            try
            {
                // access body (POST) of the message using stream
                using (Stream body = request.InputStream)
                {
                    using (StreamReader reader = new StreamReader(body, request.ContentEncoding))
                    {
                        json          = reader.ReadToEnd();
                        clientRequest = JsonConvert.DeserializeObject <Dictionary <string, string> >(json);
                        Console.WriteLine("Json successfully loaded from body");
                        return(clientRequest);
                    }
                }
            }
            catch
            {
                ApplicationError error = new ApplicationError(
                    ErrorLocationEnum.Mobile,
                    "HttpServer",
                    "ConvertJsonRequestToDictionary",
                    "Error - Converting POST to json object, check json is correctly formated.",
                    "");
                ApplicationErrorLog.logError(error);
                return(null);
            }
        }
Beispiel #2
0
        public Boolean SaveImageAsFile()
        {
            Byte[] imagebytes = null;

            try
            {
                imagebytes = Convert.FromBase64String(ImageURI);
            }
            catch
            {
                ApplicationError error = new ApplicationError(
                    ErrorLocationEnum.Server,
                    "Image",
                    "SaveImageAsFile",
                    "Failed Coverting image uri to bytes",
                    ImageName + " - URI failed to convert to bytes");
                ApplicationErrorLog.logError(error);

                return(false);
            }

            // if bytes is not null then try and write the image file
            if (imagebytes != null)
            {
                string imageFullSavePath = DEFAULT_PATH + KioskNetworkID + BLUETOOTH_FOLDER + ImageName;

                try
                {
                    // write file to share folder using kiosk ID
                    using (var img = new FileStream(imageFullSavePath, FileMode.Create))
                    {
                        img.Write(imagebytes, 0, imagebytes.Length);
                        img.Flush(); // clear write buffer so it does not effect next file creation

                        return(true);
                    }
                }
                catch
                {
                    ApplicationError error = new ApplicationError(
                        ErrorLocationEnum.Server,
                        "Image",
                        "SaveImageAsFile",
                        "Failed writing image file",
                        "Failed writing image file to path: " + imageFullSavePath);

                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
        // processes the clients request
        private void ProccessRequest(IAsyncResult result)
        {
            Dictionary <string, string> clientRequest;
            List <string>       responseData = new List <string>();
            HttpListenerContext context;

            HttpListener httpListener = (HttpListener)result.AsyncState;

            context = httpListener.EndGetContext(result);

            // convert body request (JSON object) to dictionary
            clientRequest = ConvertJsonRequestToDictionary(context.Request);

            // if client request is not null, continue to process the request
            if (clientRequest != null)
            {
                string actionKeyValue;

                // try and get value from action key and check action required
                if (clientRequest.TryGetValue(ACTION_KEY, out actionKeyValue))
                {
                    if (actionKeyValue == ACTION_REQUESTING_KIOSKS)
                    {
                        ProccessAvailableKiosksRequest(context.Response);
                    }
                    else if (actionKeyValue == ACTION_SENDING_IMAGE)
                    {
                        // proccess image request type and send response
                        ProcessImageUploadRequest(clientRequest, context.Response);
                    }
                    else if (actionKeyValue == ACTION_REQUEST_FOUND_SERVER)
                    {
                        SendResponse(context.Response, true, RequestType.FoundServer, null);
                    }
                    else
                    {
                        SendResponse(context.Response, true, RequestType.FoundServer, null);
                    }
                }
                // if no action key provided by mobile app log error
                else
                {
                    ApplicationError error = new ApplicationError(
                        ErrorLocationEnum.Mobile,
                        "HttpServer",
                        "DetermainRequestAction",
                        "No action Key in json",
                        "Client failed to provided 'action' key");
                    ApplicationErrorLog.logError(error);

                    // send response - invaild request
                    // send response error
                    responseData.Add(ERROR_ACTION_KEY);
                    SendResponse(context.Response, false, RequestType.ImageUpload, responseData);
                }
            }
            else
            {
                Console.WriteLine();
            }
        }
        private void ProcessImageUploadRequest(Dictionary <string, string> clientRequest, HttpListenerResponse responseObj)
        {
            string        imageUriKeyValue;
            string        kioskKeyValue;
            string        imageFilenameKeyValue;
            Boolean       imageUriFound    = false;
            Boolean       imageNameFound   = false;
            Boolean       kioskNumberFound = false;
            Boolean       imageCreated;
            List <string> responseData = new List <string>();

            // check request has image uri
            if (clientRequest.TryGetValue(IMAGE_URI_KEY, out imageUriKeyValue))
            {
                imageUriFound = true;
            }
            else
            {
                ApplicationError error = new ApplicationError(
                    ErrorLocationEnum.Mobile,
                    "HttpServer",
                    "DetermainRequestAction",
                    "No '" + IMAGE_URI_KEY + "' key in json",
                    "Client failed to provid '" + IMAGE_URI_KEY + "' key");
                ApplicationErrorLog.logError(error);

                // add error to response data
                responseData.Add(ERROR_IMAGE_URI);
            }

            // check request has kiosk number key
            if (clientRequest.TryGetValue(KIOSK_KEY, out kioskKeyValue))
            {
                kioskNumberFound = true;
            }
            else
            {
                ApplicationError error = new ApplicationError(
                    ErrorLocationEnum.Mobile,
                    "HttpServer",
                    "DetermainRequestAction",
                    "No '" + KIOSK_KEY + "' key in json",
                    "Client failed to provid '" + KIOSK_KEY + "' key");
                ApplicationErrorLog.logError(error);

                // add error to response data
                responseData.Add(ERROR_KIOSK_NUMBER);
            }

            // check request has image name
            if (clientRequest.TryGetValue(IMAGE_NAME_KEY, out imageFilenameKeyValue))
            {
                imageNameFound = true;
            }
            else
            {
                ApplicationError error = new ApplicationError(
                    ErrorLocationEnum.Mobile,
                    "HttpServer",
                    "DetermainRequestAction",
                    "No '" + IMAGE_NAME_KEY + "' key in json",
                    "Client failed to provid '" + IMAGE_NAME_KEY + "' key");
                ApplicationErrorLog.logError(error);

                // add error to response data
                responseData.Add(ERROR_NO_IMAGE_KEY);
            }

            // create image file if all keys where found - create image
            if ((imageUriFound == true) && (kioskNumberFound == true) && (imageNameFound == true))
            {
                Image img = new Image(imageUriKeyValue, imageFilenameKeyValue, kioskKeyValue);
                imageCreated = img.SaveImageAsFile();

                // put failed image filename into firt index of the data array
                responseData.Add(imageFilenameKeyValue);

                if (imageCreated == false)
                {
                    // send response to the client with fail to create image
                    SendResponse(responseObj, false, RequestType.ImageUpload, responseData);
                }
                // if created succcesfully, send success response
                else
                {
                    // send response to the client with success response
                    SendResponse(responseObj, true, RequestType.ImageUpload, responseData);
                }
            }
            // if an error occurred on any required keys - send error message/s back to client
            else
            {
                SendResponse(responseObj, false, RequestType.ImageUpload, responseData);
            }
        }