diff --git a/src/main/java/youtubemtop/factory/GoogleServiceFactory.java b/src/main/java/youtubemtop/factory/GoogleServiceFactory.java new file mode 100644 index 0000000..483c909 --- /dev/null +++ b/src/main/java/youtubemtop/factory/GoogleServiceFactory.java @@ -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 scopes; + + /** + * Constructor + * + * @param dataStorDir + * dataStorDir + * @param scopes + * scopes + */ + public GoogleServiceFactory(final File dataStorDir, final List 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; +} diff --git a/src/main/java/youtubemtop/gmail/GmailServiceFacade.java b/src/main/java/youtubemtop/gmail/GmailServiceFacade.java index 1146a69..3f34c24 100644 --- a/src/main/java/youtubemtop/gmail/GmailServiceFacade.java +++ b/src/main/java/youtubemtop/gmail/GmailServiceFacade.java @@ -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; @@ -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(); } diff --git a/src/main/java/youtubemtop/gmail/GmailServiceFactory.java b/src/main/java/youtubemtop/gmail/GmailServiceFactory.java index 90c16c2..803d8c1 100644 --- a/src/main/java/youtubemtop/gmail/GmailServiceFactory.java +++ b/src/main/java/youtubemtop/gmail/GmailServiceFactory.java @@ -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. * @@ -50,34 +32,11 @@ public class GmailServiceFactory { */ private static final List 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); } /** @@ -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(); diff --git a/src/main/java/youtubemtop/youtube/YoutubeServiceFacade.java b/src/main/java/youtubemtop/youtube/YoutubeServiceFacade.java index a06a7a8..d149c2a 100644 --- a/src/main/java/youtubemtop/youtube/YoutubeServiceFacade.java +++ b/src/main/java/youtubemtop/youtube/YoutubeServiceFacade.java @@ -40,7 +40,7 @@ public static YoutubeServiceFacade getInstance() throws IOException { */ public YoutubeServiceFacade() throws IOException { super(); - youtube = YoutubeServiceFactory.getYouTubeService(); + youtube = (YouTube) new YoutubeServiceFactory().getService(); } /** diff --git a/src/main/java/youtubemtop/youtube/YoutubeServiceFactory.java b/src/main/java/youtubemtop/youtube/YoutubeServiceFactory.java index 144411d..e2eccbd 100644 --- a/src/main/java/youtubemtop/youtube/YoutubeServiceFactory.java +++ b/src/main/java/youtubemtop/youtube/YoutubeServiceFactory.java @@ -1,25 +1,16 @@ 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 @@ -27,21 +18,12 @@ * @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. * @@ -50,33 +32,11 @@ public class YoutubeServiceFactory { */ private static final List 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); } /** @@ -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();