Search in sources :

Example 6 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class WebHDFSCommand method listStatus.

private Object listStatus(Map<String, String> mounts, String path) {
    Object response = null;
    try {
        String directory;
        String mountPoint = determineMountPoint(path);
        if (mountPoint != null) {
            KnoxSession session = getSessionForMountPoint(mounts, mountPoint);
            if (session != null) {
                directory = determineTargetPath(path, mountPoint);
                String json = Hdfs.ls(session).dir(directory).now().getString();
                Map<String, HashMap<String, ArrayList<HashMap<String, String>>>> map = JsonUtils.getFileStatusesAsMap(json);
                if (map != null) {
                    ArrayList<HashMap<String, String>> list = map.get("FileStatuses").get("FileStatus");
                    KnoxShellTable table = buildTableFromListStatus(directory, list);
                    response = table;
                }
            } else {
                response = "No session established for mountPoint: " + mountPoint + " Use :fs mount {topology-url} {mountpoint-name}";
            }
        } else {
            response = "No mountpoint found. Use ':fs mount {topologyURL} {mountpoint}'.";
        }
    } catch (KnoxShellException | IOException e) {
        response = "Exception ocurred: " + e.getMessage();
        e.printStackTrace();
    }
    return response;
}
Also used : KnoxShellTable(org.apache.knox.gateway.shell.table.KnoxShellTable) HashMap(java.util.HashMap) KnoxSession(org.apache.knox.gateway.shell.KnoxSession) IOException(java.io.IOException) KnoxShellException(org.apache.knox.gateway.shell.KnoxShellException)

Example 7 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class WebHDFSCommand method execute.

@Override
public Object execute(List<String> args) {
    Map<String, String> mounts = getMountPoints();
    if (args.isEmpty()) {
        args.add("ls");
    }
    if (args.get(0).equalsIgnoreCase("mount")) {
        String url = args.get(1);
        String mountPoint = args.get(2);
        return mount(mounts, url, mountPoint);
    } else if (args.get(0).equalsIgnoreCase("unmount")) {
        String mountPoint = args.get(1);
        unmount(mounts, mountPoint);
    } else if (args.get(0).equalsIgnoreCase("mounts")) {
        return listMounts(mounts);
    } else if (args.get(0).equalsIgnoreCase("ls")) {
        String path = args.get(1);
        return listStatus(mounts, path);
    } else if (args.get(0).equalsIgnoreCase("put")) {
        // Hdfs.put( session ).file( dataFile ).to( dataDir + "/" + dataFile ).now()
        // :fs put from-path to-path
        String localFile = args.get(1);
        String path = args.get(2);
        int permission = 755;
        if (args.size() >= 4) {
            permission = Integer.parseInt(args.get(3));
        }
        return put(mounts, localFile, path, permission);
    } else if (args.get(0).equalsIgnoreCase("rm")) {
        // Hdfs.rm( session ).file( dataFile ).now()
        // :fs rm target-path
        String path = args.get(1);
        return remove(mounts, path);
    } else if (args.get(0).equalsIgnoreCase("cat")) {
        // println Hdfs.get( session ).from( dataDir + "/" + dataFile ).now().string
        // :fs cat target-path
        String path = args.get(1);
        return cat(mounts, path);
    } else if (args.get(0).equalsIgnoreCase("mkdir")) {
        // println Hdfs.mkdir( session ).dir( directoryPath ).perm( "777" ).now().string
        // :fs mkdir target-path [perms]
        String path = args.get(1);
        String perms = null;
        if (args.size() == 3) {
            perms = args.get(2);
        }
        return mkdir(mounts, path, perms);
    } else if (args.get(0).equalsIgnoreCase("get")) {
        // println Hdfs.get( session ).from( dataDir + "/" + dataFile ).now().string
        // :fs get from-path [to-path]
        String path = args.get(1);
        String mountPoint = determineMountPoint(path);
        KnoxSession session = getSessionForMountPoint(mounts, mountPoint);
        if (session != null) {
            String from = determineTargetPath(path, mountPoint);
            String to = null;
            if (args.size() > 2) {
                to = args.get(2);
            } else {
                to = System.getProperty("user.home") + File.separator + path.substring(path.lastIndexOf(File.separator));
            }
            return get(mountPoint, from, to);
        } else {
            return "No session established for mountPoint: " + mountPoint + " Use :fs mount {topology-url} {mountpoint-name}";
        }
    } else {
        System.out.println("Unknown filesystem command");
        System.out.println(getUsage());
    }
    return "";
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession)

