Exemplo n.º 1
0
        public GenericDocument Read(string uri)
        {
            string result = string.Empty;

            string url        = SetURL(uri);
            Uri    requestUri = new Uri(url);

            HttpResponseMessage response = m_httpClient.GetAsync(requestUri).Result;  // Blocking call!

            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsStringAsync().Result;   // Blocking call!
                var             mimetype = response.Content.Headers.ContentType.MediaType;
                GenericDocument content  = new GenericDocument();
                content.SetMimetype(mimetype);
                content.SetContent(result);

                return(content);
            }
            else
            {
                result = string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                Console.WriteLine(result);
                throw new DocumentIOException(result);
            }
        }
Exemplo n.º 2
0
        public string Write(string uri, GenericDocument content)
        {
            HttpResponseMessage response;

            string url        = SetURL(uri);
            Uri    requestUri = new Uri(url);

            var         mediaType = content.GetMimetype();
            HttpContent httpBody  = new StringContent(content.GetContent());

            httpBody.Headers.ContentType = new MediaTypeHeaderValue(content.GetMimetype());
            response = m_httpClient.PutAsync(requestUri, httpBody).Result;

            // either this - or check the status to retrieve more information
            response.EnsureSuccessStatusCode();
            // get the rest/content of the response in a synchronous way
            var result = response.Content.ReadAsStringAsync().Result;

            if (response.IsSuccessStatusCode)
            {
                return(result);
            }
            else
            {
                result = string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                Console.WriteLine(result);
                throw new DocumentIOException(result);
            }
        }
Exemplo n.º 3
0
        public async Task AuthRoles_WriterCanPut()
        {
            var u = new Bunk.CouchBuiltins.User();

            u.SetPassword("abc");
            u.Name  = Rand.RandString("testuser");
            u.Roles = new List <string>()
            {
                "abc", "def"
            };
            u.GrantWriter();

            var resp = await this.db.couchRepo.UserMaintenance().AddUser(u);

            var repo_testuser = CouchRepo.Connect(new ConnectionConfig(Config.Get().Uri.ToString(), u.Name, "abc"));

            var test_doc = new GenericDocument()
            {
                ID = Rand.RandString("test"), TYPE = "dave-test"
            };

            test_doc["test_key"] = "hello";

            resp = await repo_testuser.DB(db.name).Put(test_doc);

            Assert.IsNotNull(resp.REV, "Writer should be able to put a document");

            test_doc = await repo_testuser.DB(db.name).Get <GenericDocument>(test_doc.ID);

            Assert.IsTrue(false, "Should have thrown an forbidden failure for writers");
        }
Exemplo n.º 4
0
        protected void NewDocument(string filename)
        {
            GenericDocument doc = new GenericDocument(filename);

            InstantiateContent(doc, filename);
            doc.Show(View.DockPanel);
        }
Exemplo n.º 5
0
        /*
         * Accept a Document path and filename from the console.
         * Also, accept the desired document's URI from the console.
         * Write the document to the "Documents" database in MarkLogic.
         */
        static void WriteToDatabase(DatabaseClient dbClient)
        {
            // Get the desired document to write to the MarkLogic
            //  "Documents" database.
            Console.Write("Document path and filename to store: ");
            string filename = Console.ReadLine();

            if (filename.Length == 0)
            {
                Console.WriteLine(("No file specified."));
                return;
            }
            string content = File.ReadAllText(filename);

            // Get a Document URI. If the URI does not already
            //  exist in the "Documents" database, the document
            //  is inserted as a new document. If the URI does exist,
            //  the existing document in the "Documents" database is
            //  updated.
            Console.Write("Document URI: ");
            var uri = Console.ReadLine();

            if (uri.Length == 0)
            {
                Console.WriteLine(("You must supply a document URI to save a document to the MarkLogic database."));
                return;
            }

            // Create a DocumentManager to act as our interface for
            //  reading and writing to/from the database.
            DocumentManager mgr = dbClient.NewDocumentManager();

            // Create a GenericDocument object to write the
            //  content from the desired file to the MarkLogic
            //  database. The connection from the Database Client
            //  is used to write to the database.
            GenericDocument doc = new GenericDocument();
            // set the mime type.
            string mimetype = MimeTypeMap.GetMimeType(Path.GetExtension(filename));

            Console.WriteLine(Path.GetExtension(filename));
            Console.WriteLine(mimetype);

            doc.SetMimetype(mimetype);
            // set the contents from the file that was read.
            doc.SetContent(content);

            // write the document to the database with the
            //  specified URI.
            var results = mgr.Write(uri, doc);

            Console.WriteLine(" ");
            Console.WriteLine("--------------------------------");
            Console.Write("Write results: ");
            Console.WriteLine(results);
        }
Exemplo n.º 6
0
        internal void RunMapAction(T item, GenericDocument gdoc)
        {
            if (gdoc == null)
            {
                return;
            }
            var ta  = this._typeMap[gdoc.TYPE];
            var doc = gdoc.ToObject(ta.Type);

            ta.Action(item, doc);
        }
