Search in sources :

Example 1 with FileContent

use of com.google.api.client.http.FileContent in project camel by apache.

the class GoogleDriveFilesConverter method genericFileToGoogleDriveFile.

@Converter
public static com.google.api.services.drive.model.File genericFileToGoogleDriveFile(GenericFile<?> file, Exchange exchange) throws Exception {
    if (file.getFile() instanceof File) {
        File f = (File) file.getFile();
        com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
        fileMetadata.setTitle(f.getName());
        FileContent mediaContent = new FileContent(null, f);
        if (exchange != null) {
            exchange.getIn().setHeader("CamelGoogleDrive.content", fileMetadata);
            exchange.getIn().setHeader("CamelGoogleDrive.mediaContent", mediaContent);
        }
        return fileMetadata;
    }
    if (exchange != null) {
        // body wasn't a java.io.File so let's try to convert it
        file.getBinding().loadContent(exchange, file);
        InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, exchange, file.getBody());
        com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
        fileMetadata.setTitle(file.getFileName());
        InputStreamContent mediaContent = new InputStreamContent(null, is);
        if (exchange != null) {
            exchange.getIn().setHeader("CamelGoogleDrive.content", fileMetadata);
            exchange.getIn().setHeader("CamelGoogleDrive.mediaContent", mediaContent);
        }
        return fileMetadata;
    }
    return null;
}
Also used : FileContent(com.google.api.client.http.FileContent) InputStream(java.io.InputStream) InputStreamContent(com.google.api.client.http.InputStreamContent) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) Converter(org.apache.camel.Converter)

Example 2 with FileContent

use of com.google.api.client.http.FileContent in project collect by opendatakit.

the class DriveHelper method uploadFileToDrive.

/**
 * Upload a file to google drive
 *
 * @param mediaName           The name of the uploaded file
 * @param destinationFolderID Id of the folder into which the file has to be uploaded
 * @param toUpload            The file which is to be uploaded
 * @return id of the file if uploaded successfully
 */
public String uploadFileToDrive(String mediaName, String destinationFolderID, File toUpload) throws IOException {
    // adding meta-data to the file
    com.google.api.services.drive.model.File fileMetadata = createNewFile(mediaName, null, destinationFolderID);
    String mimeType = FileUtils.getMimeType(toUpload.getPath());
    String fields = "id, parents";
    FileContent mediaContent = new FileContent(mimeType, toUpload);
    return driveService.uploadFile(fileMetadata, mediaContent, fields);
}
Also used : FileContent(com.google.api.client.http.FileContent)

Example 3 with FileContent

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

the class GoogleDriveAPI method createSpreadsheetFromFile.

/**
	 * Method to create a google spreadsheet from a file
	 * 
	 * @param idParentFolder
	 *            String to identify the parent folder
	 * @param emailUserOwner
	 *            String to identify email user owner
	 * @param extensionFile
	 *            String with extension file
	 * @param nameFile
	 *            String with name google spreadsheet
	 * @param fileOrigin
	 *            File with content file origin
	 * @param mimeType
	 *            String with mime type file origin
	 * 
	 * @return True if create is ok, False otherwise
	 */
