Search in sources :

Example 66 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestHttpServer method testLongHeader.

@Test
public void testLongHeader() throws Exception {
    URL url = new URL(baseUrl, "/longheader");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    testLongHeader(conn);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) Test(org.junit.Test)

Example 67 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestAuthenticationSessionCookie method testSessionCookie.

@Test
public void testSessionCookie() throws IOException {
    try {
        startServer(true);
    } catch (Exception e) {
        // Auto-generated catch block
        e.printStackTrace();
    }
    URL base = new URL("http://" + NetUtils.getHostPortString(server.getConnectorAddress(0)));
    HttpURLConnection conn = (HttpURLConnection) new URL(base, "/echo").openConnection();
    String header = conn.getHeaderField("Set-Cookie");
    List<HttpCookie> cookies = HttpCookie.parse(header);
    Assert.assertTrue(!cookies.isEmpty());
    Log.getLog().info(header);
    Assert.assertFalse(header.contains("; Expires="));
    Assert.assertTrue("token".equals(cookies.get(0).getValue()));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HttpCookie(java.net.HttpCookie) IOException(java.io.IOException) URL(java.net.URL) Test(org.junit.Test)

Example 68 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestHttpCookieFlag method testHttpCookie.

@Test
public void testHttpCookie() throws IOException {
    URL base = new URL("http://" + NetUtils.getHostPortString(server.getConnectorAddress(0)));
    HttpURLConnection conn = (HttpURLConnection) new URL(base, "/echo").openConnection();
    String header = conn.getHeaderField("Set-Cookie");
    List<HttpCookie> cookies = HttpCookie.parse(header);
    Assert.assertTrue(!cookies.isEmpty());
    Assert.assertTrue(header.contains("; HttpOnly"));
    Assert.assertTrue("token".equals(cookies.get(0).getValue()));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HttpCookie(java.net.HttpCookie) URL(java.net.URL) Test(org.junit.Test)

Example 69 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestAuditLogger method testWebHdfsAuditLogger.

@Test
public void testWebHdfsAuditLogger() throws IOException, URISyntaxException {
    Configuration conf = new HdfsConfiguration();
    conf.set(DFS_NAMENODE_AUDIT_LOGGERS_KEY, DummyAuditLogger.class.getName());
    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).build();
    GetOpParam.Op op = GetOpParam.Op.GETFILESTATUS;
    try {
        cluster.waitClusterUp();
        assertTrue(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();
        assertEquals(200, conn.getResponseCode());
        conn.disconnect();
        assertEquals(1, DummyAuditLogger.logCount);
        assertEquals("127.0.0.1", 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();
        assertEquals(200, conn.getResponseCode());
        conn.disconnect();
        assertEquals(2, DummyAuditLogger.logCount);
        assertEquals("127.0.0.1", DummyAuditLogger.remoteAddr);
        // trusted proxied request
        conf.set(ProxyServers.CONF_HADOOP_PROXYSERVERS, "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();
        assertEquals(200, conn.getResponseCode());
        conn.disconnect();
        assertEquals(3, DummyAuditLogger.logCount);
        assertEquals("1.1.1.1", DummyAuditLogger.remoteAddr);
    } finally {
        cluster.shutdown();
    }
}
Also used : GetOpParam(org.apache.hadoop.hdfs.web.resources.GetOpParam) MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) HttpURLConnection(java.net.HttpURLConnection) Configuration(org.apache.hadoop.conf.Configuration) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) URI(java.net.URI) Test(org.junit.Test)

Example 70 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestWebDelegationToken method testFallbackToPseudoDelegationTokenAuthenticator.

@Test
public void testFallbackToPseudoDelegationTokenAuthenticator() throws Exception {
    final Server jetty = createJettyServer();
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/foo");
    jetty.setHandler(context);
    context.addFilter(new FilterHolder(PseudoDTAFilter.class), "/*", EnumSet.of(DispatcherType.REQUEST));
    context.addServlet(new ServletHolder(UserServlet.class), "/bar");
    try {
        jetty.start();
        final URL url = new URL(getJettyURL() + "/foo/bar");
        UserGroupInformation ugi = UserGroupInformation.createRemoteUser(FOO_USER);
        ugi.doAs(new PrivilegedExceptionAction<Void>() {

            @Override
            public Void run() throws Exception {
                DelegationTokenAuthenticatedURL.Token token = new DelegationTokenAuthenticatedURL.Token();
                DelegationTokenAuthenticatedURL aUrl = new DelegationTokenAuthenticatedURL();
                HttpURLConnection conn = aUrl.openConnection(url, token);
                Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
                List<String> ret = IOUtils.readLines(conn.getInputStream());
                Assert.assertEquals(1, ret.size());
                Assert.assertEquals(FOO_USER, ret.get(0));
                aUrl.getDelegationToken(url, token, FOO_USER);
                Assert.assertNotNull(token.getDelegationToken());
                Assert.assertEquals(new Text("token-kind"), token.getDelegationToken().getKind());
                return null;
            }
        });
    } finally {
        jetty.stop();
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken) Text(org.apache.hadoop.io.Text) URL(java.net.URL) AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) ServletException(javax.servlet.ServletException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) List(java.util.List) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) Test(org.junit.Test)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)3831 URL (java.net.URL)2447 IOException (java.io.IOException)1634 InputStream (java.io.InputStream)1082 InputStreamReader (java.io.InputStreamReader)692 Test (org.junit.Test)650 BufferedReader (java.io.BufferedReader)573 OutputStream (java.io.OutputStream)466 MalformedURLException (java.net.MalformedURLException)372 URLConnection (java.net.URLConnection)248 HashMap (java.util.HashMap)216 OutputStreamWriter (java.io.OutputStreamWriter)208 Map (java.util.Map)199 Gson (com.google.gson.Gson)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)186 ArrayList (java.util.ArrayList)168 ExecutionException (java.util.concurrent.ExecutionException)161 File (java.io.File)159 AsyncTask (android.os.AsyncTask)158 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)157