Beispiel #1
0
        public virtual void TestQury()
        {
            string result = ReadOutput(new Uri(baseUrl, "/jmx?qry=java.lang:type=Runtime"));

            AssertReFind("\"name\"\\s*:\\s*\"java.lang:type=Runtime\"", result);
            AssertReFind("\"modelerType\"", result);
            result = ReadOutput(new Uri(baseUrl, "/jmx?qry=java.lang:type=Memory"));
            AssertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
            AssertReFind("\"modelerType\"", result);
            result = ReadOutput(new Uri(baseUrl, "/jmx"));
            AssertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
            // test to get an attribute of a mbean
            result = ReadOutput(new Uri(baseUrl, "/jmx?get=java.lang:type=Memory::HeapMemoryUsage"
                                        ));
            AssertReFind("\"name\"\\s*:\\s*\"java.lang:type=Memory\"", result);
            AssertReFind("\"committed\"\\s*:", result);
            // negative test to get an attribute of a mbean
            result = ReadOutput(new Uri(baseUrl, "/jmx?get=java.lang:type=Memory::"));
            AssertReFind("\"ERROR\"", result);
            // test to CORS headers
            HttpURLConnection conn = (HttpURLConnection) new Uri(baseUrl, "/jmx?qry=java.lang:type=Memory"
                                                                 ).OpenConnection();

            Assert.Equal("GET", conn.GetHeaderField(JMXJsonServlet.AccessControlAllowMethods
                                                    ));
            NUnit.Framework.Assert.IsNotNull(conn.GetHeaderField(JMXJsonServlet.AccessControlAllowOrigin
                                                                 ));
        }
Beispiel #2
0
        /// <exception cref="System.IO.IOException"/>
        private void VerifyConnection(Uri url, string msgToEncode, string encHash)
        {
            // Validate response code
            int rc = connection.GetResponseCode();

            if (rc != HttpURLConnection.HttpOk)
            {
                throw new IOException("Got invalid response code " + rc + " from " + url + ": " +
                                      connection.GetResponseMessage());
            }
            // get the shuffle version
            if (!ShuffleHeader.DefaultHttpHeaderName.Equals(connection.GetHeaderField(ShuffleHeader
                                                                                      .HttpHeaderName)) || !ShuffleHeader.DefaultHttpHeaderVersion.Equals(connection.GetHeaderField
                                                                                                                                                              (ShuffleHeader.HttpHeaderVersion)))
            {
                throw new IOException("Incompatible shuffle response version");
            }
            // get the replyHash which is HMac of the encHash we sent to the server
            string replyHash = connection.GetHeaderField(SecureShuffleUtils.HttpHeaderReplyUrlHash
                                                         );

            if (replyHash == null)
            {
                throw new IOException("security validation of TT Map output failed");
            }
            Log.Debug("url=" + msgToEncode + ";encHash=" + encHash + ";replyHash=" + replyHash
                      );
            // verify that replyHash is HMac of encHash
            SecureShuffleUtils.VerifyReply(replyHash, encHash, shuffleSecretKey);
            Log.Info("for url=" + msgToEncode + " sent hash and received reply");
        }
Beispiel #3
0
        /// <exception cref="System.IO.IOException"></exception>
        protected internal virtual void ValidateImpl(HttpURLConnection u, string p, string
                                                     version, string name)
        {
            string v;

            v = u.GetHeaderField(p + JETS3T_CRYPTO_VER);
            if (v == null)
            {
                v = string.Empty;
            }
            if (!version.Equals(v))
            {
                throw new IOException(MessageFormat.Format(JGitText.Get().unsupportedEncryptionVersion
                                                           , v));
            }
            v = u.GetHeaderField(p + JETS3T_CRYPTO_ALG);
            if (v == null)
            {
                v = string.Empty;
            }
            if (!name.Equals(v))
            {
                throw new IOException(JGitText.Get().unsupportedEncryptionAlgorithm + v);
            }
        }
Beispiel #4
0
        /*
         * Indicates if the response is starting a SPNEGO negotiation.
         */
        /// <exception cref="System.IO.IOException"/>
        private bool IsNegotiate()
        {
            bool negotiate = false;

            if (conn.GetResponseCode() == HttpURLConnection.HttpUnauthorized)
            {
                string authHeader = conn.GetHeaderField(WwwAuthenticate);
                negotiate = authHeader != null && authHeader.Trim().StartsWith(Negotiate);
            }
            return(negotiate);
        }
Beispiel #5
0
 public virtual void TestMRWebAppRedirection()
 {
     string[] schemePrefix = new string[] { WebAppUtils.HttpPrefix, WebAppUtils.HttpsPrefix };
     foreach (string scheme in schemePrefix)
     {
         MRApp         app  = new _MRApp_227(2, 2, true, this.GetType().FullName, true);
         Configuration conf = new Configuration();
         conf.Set(YarnConfiguration.ProxyAddress, "9.9.9.9");
         conf.Set(YarnConfiguration.YarnHttpPolicyKey, scheme.Equals(WebAppUtils.HttpsPrefix
                                                                     ) ? HttpConfig.Policy.HttpsOnly.ToString() : HttpConfig.Policy.HttpOnly.ToString
                      ());
         webProxyBase = "/proxy/" + app.GetAppID();
         conf.Set("hadoop.http.filter.initializers", typeof(TestAMWebApp.TestAMFilterInitializer
                                                            ).FullName);
         Org.Apache.Hadoop.Mapreduce.V2.App.Job.Job job = app.Submit(conf);
         string hostPort = NetUtils.GetHostPortString(((MRClientService)app.GetClientService
                                                           ()).GetWebApp().GetListenerAddress());
         Uri httpUrl            = new Uri("http://" + hostPort + "/mapreduce");
         HttpURLConnection conn = (HttpURLConnection)httpUrl.OpenConnection();
         conn.SetInstanceFollowRedirects(false);
         conn.Connect();
         string expectedURL = scheme + conf.Get(YarnConfiguration.ProxyAddress) + ProxyUriUtils
                              .GetPath(app.GetAppID(), "/mapreduce");
         NUnit.Framework.Assert.AreEqual(expectedURL, conn.GetHeaderField(HttpHeaders.Location
                                                                          ));
         NUnit.Framework.Assert.AreEqual(HttpStatus.ScMovedTemporarily, conn.GetResponseCode
                                             ());
         app.WaitForState(job, JobState.Succeeded);
         app.VerifyCompleted();
     }
 }
            private string followRedirect(string url)
            {
                HttpURLConnection connection = (HttpURLConnection)(new URL(url)).OpenConnection();

                connection.InstanceFollowRedirects = false;
                int code = (int)connection.ResponseCode;

                if (code != (int)HttpURLConnection.HttpMovedTemp)
                {
                    throw new IOException("Unexpected response: " + code + " for " + url + ", with contents: " + drainStream(connection.InputStream));
                }
                int    n = 0;
                string name, value;

                while ((name = connection.GetHeaderFieldKey(n)) != null)
                {
                    value = connection.GetHeaderField(n);
                    if (name.Equals("Location"))
                    {
                        return(value);
                    }
                    ++n;
                }
                throw new IOException("Didn't find Location header!");
            }
Beispiel #7
0
        /// <summary>Handle an authentication failure and possibly return a new response.</summary>
        /// <remarks>Handle an authentication failure and possibly return a new response.</remarks>
        /// <param name="conn">the connection that failed.</param>
        /// <returns>new authentication method to try.</returns>
        internal static HttpAuthMethod ScanResponse(HttpURLConnection conn)
        {
            string hdr = conn.GetHeaderField(HttpSupport.HDR_WWW_AUTHENTICATE);

            if (hdr == null || hdr.Length == 0)
            {
                return(NONE);
            }
            int sp = hdr.IndexOf(' ');

            if (sp < 0)
            {
                return(NONE);
            }
            string type = Sharpen.Runtime.Substring(hdr, 0, sp);

            if (Sharpen.Runtime.EqualsIgnoreCase(HttpAuthMethod.Basic.NAME, type))
            {
                return(new HttpAuthMethod.Basic());
            }
            else
            {
                if (Sharpen.Runtime.EqualsIgnoreCase(HttpAuthMethod.Digest.NAME, type))
                {
                    return(new HttpAuthMethod.Digest(Sharpen.Runtime.Substring(hdr, sp + 1)));
                }
                else
                {
                    return(NONE);
                }
            }
        }
