Java tutorial
/* * 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 java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Arrays; import java.util.Collection; import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.drive.Drive; import com.google.api.services.drive.DriveScopes; import org.apache.camel.component.google.drive.GoogleDriveClientFactory; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import de.elomagic.mag.Configuration; import de.elomagic.mag.Configuration.ConfigurationKey; import de.elomagic.mag.processors.PrepareGoogleDriveFileCreateProcessor; /** * * @author Carsten Rambow */ public class BatchGoogleDriveClientFactory implements GoogleDriveClientFactory { private static final Logger LOGGER = LogManager.getLogger(PrepareGoogleDriveFileCreateProcessor.class); private NetHttpTransport httpTransport; public BatchGoogleDriveClientFactory() { try { this.httpTransport = GoogleNetHttpTransport.newTrustedTransport();//new NetHttpTransport(); } catch (Exception ex) { LOGGER.error(ex.getMessage(), ex); } } @Override public Drive makeClient(final String clientId, final String clientSecret, final Collection<String> scopes, final String applicationName, final String refreshToken, final String accessToken) { Credential credential; try { credential = authorize(); if (refreshToken != null && !"".equals(refreshToken)) { credential.setRefreshToken(refreshToken); } if (accessToken != null && !"".equals(accessToken)) { credential.setAccessToken(accessToken); } return DriveService.getDriveService(credential); } catch (Exception e) { LOGGER.error("Could not create Google Drive client.", e); } return null; } public Drive makeClient() throws Exception { Credential credential = authorize(); return DriveService.getDriveService(credential); } /** * Authorizes the installed application to access user's protected data. * * @param clientId * @param clientSecret * @param scopes * @return * @throws Exception */ private Credential authorize() throws Exception { Path configurationFile = Paths.get(Configuration.get(ConfigurationKey.TargetGoogleDriveCredentialFile)); try (InputStream in = Files.newInputStream(configurationFile, StandardOpenOption.READ)) { return GoogleCredential.fromStream(in, httpTransport, JacksonFactory.getDefaultInstance()) .createScoped(Arrays.asList(DriveScopes.DRIVE_FILE)); } } }