Example 8 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class AliasTest method testListClusterAliases.

@Test
public void testListClusterAliases() {
    KnoxSession session = null;
    try {
        session = createMockKnoxSession();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    final String clusterName = "myCluster";
    final String expectedEndpointPath = session.base() + AbstractAliasRequest.SERVICE_PATH + "/" + clusterName;
    AbstractAliasRequest request = Alias.list(session, clusterName);
    assertTrue(request instanceof ListRequest);
    assertEquals("Endpoint mismatch", expectedEndpointPath, request.getRequestURI().toString());
    Callable callable = request.callable();
    try {
        callable.call();
    } catch (Exception e) {
    // expected
    }
    assertEquals("Unexpected HTTP method.", HttpGet.METHOD_NAME, request.getRequest().getMethod());
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 9 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class AliasTest method testAddClusterAlias.

@Test
public void testAddClusterAlias() {
    KnoxSession session = null;
    try {
        session = createMockKnoxSession();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    final String clusterName = "myCluster";
    final String alias = "aliasPut1";
    final String pwd = "aliasPut1_pwd";
    final String expectedEndpointPath = session.base() + AbstractAliasRequest.SERVICE_PATH + "/" + clusterName + "/" + alias;
    AbstractAliasRequest request = Alias.add(session, clusterName, alias, pwd);
    assertTrue(request instanceof PostRequest);
    assertEquals("Endpoint mismatch", expectedEndpointPath, request.getRequestURI().toString());
    Callable callable = request.callable();
    try {
        callable.call();
    } catch (Exception e) {
    // expected
    }
    assertEquals("Unexpected HTTP method.", HttpPost.METHOD_NAME, request.getRequest().getMethod());
    HttpRequestBase httpRequest = request.getRequest();
    assertTrue(httpRequest instanceof HttpPost);
    HttpEntity entity = ((HttpPost) httpRequest).getEntity();
    assertNotNull("Missing expected form data.", entity);
    assertTrue(entity instanceof UrlEncodedFormEntity);
    String formData = null;
    try {
        formData = EntityUtils.toString(entity);
    } catch (IOException e) {
        fail("Failed to consume request entity: " + e.getMessage());
    }
    assertNotNull(formData);
    assertEquals("Form data mismatch", PostRequest.FORM_PARAM_VALUE + "=" + pwd, formData);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) KnoxSession(org.apache.knox.gateway.shell.KnoxSession) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 10 with KnoxSession

use of org.apache.knox.gateway.shell.KnoxSession in project knox by apache.

the class AliasTest method testListGatewayAliases.

@Test
public void testListGatewayAliases() {
    KnoxSession session = null;
    try {
        session = createMockKnoxSession();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    final String expectedEndpointPath = session.base() + AbstractAliasRequest.SERVICE_PATH + "/" + AbstractAliasRequest.GATEWAY_CLUSTER_NAME;
    AbstractAliasRequest request = Alias.list(session);
    assertTrue(request instanceof ListRequest);
    assertEquals("Endpoint mismatch", expectedEndpointPath, request.getRequestURI().toString());
    Callable callable = request.callable();
    try {
        callable.call();
    } catch (Exception e) {
    // expected
    }
    assertEquals("Unexpected HTTP method.", HttpGet.METHOD_NAME, request.getRequest().getMethod());
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Aggregations

KnoxSession (org.apache.knox.gateway.shell.KnoxSession)22 IOException (java.io.IOException)12 Callable (java.util.concurrent.Callable)6 Test (org.junit.Test)6 KnoxShellException (org.apache.knox.gateway.shell.KnoxShellException)5 URISyntaxException (java.net.URISyntaxException)2 HttpEntity (org.apache.http.HttpEntity)2 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)2 HttpPost (org.apache.http.client.methods.HttpPost)2 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)2 CredentialCollectionException (org.apache.knox.gateway.shell.CredentialCollectionException)2 HashMap (java.util.HashMap)1 CredentialCollector (org.apache.knox.gateway.shell.CredentialCollector)1 KnoxShellTable (org.apache.knox.gateway.shell.table.KnoxShellTable)1