Beispiel #8
0
            /// <exception cref="System.Exception"/>
            public Void Run()
            {
                HttpURLConnection conn = aUrl.OpenConnection(nonAuthURL, new DelegationTokenAuthenticatedURL.Token
                                                                 ());

                Assert.Equal(HttpServletResponse.ScOk, conn.GetResponseCode());
                if (useQS)
                {
                    NUnit.Framework.Assert.IsNull(conn.GetHeaderField("UsingHeader"));
                    NUnit.Framework.Assert.IsNotNull(conn.GetHeaderField("UsingQueryString"));
                }
                else
                {
                    NUnit.Framework.Assert.IsNotNull(conn.GetHeaderField("UsingHeader"));
                    NUnit.Framework.Assert.IsNull(conn.GetHeaderField("UsingQueryString"));
                }
                return(null);
            }
Beispiel #9
0
        /// <exception cref="System.IO.IOException"></exception>
        internal InputStream OpenInputStream(HttpURLConnection conn)
        {
            InputStream input = conn.GetInputStream();

            if (HttpSupport.ENCODING_GZIP.Equals(conn.GetHeaderField(HttpSupport.HDR_CONTENT_ENCODING
                                                                     )))
            {
                input = new GZIPInputStream(input);
            }
            return(input);
        }
Beispiel #10
0
        /// <summary>
        /// Test get with offset and length parameters that combine to request a length
        /// greater than actual file length.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestOffsetPlusLengthParamsLongerThanFile()
        {
            WebHdfsFileSystem webhdfs = (WebHdfsFileSystem)fs;
            Path dir = new Path("/test");

            NUnit.Framework.Assert.IsTrue(webhdfs.Mkdirs(dir));
            // Create a file with some content.
            Path               testFile    = new Path("/test/testOffsetPlusLengthParamsLongerThanFile");
            string             content     = "testOffsetPlusLengthParamsLongerThanFile";
            FSDataOutputStream testFileOut = webhdfs.Create(testFile);

            try
            {
                testFileOut.Write(Sharpen.Runtime.GetBytesForString(content, "US-ASCII"));
            }
            finally
            {
                IOUtils.CloseStream(testFileOut);
            }
            // Open the file, but request offset starting at 1 and length equal to file
            // length.  Considering the offset, this is longer than the actual content.
            HttpOpParam.OP op  = GetOpParam.OP.Open;
            Uri            url = webhdfs.ToUrl(op, testFile, new LengthParam(Sharpen.Extensions.ValueOf(
                                                                                 content.Length)), new OffsetParam(1L));
            HttpURLConnection conn = null;
            InputStream       @is  = null;

            try
            {
                conn = (HttpURLConnection)url.OpenConnection();
                conn.SetRequestMethod(op.GetType().ToString());
                conn.SetDoOutput(op.GetDoOutput());
                conn.SetInstanceFollowRedirects(true);
                // Expect OK response and Content-Length header equal to actual length.
                NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScOk, conn.GetResponseCode());
                NUnit.Framework.Assert.AreEqual((content.Length - 1).ToString(), conn.GetHeaderField
                                                    ("Content-Length"));
                // Check content matches.
                byte[] respBody = new byte[content.Length - 1];
                @is = conn.GetInputStream();
                IOUtils.ReadFully(@is, respBody, 0, content.Length - 1);
                NUnit.Framework.Assert.AreEqual(Sharpen.Runtime.Substring(content, 1), Sharpen.Runtime.GetStringForBytes
                                                    (respBody, "US-ASCII"));
            }
            finally
            {
                IOUtils.CloseStream(@is);
                if (conn != null)
                {
                    conn.Disconnect();
                }
            }
        }
Beispiel #11
0
        public virtual void TestHttpCookie()
        {
            Uri @base = new Uri("http://" + NetUtils.GetHostPortString(server.GetConnectorAddress
                                                                           (0)));
            HttpURLConnection conn = (HttpURLConnection) new Uri(@base, "/echo").OpenConnection
                                         ();
            string             header  = conn.GetHeaderField("Set-Cookie");
            IList <HttpCookie> cookies = HttpCookie.Parse(header);

            Assert.True(!cookies.IsEmpty());
            Assert.True(header.Contains("; HttpOnly"));
            Assert.True("token".Equals(cookies[0].GetValue()));
        }
