Example #1
0
        /// <summary>
        /// CREATE new item, replace if it exists. URI identifies the context for the item in question.
        /// No parameters are required for POST, just thepayload.
        /// </summary>

        private void DoDelete(FileRequestData rdata)
        {
            bool   modified = false;
            bool   created  = false;
            string path     = String.Empty;

            Rest.Log.DebugFormat("{0} REST File handler, Method = <{1}> ENTRY", MsgId, rdata.method);

            if (rdata.Parameters.Length > 1)
            {
                try
                {
                    path = rdata.path.Substring(rdata.Parameters[0].Length + qPrefix.Length + 2);

                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                }
                catch (Exception e)
                {
                    Rest.Log.DebugFormat("{0} Exception during file processing : {1}", MsgId,
                                         e.Message);
                    rdata.Fail(Rest.HttpStatusCodeNotFound, String.Format("invalid parameters : {0} {1}",
                                                                          path, e.Message));
                }
            }
            else
            {
                Rest.Log.DebugFormat("{0} Invalid parameters: <{1}>", MsgId, rdata.path);
                rdata.Fail(Rest.HttpStatusCodeNotFound, "invalid parameters");
            }

            if (created)
            {
                rdata.appendStatus(String.Format("<p> Created file {0} <p>", path));
                rdata.Complete(Rest.HttpStatusCodeCreated);
            }
            else
            {
                if (modified)
                {
                    rdata.appendStatus(String.Format("<p> Modified file {0} <p>", path));
                    rdata.Complete(Rest.HttpStatusCodeOK);
                }
                else
                {
                    rdata.Complete(Rest.HttpStatusCodeNoContent);
                }
            }

            rdata.Respond(String.Format("File {0} : Normal completion", rdata.method));
        }
