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;
}
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);
}
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;
}
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;
}
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);
}
Aggregations