Example usage for org.apache.hadoop.security.token.delegation.web DelegationTokenAuthenticatedURL DelegationTokenAuthenticatedURL

List of usage examples for org.apache.hadoop.security.token.delegation.web DelegationTokenAuthenticatedURL DelegationTokenAuthenticatedURL

Introduction

In this page you can find the example usage for org.apache.hadoop.security.token.delegation.web DelegationTokenAuthenticatedURL DelegationTokenAuthenticatedURL.

Prototype

public DelegationTokenAuthenticatedURL() 

Source Link

Document

Creates an DelegationTokenAuthenticatedURL.

Usage

From source file:org.apache.sqoop.client.request.ResourceRequest.java

License:Apache License

protected String doHttpRequest(String strURL, String method, String data) {
    DataOutputStream wr = null;/* www.ja  va2 s.  c  om*/
    BufferedReader reader = null;
    try {
        //    This user name is only in simple mode. In Kerberos mode, this user name will be ignored by Sqoop server and user name in UGI which is authenticated by Kerberos server will be used instead.
        strURL = addUsername(strURL);
        URL url = new URL(strURL);
        HttpURLConnection conn = new DelegationTokenAuthenticatedURL().openConnection(url, authToken);

        conn.setRequestMethod(method);
        //      Sqoop is using JSON for data transfers
        conn.setRequestProperty("Accept", MediaType.APPLICATION_JSON);
        //      Transfer client locale to return client specific data
        conn.setRequestProperty("Accept-Language", Locale.getDefault().toString());
        if (method.equalsIgnoreCase(HttpMethod.PUT) || method.equalsIgnoreCase(HttpMethod.POST)) {
            conn.setDoOutput(true);
            data = data == null ? "" : data;
            conn.setRequestProperty("Content-Length", Integer.toString(data.getBytes().length));
            //        Send request
            wr = new DataOutputStream(conn.getOutputStream());
            wr.writeBytes(data);
            wr.flush();
            wr.close();
        }

        LOG.debug("Status code: " + conn.getResponseCode() + " " + conn.getResponseMessage());
        StringBuilder result = new StringBuilder();
        int responseCode = conn.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = reader.readLine();
            while (line != null) {
                result.append(line);
                line = reader.readLine();
            }
            reader.close();
        } else if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
            /**
             * Client filter to intercepting exceptions sent by sqoop server and
             * recreating them on client side. Current implementation will create new
             * instance of SqoopException and will attach original error code and message.
             *
             * Special handling for 500 internal server error in case that server
             * has sent us it's exception correctly. We're using default route
             * for all other 500 occurrences.
             */
            if (conn.getHeaderFields().keySet()
                    .contains(SqoopProtocolConstants.HEADER_SQOOP_INTERNAL_ERROR_CODE)) {

                ThrowableBean ex = new ThrowableBean();

                result = new StringBuilder();
                reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
                String line = reader.readLine();
                while (line != null) {
                    result.append(line);
                    line = reader.readLine();
                }
                reader.close();

                JSONObject json = JSONUtils.parse(result.toString());
                ex.restore(json);

                throw new SqoopException(ClientError.CLIENT_0001, ex.getThrowable());
            } else {
                result = new StringBuilder();
                reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                    result.append('\n');
                }
                reader.close();

                throw new SqoopException(ClientError.CLIENT_0001, result.toString());
            }
        } else {
            throw new SqoopException(ClientError.CLIENT_0000);
        }
        return result.toString();
    } catch (IOException ex) {
        LOG.trace("ERROR: ", ex);
        throw new SqoopException(ClientError.CLIENT_0000, ex);
    } catch (AuthenticationException ex) {
        LOG.trace("ERROR: ", ex);
        throw new SqoopException(ClientError.CLIENT_0004, ex);
    } finally {
        try {
            if (wr != null) {
                wr.close();
            }
        } catch (IOException e) {
            LOG.trace("Cannot close DataOutputStream.", e);
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            LOG.trace("Cannot close BufferReader.", e);
        }
    }
}

From source file:org.apache.sqoop.integration.server.InvalidRESTCallsTest.java

License:Apache License

@Test
public void test() throws Exception {
    LOG.info("Start: " + getTestName());

    URL url = new URL(getServerUrl() + desc.rest);
    HttpURLConnection connection = new DelegationTokenAuthenticatedURL().openConnection(url,
            new DelegationTokenAuthenticatedURL.Token());
    connection.setRequestMethod(desc.method);

    if (desc.data != null) {
        connection.setDoOutput(true);//from  ww w .  j a  v a2s .  co m

        byte[] byteData = desc.data.getBytes(Charset.forName("UTF-8"));
        connection.setRequestProperty("Content-Length", Integer.toString(byteData.length));
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.write(byteData, byteData.length, 0);
        wr.flush();
        wr.close();
    }

    desc.validator.setConnection(connection);
    LOG.info("error = " + desc.validator.error);
    LOG.info("input = " + desc.validator.input);
    desc.validator.validate();
}

From source file:org.apache.sqoop.integration.server.rest.RestTest.java

License:Apache License

@Test
public void test() throws Exception {
    LOG.info("Start: " + getTestName());

    URL url = new URL(getSqoopServerUrl() + desc.rest);
    HttpURLConnection connection = new DelegationTokenAuthenticatedURL().openConnection(url,
            new DelegationTokenAuthenticatedURL.Token());
    connection.setRequestMethod(desc.method);

    if (desc.data != null) {
        connection.setDoOutput(true);/*w  w w  .  j  av a2  s.c o m*/

        byte[] byteData = desc.data.getBytes(Charset.forName("UTF-8"));
        connection.setRequestProperty("Content-Length", Integer.toString(byteData.length));
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.write(byteData, 0, byteData.length);
        wr.flush();
        wr.close();
    }

    desc.validator.setConnection(connection);
    LOG.info("error = " + desc.validator.error);
    LOG.info("input = " + desc.validator.input);
    desc.validator.validate();

    LOG.info("End: " + getTestName());
}