com.mtnfog.idyl.e3.sdk.IdylE3ClientFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.mtnfog.idyl.e3.sdk.IdylE3ClientFactory.java

Source

/* Copyright 2017 Mountain Fog, Inc.
    
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.mtnfog.idyl.e3.sdk;

import org.apache.logging.log4j.Logger;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.apache.logging.log4j.LogManager;

import com.google.gson.Gson;
import com.mtnfog.idyl.e3.sdk.interceptors.HmacSha512v1SigningInterceptor;
import com.mtnfog.idyl.e3.sdk.model.AuthenticationMethod;
import com.squareup.okhttp.OkHttpClient;

import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.RestAdapter.Log;
import retrofit.client.OkClient;
import retrofit.converter.GsonConverter;

/**
 * Creates Idyl E3 clients.
 * 
 * @author Mountain Fog, Inc.
 *
 */
public class IdylE3ClientFactory {

    private static final Logger LOGGER = LogManager.getLogger(IdylE3ClientFactory.class);

    private IdylE3ClientFactory() {

    }

    /**
     * Gets an {@link IdylE3Client client}.
     * @param endpoint The Idyl E3 API endpoint.
     * @return A {@link IdylE3Client client}.
     */
    public static IdylE3Client getIdylE3Client(String endpoint) {

        return getRestAdapter(endpoint).create(IdylE3Client.class);

    }

    public static IdylE3Client getIdylE3Client(String endpoint, String apiKey, AuthenticationMethod method) {

        return getAuthenticatedRestAdapter(endpoint, apiKey, method).create(IdylE3Client.class);

    }

    public static RestAdapter getAuthenticatedRestAdapter(String endpoint, String apiKey) {

        return getAuthenticatedRestAdapter(endpoint, apiKey, AuthenticationMethod.PLAIN);

    }

    private static RestAdapter getAuthenticatedRestAdapter(String endpoint, String apiKey,
            AuthenticationMethod method) {

        final String requestDate = getRequestDate();

        RequestInterceptor requestInterceptor = new RequestInterceptor() {

            @Override
            public void intercept(RequestFacade request) {

                request.addHeader("Content-Type", "application/json");
                request.addHeader("Accept", "application/json");
                request.addHeader("x-idyle3-date", requestDate);

            }

        };

        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.interceptors().add(new HmacSha512v1SigningInterceptor(apiKey, method, requestDate));

        RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(endpoint)
                .setConverter(new GsonConverter(new Gson())).setLogLevel(RestAdapter.LogLevel.FULL)
                .setRequestInterceptor(requestInterceptor).setClient(new OkClient(okHttpClient)).setLog(new Log() {

                    @Override
                    public void log(String message) {
                        LOGGER.debug(message);
                    }

                }).build();

        return restAdapter;

    }

    private static RestAdapter getRestAdapter(String endpoint) {

        RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(endpoint)
                .setConverter(new GsonConverter(new Gson())).setLogLevel(RestAdapter.LogLevel.FULL)
                .setLog(new Log() {

                    @Override
                    public void log(String message) {
                        LOGGER.debug(message);
                    }

                }).build();

        return restAdapter;

    }

    private static String getRequestDate() {

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        return dateFormat.format(new Date());

    }

}