Beispiel #1
0
        /// <exception cref="System.Exception"/>
        public virtual void TestAppReportForEmptyTrackingUrl()
        {
            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;
            try
            {
                //set AHS_ENBALED = false to simulate getting the app report from RM
                configuration.SetBoolean(YarnConfiguration.ApplicationHistoryEnabled, false);
                ApplicationId app = ApplicationId.NewInstance(0, 0);
                appReportFetcher.answer = 6;
                Uri url = new Uri("http://localhost:" + proxyPort + "/proxy/" + app.ToString());
                HttpURLConnection proxyConn = (HttpURLConnection)url.OpenConnection();
                proxyConn.Connect();
                try
                {
                    proxyConn.GetResponseCode();
                }
                catch (ConnectException)
                {
                }
                // Connection Exception is expected as we have set
                // appReportFetcher.answer = 6, which does not set anything for
                // original tracking url field in the app report.
                string appAddressInRm = WebAppUtils.GetResolvedRMWebAppURLWithScheme(configuration
                                                                                     ) + "/cluster" + "/app/" + app.ToString();
                NUnit.Framework.Assert.IsTrue("Webapp proxy servlet should have redirected to RM"
                                              , proxyConn.GetURL().ToString().Equals(appAddressInRm));
                //set AHS_ENBALED = true to simulate getting the app report from AHS
                configuration.SetBoolean(YarnConfiguration.ApplicationHistoryEnabled, true);
                proxyConn = (HttpURLConnection)url.OpenConnection();
                proxyConn.Connect();
                try
                {
                    proxyConn.GetResponseCode();
                }
                catch (ConnectException)
                {
                }
                // Connection Exception is expected as we have set
                // appReportFetcher.answer = 6, which does not set anything for
                // original tracking url field in the app report.
                string appAddressInAhs = WebAppUtils.GetHttpSchemePrefix(configuration) + WebAppUtils
                                         .GetAHSWebAppURLWithoutScheme(configuration) + "/applicationhistory" + "/apps/"
                                         + app.ToString();
                NUnit.Framework.Assert.IsTrue("Webapp proxy servlet should have redirected to AHS"
                                              , proxyConn.GetURL().ToString().Equals(appAddressInAhs));
            }
            finally
            {
                proxy.Close();
            }
        }
Beispiel #2
0
        public virtual void TestContentTypes()
        {
            // Static CSS files should have text/css
            Uri cssUrl             = new Uri(baseUrl, "/static/test.css");
            HttpURLConnection conn = (HttpURLConnection)cssUrl.OpenConnection();

            conn.Connect();
            Assert.Equal(200, conn.GetResponseCode());
            Assert.Equal("text/css", conn.GetContentType());
            // Servlets should have text/plain with proper encoding by default
            Uri servletUrl = new Uri(baseUrl, "/echo?a=b");

            conn = (HttpURLConnection)servletUrl.OpenConnection();
            conn.Connect();
            Assert.Equal(200, conn.GetResponseCode());
            Assert.Equal("text/plain; charset=utf-8", conn.GetContentType(
                             ));
            // We should ignore parameters for mime types - ie a parameter
            // ending in .css should not change mime type
            servletUrl = new Uri(baseUrl, "/echo?a=b.css");
            conn       = (HttpURLConnection)servletUrl.OpenConnection();
            conn.Connect();
            Assert.Equal(200, conn.GetResponseCode());
            Assert.Equal("text/plain; charset=utf-8", conn.GetContentType(
                             ));
            // Servlets that specify text/html should get that content type
            servletUrl = new Uri(baseUrl, "/htmlcontent");
            conn       = (HttpURLConnection)servletUrl.OpenConnection();
            conn.Connect();
            Assert.Equal(200, conn.GetResponseCode());
            Assert.Equal("text/html; charset=utf-8", conn.GetContentType()
                         );
        }
Beispiel #3
0
        public virtual void TestWebHdfsAuditLogger()
        {
            Configuration conf = new HdfsConfiguration();

            conf.Set(DFSConfigKeys.DfsNamenodeAuditLoggersKey, typeof(TestAuditLogger.DummyAuditLogger
                                                                      ).FullName);
            MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).Build();

            GetOpParam.OP op = GetOpParam.OP.Getfilestatus;
            try
            {
                cluster.WaitClusterUp();
                NUnit.Framework.Assert.IsTrue(TestAuditLogger.DummyAuditLogger.initialized);
                URI uri = new URI("http", NetUtils.GetHostPortString(cluster.GetNameNode().GetHttpAddress
                                                                         ()), "/webhdfs/v1/", op.ToQueryString(), null);
                // non-proxy request
                HttpURLConnection conn = (HttpURLConnection)uri.ToURL().OpenConnection();
                conn.SetRequestMethod(op.GetType().ToString());
                conn.Connect();
                NUnit.Framework.Assert.AreEqual(200, conn.GetResponseCode());
                conn.Disconnect();
                NUnit.Framework.Assert.AreEqual(1, TestAuditLogger.DummyAuditLogger.logCount);
                NUnit.Framework.Assert.AreEqual("127.0.0.1", TestAuditLogger.DummyAuditLogger.remoteAddr
                                                );
                // non-trusted proxied request
                conn = (HttpURLConnection)uri.ToURL().OpenConnection();
                conn.SetRequestMethod(op.GetType().ToString());
                conn.SetRequestProperty("X-Forwarded-For", "1.1.1.1");
                conn.Connect();
                NUnit.Framework.Assert.AreEqual(200, conn.GetResponseCode());
                conn.Disconnect();
                NUnit.Framework.Assert.AreEqual(2, TestAuditLogger.DummyAuditLogger.logCount);
                NUnit.Framework.Assert.AreEqual("127.0.0.1", TestAuditLogger.DummyAuditLogger.remoteAddr
                                                );
                // trusted proxied request
                conf.Set(ProxyServers.ConfHadoopProxyservers, "127.0.0.1");
                ProxyUsers.RefreshSuperUserGroupsConfiguration(conf);
                conn = (HttpURLConnection)uri.ToURL().OpenConnection();
                conn.SetRequestMethod(op.GetType().ToString());
                conn.SetRequestProperty("X-Forwarded-For", "1.1.1.1");
                conn.Connect();
                NUnit.Framework.Assert.AreEqual(200, conn.GetResponseCode());
                conn.Disconnect();
                NUnit.Framework.Assert.AreEqual(3, TestAuditLogger.DummyAuditLogger.logCount);
                NUnit.Framework.Assert.AreEqual("1.1.1.1", TestAuditLogger.DummyAuditLogger.remoteAddr
                                                );
            }
            finally
            {
                cluster.Shutdown();
            }
        }
Beispiel #4
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestCaseInsensitive()
        {
            Path p = new Path("/test/testCaseInsensitive");
            WebHdfsFileSystem webhdfs = (WebHdfsFileSystem)fs;

            PutOpParam.OP op = PutOpParam.OP.Mkdirs;
            //replace query with mix case letters
            Uri url = webhdfs.ToUrl(op, p);

            WebHdfsFileSystem.Log.Info("url      = " + url);
            Uri replaced = new Uri(url.ToString().Replace(op.ToQueryString(), "Op=mkDIrs"));

            WebHdfsFileSystem.Log.Info("replaced = " + replaced);
            //connect with the replaced URL.
            HttpURLConnection conn = (HttpURLConnection)replaced.OpenConnection();

            conn.SetRequestMethod(op.GetType().ToString());
            conn.Connect();
            BufferedReader @in = new BufferedReader(new InputStreamReader(conn.GetInputStream
                                                                              ()));

            for (string line; (line = @in.ReadLine()) != null;)
            {
                WebHdfsFileSystem.Log.Info("> " + line);
            }
            //check if the command successes.
            NUnit.Framework.Assert.IsTrue(fs.GetFileStatus(p).IsDirectory());
        }
