All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.covisint.platform.oauth.client.sdk.SubjectSDK 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.sdk;

import java.util.List;

import org.apache.http.protocol.BasicHttpContext;

import com.covisint.core.http.service.core.Page;
import com.covisint.core.http.service.core.ServiceException;
import com.covisint.core.http.service.core.SortCriteria;
import com.covisint.core.support.constraint.Nonnull;
import com.covisint.core.support.constraint.NonnullElements;
import com.covisint.core.support.constraint.NotEmpty;
import com.covisint.core.support.httpclient.HttpClientBuilder;
import com.covisint.platform.oauth.client.subject.SubjectClientFactory;
import com.covisint.platform.oauth.client.subject.SubjectClientImpl;
import com.covisint.platform.oauth.client.token.sdk.AuthConfigurationProvider;
import com.covisint.platform.oauth.client.token.sdk.DefaultAuthConfigurationProvider;
import com.covisint.platform.oauth.client.token.sdk.TokenSupplier;
import com.covisint.platform.oauth.core.domain.Subject;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.util.concurrent.CheckedFuture;

/** Subject SDK. */
public class SubjectSDK extends SubjectClientFactory {

    /** A lazy-loaded configuration provider. */
    private AuthConfigurationProvider authConfigProvider;

    /**
     * Constructor.
     * 
     * @param serviceUrl the service url.
     */
    public SubjectSDK(@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 HttpClientBuilder populateHttpClientBuilder(HttpClientBuilder builder) {
        final Supplier tokenSupplier = new TokenSupplier(getAuthConfigProvider());
        builder.addAuthorizationTokenInterceptor(tokenSupplier);
        return builder;
    }

    /**
     * Sets the auth configuration provider for this SDK to use.
     * 
     * @param provider The {@link AuthConfigurationProvider} to set.
     * @return Returns this object.
     */
    public SubjectSDK setAuthConfigProvider(@Nonnull AuthConfigurationProvider provider) {
        authConfigProvider = provider;
        return this;
    }

    /** {@inheritDoc} */
    public SubjectClientImpl create() {
        throw new UnsupportedOperationException("Please use #newClient to create new client instances.");
    }

    /**
     * Creates a new subject client.
     * 
     * @return returns a new subject client.
     */
    @Nonnull public SubjectClient newClient() {
        return new ClientImpl(super.create());
    }

    /** Client interface for the {@link Subject} resource APIs. */
    public interface SubjectClient {

        /**
         * Delete a subject by id.
         * 
         * @param subjectId The id of the subject to delete.
         * @return Returns a future which, when complete, indicates that the subject was successfully deleted.
         */
        @Nonnull CheckedFuture delete(@Nonnull @NotEmpty String subjectId);

        /**
         * Updates the given subject on the server. If the subject doesn't exist yet, it will be created with the given
         * id.
         * 
         * @param subject The subject to persist. The id field must be populated.
         * @return Returns a future producing the updated subject. This object contains the new version of the subject
         *         resource.
         */
        @Nonnull CheckedFuture update(@Nonnull Subject subject);

        /**
         * Retrieves a subject by its id.
         * 
         * @param subjectId The id of the subject to retrieve.
         * @return Returns a future producing the subject retrieved for the given id, or null if no matching group was
         *         found.
         */
        @Nonnull CheckedFuture get(@Nonnull @NotEmpty String subjectId);

        /**
         * Searches for subjects.
         * 
         * @param subjectIds The subject ids to search for. Pass null to search across all subjects.
         * @param page The pagination criteria to apply. Pass {@link Page#DEFAULT} to return the standard result
         *            collection.
         * @return Returns a future producing the iterable collection of subjects.
         */
        @Nonnull CheckedFuture, ServiceException> search(
                @Nonnull @NonnullElements List subjectIds, @Nonnull Page page);

    }

    /** {@link SubjectClient} implementation that wraps and delegates to {@link SubjectClientImpl}. */
    private static final class ClientImpl implements SubjectClient {

        /** The client delegate. */
        private final SubjectClientImpl delegate;

        /**
         * Constructor.
         *
         * @param newDelegate The client delegate.
         */
        private ClientImpl(@Nonnull SubjectClientImpl newDelegate) {
            delegate = newDelegate;
        }

        /** {@inheritDoc} */
        @Nonnull public CheckedFuture delete(@Nonnull @NotEmpty String subjectId) {
            return delegate.delete(subjectId, new BasicHttpContext());
        }

        /** {@inheritDoc} */
        @Nonnull public CheckedFuture update(@Nonnull Subject subject) {
            return delegate.persist(subject, new BasicHttpContext());
        }

        /** {@inheritDoc} */
        @Nonnull public CheckedFuture get(@Nonnull @NotEmpty String subjectId) {
            return delegate.get(subjectId, new BasicHttpContext());
        }

        /** {@inheritDoc} */
        @Nonnull public CheckedFuture, ServiceException> search(List subjectIds,
                @Nonnull Page page) {

            final Multimap searchCriteria = ArrayListMultimap. create();
            searchCriteria.putAll("id", Iterables.filter(subjectIds, Predicates.notNull()));

            return delegate.search(searchCriteria, SortCriteria.NONE, page, new BasicHttpContext());
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy