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

com.covisint.platform.oauth.client.granttype.clientsecret.ClientSecretGrantTypeClientImpl 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.granttype.clientsecret;

import static com.covisint.platform.oauth.core.SupportedMediaType.CLIENT_SECRET_GRANT_MT;

import java.util.List;

import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.covisint.core.http.service.client.BaseResourceClient;
import com.covisint.core.http.service.client.CachingFutureCallback;
import com.covisint.core.http.service.client.ClientException;
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.NotEmpty;
import com.covisint.platform.oauth.core.domain.ClientSecretGrantType;
import com.damnhandy.uri.template.UriTemplate;
import com.damnhandy.uri.template.VariableExpansionException;
import com.google.common.collect.Multimap;
import com.google.common.net.MediaType;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;

/** Basic implementation of a {@link ClientSecretGrantType}. */
public class ClientSecretGrantTypeClientImpl extends BaseResourceClient implements
        ClientSecretGrantTypeClient {

    /** Media type for the client secret grant type resource. */
    private static final MediaType CLIENT_SECRET_GRANT_V1_MEDIA_TYPE = MediaType.parse(CLIENT_SECRET_GRANT_MT.value());

    /** Class logger. */
    private final Logger log = LoggerFactory.getLogger(ClientSecretGrantTypeClientImpl.class);

    /** Collection URI template for the get client secret grant type end point. */
    private UriTemplate getClientSecretGrantTypeUriTemplate;

    /** Collection URI template for the search client secret grant type end point. */
    private UriTemplate searchClientSecretGrantTypeUriTemplate;

    /** Collection URI template for the post client secret grant type end point. */
    private UriTemplate postClientSecretGrantTypeUriTemplate;

    /** Collection URI template for the delete client secret grant type end point. */
    private UriTemplate deleteClientSecretGrantTypeUriTemplate;

    /**
     * Build HttpGet object with headers.
     * 
     * @param url The url.
     * @param httpContext The http context'
     * @return http get instance.
     */
    @Nonnull private HttpGet getHttpGet(@Nonnull @NotEmpty String url, @Nonnull HttpContext httpContext) {
        HttpGet get = new HttpGet(url);
        get.setHeader("type", "client_credentials");
        return get;
    }

    /**
     * Sets the URI template for the get client secret grant type end point.
     * 
     * @param template The URI template for the get client secret grant type end point.
     */
    void setGetClientSecretGrantTypeUriTemplate(@Nonnull UriTemplate template) {
        getClientSecretGrantTypeUriTemplate = template;
    }

    /**
     * Sets the URI template for the search client secret grant type end point.
     * 
     * @param template The URI template for the search client secret grant type end point.
     */
    void setSearchClientSecretGrantTypeUriTemplate(@Nonnull UriTemplate template) {
        searchClientSecretGrantTypeUriTemplate = template;
    }

    /**
     * Sets the URI template for the add client secret grant type end point.
     * 
     * @param template The URI template for the add client secret grant type end point.
     */
    void setPostClientSecretGrantTypeUriTemplate(@Nonnull UriTemplate template) {
        postClientSecretGrantTypeUriTemplate = template;
    }

    /**
     * Sets the URI template for the delete client secret grant type end point.
     * 
     * @param template The URI template for the delete client secret grant type end point.
     */
    void setDeleteClientSecretGrantTypeUriTemplate(@Nonnull UriTemplate template) {
        deleteClientSecretGrantTypeUriTemplate = template;
    }

    /** {@inheritDoc} */
    @Nonnull public CheckedFuture get(@Nonnull @NotEmpty String subjectId,
            @Nonnull @NotEmpty String grantTypeId, @Nonnull HttpContext httpContext) {

        final String cacheKey = String.format("get-by-id-subjectId:%s-grantTypeId:%s", subjectId, grantTypeId);

        final ClientSecretGrantType cachedResult = (ClientSecretGrantType) getLocalCache().getIfPresent(cacheKey);

        if (cachedResult != null) {
            log.debug("Found active cache for key {}; using it.", cacheKey);
            return Futures.immediateCheckedFuture(cachedResult);
        }

        try {
            final String url =
                    getClientSecretGrantTypeUriTemplate.set("subjectId", subjectId).set("grantTypeId", grantTypeId)
                            .expand();

            final CheckedFuture futureResult =
                    execute(new HttpGet(url), CLIENT_SECRET_GRANT_V1_MEDIA_TYPE, httpContext);

            Futures.addCallback(futureResult, new CachingFutureCallback(getLocalCache(),
                    cacheKey));

            return futureResult;

        } catch (final VariableExpansionException e) {
            log.error("Error occurred while expanding URL template.", e);
            throw new ClientException("Error occurred while expanding URL template.");
        }

    }

    /** {@inheritDoc} */
    @Nonnull public CheckedFuture, ServiceException> search(
            @Nonnull @NotEmpty String subjectId, @Nonnull Multimap searchCriteria,
            @Nonnull SortCriteria sortCriteria, @Nonnull Page page, @Nonnull HttpContext context) {

        final String cacheKey = buildSearchKey(searchCriteria, sortCriteria, page);

        final List cachedResult =
                (List) getLocalCache().getIfPresent(cacheKey);

        if (cachedResult != null) {
            log.debug("Found active cache for key {}; using it.", cacheKey);
            return Futures.immediateCheckedFuture(cachedResult);
        }
        try {
            final String url = searchClientSecretGrantTypeUriTemplate.set("subjectId", subjectId).expand();

            final CheckedFuture, ServiceException> futureResult =
                    execute(getHttpGet(getEndpointWithQueryParams(searchCriteria, sortCriteria, page, url), context),
                            CLIENT_SECRET_GRANT_V1_MEDIA_TYPE, context);

            Futures.addCallback(futureResult, new CachingFutureCallback>(getLocalCache(),
                    cacheKey));

            return futureResult;

        } catch (final VariableExpansionException e) {
            log.error("Error occurred while expanding URL template.", e);
            throw new ClientException("Error occurred while expanding URL template.");
        }

    }

    /** {@inheritDoc} */
    @Nonnull public CheckedFuture add(@Nonnull @NotEmpty String subjectId,
            @Nonnull ClientSecretGrantType clientSecretGrantType, @Nonnull HttpContext httpContext) {
        try {
            final String url = postClientSecretGrantTypeUriTemplate.set("subjectId", subjectId).expand();

            return executeWithBody(new HttpPost(url), CLIENT_SECRET_GRANT_V1_MEDIA_TYPE, clientSecretGrantType,
                    httpContext);
        } catch (final VariableExpansionException e) {
            log.error("Error occurred while expanding URL template.", e);
            throw new ClientException("Error occurred while expanding URL template.");
        }
    }

    /** {@inheritDoc} */
    @Nonnull public CheckedFuture delete(@Nonnull @NotEmpty String subjectId,
            @Nonnull @NotEmpty String grantTypeId, @Nonnull HttpContext httpContext) {
        try {
            final String url =
                    deleteClientSecretGrantTypeUriTemplate.set("subjectId", subjectId).set("grantTypeId", grantTypeId)
                            .expand();

            return execute(new HttpDelete(url), CLIENT_SECRET_GRANT_V1_MEDIA_TYPE, httpContext);
        } catch (final VariableExpansionException e) {
            log.error("Error occurred while expanding URL template.", e);
            throw new ClientException("Error occurred while expanding URL template.");
        }
    }

    /** {@inheritDoc} */
    public CheckedFuture add(ClientSecretGrantType resource,
            HttpContext httpContext) {
        throw new UnsupportedOperationException();
    }

    /** {@inheritDoc} */
    public CheckedFuture delete(String id, HttpContext httpContext) {
        throw new UnsupportedOperationException();
    }

    /** {@inheritDoc} */
    public CheckedFuture get(ClientSecretGrantType resource,
            HttpContext httpContext) {
        throw new UnsupportedOperationException();
    }

    /** {@inheritDoc} */
    public CheckedFuture get(String id, HttpContext httpContext) {
        throw new UnsupportedOperationException();
    }

    /** {@inheritDoc} */
    public CheckedFuture persist(ClientSecretGrantType resource,
            HttpContext httpContext) {
        throw new UnsupportedOperationException();
    }

    /** {@inheritDoc} */
    public CheckedFuture, ServiceException> search(Multimap searchCriteria,
            SortCriteria sortCriteria, Page page, HttpContext httpContext) {
        throw new UnsupportedOperationException();
    }

    /** {@inheritDoc} */
    public CheckedFuture, ServiceException> search(Multimap searchCriteria,
            SortCriteria sortCriteria, Page page, HttpContext httpContext, MediaType mediaType,
            String searchResourcesEndPoint) {
        throw new UnsupportedOperationException();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy