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;
}
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 "";
}
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());
}
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);
}
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());
}
Aggregations