Search in sources :

Example 31 with HttpResponse

use of com.google.api.client.http.HttpResponse 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 32 with HttpResponse

use of com.google.api.client.http.HttpResponse 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 33 with HttpResponse

use of com.google.api.client.http.HttpResponse 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

HttpResponse (com.google.api.client.http.HttpResponse)33 IOException (java.io.IOException)22 HttpRequest (com.google.api.client.http.HttpRequest)20 GenericUrl (com.google.api.client.http.GenericUrl)19 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)11 HttpTransport (com.google.api.client.http.HttpTransport)8 InputStream (java.io.InputStream)8 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)7 Test (org.junit.Test)7 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)6 HttpBackOffIOExceptionHandler (com.google.api.client.http.HttpBackOffIOExceptionHandler)4 HttpBackOffUnsuccessfulResponseHandler (com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler)4 HttpResponseException (com.google.api.client.http.HttpResponseException)4 HttpUnsuccessfulResponseHandler (com.google.api.client.http.HttpUnsuccessfulResponseHandler)4 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)4 JsonFactory (com.google.api.client.json.JsonFactory)4 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)4 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)4 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)4 ExponentialBackOff (com.google.api.client.util.ExponentialBackOff)4