com.google.api.client.http.HttpExecuteInterceptor Java Examples

The following examples show how to use com.google.api.client.http.HttpExecuteInterceptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpExecuteInterceptorChainTest.java    From client-encryption-java with MIT License 6 votes vote down vote up
@Test
public void testIntercept() throws IOException {

    // GIVEN
    HttpExecuteInterceptor interceptor1 = mock(HttpExecuteInterceptor.class);
    HttpExecuteInterceptor interceptor2 = mock(HttpExecuteInterceptor.class);
    List<HttpExecuteInterceptor> interceptors = Arrays.asList(interceptor1, interceptor2);
    HttpRequest request = mock(HttpRequest.class);

    // WHEN
    HttpExecuteInterceptorChain instanceUnderTest = new HttpExecuteInterceptorChain(interceptors);
    instanceUnderTest.intercept(request);

    // THEN
    verify(interceptor1).intercept(request);
    verify(interceptor2).intercept(request);
}
 
Example #2
Source File: AuthorizationFlow.java    From mirror with Apache License 2.0 6 votes vote down vote up
/**
 * @param method                        method of presenting the access token to the resource
 *                                      server (for example
 *                                      {@link BearerToken#authorizationHeaderAccessMethod})
 * @param transport                     HTTP transport
 * @param jsonFactory                   JSON factory
 * @param tokenServerUrl                token server URL
 * @param clientAuthentication          client authentication or {@code null} for
 *                                      none (see
 *                                      {@link TokenRequest#setClientAuthentication(HttpExecuteInterceptor)}
 *                                      )
 * @param clientId                      client identifier
 * @param authorizationServerEncodedUrl authorization server encoded URL
 */
public Builder(AccessMethod method,
               HttpTransport transport,
               JsonFactory jsonFactory,
               GenericUrl tokenServerUrl,
               HttpExecuteInterceptor clientAuthentication,
               String clientId,
               String authorizationServerEncodedUrl) {
    super(method,
            transport,
            jsonFactory,
            tokenServerUrl,
            clientAuthentication,
            clientId,
            authorizationServerEncodedUrl);
}
 
Example #3
Source File: AuthorizationFlow.java    From android-oauth-client with Apache License 2.0 6 votes vote down vote up
/**
 * @param method method of presenting the access token to the resource
 *            server (for example
 *            {@link BearerToken#authorizationHeaderAccessMethod})
 * @param transport HTTP transport
 * @param jsonFactory JSON factory
 * @param tokenServerUrl token server URL
 * @param clientAuthentication client authentication or {@code null} for
 *            none (see
 *            {@link TokenRequest#setClientAuthentication(HttpExecuteInterceptor)}
 *            )
 * @param clientId client identifier
 * @param authorizationServerEncodedUrl authorization server encoded URL
 */
public Builder(AccessMethod method,
        HttpTransport transport,
        JsonFactory jsonFactory,
        GenericUrl tokenServerUrl,
        HttpExecuteInterceptor clientAuthentication,
        String clientId,
        String authorizationServerEncodedUrl) {
    super(method,
            transport,
            jsonFactory,
            tokenServerUrl,
            clientAuthentication,
            clientId,
            authorizationServerEncodedUrl);
}
 