Example #2
0
        /// <summary>
        /// The only parameter we recognize is a UUID.If an asset with this identification is
        /// found, it's content, base-64 encoded, is returned to the client.
        /// </summary>

        private void DoGet(FileRequestData rdata)
        {
            string path = String.Empty;

            Rest.Log.DebugFormat("{0} REST File handler, Method = <{1}> ENTRY", MsgId, rdata.method);

            if (rdata.Parameters.Length > 1)
            {
                try
                {
                    path = rdata.path.Substring(rdata.Parameters[0].Length + qPrefix.Length + 2);
                    if (File.Exists(path))
                    {
                        Rest.Log.DebugFormat("{0}  File located <{1}>", MsgId, path);
                        Byte[] data = File.ReadAllBytes(path);
                        rdata.initXmlWriter();
                        rdata.writer.WriteStartElement(String.Empty, "File", String.Empty);
                        rdata.writer.WriteAttributeString("name", path);
                        rdata.writer.WriteBase64(data, 0, data.Length);
                        rdata.writer.WriteFullEndElement();
                    }
                    else
                    {
                        Rest.Log.DebugFormat("{0} Invalid parameters: <{1}>", MsgId, path);
                        rdata.Fail(Rest.HttpStatusCodeNotFound, String.Format("invalid parameters : {0}", path));
                    }
                }
                catch (Exception e)
                {
                    Rest.Log.DebugFormat("{0} Invalid parameters: <{1}>", MsgId, e.Message);
                    rdata.Fail(Rest.HttpStatusCodeNotFound, String.Format("invalid parameters : {0} {1}",
                                                                          path, e.Message));
                }
            }

            rdata.Complete();
            rdata.Respond(String.Format("File <{0}> : Normal completion", rdata.method));
        }
        /// <summary>
        /// CREATE new item, replace if it exists. URI identifies the context for the item in question.
        /// No parameters are required for POST, just thepayload.
        /// </summary>

        private void DoDelete(FileRequestData rdata)
        {

            bool modified = false;
            bool created  = false;
            string path   = String.Empty;

            Rest.Log.DebugFormat("{0} REST File handler, Method = <{1}> ENTRY", MsgId, rdata.method);

            if (rdata.Parameters.Length > 1)
            {
                try
                {
                    path = rdata.path.Substring(rdata.Parameters[0].Length+qPrefix.Length+2);

                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                }
                catch (Exception e)
                {
                    Rest.Log.DebugFormat("{0} Exception during file processing : {1}", MsgId, 
                          e.Message);
                    rdata.Fail(Rest.HttpStatusCodeNotFound, String.Format("invalid parameters : {0} {1}",
                          path, e.Message));
                }
            }
            else
            {
                Rest.Log.DebugFormat("{0} Invalid parameters: <{1}>", MsgId, rdata.path);
                rdata.Fail(Rest.HttpStatusCodeNotFound, "invalid parameters");
            }

            if (created)
            {
                rdata.appendStatus(String.Format("<p> Created file {0} <p>", path));
                rdata.Complete(Rest.HttpStatusCodeCreated);
            }
            else
            {
                if (modified)
                {
                    rdata.appendStatus(String.Format("<p> Modified file {0} <p>", path));
                    rdata.Complete(Rest.HttpStatusCodeOK);
                }
                else
                {
                    rdata.Complete(Rest.HttpStatusCodeNoContent);
                }
            }

            rdata.Respond(String.Format("File {0} : Normal completion", rdata.method));

        }
        /// <summary>
        /// CREATE new item, replace if it exists. URI identifies the context for the item in question.
        /// No parameters are required for POST, just thepayload.
        /// </summary>

        private void DoPost(FileRequestData rdata)
        {

            bool modified = false;
            bool created  = false;
            string path   = String.Empty;

            Rest.Log.DebugFormat("{0} REST File handler, Method = <{1}> ENTRY", MsgId, rdata.method);

            if (rdata.Parameters.Length > 1)
            {
                try
                {
                    path = rdata.path.Substring(rdata.Parameters[0].Length+qPrefix.Length+2);
                    bool maymod = File.Exists(path);
                    
                    rdata.initXmlReader();
                    XmlReader xml = rdata.reader;

                    if (!xml.ReadToFollowing("File"))
                    {
                        Rest.Log.DebugFormat("{0} Invalid request data: <{1}>", MsgId, rdata.path);
                        rdata.Fail(Rest.HttpStatusCodeBadRequest,"invalid request data");
                    }

                    Byte[] data = Convert.FromBase64String(xml.ReadElementContentAsString("File", ""));

                    File.WriteAllBytes(path,data);
                    modified =   maymod;
                    created  = ! maymod;
                }
                catch (Exception e)
                {
                    Rest.Log.DebugFormat("{0} Exception during file processing : {1}", MsgId, 
                          e.Message);
                }
            }
            else
            {
                Rest.Log.DebugFormat("{0} Invalid parameters: <{1}>", MsgId, rdata.path);
                rdata.Fail(Rest.HttpStatusCodeNotFound, "invalid parameters");
            }

            if (created)
            {
                rdata.appendStatus(String.Format("<p> Created file {0} <p>", path));
                rdata.Complete(Rest.HttpStatusCodeCreated);
            }
            else
            {
                if (modified)
                {
                    rdata.appendStatus(String.Format("<p> Modified file {0} <p>", path));
                    rdata.Complete(Rest.HttpStatusCodeOK);
                }
                else
                {
                    rdata.Complete(Rest.HttpStatusCodeNoContent);
                }
            }

            rdata.Respond(String.Format("File {0} : Normal completion", rdata.method));

        }
        /// <summary>
        /// The only parameter we recognize is a UUID.If an asset with this identification is
        /// found, it's content, base-64 encoded, is returned to the client.
        /// </summary>

        private void DoGet(FileRequestData rdata)
        {

            string path = String.Empty;

            Rest.Log.DebugFormat("{0} REST File handler, Method = <{1}> ENTRY", MsgId, rdata.method);

            if (rdata.Parameters.Length > 1)
            {
                try
                {
                    path = rdata.path.Substring(rdata.Parameters[0].Length+qPrefix.Length+2);
                    if (File.Exists(path))
                    {
                        Rest.Log.DebugFormat("{0}  File located <{1}>", MsgId, path);
                        Byte[] data = File.ReadAllBytes(path);
                        rdata.initXmlWriter();
                        rdata.writer.WriteStartElement(String.Empty,"File",String.Empty);
                        rdata.writer.WriteAttributeString("name", path);
                        rdata.writer.WriteBase64(data,0,data.Length);
                        rdata.writer.WriteFullEndElement();
                    }
                    else
                    {
                        Rest.Log.DebugFormat("{0} Invalid parameters: <{1}>", MsgId, path);
                        rdata.Fail(Rest.HttpStatusCodeNotFound, String.Format("invalid parameters : {0}", path));
                    }
                }
                catch (Exception e)
                {
                    Rest.Log.DebugFormat("{0} Invalid parameters: <{1}>", MsgId, e.Message);
                    rdata.Fail(Rest.HttpStatusCodeNotFound, String.Format("invalid parameters : {0} {1}", 
                                     path, e.Message));
                }
            }

            rdata.Complete();
            rdata.Respond(String.Format("File <{0}> : Normal completion", rdata.method));

        }
