Search in sources :

Example 26 with GenericUrl

use of com.google.api.client.http.GenericUrl in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method downloadFile.

/**
	 * Download all owner files
	 * 
	 * @param path
	 *            Path where download files
	 */
public java.io.File downloadFile(String path, File file, String format) {
    if (file == null)
        return null;
    String mimetype = "";
    if (com.localidata.generic.Constants.CSV.equals(format))
        mimetype = "text/csv";
    else if (com.localidata.generic.Constants.XLSX.equals(format)) {
        mimetype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    }
    try {
        String downloadUrl = file.getExportLinks().get(mimetype);
        HttpResponse resp = drive.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl)).execute();
        InputStream input = resp.getContent();
        java.io.File f = null;
        if (Utils.v(path)) {
            f = new java.io.File(path + java.io.File.separator + file.getTitle() + "." + format);
        } else {
            f = new java.io.File(file.getTitle() + "." + format);
        }
        FileUtils.copyInputStreamToFile(input, f);
        return f;
    } catch (Exception e) {
        log.error("Error decargando fichero en google drive", e);
    }
    return null;
}
Also used : InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) GeneralSecurityException(java.security.GeneralSecurityException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 27 with GenericUrl

use of com.google.api.client.http.GenericUrl in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method downloadFilesAfterDate.

/**
	 * Download files after a date
	 * 
	 * @param path
	 *            Path where download files
	 * @param stringDateLastChange
	 *            String with a date
	 */
public void downloadFilesAfterDate(String path, String stringDateLastChange) {
    List<File> files = listOwnerFilesAfterDate(stringDateLastChange);
    for (File file : files) {
        try {
            String downloadUrl = file.getExportLinks().get("text/csv");
            HttpResponse resp = drive.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl)).execute();
            InputStream input = resp.getContent();
            java.io.File f = new java.io.File(path + java.io.File.separator + file.getTitle() + ".csv");
            FileUtils.copyInputStreamToFile(input, f);
        } catch (Exception e) {
            log.error("Error decargando ficheros despues de una fecha en google drive", e);
        }
    }
}
Also used : InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) File(com.google.api.services.drive.model.File) GeneralSecurityException(java.security.GeneralSecurityException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 28 with GenericUrl

use of com.google.api.client.http.GenericUrl in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method download.

private void download(String path, File file) throws IOException {
    if (file.getShared()) {
        String downloadUrl = file.getExportLinks().get("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        HttpResponse resp = drive.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl)).execute();
        InputStream input = resp.getContent();
        java.io.File f = new java.io.File(path + java.io.File.separator + file.getTitle() + "." + Prop.formatConfig);
        FileUtils.copyInputStreamToFile(input, f);
    }
}
Also used : InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) File(com.google.api.services.drive.model.File)

Aggregations

GenericUrl (com.google.api.client.http.GenericUrl)28 HttpResponse (com.google.api.client.http.HttpResponse)19 HttpRequest (com.google.api.client.http.HttpRequest)15 IOException (java.io.IOException)15 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)8 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)7 InputStream (java.io.InputStream)7 HttpTransport (com.google.api.client.http.HttpTransport)6 HttpResponseException (com.google.api.client.http.HttpResponseException)4 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)4 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)4 File (com.google.api.services.drive.model.File)4 GeneralSecurityException (java.security.GeneralSecurityException)4 MockGoogleCredential (com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential)3 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)3 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)3 JsonFactory (com.google.api.client.json.JsonFactory)3 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)3 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)3 MockSleeper (com.google.api.client.testing.util.MockSleeper)3