Skip to content

Commit

Permalink
Fix sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Orain committed Jan 8, 2018
1 parent 4c0e85d commit 98c8859
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 101 deletions.
76 changes: 76 additions & 0 deletions src/main/java/youtubemtop/factory/GoogleServiceFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package youtubemtop.factory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;

import youtubemtop.Application;

public abstract class GoogleServiceFactory {

/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;

/** Global instance of the HTTP transport. */
protected static HttpTransport HTTP_TRANSPORT;

/** Global instance of the JSON factory. */
protected static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

private List<String> scopes;

/**
* Constructor
*
* @param dataStorDir
* dataStorDir
* @param scopes
* scopes
*/
public GoogleServiceFactory(final File dataStorDir, final List<String> scopes) {
super();
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(dataStorDir);
this.scopes = scopes;
} catch (final Throwable t) {
t.printStackTrace();
System.exit(1);
}
}

/**
* Create an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
*/
public Credential authorize() throws IOException {
// Load client secrets.
final InputStream in = Application.class.getResourceAsStream("/client_secret.json");
final GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

// Build flow and trigger user authorization request.
final GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
clientSecrets, scopes).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
final Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
.authorize("user");
return credential;
}

public abstract AbstractGoogleJsonClient getService() throws IOException;
}
8 changes: 4 additions & 4 deletions src/main/java/youtubemtop/gmail/GmailServiceFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class GmailServiceFacade {
private static final String UNREAD = "UNREAD";

/** Service Gmail */
private static Gmail gmail;
private final Gmail gmail;

/** Mail Reader */
private static MailParser mailReader;
private final MailParser mailReader;

private static GmailServiceFacade instance = null;

Expand All @@ -61,9 +61,9 @@ public static GmailServiceFacade getInstance() throws IOException {
* @throws IOException
* IOException
*/
private GmailServiceFacade() throws IOException {
public GmailServiceFacade() throws IOException {
super();
gmail = GmailServiceFactory.getGmailService();
gmail = (Gmail) new GmailServiceFactory().getService();
mailReader = new MailParser();
}

Expand Down
58 changes: 9 additions & 49 deletions src/main/java/youtubemtop/gmail/GmailServiceFactory.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,29 @@
package youtubemtop.gmail;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.GmailScopes;

import youtubemtop.Application;
import youtubemtop.factory.GoogleServiceFactory;

/**
* Factory for Gmail
*
*
* @author julien.orain@gmail.com
*
*/
public class GmailServiceFactory {
public class GmailServiceFactory extends GoogleServiceFactory {

/** Directory to store user credentials for this application. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"),
".credentials/youtube-mail-to-playlist-gmail");

/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;

/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;

/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

/**
* Global instance of the scopes required by this quickstart.
*
Expand All @@ -50,34 +32,11 @@ public class GmailServiceFactory {
*/
private static final List<String> SCOPES = Arrays.asList(GmailScopes.GMAIL_MODIFY);

static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (final Throwable t) {
t.printStackTrace();
System.exit(1);
}
}

/**
* Creates an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
* Constructor
*/
public static Credential authorize() throws IOException {
// Load client secrets.
final InputStream in = Application.class.getResourceAsStream("/client_secret.json");
final GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

// Build flow and trigger user authorization request.
final GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
final Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
.authorize("user");
System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
public GmailServiceFactory() {
super(DATA_STORE_DIR, SCOPES);
}

/**
Expand All @@ -86,7 +45,8 @@ public static Credential authorize() throws IOException {
* @return an authorized Gmail client service
* @throws IOException
*/
public static Gmail getGmailService() throws IOException {
@Override
public AbstractGoogleJsonClient getService() throws IOException {
final Credential credential = authorize();
return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(Application.APPLICATION_NAME).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static YoutubeServiceFacade getInstance() throws IOException {
*/
public YoutubeServiceFacade() throws IOException {
super();
youtube = YoutubeServiceFactory.getYouTubeService();
youtube = (YouTube) new YoutubeServiceFactory().getService();
}

/**
Expand Down
55 changes: 8 additions & 47 deletions src/main/java/youtubemtop/youtube/YoutubeServiceFactory.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,29 @@
package youtubemtop.youtube;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.YouTubeScopes;

import youtubemtop.Application;
import youtubemtop.factory.GoogleServiceFactory;

/**
* Factory for Youtube
*
* @author julien.orain@gmail.com
*
*/
public class YoutubeServiceFactory {
public class YoutubeServiceFactory extends GoogleServiceFactory {

/** Directory to store user credentials for this application. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"),
".credentials/youtube-mail-to-playlist-youtube");

/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;

/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;

/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

/**
* Global instance of the scopes required by this quickstart.
*
Expand All @@ -50,33 +32,11 @@ public class YoutubeServiceFactory {
*/
private static final List<String> SCOPES = Arrays.asList(YouTubeScopes.YOUTUBEPARTNER);

static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (final Throwable t) {
t.printStackTrace();
System.exit(1);
}
}

/**
* Create an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
* Constructor
*/
public static Credential authorize() throws IOException {
// Load client secrets.
final InputStream in = Application.class.getResourceAsStream("/client_secret.json");
final GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

// Build flow and trigger user authorization request.
final GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
final Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
.authorize("user");
return credential;
public YoutubeServiceFactory() {
super(DATA_STORE_DIR, SCOPES);
}

/**
Expand All @@ -86,7 +46,8 @@ public static Credential authorize() throws IOException {
* @return an authorized API client service
* @throws IOException
*/
public static YouTube getYouTubeService() throws IOException {
@Override
public AbstractGoogleJsonClient getService() throws IOException {
final Credential credential = authorize();
return new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(Application.APPLICATION_NAME).build();
Expand Down

0 comments on commit 98c8859

Please sign in to comment.