Beispiel #12
0
        /// <exception cref="System.IO.IOException"/>
        private static long GetStreamLength(HttpURLConnection connection, IDictionary <string
                                                                                       , IList <string> > headers)
        {
            string cl = connection.GetHeaderField(HttpHeaders.ContentLength);

            if (cl == null)
            {
                // Try to get the content length by parsing the content range
                // because HftpFileSystem does not return the content length
                // if the content is partial.
                if (connection.GetResponseCode() == HttpStatus.ScPartialContent)
                {
                    cl = connection.GetHeaderField(HttpHeaders.ContentRange);
                    return(GetLengthFromRange(cl));
                }
                else
                {
                    throw new IOException(HttpHeaders.ContentLength + " is missing: " + headers);
                }
            }
            return(long.Parse(cl));
        }
Beispiel #13
0
        internal static string GetRefreshURL(string url)
        {
            string redirectUrl = null;

            try
            {
                HttpURLConnection conn = (HttpURLConnection) new Uri(url).OpenConnection();
                // do not automatically follow the redirection
                // otherwise we get too many redirections exception
                conn.SetInstanceFollowRedirects(false);
                redirectUrl = conn.GetHeaderField("Refresh");
            }
            catch (Exception)
            {
            }
            // throw new RuntimeException(e);
            return(redirectUrl);
        }
Beispiel #14
0
        public virtual void TestNoCacheHeader()
        {
            Uri url = new Uri(baseUrl, "/echo?a=b&c=d");
            HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();

            Assert.Equal(HttpURLConnection.HttpOk, conn.GetResponseCode());
            Assert.Equal("no-cache", conn.GetHeaderField("Cache-Control"));
            Assert.Equal("no-cache", conn.GetHeaderField("Pragma"));
            NUnit.Framework.Assert.IsNotNull(conn.GetHeaderField("Expires"));
            NUnit.Framework.Assert.IsNotNull(conn.GetHeaderField("Date"));
            Assert.Equal(conn.GetHeaderField("Expires"), conn.GetHeaderField
                             ("Date"));
        }
Beispiel #15
0
        // set up http connection with the given url and get the redirection url from the response
        // return null if the url is not redirected
        internal static string GetRedirectURL(string url)
        {
            string redirectUrl = null;

            try
            {
                HttpURLConnection conn = (HttpURLConnection) new Uri(url).OpenConnection();
                // do not automatically follow the redirection
                // otherwise we get too many redirections exception
                conn.SetInstanceFollowRedirects(false);
                if (conn.GetResponseCode() == HttpServletResponse.ScTemporaryRedirect)
                {
                    redirectUrl = conn.GetHeaderField("Location");
                }
            }
            catch (Exception)
            {
            }
            // throw new RuntimeException(e);
            return(redirectUrl);
        }
        /// <exception cref="System.Exception"/>
        public virtual void TestNotAuthenticated()
        {
            AuthenticatorTestCase auth = new AuthenticatorTestCase(useTomcat);

            AuthenticatorTestCase.SetAuthenticationHandlerConfig(GetAuthenticationHandlerConfiguration
                                                                     ());
            auth.Start();
            try
            {
                Uri url = new Uri(auth.GetBaseURL());
                HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
                conn.Connect();
                Assert.Equal(HttpURLConnection.HttpUnauthorized, conn.GetResponseCode
                                 ());
                Assert.True(conn.GetHeaderField(KerberosAuthenticator.WwwAuthenticate
                                                ) != null);
            }
            finally
            {
                auth.Stop();
            }
        }
