com.covisint.platform.oauth.client.token.sdk.DefaultAuthConfigurationProvider 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 java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import com.covisint.core.support.constraint.Constraints;
import com.covisint.core.support.constraint.Nonnull;
import com.covisint.core.support.constraint.NotEmpty;
import com.covisint.core.support.constraint.Nullable;
import com.covisint.core.support.primitive.StringSupport;
/**
* Implementation of a {@link AuthConfigurationProvider} that reads from a standard properties file located within the
* classpath.
*/
public final class DefaultAuthConfigurationProvider extends AbstractClasspathPropertyLoader implements
AuthConfigurationProvider {
/** The name of the client properties file. */
private static final String DEFAULT_FILE_NAME = "client.conf";
/** The application id. */
private String applicationId;
/** The client id. */
private String clientId;
/** The client secret. */
private String clientSecret;
/** The auth service base URL. */
private URL authServiceBaseUrl;
/** Initializing constructor. */
public DefaultAuthConfigurationProvider() {
loadCredentials();
}
/** Load credentials from the loaded properties. */
private void loadCredentials() {
final Properties props = getProperties();
applicationId = getRequiredProperty(props, "applicationId");
clientId = getRequiredProperty(props, "clientId");
clientSecret = getRequiredProperty(props, "clientSecret");
try {
authServiceBaseUrl = new URL(getRequiredProperty(props, "authServiceBaseUrl"));
} catch (MalformedURLException e) {
throw new IllegalStateException("Bad auth service URL", e);
}
}
/**
* Returns a required property from the given properties. Throw a constraint violation exception if it doesn't exist
* or is null.
*
* @param props The properties to load.
* @param propertyName The name of the property to load.
* @return Returns a required property from the given properties. Throw a constraint violation exception if it
* doesn't exist or is null.
*/
private String getRequiredProperty(Properties props, String propertyName) {
return Constraints.isNotNull(StringSupport.trimOrNull(props.getProperty(propertyName)), propertyName
+ " property not set.");
}
/** {@inheritDoc} */
@Nullable public String getApplicationId() {
return applicationId;
}
/** {@inheritDoc} */
@Nonnull @NotEmpty public String getClientId() {
return clientId;
}
/** {@inheritDoc} */
@Nonnull @NotEmpty public String getClientSecret() {
return clientSecret;
}
/** {@inheritDoc} */
@Nonnull public URL getAuthServiceBaseUrl() {
return authServiceBaseUrl;
}
/** {@inheritDoc} */
@Nonnull @NotEmpty protected String getPropertyFileName() {
return DEFAULT_FILE_NAME;
}
}