de.elomagic.mag.google.DriveService.java Source code

Java tutorial

Introduction

Here is the source code for de.elomagic.mag.google.DriveService.java

Source

/*
 * Mail Attachment Gateway
 * Copyright (c) 2016-2017 Carsten Rambow
 * mailto:developer AT elomagic DOT de
 *
 * 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 de.elomagic.mag.google;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.drive.Drive;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Carsten Rambow
 */
public final class DriveService {

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

    private DriveService() {
    }

    /**
     * Build and return an authorized Drive client service.
     *
     * @param credential
     * @return Returns an authorized Drive client service or null when something went wrong
     */
    public static Drive getDriveService(final Credential credential) {

        Drive result = null;

        try {
            HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();//new NetHttpTransport();
            result = new Drive.Builder(httpTransport, JacksonFactory.getDefaultInstance(), credential)
                    .setApplicationName("MailAttachmentGateway").build();
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }

        return result;

    }
}