knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lmc...@apache.org
Subject [3/3] knox git commit: checkpoint
Date Sat, 04 Feb 2017 20:05:01 GMT
checkpoint

Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/54281ba5
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/54281ba5
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/54281ba5

Branch: refs/heads/knoxinit
Commit: 54281ba585b67c8cd387c0076bc9d86184fd8351
Parents: b8a2b11
Author: Larry McCay <lmccay@hortonworks.com>
Authored: Sat Feb 4 15:04:47 2017 -0500
Committer: Larry McCay <lmccay@hortonworks.com>
Committed: Sat Feb 4 15:04:47 2017 -0500

----------------------------------------------------------------------
 gateway-shell/pom.xml                           |  5 --
 .../hadoop/gateway/shell/ClientContext.java     |  7 ++-
 .../org/apache/hadoop/gateway/shell/Hadoop.java |  8 ++-
 .../org/apache/hadoop/gateway/shell/KnoxSh.java | 53 ++++++++++++++++++--
 4 files changed, 63 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/54281ba5/gateway-shell/pom.xml
----------------------------------------------------------------------
diff --git a/gateway-shell/pom.xml b/gateway-shell/pom.xml
index a91ade1..a6f1ea2 100644
--- a/gateway-shell/pom.xml
+++ b/gateway-shell/pom.xml
@@ -65,11 +65,6 @@
             <artifactId>httpclient</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.apache.httpcomponents</groupId>
-            <artifactId>httpcore</artifactId>
-            <version>4.4.6</version>
-         </dependency>
-        <dependency>
             <groupId>com.jayway.jsonpath</groupId>
             <artifactId>json-path</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/knox/blob/54281ba5/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/ClientContext.java
----------------------------------------------------------------------
diff --git a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/ClientContext.java
b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/ClientContext.java
index 6055f68..b83daf4 100644
--- a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/ClientContext.java
+++ b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/ClientContext.java
@@ -191,6 +191,12 @@ public class ClientContext {
     return context;
   }
 
+  public static ClientContext with(final String url) {
+    ClientContext context = new ClientContext();
+    context.configuration.addProperty("url", url);
+    return context;
+  }
+
   public String username() {
     return configuration.getString("username");
   }
@@ -202,5 +208,4 @@ public class ClientContext {
   public String url() {
     return configuration.getString("url");
   }
-
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/54281ba5/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java
----------------------------------------------------------------------
diff --git a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java
index 6bb6ba2..ed0feb3 100644
--- a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java
+++ b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java
@@ -91,7 +91,13 @@ public class Hadoop implements Closeable {
   public void setHeaders(Map<String, String> headers) {
     this.headers = headers;
   }
-  
+
+  public static Hadoop login( String url, Map<String,String> headers ) throws URISyntaxException
{
+    Hadoop instance = new Hadoop(ClientContext.with(url));
+    instance.setHeaders(headers);
+    return instance;
+  }
+
   public static Hadoop login( String url, String username, String password ) throws URISyntaxException
{
     return new Hadoop(ClientContext.with(username, password, url));
   }

http://git-wip-us.apache.org/repos/asf/knox/blob/54281ba5/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
----------------------------------------------------------------------
diff --git a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
index 088bd6c..468b6b7 100644
--- a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
+++ b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
@@ -17,14 +17,20 @@
  */
 package org.apache.hadoop.gateway.shell;
 
+import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.PrintStream;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.nio.file.attribute.PosixFilePermission;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 import java.util.Arrays;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.HashSet;
 import java.util.Map;
@@ -171,13 +177,12 @@ public class KnoxSh {
 
       Hadoop session = Hadoop.login(gateway, username, pass);
 
-      String text = Token.get( session ).now().toString();
+      String text = Token.get( session ).now().getString();
       Map<String, String> json = JsonUtils.getMapFromJsonString(text);
 
       //println "Access Token: " + json.access_token
       System.out.println("knoxinit successful!");
-      System.out.println("Token Type: " + json.get("token_type"));
-      System.out.println("Expires On: " + new Date(json.get("expires_in")));
+      displayTokenDetails(json);
 
       File tokenfile = new File(System.getProperty("user.home"), ".knoxtokencache");
       FileOutputStream fos = new FileOutputStream(tokenfile);
@@ -209,6 +214,8 @@ public class KnoxSh {
 
     @Override
     public void execute() throws Exception {
+      File tokenfile = new File(System.getProperty("user.home"), ".knoxtokencache");
+      tokenfile.delete();
     }
 
     @Override
@@ -225,6 +232,14 @@ public class KnoxSh {
 
     @Override
     public void execute() throws Exception {
+      String tokenfile = readFile(
+          System.getProperty("user.home") +
+          File.separator + ".knoxtokencache");
+
+      if (tokenfile != null) {
+        Map<String, String> json = JsonUtils.getMapFromJsonString(tokenfile);
+        displayTokenDetails(json);
+      }
     }
 
     @Override
@@ -234,6 +249,38 @@ public class KnoxSh {
 
   }
 
+  private void displayTokenDetails(Map<String, String> json) {
+    System.out.println("Token Type: " + json.get("token_type"));
+
+    DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
+
+    long milliSeconds= Long.parseLong(json.get("expires_in"));
+
+    Calendar calendar = Calendar.getInstance();
+    calendar.setTimeInMillis(milliSeconds);
+    System.out.println("Expires On: " + formatter.format(calendar.getTime()));
+  }
+
+  private String readFile(String file) throws IOException {
+    BufferedReader reader = new BufferedReader(new FileReader (file));
+    String line = null;
+    String content = null;
+    StringBuilder  stringBuilder = new StringBuilder();
+    String ls = System.getProperty("line.separator");
+
+    try {
+        while((line = reader.readLine()) != null) {
+            stringBuilder.append(line);
+            stringBuilder.append(ls);
+        }
+
+        content = stringBuilder.toString();
+    } finally {
+        reader.close();
+    }
+    return content;
+}
+
   /**
    * @param args
    * @throws Exception


Mime
View raw message