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

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

import static com.covisint.platform.oauth.core.SupportedMediaType.CERTIFICATE_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.client.granttype.clientsecret.ClientSecretGrantTypeClientImpl;
import com.covisint.platform.oauth.core.domain.CertificateGrantType;
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 CertificateGrantType}. */
public class CertificateGrantTypeClientImpl extends BaseResourceClient implements
        CertificateGrantTypeClient {

    /** Media type for the certificate grant type resource. */
    private static final MediaType CERTIFICATE_GRANT_V1_MEDIA_TYPE = MediaType.parse(CERTIFICATE_GRANT_MT.value());

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

    /** Collection URI template for the get certificate grant type end point. */
    private UriTemplate getCertificateGrantTypeUriTemplate;

    /** Collection URI template for the search certificate grant type end point. */
    private UriTemplate searchCertificateGrantTypeUriTemplate;

    /** Collection URI template for the post certificate grant type end point. */
    private UriTemplate postCertificateGrantTypeUriTemplate;

    /** Collection URI template for the delete certificate grant type end point. */
    private UriTemplate deleteCertificateGrantTypeUriTemplate;

    /**
     * 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);
        String authorization = (String) httpContext.getAttribute("Authorization");
        if (authorization != null) {
            get.setHeader("Authorization", authorization);
        }
        get.setHeader("type", "certificate");
        return get;
    }

    /**
     * Sets the URI template for the get certificate grant type end point.
     * 
     * @param template The URI template for the get certificate grant type end point.
     */
    void setGetCertificateGrantTypeUriTemplate(@Nonnull UriTemplate template) {
        getCertificateGrantTypeUriTemplate = template;
    }

    /**
     * Sets the URI template for the search certificate grant type end point.
     * 
     * @param template The URI template for the search certificate grant type end point.
     */
    void setSearchCertificateGrantTypeUriTemplate(@Nonnull UriTemplate template) {
        searchCertificateGrantTypeUriTemplate = template;
    }

    /**
     * Sets the URI template for the add certificate grant type end point.
     * 
     * @param template The URI template for the add certificate grant type end point.
     */
    void setPostCertificateGrantTypeUriTemplate(@Nonnull UriTemplate template) {
        postCertificateGrantTypeUriTemplate = template;
    }

    /**
     * Sets the URI template for the delete certificate grant type end point.
     * 
     * @param template The URI template for the delete certificate grant type end point.
     */
    void setDeleteCertificateGrantTypeUriTemplate(@Nonnull UriTemplate template) {
        deleteCertificateGrantTypeUriTemplate = 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 CertificateGrantType cachedResult = (CertificateGrantType) getLocalCache().getIfPresent(cacheKey);

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

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

            final CheckedFuture futureResult =
                    execute(new HttpGet(url), CERTIFICATE_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 = searchCertificateGrantTypeUriTemplate.set("subjectId", subjectId).expand();

            final CheckedFuture, ServiceException> futureResult =
                    execute(getHttpGet(getEndpointWithQueryParams(searchCriteria, sortCriteria, page, url), context),
                            CERTIFICATE_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 CertificateGrantType certificateGrantType, @Nonnull HttpContext httpContext) {
        try {
            final String url = postCertificateGrantTypeUriTemplate.set("subjectId", subjectId).expand();

            return executeWithBody(new HttpPost(url), CERTIFICATE_GRANT_V1_MEDIA_TYPE, certificateGrantType,
                    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 =
                    deleteCertificateGrantTypeUriTemplate.set("subjectId", subjectId).set("grantTypeId", grantTypeId)
                            .expand();

            return execute(new HttpDelete(url), CERTIFICATE_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(CertificateGrantType resource,
            HttpContext httpContext) {
        throw new UnsupportedOperationException();
    }

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

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

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

    /** {@inheritDoc} */
    public CheckedFuture persist(CertificateGrantType 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