LoadOdtFromGoogle.java

  1. package org.jbehave.core.io.google;

  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.util.List;

  7. import com.google.gdata.client.DocumentQuery;
  8. import com.google.gdata.client.docs.DocsService;
  9. import com.google.gdata.data.MediaContent;
  10. import com.google.gdata.data.docs.DocumentListEntry;
  11. import com.google.gdata.data.docs.DocumentListFeed;
  12. import com.google.gdata.data.media.MediaSource;
  13. import com.google.gdata.util.AuthenticationException;
  14. import com.google.gdata.util.ServiceException;

  15. import org.jbehave.core.io.odf.LoadOdtFromURL;

  16. public class LoadOdtFromGoogle extends LoadOdtFromURL {

  17.     private static final DocsService DEFAULT_DOCS_SERVICE = new DocsService("jbehave");
  18.     private static final String DEFAULT_FEED_URI = "https://docs.google.com/feeds/default/private/full/";
  19.     private final DocsService service;
  20.     private final String feedURI;

  21.     public LoadOdtFromGoogle(String username, String password) {
  22.         this(username, password, DEFAULT_FEED_URI);
  23.     }

  24.     public LoadOdtFromGoogle(String username, String password, String feedURI) {
  25.         this(username, password, feedURI, DEFAULT_DOCS_SERVICE);
  26.     }

  27.     public LoadOdtFromGoogle(String username, String password, String feedURI, DocsService service) {
  28.         this.service = service;
  29.         this.feedURI = feedURI;
  30.         try {
  31.             service.setUserCredentials(username, password);
  32.         } catch (AuthenticationException e) {
  33.             throw new GoogleAccessFailed(username, e);
  34.         }
  35.     }

  36.     @Override
  37.     protected InputStream resourceAsStream(String title) throws IOException {
  38.         try {
  39.             return documentAsStream(exportURL(title));
  40.         } catch (ServiceException e) {
  41.             throw new IOException(e);
  42.         }
  43.     }

  44.     private String exportURL(String title) throws IOException, ServiceException {
  45.         DocumentQuery query = documentQuery(title);
  46.         List<DocumentListEntry> entries = service.getFeed(query, DocumentListFeed.class).getEntries();
  47.         if (entries.isEmpty()) {
  48.             throw new GoogleDocumentNotFound(title);
  49.         }
  50.         return ((MediaContent) entries.get(0).getContent()).getUri() + "&exportFormat=odt";
  51.     }

  52.     DocumentQuery documentQuery(String title) throws MalformedURLException {
  53.         DocumentQuery query = new DocumentQuery(new URL(feedURI));
  54.         query.setTitleQuery(title);
  55.         query.setTitleExact(true);
  56.         query.setMaxResults(1);
  57.         return query;
  58.     }

  59.     private InputStream documentAsStream(String url) throws IOException {
  60.         try {
  61.             MediaSource ms = service.getMedia(mediaContent(url));
  62.             return ms.getInputStream();
  63.         } catch (ServiceException e) {
  64.             throw new GoogleMediaExportFailed(url, e);
  65.         }
  66.     }

  67.     MediaContent mediaContent(String url) {
  68.         MediaContent mc = new MediaContent();
  69.         mc.setUri(url);
  70.         return mc;
  71.     }

  72.     @SuppressWarnings("serial")
  73.     public static class GoogleAccessFailed extends RuntimeException {

  74.         public GoogleAccessFailed(String username, Throwable cause) {
  75.             super("Google access failed for user " + username, cause);
  76.         }

  77.     }

  78.     @SuppressWarnings("serial")
  79.     public static class GoogleDocumentNotFound extends RuntimeException {

  80.         public GoogleDocumentNotFound(String title) {
  81.             super("Failed to find Google document from " + title);
  82.         }

  83.     }

  84.     @SuppressWarnings("serial")
  85.     public static class GoogleMediaExportFailed extends RuntimeException {

  86.         public GoogleMediaExportFailed(String url, Throwable cause) {
  87.             super("Failed to export Google media from " + url, cause);
  88.         }

  89.     }
  90. }