Beispiel #5
0
            /// <summary>Use HTTP Range header for specifying offset.</summary>
            /// <exception cref="System.IO.IOException"/>
            protected internal override HttpURLConnection Connect(long offset, bool resolved)
            {
                HttpURLConnection conn = OpenConnection();

                conn.SetRequestMethod("GET");
                if (offset != 0L)
                {
                    conn.SetRequestProperty("Range", "bytes=" + offset + "-");
                }
                conn.Connect();
                //Expects HTTP_OK or HTTP_PARTIAL response codes.
                int code = conn.GetResponseCode();

                if (offset != 0L && code != HttpURLConnection.HttpPartial)
                {
                    throw new IOException("HTTP_PARTIAL expected, received " + code);
                }
                else
                {
                    if (offset == 0L && code != HttpURLConnection.HttpOk)
                    {
                        throw new IOException("HTTP_OK expected, received " + code);
                    }
                }
                return(conn);
            }
 public async Task <string> GetData(string fileUrl)
 {
     return(await Task.Run <string> (() => {
         HttpURLConnection connection = null;
         string result = null;
         try
         {
             URL url = new URL(fileUrl);
             connection = (HttpURLConnection)url.OpenConnection();
             connection.ReadTimeout = 60000;
             connection.DoInput = true;
             connection.Connect();
             _loggingSrevice.Log(string.Format("Response Code {0} ", connection.ResponseCode));
             result = ReadString(connection.InputStream, 1024);
             _loggingSrevice.Log(string.Format("[DownloadService] - Response String =  {0} ", result));
         }
         catch (Exception e)
         {
             _loggingSrevice.Log(string.Format("[DownloadService] Exception {0} ", e.Message));
         }
         finally
         {
             if (connection != null)
             {
                 connection.Disconnect();
             }
         }
         return result;
     }));
 }
        /// <summary>General-purpose http PUT command to the httpfs server.</summary>
        /// <param name="filename">The file to operate upon</param>
        /// <param name="command">The command to perform (SETXATTR, etc)</param>
        /// <param name="params">Parameters</param>
        /// <exception cref="System.Exception"/>
        private void PutCmd(string filename, string command, string @params)
        {
            string user = HadoopUsersConfTestHelper.GetHadoopUsers()[0];

            // Remove leading / from filename
            if (filename[0] == '/')
            {
                filename = Sharpen.Runtime.Substring(filename, 1);
            }
            string pathOps = MessageFormat.Format("/webhdfs/v1/{0}?user.name={1}{2}{3}&op={4}"
                                                  , filename, user, (@params == null) ? string.Empty : "&", (@params == null) ? string.Empty
                                 : @params, command);
            Uri url = new Uri(TestJettyHelper.GetJettyURL(), pathOps);
            HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();

            conn.SetRequestMethod("PUT");
            conn.Connect();
            int resp = conn.GetResponseCode();

            NUnit.Framework.Assert.AreEqual(HttpURLConnection.HttpInternalError, resp);
            BufferedReader reader;

            reader = new BufferedReader(new InputStreamReader(conn.GetErrorStream()));
            string err = reader.ReadLine();

            NUnit.Framework.Assert.IsTrue(err.Contains("RemoteException"));
            NUnit.Framework.Assert.IsTrue(err.Contains("XAttr"));
            NUnit.Framework.Assert.IsTrue(err.Contains("rejected"));
        }
Beispiel #8
0
 /// <exception cref="System.IO.IOException"/>
 public static IDictionary <object, object> ConnectAndGetJson(HttpURLConnection conn
                                                              , int expectedResponseCode)
 {
     conn.Connect();
     NUnit.Framework.Assert.AreEqual(expectedResponseCode, conn.GetResponseCode());
     return(WebHdfsFileSystem.JsonParse(conn, false));
 }
 /// <summary>A utility to ping an endpoint.</summary>
 /// <remarks>
 /// A utility to ping an endpoint. Useful for
 /// <see cref="Live()"/>
 /// and
 /// <see cref="Ready(bool)"/>
 /// .
 /// </remarks>
 /// <param name="uri">The URL we are trying to ping.</param>
 /// <returns>True if we got any non-5XX response from the endpoint.</returns>
 protected internal virtual bool Ping(string uri)
 {
     try
     {
         URL url = new URL(uri);
         HttpURLConnection connection = (HttpURLConnection)url.OpenConnection();
         connection.SetRequestProperty("Accept-Charset", "UTF-8");
         connection.SetRequestMethod("GET");
         connection.Connect();
         int code = connection.GetResponseCode();
         return(code < 500 || code >= 600);
     }
     catch (MalformedURLException)
     {
         log.Warn("Could not parse URL: " + uri);
         return(false);
     }
     catch (InvalidCastException)
     {
         log.Warn("Not an HTTP URI");
         return(false);
     }
     catch (IOException)
     {
         return(false);
     }
 }
Beispiel #10
0
        public Bitmap getbitmap(string url)
        {
            URL    myfileurl = null;
            Bitmap bm        = null;

            try
            {
                myfileurl = new URL(url);
            }
            catch (MalformedURLException e)
            {
                e.PrintStackTrace();
            }
            try
            {
                HttpURLConnection conn = (HttpURLConnection)myfileurl.OpenConnection();
                conn.DoInput = true;
                conn.Connect();
                System.IO.Stream instream = conn.InputStream;
                bm = BitmapFactory.DecodeStream(instream);
            }
            catch (IOException e)
            {
                throw e;
            }
            return(bm);
        }
Beispiel #11
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();
     }
 }
Beispiel #12
0
        public void PutHTTPData(String urlString, String json)
        {
            try
            {
                URL url = new URL(urlString);
                HttpURLConnection urlConnection = (HttpURLConnection)url.OpenConnection();

                urlConnection.RequestMethod = "PUT";
                urlConnection.DoOutput      = true;

                byte[] _out   = Encoding.UTF8.GetBytes(json);
                int    length = _out.Length;

                urlConnection.SetFixedLengthStreamingMode(length);
                urlConnection.SetRequestProperty("Content-Type", "application/json");
                urlConnection.SetRequestProperty("charset", "utf-8");

                urlConnection.Connect();

                try
                {
                    Stream str = urlConnection.OutputStream;
                    str.Write(_out, 0, length);
                }
                catch {}

                var status = urlConnection.ResponseCode;
            }
            catch { }
        }
Beispiel #13
0
        public void DeleteHTTPData(String urlString, String json)
        {
            try
            {
                URL url = new URL(urlString);
                HttpURLConnection urlConnection = (HttpURLConnection)url.OpenConnection();
                urlConnection.RequestMethod = "DELETE";
                urlConnection.DoOutput      = true;

                byte[] _out   = Encoding.UTF8.GetBytes(json);
                int    lenght = _out.Length;

                urlConnection.SetFixedLengthStreamingMode(lenght);
                urlConnection.SetRequestProperty("Content-Type", "application/json");
                urlConnection.SetRequestProperty("charset", "utf-8");

                urlConnection.Connect();
                try
                {
                    Stream str = urlConnection.OutputStream;
                    str.Write(_out, 0, lenght);
                }
                catch (Exception ex) { System.Console.WriteLine("{0} Exception caught.", ex); }
                var status = urlConnection.ResponseCode;
            }
            catch (Exception ex) { System.Console.WriteLine("{0} Exception caught.", ex); }
        }
        public string DownloadUrl(string urlIput)
        {
            Stream inputStream     = null;
            String contentAsString = "";

            try
            {
                URL url = new URL(urlIput);
                HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
                conn.ReadTimeout    = 10000;
                conn.ConnectTimeout = 15000;
                conn.RequestMethod  = "GET";
                conn.DoInput        = true;
                conn.Connect();
                inputStream     = conn.InputStream;
                contentAsString = readIt(inputStream);
                return(contentAsString);
            }catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (inputStream != null)
                {
                    inputStream.Close();
                }
            }
        }
Beispiel #15
0
        private Object Post(string parameters)
        {
            try
            {
                conn = (HttpURLConnection)url.OpenConnection();
                conn.RequestMethod = "POST";
                conn.DoOutput      = true;
                conn.DoInput       = true;
                conn.Connect();

                Android.Util.Log.Debug("SendToServer", "parametros " + parameters);

                byte[] bytes = Encoding.ASCII.GetBytes(parameters);

                OutputStream outputStream = new BufferedOutputStream(conn.OutputStream);
                outputStream.Write(bytes);
                outputStream.Flush();
                outputStream.Close();

                InputStream inputStream = new BufferedInputStream(conn.InputStream);

                return(ReadString(inputStream));
            }
            catch (IOException e)
            {
                Android.Util.Log.Debug("SendToServer", "Problems to send data to server!! " + e.Message);
                return(false);
            }
            finally
            {
                conn.Disconnect();
            }
        }
        public virtual void TestJsonParseClosesInputStream()
        {
            WebHdfsFileSystem webhdfs = (WebHdfsFileSystem)fileSystem;
            Path file = GetTestRootPath(fSys, "test/hadoop/file");

            CreateFile(file);
            HttpOpParam.OP    op   = GetOpParam.OP.Gethomedirectory;
            Uri               url  = webhdfs.ToUrl(op, file);
            HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();

            conn.SetRequestMethod(op.GetType().ToString());
            conn.Connect();
            InputStream       myIn    = new _InputStream_184(this, conn);
            HttpURLConnection spyConn = Org.Mockito.Mockito.Spy(conn);

            Org.Mockito.Mockito.DoReturn(myIn).When(spyConn).GetInputStream();
            try
            {
                NUnit.Framework.Assert.IsFalse(closedInputStream);
                WebHdfsFileSystem.JsonParse(spyConn, false);
                NUnit.Framework.Assert.IsTrue(closedInputStream);
            }
            catch (IOException)
            {
                TestCase.Fail();
            }
            conn.Disconnect();
        }
Beispiel #17
0
        /// <summary>Talks to the http interface to create a file.</summary>
        /// <param name="filename">The file to create</param>
        /// <param name="perms">The permission field, if any (may be null)</param>
        /// <exception cref="System.Exception"/>
        private void CreateWithHttp(string filename, string perms)
        {
            string user = HadoopUsersConfTestHelper.GetHadoopUsers()[0];

            // Remove leading / from filename
            if (filename[0] == '/')
            {
                filename = Sharpen.Runtime.Substring(filename, 1);
            }
            string pathOps;

            if (perms == null)
            {
                pathOps = MessageFormat.Format("/webhdfs/v1/{0}?user.name={1}&op=CREATE", filename
                                               , user);
            }
            else
            {
                pathOps = MessageFormat.Format("/webhdfs/v1/{0}?user.name={1}&permission={2}&op=CREATE"
                                               , filename, user, perms);
            }
            Uri url = new Uri(TestJettyHelper.GetJettyURL(), pathOps);
            HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();

            conn.AddRequestProperty("Content-Type", "application/octet-stream");
            conn.SetRequestMethod("PUT");
            conn.Connect();
            NUnit.Framework.Assert.AreEqual(HttpURLConnection.HttpCreated, conn.GetResponseCode
                                                ());
        }
Beispiel #18
0
        public bool IsHostReachable(string host = null)
        {
            host = host ?? HostName;
            var isConnected      = false;
            var activeConnection = _connectivityManager.ActiveNetworkInfo;

            if ((activeConnection != null) && activeConnection.IsConnected)
            {
                try {
                    var task = Task.Factory.StartNew(() => {
                        URL url = new URL(HostName);
                        HttpURLConnection urlc = (HttpURLConnection)url.OpenConnection();
                        urlc.SetRequestProperty("User-Agent", "Android Application");
                        urlc.SetRequestProperty("Connection", "close");
                        urlc.ConnectTimeout = _connectionTimeOutInMillisec;
                        urlc.Connect();
                        isConnected = (urlc.ResponseCode == HttpStatus.Ok);
                        isConnected = true;
                    });
                    task.Wait();
                }
                catch (Exception e) {
                    System.Diagnostics.Trace.WriteLine("Connectivity issue: " + e.ToString());
                }
            }
            return(isConnected);
        }
Beispiel #19
0
 /// <exception cref="System.Exception"/>
 protected internal virtual void _testAuthentication(Authenticator authenticator,
                                                     bool doPost)
 {
     Start();
     try
     {
         Uri url = new Uri(GetBaseURL());
         AuthenticatedURL.Token token = new AuthenticatedURL.Token();
         NUnit.Framework.Assert.IsFalse(token.IsSet());
         AuthenticatorTestCase.TestConnectionConfigurator connConf = new AuthenticatorTestCase.TestConnectionConfigurator
                                                                         ();
         AuthenticatedURL  aUrl = new AuthenticatedURL(authenticator, connConf);
         HttpURLConnection conn = aUrl.OpenConnection(url, token);
         Assert.True(connConf.invoked);
         string tokenStr = token.ToString();
         if (doPost)
         {
             conn.SetRequestMethod("POST");
             conn.SetDoOutput(true);
         }
         conn.Connect();
         if (doPost)
         {
             TextWriter writer = new OutputStreamWriter(conn.GetOutputStream());
             writer.Write(Post);
             writer.Close();
         }
         Assert.Equal(HttpURLConnection.HttpOk, conn.GetResponseCode());
         if (doPost)
         {
             BufferedReader reader = new BufferedReader(new InputStreamReader(conn.GetInputStream
                                                                                  ()));
             string echo = reader.ReadLine();
             Assert.Equal(Post, echo);
             NUnit.Framework.Assert.IsNull(reader.ReadLine());
         }
         aUrl = new AuthenticatedURL();
         conn = aUrl.OpenConnection(url, token);
         conn.Connect();
         Assert.Equal(HttpURLConnection.HttpOk, conn.GetResponseCode());
         Assert.Equal(tokenStr, token.ToString());
     }
     finally
     {
         Stop();
     }
 }
        /// <exception cref="System.IO.IOException"/>
        private void VerifyHttpResponseCode(int expectedCode, Uri url)
        {
            HttpURLConnection connection = (HttpURLConnection)url.OpenConnection();

            connection.SetRequestMethod("GET");
            connection.Connect();
            NUnit.Framework.Assert.AreEqual(expectedCode, connection.GetResponseCode());
        }
        public virtual void TestWebImageViewer()
        {
            WebImageViewer viewer = new WebImageViewer(NetUtils.CreateSocketAddr("localhost:0"
                                                                                 ));

            try
            {
                viewer.InitServer(originalFsimage.GetAbsolutePath());
                int port = viewer.GetPort();
                // create a WebHdfsFileSystem instance
                URI               uri     = new URI("webhdfs://localhost:" + port.ToString());
                Configuration     conf    = new Configuration();
                WebHdfsFileSystem webhdfs = (WebHdfsFileSystem)FileSystem.Get(uri, conf);
                // verify the number of directories
                FileStatus[] statuses = webhdfs.ListStatus(new Path("/"));
                NUnit.Framework.Assert.AreEqual(NumDirs + 3, statuses.Length);
                // contains empty and xattr directory
                // verify the number of files in the directory
                statuses = webhdfs.ListStatus(new Path("/dir0"));
                NUnit.Framework.Assert.AreEqual(FilesPerDir, statuses.Length);
                // compare a file
                FileStatus status   = webhdfs.ListStatus(new Path("/dir0/file0"))[0];
                FileStatus expected = writtenFiles["/dir0/file0"];
                CompareFile(expected, status);
                // LISTSTATUS operation to an empty directory
                statuses = webhdfs.ListStatus(new Path("/emptydir"));
                NUnit.Framework.Assert.AreEqual(0, statuses.Length);
                // LISTSTATUS operation to a invalid path
                Uri url = new Uri("http://localhost:" + port + "/webhdfs/v1/invalid/?op=LISTSTATUS"
                                  );
                VerifyHttpResponseCode(HttpURLConnection.HttpNotFound, url);
                // LISTSTATUS operation to a invalid prefix
                url = new Uri("http://localhost:" + port + "/foo");
                VerifyHttpResponseCode(HttpURLConnection.HttpNotFound, url);
                // GETFILESTATUS operation
                status = webhdfs.GetFileStatus(new Path("/dir0/file0"));
                CompareFile(expected, status);
                // GETFILESTATUS operation to a invalid path
                url = new Uri("http://localhost:" + port + "/webhdfs/v1/invalid/?op=GETFILESTATUS"
                              );
                VerifyHttpResponseCode(HttpURLConnection.HttpNotFound, url);
                // invalid operation
                url = new Uri("http://localhost:" + port + "/webhdfs/v1/?op=INVALID");
                VerifyHttpResponseCode(HttpURLConnection.HttpBadRequest, url);
                // invalid method
                url = new Uri("http://localhost:" + port + "/webhdfs/v1/?op=LISTSTATUS");
                HttpURLConnection connection = (HttpURLConnection)url.OpenConnection();
                connection.SetRequestMethod("POST");
                connection.Connect();
                NUnit.Framework.Assert.AreEqual(HttpURLConnection.HttpBadMethod, connection.GetResponseCode
                                                    ());
            }
            finally
            {
                // shutdown the viewer
                viewer.Close();
            }
        }
Beispiel #22
0
        // >> autocomplete-remote-do-in-background-xamarin
        protected override Java.Lang.Void RunInBackground(params string[] @params)
        {
            HttpURLConnection urlConnection = null;

            try
            {
                URL url = new URL
                              ("http://www.telerik.com/docs/default-source/ui-for-ios/airports.json?sfvrsn=2");

                urlConnection = (HttpURLConnection)url.OpenConnection();

                urlConnection.RequestMethod        = "GET";
                urlConnection.UseCaches            = false;
                urlConnection.AllowUserInteraction = false;
                urlConnection.Connect();
                HttpStatus status = urlConnection.ResponseCode;

                if (status.Equals(HttpStatus.Ok))
                {
                    BufferedReader reader = new BufferedReader
                                                (new InputStreamReader(urlConnection.InputStream));
                    char[] buffer = new char[1024];
                    int    n;
                    Writer writer = new Java.IO.StringWriter();

                    while ((n = reader.Read(buffer)) != -1)
                    {
                        writer.Write(buffer, 0, n);
                    }

                    string json = writer.ToString();

                    try
                    {
                        JSONObject jObj = new JSONObject(json);
                        data = jObj.GetJSONArray("airports");
                    }
                    catch (JSONException ex)
                    {
                        ex.PrintStackTrace();
                    }
                }
            }
            catch (Java.IO.IOException e)
            {
                e.PrintStackTrace();
            }
            finally
            {
                if (urlConnection != null)
                {
                    urlConnection.Disconnect();
                }
            }

            return(null);
        }
Beispiel #23
0
 /// <summary>Performs SPNEGO authentication against the specified URL.</summary>
 /// <remarks>
 /// Performs SPNEGO authentication against the specified URL.
 /// <p>
 /// If a token is given it does a NOP and returns the given token.
 /// <p>
 /// If no token is given, it will perform the SPNEGO authentication sequence using an
 /// HTTP <code>OPTIONS</code> request.
 /// </remarks>
 /// <param name="url">the URl to authenticate against.</param>
 /// <param name="token">the authentication token being used for the user.</param>
 /// <exception cref="System.IO.IOException">if an IO error occurred.</exception>
 /// <exception cref="AuthenticationException">if an authentication error occurred.</exception>
 /// <exception cref="Org.Apache.Hadoop.Security.Authentication.Client.AuthenticationException
 ///     "/>
 public virtual void Authenticate(Uri url, AuthenticatedURL.Token token)
 {
     if (!token.IsSet())
     {
         this.url = url;
         base64   = new Base64(0);
         conn     = (HttpURLConnection)url.OpenConnection();
         if (connConfigurator != null)
         {
             conn = connConfigurator.Configure(conn);
         }
         conn.SetRequestMethod(AuthHttpMethod);
         conn.Connect();
         bool needFallback = false;
         if (conn.GetResponseCode() == HttpURLConnection.HttpOk)
         {
             Log.Debug("JDK performed authentication on our behalf.");
             // If the JDK already did the SPNEGO back-and-forth for
             // us, just pull out the token.
             AuthenticatedURL.ExtractToken(conn, token);
             if (IsTokenKerberos(token))
             {
                 return;
             }
             needFallback = true;
         }
         if (!needFallback && IsNegotiate())
         {
             Log.Debug("Performing our own SPNEGO sequence.");
             DoSpnegoSequence(token);
         }
         else
         {
             Log.Debug("Using fallback authenticator sequence.");
             Authenticator auth = GetFallBackAuthenticator();
             // Make sure that the fall back authenticator have the same
             // ConnectionConfigurator, since the method might be overridden.
             // Otherwise the fall back authenticator might not have the information
             // to make the connection (e.g., SSL certificates)
             auth.SetConnectionConfigurator(connConfigurator);
             auth.Authenticate(url, token);
         }
     }
 }
Beispiel #24
0
        /// <summary>Access a URL and get the corresponding return Http status code.</summary>
        /// <remarks>
        /// Access a URL and get the corresponding return Http status code. The URL
        /// will be accessed as the passed user, by sending user.name request
        /// parameter.
        /// </remarks>
        /// <param name="urlstring"/>
        /// <param name="userName"/>
        /// <returns/>
        /// <exception cref="System.IO.IOException"/>
        internal static int GetHttpStatusCode(string urlstring, string userName)
        {
            Uri url = new Uri(urlstring + "?user.name=" + userName);

            System.Console.Out.WriteLine("Accessing " + url + " as user " + userName);
            HttpURLConnection connection = (HttpURLConnection)url.OpenConnection();

            connection.Connect();
            return(connection.GetResponseCode());
        }
Beispiel #25
0
 Task ConnectAsync(HttpURLConnection httpConnection, CancellationToken ct)
 {
     return(Task.Run(() => {
         try {
             using (ct.Register(() => httpConnection?.Disconnect()))
                 httpConnection?.Connect();
         } catch {
             ct.ThrowIfCancellationRequested();
             throw;
         }
     }, ct));
 }