public boolean createSpreadsheetFromFile(String idParentFolder, String emailUserOwner, String extensionFile, String nameFile, java.io.File fileOrigin, String mimeType) {
    boolean result = true;
    Permission newPermission = createPermission();
    File body = new File();
    body.setTitle(nameFile);
    body.setMimeType("application/vnd.google-apps.spreadsheet");
    body.setEditable(true);
    body.setShared(true);
    body.setPermissions(Arrays.asList(newPermission));
    body.setUserPermission(newPermission);
    body.setWritersCanShare(true);
    body.setFileExtension(extensionFile);
    User user = new User();
    user.setEmailAddress(emailUserOwner);
    user.setIsAuthenticatedUser(true);
    List<User> list = new ArrayList<User>();
    list.add(user);
    body.setOwners(list);
    body.setParents(Arrays.asList(new ParentReference().setId(idParentFolder)));
    FileContent mediaContent = new FileContent(mimeType, fileOrigin);
    String fileId = "";
    try {
        File file = drive.files().insert(body, mediaContent).execute();
        fileId = file.getId();
        insertPermission(newPermission, fileId);
    } catch (Exception e) {
        log.error("Error create spreadsheet from file ", e);
        result = false;
    }
    log.info("create Spreadsheet in google Drive from " + nameFile);
    return result;
}
Also used : FileContent(com.google.api.client.http.FileContent) ParentReference(com.google.api.services.drive.model.ParentReference) User(com.google.api.services.drive.model.User) Permission(com.google.api.services.drive.model.Permission) ArrayList(java.util.ArrayList) File(com.google.api.services.drive.model.File) GeneralSecurityException(java.security.GeneralSecurityException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 4 with FileContent

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

the class GoogleDriveAPI method createSpreadsheetFromFile.

public boolean createSpreadsheetFromFile(String idParentFolder, String emailUserOwner, String extensionFile, String nameFile, java.io.File fileOrigin, String mimeType) {
    boolean result = true;
    Permission newPermission = createPermission();
    File body = new File();
    body.setTitle(nameFile);
    body.setMimeType("application/vnd.google-apps.spreadsheet");
    body.setEditable(true);
    body.setShared(true);
    body.setPermissions(Arrays.asList(newPermission));
    body.setUserPermission(newPermission);
    body.setWritersCanShare(true);
    body.setFileExtension(extensionFile);
    User user = new User();
    user.setEmailAddress(emailUserOwner);
    user.setIsAuthenticatedUser(true);
    List<User> list = new ArrayList<User>();
    list.add(user);
    body.setOwners(list);
    body.setParents(Arrays.asList(new ParentReference().setId(idParentFolder)));
    FileContent mediaContent = new FileContent(mimeType, fileOrigin);
    String fileId = "";
    try {
        File file = service.files().insert(body, mediaContent).execute();
        fileId = file.getId();
        insertPermission(newPermission, fileId);
    } catch (IOException e) {
        log.error("Error create spreadsheet from file ", e);
        result = false;
    }
    log.info("create Spreadsheet in google Drive from " + nameFile);
    return result;
}
Also used : FileContent(com.google.api.client.http.FileContent) ParentReference(com.google.api.services.drive.model.ParentReference) User(com.google.api.services.drive.model.User) Permission(com.google.api.services.drive.model.Permission) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Example 5 with FileContent

use of com.google.api.client.http.FileContent in project java-docs-samples by GoogleCloudPlatform.

the class OnlinePredictionSample method main.

public static void main(String[] args) throws Exception {
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    Discovery discovery = new Discovery.Builder(httpTransport, jsonFactory, null).build();
    RestDescription api = discovery.apis().getRest("ml", "v1").execute();
    RestMethod method = api.getResources().get("projects").getMethods().get("predict");
    JsonSchema param = new JsonSchema();
    String projectId = "YOUR_PROJECT_ID";
    // You should have already deployed a model and a version.
    // For reference, see https://cloud.google.com/ml-engine/docs/deploying-models.
    String modelId = "YOUR_MODEL_ID";
    String versionId = "YOUR_VERSION_ID";
    param.set("name", String.format("projects/%s/models/%s/versions/%s", projectId, modelId, versionId));
    GenericUrl url = new GenericUrl(UriTemplate.expand(api.getBaseUrl() + method.getPath(), param, true));
    System.out.println(url);
    String contentType = "application/json";
    File requestBodyFile = new File("input.txt");
    HttpContent content = new FileContent(contentType, requestBodyFile);
    System.out.println(content.getLength());
    GoogleCredential credential = GoogleCredential.getApplicationDefault();
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
    HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content);
    String response = request.execute().parseAsString();
    System.out.println(response);
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonSchema(com.google.api.services.discovery.model.JsonSchema) JsonFactory(com.google.api.client.json.JsonFactory) Discovery(com.google.api.services.discovery.Discovery) RestDescription(com.google.api.services.discovery.model.RestDescription) GenericUrl(com.google.api.client.http.GenericUrl) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) FileContent(com.google.api.client.http.FileContent) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) File(java.io.File) HttpContent(com.google.api.client.http.HttpContent) RestMethod(com.google.api.services.discovery.model.RestMethod)

Aggregations

FileContent (com.google.api.client.http.FileContent)13 File (com.google.api.services.drive.model.File)9 IOException (java.io.IOException)7 ParentReference (com.google.api.services.drive.model.ParentReference)3 GeneralSecurityException (java.security.GeneralSecurityException)3 ArrayList (java.util.ArrayList)3 AbstractInputStreamContent (com.google.api.client.http.AbstractInputStreamContent)2 Permission (com.google.api.services.drive.model.Permission)2 User (com.google.api.services.drive.model.User)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 HashMap (java.util.HashMap)2 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 UserRecoverableAuthIOException (com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1 MediaHttpUploader (com.google.api.client.googleapis.media.MediaHttpUploader)1 ByteArrayContent (com.google.api.client.http.ByteArrayContent)1 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpContent (com.google.api.client.http.HttpContent)1 HttpRequest (com.google.api.client.http.HttpRequest)1