Search in sources :

Example 1 with KnoxShellException

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

the class WebHDFSCommand method remove.

private String remove(Map<String, String> mounts, String path) {
    String mountPoint = determineMountPoint(path);
    KnoxSession session = getSessionForMountPoint(mounts, mountPoint);
    if (session != null) {
        String targetPath = determineTargetPath(path, mountPoint);
        try {
            Hdfs.rm(session).file(targetPath).now().getString();
        } catch (KnoxShellException | IOException e) {
            e.printStackTrace();
        }
    } else {
        return "No session established for mountPoint: " + mountPoint + " Use :fs mount {topology-url} {mountpoint-name}";
    }
    return "Successfully removed: " + path;
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession) IOException(java.io.IOException) KnoxShellException(org.apache.knox.gateway.shell.KnoxShellException)

Example 2 with KnoxShellException

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

the class WebHDFSCommand method exists.

private boolean exists(KnoxSession session, String path) {
    boolean rc = false;
    try {
        Response response = Hdfs.status(session).file(path).now();
        rc = response.exists();
    } catch (KnoxShellException e) {
    // NOP
    }
    return rc;
}
Also used : Response(org.apache.knox.gateway.shell.hdfs.Status.Response) KnoxShellException(org.apache.knox.gateway.shell.KnoxShellException)

Example 3 with KnoxShellException

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

the class WebHDFSCommand method cat.

private String cat(Map<String, String> mounts, String path) {
    String response = null;
    String mountPoint = determineMountPoint(path);
    KnoxSession session = getSessionForMountPoint(mounts, mountPoint);
    if (session != null) {
        String targetPath = determineTargetPath(path, mountPoint);
        try {
            String contents = Hdfs.get(session).from(targetPath).now().getString();
            response = contents;
        } catch (KnoxShellException | IOException e) {
            e.printStackTrace();
            response = "Exception ocurred: " + e.getMessage();
        }
    } else {
        response = "No session established for mountPoint: " + mountPoint + " Use :fs mount {topology-url} {mountpoint-name}";
    }
    return response;
}
Also used : KnoxSession(org.apache.knox.gateway.shell.KnoxSession) IOException(java.io.IOException) KnoxShellException(org.apache.knox.gateway.shell.KnoxShellException)

Example 4 with KnoxShellException

use of org.apache.knox.gateway.shell.KnoxShellException 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 5 with KnoxShellException

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

the class AliasResponse method parseResponseEntity.

private void parseResponseEntity() {
    if (!isExpectedResponseStatus()) {
        throw new KnoxShellException("Unexpected response: " + response().getStatusLine().getReasonPhrase());
    }
    HttpEntity entity = response().getEntity();
    if (entity == null) {
        throw new KnoxShellException("Missing expected response content");
    }
    String contentType = entity.getContentType().getValue();
    if (!CONTENT_TYPE.equals(contentType)) {
        throw new KnoxShellException("Unexpected response content type: " + contentType);
    }
    try {
        responseContent = EntityUtils.toString(entity);
        parsedResponse = (new ObjectMapper()).readValue(responseContent, new TypeReference<Map<String, Object>>() {
        });
    } catch (Exception e) {
        throw new KnoxShellException("Unable to process response content", e);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) TypeReference(com.fasterxml.jackson.core.type.TypeReference) KnoxShellException(org.apache.knox.gateway.shell.KnoxShellException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) KnoxShellException(org.apache.knox.gateway.shell.KnoxShellException)

Aggregations

KnoxShellException (org.apache.knox.gateway.shell.KnoxShellException)9 IOException (java.io.IOException)5 KnoxSession (org.apache.knox.gateway.shell.KnoxSession)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HttpResponse (org.apache.http.HttpResponse)2 StringEntity (org.apache.http.entity.StringEntity)2 Test (org.junit.Test)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)1 HashMap (java.util.HashMap)1 HttpEntity (org.apache.http.HttpEntity)1 Response (org.apache.knox.gateway.shell.hdfs.Status.Response)1 KnoxShellTable (org.apache.knox.gateway.shell.table.KnoxShellTable)1