Beispiel #26
0
        public virtual void TestEmbeddedWebAppProxy()
        {
            conf.SetBoolean(YarnConfiguration.AutoFailoverEnabled, false);
            cluster.Init(conf);
            cluster.Start();
            GetAdminService(0).TransitionToActive(req);
            NUnit.Framework.Assert.IsFalse("RM never turned active", -1 == cluster.GetActiveRMIndex
                                               ());
            VerifyConnections();
            // send httpRequest with fakeApplicationId
            // expect to get "Not Found" response and 404 response code
            Uri wrongUrl = new Uri("http://0.0.0.0:18088/proxy/" + fakeAppId);
            HttpURLConnection proxyConn = (HttpURLConnection)wrongUrl.OpenConnection();

            proxyConn.Connect();
            VerifyResponse(proxyConn);
            ExplicitFailover();
            VerifyConnections();
            proxyConn.Connect();
            VerifyResponse(proxyConn);
        }
 private static Bitmap GetBitmapFromURL(String src)
 {
     try {
         URL url = new URL(src);
         HttpURLConnection connection = (HttpURLConnection)url.OpenConnection();
         connection.DoInput = true;
         connection.Connect();
         Stream input    = connection.InputStream;
         Bitmap myBitmap = BitmapFactory.DecodeStream(input);
         return(myBitmap);
     } catch (IOException) {
         return(null);
     }
 }
Beispiel #28
0
        public virtual void TestWebAppProxyInStandAloneMode()
        {
            conf.SetBoolean(YarnConfiguration.AutoFailoverEnabled, false);
            WebAppProxyServer webAppProxyServer = new WebAppProxyServer();

            try
            {
                conf.Set(YarnConfiguration.ProxyAddress, "0.0.0.0:9099");
                cluster.Init(conf);
                cluster.Start();
                GetAdminService(0).TransitionToActive(req);
                NUnit.Framework.Assert.IsFalse("RM never turned active", -1 == cluster.GetActiveRMIndex
                                                   ());
                VerifyConnections();
                webAppProxyServer.Init(conf);
                // Start webAppProxyServer
                NUnit.Framework.Assert.AreEqual(Service.STATE.Inited, webAppProxyServer.GetServiceState
                                                    ());
                webAppProxyServer.Start();
                NUnit.Framework.Assert.AreEqual(Service.STATE.Started, webAppProxyServer.GetServiceState
                                                    ());
                // send httpRequest with fakeApplicationId
                // expect to get "Not Found" response and 404 response code
                Uri wrongUrl = new Uri("http://0.0.0.0:9099/proxy/" + fakeAppId);
                HttpURLConnection proxyConn = (HttpURLConnection)wrongUrl.OpenConnection();
                proxyConn.Connect();
                VerifyResponse(proxyConn);
                ExplicitFailover();
                VerifyConnections();
                proxyConn.Connect();
                VerifyResponse(proxyConn);
            }
            finally
            {
                webAppProxyServer.Stop();
            }
        }
Beispiel #29
0
 Task ConnectAsync(HttpURLConnection httpConnection, CancellationToken ct)
 {
     return(Task.Run(() => {
         try {
             using (ct.Register(() => DisconnectAsync(httpConnection).ContinueWith(t => {
                 if (t.Exception != null)
                 {
                     Logger.Log(LogLevel.Info, LOG_APP, $"Disconnection exception: {t.Exception}");
                 }
             }, TaskScheduler.Default)))
                 httpConnection?.Connect();
         } catch {
             ct.ThrowIfCancellationRequested();
             throw;
         }
     }, ct));
 }
            protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
            {
                HttpURLConnection urlConnection = null;
                BufferedReader    reader        = null;
                string            line          = null;

                try
                {
                    URL url = new URL(urlString + urlQuery);
                    urlConnection = (HttpURLConnection)url.OpenConnection();
                    urlConnection.RequestMethod = "GET";
                    urlConnection.Connect();

                    Stream inputStream = urlConnection.InputStream;

                    if (inputStream != null)
                    {
                        reader = new BufferedReader(new InputStreamReader(inputStream));
                        line   = reader.ReadLine();
                    }
                }
                catch (System.IO.IOException e)
                {
                    //Log.e("PlaceholderFragment", "Error ", e);
                }
                finally
                {
                    if (urlConnection != null)
                    {
                        urlConnection.Disconnect();
                    }
                    if (reader != null)
                    {
                        try
                        {
                            reader.Close();
                        }
                        catch (System.IO.IOException e)
                        {
                            //Log.e("PlaceholderFragment", "Error closing stream", e);
                        }
                    }
                }

                return(line);
            }