Beispiel #17
0
        public Task <Stream> PerformRequestAsync()
        {
            return(Task.Run(() =>
            {
                using (var url = new URL("http://httpbin.org/"))
                {
                    conn = (HttpURLConnection)url.OpenConnection();
                }
                conn.RequestMethod = "GET";
                conn.DoInput = true;

                if ((int)conn.ResponseCode != 200)
                {
                    return null;
                }

                int headerIndex = 0;
                while (true)
                {
                    var key = conn.GetHeaderFieldKey(headerIndex);
                    var value = conn.GetHeaderField(headerIndex);
                    if (key == null || value == null)
                    {
                        break;
                    }
                    TotalHeaderCount++;
                    headerIndex++;
                }

                try {
                    return conn.InputStream;
                } catch (IOException) {
                    return null;
                } finally {
                    conn = null;
                }
            }));
        }
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="Org.Apache.Hadoop.Security.Authentication.Client.AuthenticationException
        ///     "/>
        private IDictionary DoDelegationTokenOperation <_T0>(Uri url, AuthenticatedURL.Token
                                                             token, DelegationTokenAuthenticator.DelegationTokenOperation operation, string
                                                             renewer, Org.Apache.Hadoop.Security.Token.Token <_T0> dToken, bool hasResponse, string
                                                             doAsUser)
            where _T0 : TokenIdentifier
        {
            IDictionary ret = null;
            IDictionary <string, string> @params = new Dictionary <string, string>();

            @params[OpParam] = operation.ToString();
            if (renewer != null)
            {
                @params[RenewerParam] = renewer;
            }
            if (dToken != null)
            {
                @params[TokenParam] = dToken.EncodeToUrlString();
            }
            // proxyuser
            if (doAsUser != null)
            {
                @params[DelegationTokenAuthenticatedURL.DoAs] = URLEncoder.Encode(doAsUser, "UTF-8"
                                                                                  );
            }
            string        urlStr    = url.ToExternalForm();
            StringBuilder sb        = new StringBuilder(urlStr);
            string        separator = (urlStr.Contains("?")) ? "&" : "?";

            foreach (KeyValuePair <string, string> entry in @params)
            {
                sb.Append(separator).Append(entry.Key).Append("=").Append(URLEncoder.Encode(entry
                                                                                            .Value, "UTF8"));
                separator = "&";
            }
            url = new Uri(sb.ToString());
            AuthenticatedURL  aUrl = new AuthenticatedURL(this, connConfigurator);
            HttpURLConnection conn = aUrl.OpenConnection(url, token);

            conn.SetRequestMethod(operation.GetHttpMethod());
            HttpExceptionUtils.ValidateResponse(conn, HttpURLConnection.HttpOk);
            if (hasResponse)
            {
                string contentType = conn.GetHeaderField(ContentType);
                contentType = (contentType != null) ? StringUtils.ToLowerCase(contentType) : null;
                if (contentType != null && contentType.Contains(ApplicationJsonMime))
                {
                    try
                    {
                        ObjectMapper mapper = new ObjectMapper();
                        ret = mapper.ReadValue <IDictionary>(conn.GetInputStream());
                    }
                    catch (Exception ex)
                    {
                        throw new AuthenticationException(string.Format("'%s' did not handle the '%s' delegation token operation: %s"
                                                                        , url.GetAuthority(), operation, ex.Message), ex);
                    }
                }
                else
                {
                    throw new AuthenticationException(string.Format("'%s' did not " + "respond with JSON to the '%s' delegation token operation"
                                                                    , url.GetAuthority(), operation));
                }
            }
            return(ret);
        }
