You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@hive.apache.org by Rachna Jotwani <rj...@dag.com> on 2016/02/05 22:06:57 UTC

WebHCat and knox

Could someone send me an example of how to authenticate with apache knox  for templeton ?
I tried the example below , but it doesn't work.

Thanks
Rachna


package com.test.hadoop;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.sql.Timestamp;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.codec.binary.Base64;

public class TempletonTest {

                private static final String LISTDATABASES = "/database";
                private static final String PACKAGE_NAME = TempletonTest.class
                                                .getPackage().getName();

                /** Logger for this package */
                private static final Logger logger = Logger.getLogger(PACKAGE_NAME);
                public TempletonTest(Map options) {

                                try {
                                                logger.info("EXECUTING TEMPLETON TEST");
                String templetonURL = "https://{gateway-host}:{gateway-port}/gateway/default/templeton/v1/ddl";
                                                String user = "user1";
                                                String password = "user1-password";

                                                String url = templetonURL.replace("ddl", "status");
                                                logger.info("calling url.... " + url);
                                                long start = System.currentTimeMillis();
                                                logger.info("Start Time : " + new Timestamp(start));
                                                String authString = user+":"+password;
                                                byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
                                                String navigatorAuthStringEncoded = new String(authEncBytes);
                                                StringBuilder response = sendRequestToUrl(url, navigatorAuthStringEncoded);

                                                //print result
                                                logger.info("**************************************");
                                                logger.info(response.toString());
                                                logger.info("**************************************");

                                                url =templetonURL+LISTDATABASES;

                                                logger.info("calling url.... " + url);
                                                start = System.currentTimeMillis();
                                                logger.info("Start Time : " + new Timestamp(start));
                                                response = sendRequestToUrl(url, navigatorAuthStringEncoded);

                                                //print result
                                                logger.info("**************************************");
                                                logger.info(response.toString());
                                                logger.info("**************************************");
                                } catch (Throwable e) {
                                                logger.log(Level.SEVERE,"error executing test\n"+e.getMessage() ,e);
                                }

                }

                private static StringBuilder sendRequestToUrl(String url, String navigatorAuthStringEncoded)
                                                throws MalformedURLException, IOException, ProtocolException {

                                URL obj = new URL(url);
                                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                                // optional default is GET
                                con.setRequestMethod("GET");
                                //add request header
                                con.setRequestProperty("Content-Type", "application/json");

                                // Adding HTTP Basic Authentication for Navigator
                                con.setRequestProperty("Authorization", "Basic " + navigatorAuthStringEncoded);

                                int responseCode = con.getResponseCode();

                                int length = con.getContentLength();

                                logger.info("\nSending 'GET' request to URL : " + url);
                                logger.info("Response Code : " + responseCode);
                                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                                String inputLine;
                                StringBuilder response = new StringBuilder();
                                while ((inputLine = in.readLine()) != null) {
                                                response.append(inputLine);
                                }
                                in.close();
                                return response;
                }
}