Example #4
Source File: AuthorizationCodeFlow.java    From google-oauth-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * @param method method of presenting the access token to the resource server (for example
 *        {@link BearerToken#authorizationHeaderAccessMethod})
 * @param transport HTTP transport
 * @param jsonFactory JSON factory
 * @param tokenServerUrl token server URL
 * @param clientAuthentication client authentication or {@code null} for none (see
 *        {@link TokenRequest#setClientAuthentication(HttpExecuteInterceptor)})
 * @param clientId client identifier
 * @param authorizationServerEncodedUrl authorization server encoded URL
 */
public Builder(AccessMethod method,
    HttpTransport transport,
    JsonFactory jsonFactory,
    GenericUrl tokenServerUrl,
    HttpExecuteInterceptor clientAuthentication,
    String clientId,
    String authorizationServerEncodedUrl) {
  setMethod(method);
  setTransport(transport);
  setJsonFactory(jsonFactory);
  setTokenServerUrl(tokenServerUrl);
  setClientAuthentication(clientAuthentication);
  setClientId(clientId);
  setAuthorizationServerEncodedUrl(authorizationServerEncodedUrl);
}
 
Example #5
Source File: AuthorizationCodeFlow.java    From google-oauth-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * @param method method of presenting the access token to the resource server (for example
 *        {@link BearerToken#authorizationHeaderAccessMethod})
 * @param transport HTTP transport
 * @param jsonFactory JSON factory
 * @param tokenServerUrl token server URL
 * @param clientAuthentication client authentication or {@code null} for none (see
 *        {@link TokenRequest#setClientAuthentication(HttpExecuteInterceptor)})
 * @param clientId client identifier
 * @param authorizationServerEncodedUrl authorization server encoded URL
 *
 * @since 1.14
 */
public AuthorizationCodeFlow(AccessMethod method,
    HttpTransport transport,
    JsonFactory jsonFactory,
    GenericUrl tokenServerUrl,
    HttpExecuteInterceptor clientAuthentication,
    String clientId,
    String authorizationServerEncodedUrl) {
  this(new Builder(method,
      transport,
      jsonFactory,
      tokenServerUrl,
      clientAuthentication,
      clientId,
      authorizationServerEncodedUrl));
}
 
Example #6
Source File: MicrosoftAuthorizationCodeFlow.java    From codenvy with Eclipse Public License 1.0 5 votes vote down vote up
public Builder(
    Credential.AccessMethod method,
    HttpTransport transport,
    JsonFactory jsonFactory,
    GenericUrl tokenServerUrl,
    HttpExecuteInterceptor clientAuthentication,
    String clientId,
    String authorizationServerEncodedUrl) {
  super(
      method,
      transport,
      jsonFactory,
      tokenServerUrl,
      clientAuthentication,
      clientId,
      authorizationServerEncodedUrl);
}
 
Example #7
Source File: GoogleAuthorizationCodeTokenRequest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
@Override
public GoogleAuthorizationCodeTokenRequest setClientAuthentication(
    HttpExecuteInterceptor clientAuthentication) {
  Preconditions.checkNotNull(clientAuthentication);
  return (GoogleAuthorizationCodeTokenRequest) super.setClientAuthentication(
      clientAuthentication);
}
 
Example #8
Source File: OAuth2HelperTest.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  credential =
      new Credential.Builder(BearerToken.authorizationHeaderAccessMethod())
          .setTransport(new NetHttpTransport())
          .setJsonFactory(Mockito.mock(JsonFactory.class))
          .setClientAuthentication(Mockito.mock(HttpExecuteInterceptor.class))
          .setTokenServerUrl(TOKEN_SERVER_URL).build();
  oAuth2Helper = spy(new OAuth2Helper(libLogger, REFRESH_WINDOW_SECS));
}
 
Example #9
Source File: CoopLockRepairIntegrationTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
private static HttpRequestInitializer newFailingRequestInitializer(
    Predicate<HttpRequest> failurePredicate) {
  return request -> {
    httpRequestInitializer.initialize(request);
    HttpExecuteInterceptor executeInterceptor = checkNotNull(request.getInterceptor());
    request.setInterceptor(
        interceptedRequest -> {
          executeInterceptor.intercept(interceptedRequest);
          if (failurePredicate.test(interceptedRequest)) {
            throw new RuntimeException("Injected failure");
          }
        });
  };
}
 
Example #10
Source File: ChainingHttpRequestInitializer.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(HttpRequest request) throws IOException {
  List<HttpIOExceptionHandler> ioExceptionHandlers = new ArrayList<>();
  List<HttpUnsuccessfulResponseHandler> unsuccessfulResponseHandlers = new ArrayList<>();
  List<HttpExecuteInterceptor> interceptors = new ArrayList<>();
  List<HttpResponseInterceptor> responseInterceptors = new ArrayList<>();
  for (HttpRequestInitializer initializer : initializers) {
    initializer.initialize(request);
    if (request.getIOExceptionHandler() != null) {
      ioExceptionHandlers.add(request.getIOExceptionHandler());
      request.setIOExceptionHandler(null);
    }
    if (request.getUnsuccessfulResponseHandler() != null) {
      unsuccessfulResponseHandlers.add(request.getUnsuccessfulResponseHandler());
      request.setUnsuccessfulResponseHandler(null);
    }
    if (request.getInterceptor() != null) {
      interceptors.add(request.getInterceptor());
      request.setInterceptor(null);
    }
    if (request.getResponseInterceptor() != null) {
      responseInterceptors.add(request.getResponseInterceptor());
      request.setResponseInterceptor(null);
    }
  }
  request.setIOExceptionHandler(
      makeIoExceptionHandler(ioExceptionHandlers));
  request.setUnsuccessfulResponseHandler(
      makeUnsuccessfulResponseHandler(unsuccessfulResponseHandlers));
  request.setInterceptor(
      makeInterceptor(interceptors));
  request.setResponseInterceptor(
      makeResponseInterceptor(responseInterceptors));
}
 
Example #11
Source File: ChainingHttpRequestInitializer.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
private HttpExecuteInterceptor makeInterceptor(
    final Iterable<HttpExecuteInterceptor> interceptors) {
  return new HttpExecuteInterceptor() {
    @Override
    public void intercept(HttpRequest request) throws IOException {
      for (HttpExecuteInterceptor interceptor : interceptors) {
        interceptor.intercept(request);
      }
    }
  };
}
 
Example #12
Source File: CoopLockIntegrationTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
private static HttpRequestInitializer interceptingRequestInitializer(
    Consumer<HttpRequest> interceptFn) {
  return request -> {
    httpRequestInitializer.initialize(request);
    HttpExecuteInterceptor executeInterceptor = checkNotNull(request.getInterceptor());
    request.setInterceptor(
        interceptedRequest -> {
          executeInterceptor.intercept(interceptedRequest);
          interceptFn.accept(interceptedRequest);
        });
  };
}
 
Example #13
Source File: TrackingHttpRequestInitializer.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(HttpRequest request) throws IOException {
  if (delegate != null) {
    delegate.initialize(request);
  }
  HttpExecuteInterceptor executeInterceptor = request.getInterceptor();
  request.setInterceptor(
      r -> {
        if (executeInterceptor != null) {
          executeInterceptor.intercept(r);
        }
        requests.add(r);
      });
}
 
Example #14
Source File: BatchRequest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void intercept(HttpRequest batchRequest) throws IOException {
  if (originalInterceptor != null) {
    originalInterceptor.intercept(batchRequest);
  }
  for (RequestInfo<?, ?> requestInfo : requestInfos) {
    HttpExecuteInterceptor interceptor = requestInfo.request.getInterceptor();
    if (interceptor != null) {
      interceptor.intercept(requestInfo.request);
    }
  }
}
 
Example #15
Source File: PasswordTokenRequest.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public PasswordTokenRequest setClientAuthentication(HttpExecuteInterceptor clientAuthentication) {
  return (PasswordTokenRequest) super.setClientAuthentication(clientAuthentication);
}
 
Example #16
Source File: Credential.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
/** Returns the client authentication or {@code null} for none. */
public final HttpExecuteInterceptor getClientAuthentication() {
  return clientAuthentication;
}
 
Example #17
Source File: TokenRequest.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
/** Returns the client authentication or {@code null} for none. */
public final HttpExecuteInterceptor getClientAuthentication() {
  return clientAuthentication;
}
 
Example #18
Source File: AuthorizationFlow.java    From android-oauth-client with Apache License 2.0 4 votes vote down vote up
@Override
public Builder setClientAuthentication(HttpExecuteInterceptor clientAuthentication) {
    return (Builder) super.setClientAuthentication(clientAuthentication);
}
 
Example #19
Source File: RefreshTokenRequest.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public RefreshTokenRequest setClientAuthentication(HttpExecuteInterceptor clientAuthentication) {
  return (RefreshTokenRequest) super.setClientAuthentication(clientAuthentication);
}
 
Example #20
Source File: ClientCredentialsTokenRequest.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public ClientCredentialsTokenRequest setClientAuthentication(
    HttpExecuteInterceptor clientAuthentication) {
  return (ClientCredentialsTokenRequest) super.setClientAuthentication(clientAuthentication);
}
 
Example #21
Source File: AuthorizationCodeFlow.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a new instance of an authorization code token request based on the given authorization
 * code.
 *
 * <p>
 * This is used to make a request for an access token using the authorization code. It uses
 * {@link #getTransport()}, {@link #getJsonFactory()}, {@link #getTokenServerEncodedUrl()},
 * {@link #getClientAuthentication()}, {@link #getRequestInitializer()}, and {@link #getScopes()}.
 * </p>
 *
 * <pre>
static TokenResponse requestAccessToken(AuthorizationCodeFlow flow, String code)
    throws IOException, TokenResponseException {
  return flow.newTokenRequest(code).setRedirectUri("https://client.example.com/rd").execute();
}
 * </pre>
 *
 * @param authorizationCode authorization code.
 */
public AuthorizationCodeTokenRequest newTokenRequest(String authorizationCode) {
  HttpExecuteInterceptor pkceClientAuthenticationWrapper = new HttpExecuteInterceptor() {
    @Override
    public void intercept(HttpRequest request) throws IOException {
      clientAuthentication.intercept(request);
      if (pkce != null) {
        Map<String, Object> data = Data.mapOf(UrlEncodedContent.getContent(request).getData());
        data.put("code_verifier", pkce.getVerifier());
      }
    }
  };

  return new AuthorizationCodeTokenRequest(transport, jsonFactory,
      new GenericUrl(tokenServerEncodedUrl), authorizationCode).setClientAuthentication(
      pkceClientAuthenticationWrapper).setRequestInitializer(requestInitializer).setScopes(scopes);
}
 
Example #22
Source File: OAuthHmacCredential.java    From android-oauth-client with Apache License 2.0 4 votes vote down vote up
@Override
public Builder setClientAuthentication(HttpExecuteInterceptor clientAuthentication) {
    return (Builder) super.setClientAuthentication(clientAuthentication);
}
 
Example #23
Source File: HttpExecuteInterceptorChain.java    From client-encryption-java with MIT License 4 votes vote down vote up
public HttpExecuteInterceptorChain(List<HttpExecuteInterceptor> requestInterceptors) {
    this.requestInterceptors = requestInterceptors;
}
 
Example #24
Source File: GoogleAuthorizationCodeFlow.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * @since 1.11
 */
@Override
public Builder setClientAuthentication(HttpExecuteInterceptor clientAuthentication) {
  return (Builder) super.setClientAuthentication(clientAuthentication);
}
 
Example #25
Source File: HttpExecuteInterceptorChain.java    From client-encryption-java with MIT License 4 votes vote down vote up
@Override
public void intercept(HttpRequest request) throws IOException {
    for (HttpExecuteInterceptor interceptor: requestInterceptors) {
        interceptor.intercept(request);
    }
}
 
Example #26
Source File: AuthorizationFlow.java    From mirror with Apache License 2.0 4 votes vote down vote up
@Override
public Builder setClientAuthentication(HttpExecuteInterceptor clientAuthentication) {
    return (Builder) super.setClientAuthentication(clientAuthentication);
}
 
Example #27
Source File: MicrosoftAuthorizationCodeTokenRequest.java    From codenvy with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public MicrosoftAuthorizationCodeTokenRequest setClientAuthentication(
    HttpExecuteInterceptor clientAuthentication) {
  Preconditions.checkNotNull(clientAuthentication);
  return (MicrosoftAuthorizationCodeTokenRequest)
      super.setClientAuthentication(clientAuthentication);
}
 
Example #28
Source File: OAuth2CredentialsTest.java    From rides-java-sdk with MIT License 4 votes vote down vote up
@Test
public void useCustomDataStore() throws Exception {
    Credential credential = new Credential.Builder(BearerToken.authorizationHeaderAccessMethod())
            .setTransport(new MockHttpTransport())
            .setJsonFactory(new MockJsonFactory())
            .setClientAuthentication(Mockito.mock(HttpExecuteInterceptor.class))
            .setTokenServerUrl(new GenericUrl(TOKEN_REQUEST_URL))
            .build();

    credential.setAccessToken("accessToken2");
    credential.setRefreshToken("refreshToken2");
    credential.setExpiresInSeconds(1000L);

    DataStore mockDataStore = Mockito.mock(DataStore.class);
    MockDataStoreFactory mockDataStoreFactory = new MockDataStoreFactory(mockDataStore);
    Mockito.when(mockDataStore.get(eq("userId"))).thenReturn(new StoredCredential(credential));

    OAuth2Credentials oAuth2Credentials = new OAuth2Credentials.Builder()
            .setClientSecrets("CLIENT_ID", "CLIENT_SECRET")
            .setRedirectUri("http://redirect")
            .setHttpTransport(mockHttpTransport)
            .setCredentialDataStoreFactory(mockDataStoreFactory)
            .setScopes(Arrays.asList(Scope.PROFILE, Scope.REQUEST))
            .build();

    Credential storedCredential = oAuth2Credentials.authenticate("authorizationCode", "userId");
    Credential loadedCredential = oAuth2Credentials.loadCredential("userId");

    assertEquals("Refresh token does not match.", "refreshToken", storedCredential.getRefreshToken());
    assertTrue("Expected expires_in between 0 and 3600. Was actually: " + storedCredential.getExpiresInSeconds(),
            storedCredential.getExpiresInSeconds() > 0 && storedCredential.getExpiresInSeconds() <= 3600);
    assertEquals("Access token does not match.", "accessToken", storedCredential.getAccessToken());
    assertEquals("Access method (Bearer) does not match",
            BearerToken.authorizationHeaderAccessMethod().getClass(), storedCredential.getMethod().getClass());

    assertEquals("Refresh token does not match.", "refreshToken2", loadedCredential.getRefreshToken());
    assertTrue("Expected expires_in between 0 and 1000. Was actually: " + loadedCredential.getExpiresInSeconds(),
            loadedCredential.getExpiresInSeconds() > 0 && loadedCredential.getExpiresInSeconds() <= 1000L);
    assertEquals("Access token does not match.", "accessToken2", loadedCredential.getAccessToken());
    assertEquals("Access method (Bearer) does not match",
            BearerToken.authorizationHeaderAccessMethod().getClass(), loadedCredential.getMethod().getClass());
}
 
Example #29
Source File: ComputeCredential.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public Builder setClientAuthentication(HttpExecuteInterceptor clientAuthentication) {
  Preconditions.checkArgument(clientAuthentication == null);
  return this;
}
 
Example #30
Source File: AuthorizationCodeTokenRequest.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public AuthorizationCodeTokenRequest setClientAuthentication(
    HttpExecuteInterceptor clientAuthentication) {
  return (AuthorizationCodeTokenRequest) super.setClientAuthentication(clientAuthentication);
}