Beispiel #19
0
        /// <exception cref="System.Exception"/>
        public virtual void TestWebAppProxyServlet()
        {
            configuration.Set(YarnConfiguration.ProxyAddress, "localhost:9090");
            // overriding num of web server threads, see HttpServer.HTTP_MAXTHREADS
            configuration.SetInt("hadoop.http.max.threads", 5);
            TestWebAppProxyServlet.WebAppProxyServerForTest proxy = new TestWebAppProxyServlet.WebAppProxyServerForTest
                                                                        (this);
            proxy.Init(configuration);
            proxy.Start();
            int proxyPort = proxy.proxy.proxyServer.GetConnectorAddress(0).Port;

            TestWebAppProxyServlet.AppReportFetcherForTest appReportFetcher = proxy.proxy.appReportFetcher;
            // wrong url
            try
            {
                // wrong url. Set wrong app ID
                Uri wrongUrl = new Uri("http://localhost:" + proxyPort + "/proxy/app");
                HttpURLConnection proxyConn = (HttpURLConnection)wrongUrl.OpenConnection();
                proxyConn.Connect();
                NUnit.Framework.Assert.AreEqual(HttpURLConnection.HttpInternalError, proxyConn.GetResponseCode
                                                    ());
                // set true Application ID in url
                Uri url = new Uri("http://localhost:" + proxyPort + "/proxy/application_00_0");
                proxyConn = (HttpURLConnection)url.OpenConnection();
                // set cookie
                proxyConn.SetRequestProperty("Cookie", "checked_application_0_0000=true");
                proxyConn.Connect();
                NUnit.Framework.Assert.AreEqual(HttpURLConnection.HttpOk, proxyConn.GetResponseCode
                                                    ());
                NUnit.Framework.Assert.IsTrue(IsResponseCookiePresent(proxyConn, "checked_application_0_0000"
                                                                      , "true"));
                // cannot found application 1: null
                appReportFetcher.answer = 1;
                proxyConn = (HttpURLConnection)url.OpenConnection();
                proxyConn.SetRequestProperty("Cookie", "checked_application_0_0000=true");
                proxyConn.Connect();
                NUnit.Framework.Assert.AreEqual(HttpURLConnection.HttpNotFound, proxyConn.GetResponseCode
                                                    ());
                NUnit.Framework.Assert.IsFalse(IsResponseCookiePresent(proxyConn, "checked_application_0_0000"
                                                                       , "true"));
                // cannot found application 2: ApplicationNotFoundException
                appReportFetcher.answer = 4;
                proxyConn = (HttpURLConnection)url.OpenConnection();
                proxyConn.SetRequestProperty("Cookie", "checked_application_0_0000=true");
                proxyConn.Connect();
                NUnit.Framework.Assert.AreEqual(HttpURLConnection.HttpNotFound, proxyConn.GetResponseCode
                                                    ());
                NUnit.Framework.Assert.IsFalse(IsResponseCookiePresent(proxyConn, "checked_application_0_0000"
                                                                       , "true"));
                // wrong user
                appReportFetcher.answer = 2;
                proxyConn = (HttpURLConnection)url.OpenConnection();
                proxyConn.Connect();
                NUnit.Framework.Assert.AreEqual(HttpURLConnection.HttpOk, proxyConn.GetResponseCode
                                                    ());
                string s = ReadInputStream(proxyConn.GetInputStream());
                NUnit.Framework.Assert.IsTrue(s.Contains("to continue to an Application Master web interface owned by"
                                                         ));
                NUnit.Framework.Assert.IsTrue(s.Contains("WARNING: The following page may not be safe!"
                                                         ));
                //case if task has a not running status
                appReportFetcher.answer = 3;
                proxyConn = (HttpURLConnection)url.OpenConnection();
                proxyConn.SetRequestProperty("Cookie", "checked_application_0_0000=true");
                proxyConn.Connect();
                NUnit.Framework.Assert.AreEqual(HttpURLConnection.HttpOk, proxyConn.GetResponseCode
                                                    ());
                // test user-provided path and query parameter can be appended to the
                // original tracking url
                appReportFetcher.answer = 5;
                Uri clientUrl = new Uri("http://localhost:" + proxyPort + "/proxy/application_00_0/test/tez?x=y&h=p"
                                        );
                proxyConn = (HttpURLConnection)clientUrl.OpenConnection();
                proxyConn.Connect();
                Log.Info(string.Empty + proxyConn.GetURL());
                Log.Info("ProxyConn.getHeaderField(): " + proxyConn.GetHeaderField(ProxyUtils.Location
                                                                                   ));
                NUnit.Framework.Assert.AreEqual("http://localhost:" + originalPort + "/foo/bar/test/tez?a=b&x=y&h=p#main"
                                                , proxyConn.GetURL().ToString());
            }
            finally
            {
                proxy.Close();
            }
        }
Beispiel #20
0
        /// <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();
                }
            }
        }
Beispiel #21
0
        private static MD5Hash ParseMD5Header(HttpURLConnection connection)
        {
            string header = connection.GetHeaderField(Md5Header);

            return((header != null) ? new MD5Hash(header) : null);
        }