Exemplo n.º 1
0
  protected void initAuthentication() {
    if (registration.getAuthenticationMechanism() != null) {
      if (registration.getAuthenticationMechanism().getType() instanceof BasicAuth) {
        BasicAuth basic = (BasicAuth) registration.getAuthenticationMechanism().getType();
        UsernamePasswordCredentials creds =
            new UsernamePasswordCredentials(basic.getUsername(), basic.getPassword());
        AuthScope authScope = new AuthScope(AuthScope.ANY);
        ((DefaultHttpClient) client).getCredentialsProvider().setCredentials(authScope, creds);

        localContext = new BasicHttpContext();

        // Generate BASIC scheme object and stick it to the local execution context
        BasicScheme basicAuth = new BasicScheme();
        localContext.setAttribute("preemptive-auth", basicAuth);

        // Add as the first request interceptor
        ((DefaultHttpClient) client).addRequestInterceptor(new PreemptiveAuth(), 0);
        executor.setHttpContext(localContext);
      }
    }
  }