Example #6
0
        /// <summary>
        /// CREATE new item, replace if it exists. URI identifies the context for the item in question.
        /// No parameters are required for POST, just thepayload.
        /// </summary>

        private void DoPost(FileRequestData rdata)
        {
            bool   modified = false;
            bool   created  = false;
            string path     = String.Empty;

            Rest.Log.DebugFormat("{0} REST File handler, Method = <{1}> ENTRY", MsgId, rdata.method);

            if (rdata.Parameters.Length > 1)
            {
                try
                {
                    path = rdata.path.Substring(rdata.Parameters[0].Length + qPrefix.Length + 2);
                    bool maymod = File.Exists(path);

                    rdata.initXmlReader();
                    XmlReader xml = rdata.reader;

                    if (!xml.ReadToFollowing("File"))
                    {
                        Rest.Log.DebugFormat("{0} Invalid request data: <{1}>", MsgId, rdata.path);
                        rdata.Fail(Rest.HttpStatusCodeBadRequest, "invalid request data");
                    }

                    Byte[] data = Convert.FromBase64String(xml.ReadElementContentAsString("File", ""));

                    File.WriteAllBytes(path, data);
                    modified = maymod;
                    created  = !maymod;
                }
                catch (Exception e)
                {
                    Rest.Log.DebugFormat("{0} Exception during file processing : {1}", MsgId,
                                         e.Message);
                }
            }
            else
            {
                Rest.Log.DebugFormat("{0} Invalid parameters: <{1}>", MsgId, rdata.path);
                rdata.Fail(Rest.HttpStatusCodeNotFound, "invalid parameters");
            }

            if (created)
            {
                rdata.appendStatus(String.Format("<p> Created file {0} <p>", path));
                rdata.Complete(Rest.HttpStatusCodeCreated);
            }
            else
            {
                if (modified)
                {
                    rdata.appendStatus(String.Format("<p> Modified file {0} <p>", path));
                    rdata.Complete(Rest.HttpStatusCodeOK);
                }
                else
                {
                    rdata.Complete(Rest.HttpStatusCodeNoContent);
                }
            }

            rdata.Respond(String.Format("File {0} : Normal completion", rdata.method));
        }
Example #7
0
        // Asset Handler

        private void DoFile(RequestData rparm)
        {
            if (!enabled)
            {
                return;
            }

            FileRequestData rdata = (FileRequestData)rparm;

            Rest.Log.DebugFormat("{0} REST File handler ({1}) ENTRY", MsgId, qPrefix);

            // Now that we know this is a serious attempt to
            // access file data, we should find out who
            // is asking, and make sure they are authorized
            // to do so. We need to validate the caller's
            // identity before revealing anything about the
            // status quo. Authenticate throws an exception
            // via Fail if no identity information is present.
            //
            // With the present HTTP server we can't use the
            // builtin authentication mechanisms because they
            // would be enforced for all in-bound requests.
            // Instead we look at the headers ourselves and
            // handle authentication directly.

            try
            {
                if (!rdata.IsAuthenticated)
                {
                    rdata.Fail(Rest.HttpStatusCodeNotAuthorized, String.Format("user \"{0}\" could not be authenticated"));
                }
            }
            catch (RestException e)
            {
                if (e.statusCode == Rest.HttpStatusCodeNotAuthorized)
                {
                    Rest.Log.WarnFormat("{0} User not authenticated", MsgId);
                    Rest.Log.DebugFormat("{0} Authorization header: {1}", MsgId,
                                         rdata.request.Headers.Get("Authorization"));
                }
                else
                {
                    Rest.Log.ErrorFormat("{0} User authentication failed", MsgId);
                    Rest.Log.DebugFormat("{0} Authorization header: {1}", MsgId,
                                         rdata.request.Headers.Get("Authorization"));
                }
                throw (e);
            }

            // Remove the prefix and what's left are the parameters. If we don't have
            // the parameters we need, fail the request. Parameters do NOT include
            // any supplied query values.

            if (rdata.Parameters.Length > 0)
            {
                switch (rdata.method)
                {
                case "get":
                    DoGet(rdata);
                    break;

                case "put":
                    DoPut(rdata);
                    break;

                case "post":
                    DoPost(rdata);
                    break;

                case "delete":
                    DoDelete(rdata);
                    break;

                default:
                    Rest.Log.WarnFormat("{0} File: Method not supported: {1}",
                                        MsgId, rdata.method);
                    rdata.Fail(Rest.HttpStatusCodeBadRequest, String.Format("method <{0}> not supported", rdata.method));
                    break;
                }
            }
            else
            {
                Rest.Log.WarnFormat("{0} File: No agent information provided", MsgId);
                rdata.Fail(Rest.HttpStatusCodeBadRequest, "no agent information provided");
            }

            Rest.Log.DebugFormat("{0} REST File handler EXIT", MsgId);
        }