Exemplo n.º 7
0
        protected IDockContent InstantiateContainer(string typeName, string metadata)
        {
            IDockContent container = null;

            if (typeName == typeof(GenericPane).ToString())
            {
                container = new GenericPane(metadata);
            }
            else if (typeName == typeof(GenericDocument).ToString())
            {
                container = new GenericDocument(metadata);
            }

            return(container);
        }
Exemplo n.º 8
0
        public async Task <string> WriteAsync(string uri, GenericDocument content)
        {
            string url        = SetURL(uri);
            Uri    requestUri = new Uri(url);

            HttpContent httpBody = new StringContent(content.GetContent());

            httpBody.Headers.ContentType = new MediaTypeHeaderValue(content.GetMimetype());

            using (var r = await m_httpClient.PutAsync(requestUri, httpBody))
            {
                string result = await r.Content.ReadAsStringAsync();

                return(result);
            }
        }
Exemplo n.º 9
0
        public async Task <GenericDocument> ReadAsync(string uri)
        {
            string url        = SetURL(uri);
            Uri    requestUri = new Uri(url);

            using (var r = await m_httpClient.GetAsync(requestUri))
            {
                string result = await r.Content.ReadAsStringAsync();

                var mimetype = r.Content.Headers.ContentType.MediaType;

                GenericDocument content = new GenericDocument();
                content.SetMimetype(mimetype);
                content.SetContent(result);

                return(content);
            }
        }
Exemplo n.º 10
0
        public ActionResult Index(string uri, HttpPostedFileBase Picture)
        {
            string path = null;

            if (Picture != null)
            {
                path = System.IO.Path.GetFileName(Picture.FileName);
            }

            // Get the desired document to write to the MarkLogic
            //  "Documents" database.

            string str     = Path.GetFullPath(Picture.FileName);
            string content = System.IO.File.ReadAllText("C:\\Users\\Антон Ересковский\\Downloads\\2_kurs.xml");

            // Get a Document URI. If the URI does not already
            //  exist in the "Documents" database, the document
            //  is inserted as a new document. If the URI does exist,
            //  the existing document in the "Documents" database is
            //  updated.


            // Create a DocumentManager to act as our interface for
            //  reading and writing to/from the database.
            DocumentManager mgr       = dbClient.NewDocumentManager();
            GenericDocument doc       = new GenericDocument();
            string          mimetype  = MimeTypeMap.GetMimeType(Path.GetExtension(path));
            var             extension = Path.GetExtension(path);
            var             mimeType  = mimetype;

            doc.SetMimetype(mimetype);
            doc.SetContent(content);
            var results = mgr.Write(uri, doc);

            return(View());
        }
Exemplo n.º 11
0
        /*
         * Accept a Document ID (URI) from the console. Read the desired
         * document from the Documents database in MarkLogic. Display the
         * returned document plus write the document to the current working
         * directory.
         */
        static void ReadFromDatabase(DatabaseClient dbClient)
        {
            // Get the desired document's URI from the console.
            Console.Write("Document URI: ");
            var uri = Console.ReadLine();

            // The document content will be saved to the filename specified.
            //  If not filename is specified, the filename part of the URI
            //  is used as the filename.

            string myDocumentPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string defaultFname   = string.Empty;

            if (uri.StartsWith("/") || uri.StartsWith("\\"))
            {
                defaultFname = myDocumentPath + uri;
            }
            else
            {
                defaultFname = myDocumentPath + "/" + uri;
            }
            defaultFname = defaultFname.Replace("\\", "/");
            Console.Write("Save as (ENTER for " + defaultFname + "): ");
            string filename = Console.ReadLine();

            if (filename.Length == 0)
            {
                filename = defaultFname;
            }

            // Create a DocumentManager to act as our interface for
            //  reading and writing to/from the database.
            DocumentManager mgr = dbClient.NewDocumentManager();

            // Use the DocumentManager object to read a document
            // with the uri of "/doc1.xml" from the "Documents" database.
            string mimetype = string.Empty;
            string content  = string.Empty;

            // Use the GenericDocument class to read a non-binary document
            //  from the "Documents" database. This example code reads text,
            //  JSON and XML data but binary is not implemented.
            GenericDocument doc = mgr.Read(uri);

            mimetype = doc.GetMimetype();
            content  = doc.GetContent();

            // Display the retuned document's mime type and content.
            Console.WriteLine(" ");
            Console.WriteLine("---Document Saved-------------");
            Console.WriteLine("---Document Content from DB---");
            Console.Write("Mime type: ");
            Console.WriteLine(mimetype);
            Console.Write("Content: ");
            Console.WriteLine(content);

            // Write the returned content to the specified file.
            //File.WriteAllText(filename, content);
            FileInfo file = new FileInfo(filename);

            file.Directory.Create();             // If the directory already exists, this method does nothing.
            File.WriteAllText(file.FullName, content);
        }
Exemplo n.º 12
0
 private GenericDocument Map(tblDocument tblDoc)
 {
     var doc = new GenericDocument(tblDoc.Id);
     _Map(tblDoc, doc);
     return doc;
 }