/// <exception cref="System.IO.IOException"></exception> internal override WalkRemoteObjectDatabase.FileStream Open(string path) { Uri @base = this.objectsUrl; Uri u = new Uri(@base, path); HttpURLConnection c = this._enclosing.HttpOpen(u); switch (HttpSupport.Response(c)) { case HttpURLConnection.HTTP_OK: { InputStream @in = this._enclosing.OpenInputStream(c); int len = c.GetContentLength(); return(new WalkRemoteObjectDatabase.FileStream(@in, len)); } case HttpURLConnection.HTTP_NOT_FOUND: { throw new FileNotFoundException(u.ToString()); } default: { throw new IOException(u.ToString() + ": " + HttpSupport.Response(c) + " " + c.GetResponseMessage ()); } } }
public virtual void TestWebHdfsOffsetAndLength() { MiniDFSCluster cluster = null; Configuration conf = WebHdfsTestUtil.CreateConf(); int Offset = 42; int Length = 512; string Path = "/foo"; byte[] Contents = new byte[1024]; Random.NextBytes(Contents); try { cluster = new MiniDFSCluster.Builder(conf).NumDataNodes(1).Build(); WebHdfsFileSystem fs = WebHdfsTestUtil.GetWebHdfsFileSystem(conf, WebHdfsFileSystem .Scheme); using (OutputStream os = fs.Create(new Path(Path))) { os.Write(Contents); } IPEndPoint addr = cluster.GetNameNode().GetHttpAddress(); Uri url = new Uri("http", addr.GetHostString(), addr.Port, WebHdfsFileSystem.PathPrefix + Path + "?op=OPEN" + Param.ToSortedString("&", new OffsetParam((long)Offset), new LengthParam((long)Length))); HttpURLConnection conn = (HttpURLConnection)url.OpenConnection(); conn.SetInstanceFollowRedirects(true); NUnit.Framework.Assert.AreEqual(Length, conn.GetContentLength()); byte[] subContents = new byte[Length]; byte[] realContents = new byte[Length]; System.Array.Copy(Contents, Offset, subContents, 0, Length); IOUtils.ReadFully(conn.GetInputStream(), realContents); Assert.AssertArrayEquals(subContents, realContents); } finally { if (cluster != null) { cluster.Shutdown(); } } }
/// <exception cref="System.IO.IOException"/> public virtual void TestResponseCode() { WebHdfsFileSystem webhdfs = (WebHdfsFileSystem)fs; Path root = new Path("/"); Path dir = new Path("/test/testUrl"); NUnit.Framework.Assert.IsTrue(webhdfs.Mkdirs(dir)); Path file = new Path("/test/file"); FSDataOutputStream @out = webhdfs.Create(file); @out.Write(1); @out.Close(); { //test GETHOMEDIRECTORY Uri url = webhdfs.ToUrl(GetOpParam.OP.Gethomedirectory, root); HttpURLConnection conn = (HttpURLConnection)url.OpenConnection(); IDictionary <object, object> m = WebHdfsTestUtil.ConnectAndGetJson(conn, HttpServletResponse .ScOk); NUnit.Framework.Assert.AreEqual(WebHdfsFileSystem.GetHomeDirectoryString(ugi), m[ typeof(Path).Name]); conn.Disconnect(); } { //test GETHOMEDIRECTORY with unauthorized doAs Uri url = webhdfs.ToUrl(GetOpParam.OP.Gethomedirectory, root, new DoAsParam(ugi.GetShortUserName () + "proxy")); HttpURLConnection conn = (HttpURLConnection)url.OpenConnection(); conn.Connect(); NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScForbidden, conn.GetResponseCode ()); conn.Disconnect(); } { //test set owner with empty parameters Uri url = webhdfs.ToUrl(PutOpParam.OP.Setowner, dir); HttpURLConnection conn = (HttpURLConnection)url.OpenConnection(); conn.Connect(); NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScBadRequest, conn.GetResponseCode ()); conn.Disconnect(); } { //test set replication on a directory HttpOpParam.OP op = PutOpParam.OP.Setreplication; Uri url = webhdfs.ToUrl(op, dir); HttpURLConnection conn = (HttpURLConnection)url.OpenConnection(); conn.SetRequestMethod(op.GetType().ToString()); conn.Connect(); NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScOk, conn.GetResponseCode()); NUnit.Framework.Assert.IsFalse(webhdfs.SetReplication(dir, (short)1)); conn.Disconnect(); } { //test get file status for a non-exist file. Path p = new Path(dir, "non-exist"); Uri url = webhdfs.ToUrl(GetOpParam.OP.Getfilestatus, p); HttpURLConnection conn = (HttpURLConnection)url.OpenConnection(); conn.Connect(); NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScNotFound, conn.GetResponseCode ()); conn.Disconnect(); } { //test set permission with empty parameters HttpOpParam.OP op = PutOpParam.OP.Setpermission; Uri url = webhdfs.ToUrl(op, dir); HttpURLConnection conn = (HttpURLConnection)url.OpenConnection(); conn.SetRequestMethod(op.GetType().ToString()); conn.Connect(); NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScOk, conn.GetResponseCode()); NUnit.Framework.Assert.AreEqual(0, conn.GetContentLength()); NUnit.Framework.Assert.AreEqual(MediaType.ApplicationOctetStream, conn.GetContentType ()); NUnit.Framework.Assert.AreEqual((short)0x1ed, webhdfs.GetFileStatus(dir).GetPermission ().ToShort()); conn.Disconnect(); } { //test append. AppendTestUtil.TestAppend(fs, new Path(dir, "append")); } { //test NamenodeAddressParam not set. HttpOpParam.OP op = PutOpParam.OP.Create; Uri url = webhdfs.ToUrl(op, dir); HttpURLConnection conn = (HttpURLConnection)url.OpenConnection(); conn.SetRequestMethod(op.GetType().ToString()); conn.SetDoOutput(false); conn.SetInstanceFollowRedirects(false); conn.Connect(); string redirect = conn.GetHeaderField("Location"); conn.Disconnect(); //remove NamenodeAddressParam WebHdfsFileSystem.Log.Info("redirect = " + redirect); int i = redirect.IndexOf(NamenodeAddressParam.Name); int j = redirect.IndexOf("&", i); string modified = Sharpen.Runtime.Substring(redirect, 0, i - 1) + Sharpen.Runtime.Substring (redirect, j); WebHdfsFileSystem.Log.Info("modified = " + modified); //connect to datanode conn = (HttpURLConnection) new Uri(modified).OpenConnection(); conn.SetRequestMethod(op.GetType().ToString()); conn.SetDoOutput(op.GetDoOutput()); conn.Connect(); NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScBadRequest, conn.GetResponseCode ()); } { //test jsonParse with non-json type. HttpOpParam.OP op = GetOpParam.OP.Open; Uri url = webhdfs.ToUrl(op, file); HttpURLConnection conn = (HttpURLConnection)url.OpenConnection(); conn.SetRequestMethod(op.GetType().ToString()); conn.Connect(); try { WebHdfsFileSystem.JsonParse(conn, false); Fail(); } catch (IOException ioe) { WebHdfsFileSystem.Log.Info("GOOD", ioe); } conn.Disconnect(); } { //test create with path containing spaces HttpOpParam.OP op = PutOpParam.OP.Create; Path path = new Path("/test/path with spaces"); Uri url = webhdfs.ToUrl(op, path); HttpURLConnection conn = (HttpURLConnection)url.OpenConnection(); conn.SetRequestMethod(op.GetType().ToString()); conn.SetDoOutput(false); conn.SetInstanceFollowRedirects(false); string redirect; try { conn.Connect(); NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScTemporaryRedirect, conn.GetResponseCode ()); redirect = conn.GetHeaderField("Location"); } finally { conn.Disconnect(); } conn = (HttpURLConnection) new Uri(redirect).OpenConnection(); conn.SetRequestMethod(op.GetType().ToString()); conn.SetDoOutput(op.GetDoOutput()); try { conn.Connect(); NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScCreated, conn.GetResponseCode ()); } finally { conn.Disconnect(); } } }