Esempio n. 1
0
 @Before
 public void setup() {
   DefaultClientConfig config = new DefaultClientConfig();
   config.getClasses().add(FeedProvider.class);
   config.getClasses().add(JSONRootElementProvider.App.class);
   client = CacheableClient.create(config);
   httpClient = new HttpClient(client, "localhost", 20090);
 }
  public static URLConnectionClientHandler getClientConnectionHandler(
      DefaultClientConfig config,
      PropertiesConfiguration clientConfig,
      final String doAsUser,
      final UserGroupInformation ugi) {
    config
        .getProperties()
        .put(URLConnectionClientHandler.PROPERTY_HTTP_URL_CONNECTION_SET_METHOD_WORKAROUND, true);
    Configuration conf = new Configuration();
    conf.addResource(conf.get(SSLFactory.SSL_CLIENT_CONF_KEY, "ssl-client.xml"));
    UserGroupInformation.setConfiguration(conf);
    final ConnectionConfigurator connConfigurator = newConnConfigurator(conf);
    String authType = "simple";
    if (clientConfig != null) {
      authType = clientConfig.getString("atlas.http.authentication.type", "simple");
    }
    Authenticator authenticator = new PseudoDelegationTokenAuthenticator();
    if (!authType.equals("simple")) {
      authenticator = new KerberosDelegationTokenAuthenticator();
    }
    authenticator.setConnectionConfigurator(connConfigurator);
    final DelegationTokenAuthenticator finalAuthenticator =
        (DelegationTokenAuthenticator) authenticator;
    final DelegationTokenAuthenticatedURL.Token token = new DelegationTokenAuthenticatedURL.Token();
    HttpURLConnectionFactory httpURLConnectionFactory = null;
    try {
      UserGroupInformation ugiToUse = ugi != null ? ugi : UserGroupInformation.getCurrentUser();
      final UserGroupInformation actualUgi =
          (ugiToUse.getAuthenticationMethod() == UserGroupInformation.AuthenticationMethod.PROXY)
              ? ugiToUse.getRealUser()
              : ugiToUse;
      LOG.info(
          "Real User: {}, is from ticket cache? {}", actualUgi, actualUgi.isLoginTicketBased());
      LOG.info("doAsUser: {}", doAsUser);
      httpURLConnectionFactory =
          new HttpURLConnectionFactory() {
            @Override
            public HttpURLConnection getHttpURLConnection(final URL url) throws IOException {
              try {
                return actualUgi.doAs(
                    new PrivilegedExceptionAction<HttpURLConnection>() {
                      @Override
                      public HttpURLConnection run() throws Exception {
                        try {
                          return new DelegationTokenAuthenticatedURL(
                                  finalAuthenticator, connConfigurator)
                              .openConnection(url, token, doAsUser);
                        } catch (Exception e) {
                          throw new IOException(e);
                        }
                      }
                    });
              } catch (Exception e) {
                if (e instanceof IOException) {
                  throw (IOException) e;
                } else {
                  throw new IOException(e);
                }
              }
            }
          };
    } catch (IOException e) {
      LOG.warn("Error obtaining user", e);
    }

    return new URLConnectionClientHandler(httpURLConnectionFactory);
  }