com.covisint.platform.oauth.client.token.sdk.TokenSDK Maven / Gradle / Ivy
/*
* Copyright 2015 Covisint
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.covisint.platform.oauth.client.token.sdk;
import org.apache.http.protocol.BasicHttpContext;
import com.covisint.core.http.service.core.ServiceException;
import com.covisint.core.support.constraint.Nonnull;
import com.covisint.core.support.constraint.NotEmpty;
import com.covisint.core.support.primitive.StringSupport;
import com.covisint.platform.oauth.client.token.TokenClientBuilder;
import com.covisint.platform.oauth.client.token.TokenClientFactory;
import com.covisint.platform.oauth.client.token.TokenClientImpl;
import com.covisint.platform.oauth.core.token.Token;
import com.google.common.util.concurrent.CheckedFuture;
/** Token SDK. */
public class TokenSDK extends TokenClientFactory {
/** A lazy-loaded configuration provider. */
private AuthConfigurationProvider authConfigProvider;
/**
* Constructor.
*
* @param serviceUrl the service url.
*/
public TokenSDK(@Nonnull @NotEmpty String serviceUrl) {
super(serviceUrl);
}
/**
* Method responsible for returning a {@link AuthConfigurationProvider}. The default implementation lazy-loads and
* returns a property file-based provider, which searches the classpath for the expected configuration file. This
* method may be overridden to allow custom providers.
*
* @return Returns a {@link AuthConfigurationProvider}.
*/
@Nonnull protected AuthConfigurationProvider getAuthConfigProvider() {
if (authConfigProvider == null) {
authConfigProvider = new DefaultAuthConfigurationProvider();
}
return authConfigProvider;
}
/** {@inheritDoc} */
protected TokenClientImpl buildResourceClient(TokenClientBuilder builder) {
final TokenClientImpl client = super.buildResourceClient(builder);
// Get the id of the application that will need to authorize calls with these tokens, and set into the client.
final String appId = getAuthConfigProvider().getApplicationId();
if (StringSupport.trimOrNull(appId) != null) {
client.setApplicationId(appId);
}
return client;
}
/** {@inheritDoc} */
public TokenClientImpl create() {
throw new UnsupportedOperationException("Please use #newClient to create new client instances.");
}
/**
* Creates a new token client.
*
* @return returns a new token client.
*/
@Nonnull public TokenClient newClient() {
return new ClientImpl(super.create());
}
/**
* Set a custom auth configuration provider via direct injection.
*
* @param provider The auth configuration provider to set.
* @return Returns this object.
*/
public TokenSDK setAuthConfigProvider(@Nonnull AuthConfigurationProvider provider) {
authConfigProvider = provider;
return this;
}
/** Client interface for the {@link Token} resource APIs. */
public interface TokenClient {
/**
* Exchange the given client credentials for an access token.
*
* @param clientId the sender's client id.
* @param clientSecret the sender's client secret.
* @return Returns a future providing a valid access token.
*/
@Nonnull CheckedFuture getToken(@Nonnull @NotEmpty String clientId,
@Nonnull @NotEmpty String clientSecret);
}
/** {@link TokenClient} implementation that wraps and delegates to {@link TokenClientImpl}. */
private static final class ClientImpl implements TokenClient {
/** The client delegate. */
private final TokenClientImpl delegate;
/**
* Constructor.
*
* @param newDelegate The client delegate.
*/
private ClientImpl(@Nonnull TokenClientImpl newDelegate) {
delegate = newDelegate;
}
/** {@inheritDoc} */
@Nonnull public CheckedFuture getToken(@Nonnull @NotEmpty String clientId,
@Nonnull @NotEmpty String clientSecret) {
return delegate.postAccessToken(clientId, clientSecret, new BasicHttpContext());
}
}
}