CAMEL-9047: Replace deprecated boxjavalibv2 with box-java-sdk Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/45335d1e Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/45335d1e Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/45335d1e
Branch: refs/heads/master Commit: 45335d1ea29ecbf65e8bb983b623eccc86c58411 Parents: b259c00 Author: William Collins <[email protected]> Authored: Fri Mar 3 12:30:29 2017 -0500 Committer: Claus Ibsen <[email protected]> Committed: Mon Mar 6 08:48:05 2017 +0100 ---------------------------------------------------------------------- components/camel-box2/camel-box2-api/pom.xml | 79 ++ .../box2/api/Box2CollaborationsManager.java | 238 ++++++ .../component/box2/api/Box2CommentsManager.java | 205 +++++ .../box2/api/Box2EventLogsManager.java | 110 +++ .../component/box2/api/Box2EventsManager.java | 99 +++ .../component/box2/api/Box2FilesManager.java | 798 +++++++++++++++++++ .../component/box2/api/Box2FoldersManager.java | 407 ++++++++++ .../component/box2/api/Box2GroupsManager.java | 274 +++++++ .../component/box2/api/Box2SearchManager.java | 93 +++ .../component/box2/api/Box2TasksManager.java | 324 ++++++++ .../component/box2/api/Box2UsersManager.java | 333 ++++++++ .../camel-box2/camel-box2-component/pom.xml | 612 ++++++++++++++ .../src/main/docs/box2-component.adoc | 744 +++++++++++++++++ .../camel/component/box2/Box2Component.java | 115 +++ .../camel/component/box2/Box2Configuration.java | 475 +++++++++++ .../camel/component/box2/Box2Consumer.java | 111 +++ .../camel/component/box2/Box2Endpoint.java | 214 +++++ .../camel/component/box2/Box2Producer.java | 35 + .../box2/internal/Box2ConnectionHelper.java | 286 +++++++ .../component/box2/internal/Box2Constants.java | 32 + .../box2/internal/Box2PropertiesHelper.java | 42 + .../src/main/resources/META-INF/LICENSE.txt | 203 +++++ .../src/main/resources/META-INF/NOTICE.txt | 11 + .../services/org/apache/camel/component/box2 | 18 + .../component/box2/AbstractBox2TestSupport.java | 82 ++ ...ox2CollaborationsManagerIntegrationTest.java | 231 ++++++ .../Box2CommentsManagerIntegrationTest.java | 201 +++++ .../Box2EventLogsManagerIntegrationTest.java | 76 ++ .../box2/Box2EventsManagerIntegrationTest.java | 105 +++ .../box2/Box2FilesManagerIntegrationTest.java | 575 +++++++++++++ .../box2/Box2FoldersManagerIntegrationTest.java | 322 ++++++++ .../box2/Box2GroupsManagerIntegrationTest.java | 268 +++++++ .../box2/Box2SearchManagerIntegrationTest.java | 106 +++ .../box2/Box2TasksManagerIntegrationTest.java | 284 +++++++ .../box2/Box2UsersManagerIntegrationTest.java | 323 ++++++++ .../src/test/resources/CamelTestFile.txt | 1 + .../src/test/resources/log4j.properties | 14 + .../src/test/resources/test-options.properties | 33 + .../tmp/test-options.properties | 67 ++ components/camel-box2/pom.xml | 32 + components/readme.adoc | 3 + docs/user-manual/en/SUMMARY.md | 1 + parent/pom.xml | 1 + .../spring-boot/components-starter/pom.xml | 1 + .../camel-spring-boot-dependencies/pom.xml | 5 + 45 files changed, 8589 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/45335d1e/components/camel-box2/camel-box2-api/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-box2/camel-box2-api/pom.xml b/components/camel-box2/camel-box2-api/pom.xml new file mode 100644 index 0000000..a347278 --- /dev/null +++ b/components/camel-box2/camel-box2-api/pom.xml @@ -0,0 +1,79 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>camel-box2-parent</artifactId> + <version>2.19.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-box2-api</artifactId> + <name>Camel Box2 Component API</name> + <description>API for Camel Box2 Component</description> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> + </properties> + + <build> + <defaultGoal>install</defaultGoal> + + <plugins> + + <!-- to generate API Javadoc --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <executions> + <execution> + <id>add-javadoc</id> + <goals> + <goal>jar</goal> + </goals> + <configuration> + <attach>true</attach> + <source>1.7</source> + <quiet>true</quiet> + <detectOfflineLinks>false</detectOfflineLinks> + <javadocVersion>1.7</javadocVersion> + <encoding>UTF-8</encoding> + </configuration> + </execution> + </executions> + </plugin> + + </plugins> + </build> + + <!-- Disable Java 8 doclint checks to avoid Javadoc plugin failures --> + <profiles> + <profile> + <id>doclint-java8-disable</id> + <activation> + <jdk>[1.8,</jdk> + </activation> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <configuration> + <additionalparam>-Xdoclint:none</additionalparam> + </configuration> + </plugin> + </plugins> + </build> + </profile> + </profiles> + + <dependencies> + <dependency> + <groupId>com.box</groupId> + <artifactId>box-java-sdk</artifactId> + <version>2.1.1</version> + </dependency> + </dependencies> +</project> http://git-wip-us.apache.org/repos/asf/camel/blob/45335d1e/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2CollaborationsManager.java ---------------------------------------------------------------------- diff --git a/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2CollaborationsManager.java b/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2CollaborationsManager.java new file mode 100644 index 0000000..5cbd7fb --- /dev/null +++ b/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2CollaborationsManager.java @@ -0,0 +1,238 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.box2.api; + +import java.util.Collection; + +import com.box.sdk.BoxAPIConnection; +import com.box.sdk.BoxAPIException; +import com.box.sdk.BoxCollaboration; +import com.box.sdk.BoxCollaborator; +import com.box.sdk.BoxFolder; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Box2 Collaborations Manager + * + * <p> + * Provides operations to manage Box collaborations. + * + * @author <a href="mailto:[email protected]">William Collins</a> + * + */ +public class Box2CollaborationsManager { + + private static final Logger LOG = LoggerFactory.getLogger(Box2CollaborationsManager.class); + + /** + * Box connection to authenticated user account. + */ + private BoxAPIConnection boxConnection; + + /** + * Create collaborations manager to manage the comments of Box connection's + * authenticated user. + * + * @param boxConnection + * - Box connection to authenticated user account. + */ + public Box2CollaborationsManager(BoxAPIConnection boxConnection) { + this.boxConnection = boxConnection; + } + + /** + * Get information about all of the collaborations for folder. + * + * @param folderId + * - the id of folder to get collaborations information on. + * + * @return The collection of collaboration information for folder. + */ + public Collection<BoxCollaboration.Info> getFolderCollaborations(String folderId) { + try { + LOG.debug("Getting collaborations for folder(id=" + folderId + ")"); + if (folderId == null) { + throw new IllegalArgumentException("Parameter 'folderId' can not be null"); + } + BoxFolder folder = new BoxFolder(boxConnection, folderId); + return folder.getCollaborations(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Add a collaboration to this folder. + * + * @param folderId + * - the id of folder to add collaboration to. + * @param collaborator + * - the collaborator to add. + * @param role + * - the role of the collaborator. + * + * @return The new collaboration. + */ + @SuppressWarnings("unused") // compiler for some reason thinks 'if + // (collaborator == null)' clause is dead code. + public BoxCollaboration addFolderCollaboration(String folderId, BoxCollaborator collaborator, + BoxCollaboration.Role role) { + try { + LOG.debug("Creating collaborations for folder(id=" + folderId + ") with collaborator(" + + collaborator.getID() + ")"); + if (folderId == null) { + throw new IllegalArgumentException("Parameter 'folderId' can not be null"); + } + if (collaborator == null) { + throw new IllegalArgumentException("Parameter 'collaborator' can not be null"); + } + if (role == null) { + throw new IllegalArgumentException("Parameter 'role' can not be null"); + } + + BoxFolder folder = new BoxFolder(boxConnection, folderId); + return folder.collaborate(collaborator, role).getResource(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Add a collaboration to this folder. An email will be sent to the + * collaborator if they don't already have a Box account. + * + * @param folderId + * - the id of folder to add collaboration to. + * @param email + * - the email address of the collaborator to add. + * @param role + * - the role of the collaborator. + * + * @return The new collaboration. + */ + public BoxCollaboration addFolderCollaborationByEmail(String folderId, String email, BoxCollaboration.Role role) { + try { + LOG.debug("Creating collaborations for folder(id=" + folderId + ") with collaborator(" + email + ")"); + if (folderId == null) { + throw new IllegalArgumentException("Parameter 'folderId' can not be null"); + } + if (email == null) { + throw new IllegalArgumentException("Parameter 'email' can not be null"); + } + if (role == null) { + throw new IllegalArgumentException("Parameter 'role' can not be null"); + } + + BoxFolder folder = new BoxFolder(boxConnection, folderId); + return folder.collaborate(email, role).getResource(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Get collaboration information. + * + * @param collaborationId + * - the id of collaboration. + * @return The collaboration information. + */ + public BoxCollaboration.Info getCollaborationInfo(String collaborationId) { + try { + LOG.debug("Getting info for collaboration(id=" + collaborationId + ")"); + if (collaborationId == null) { + throw new IllegalArgumentException("Parameter 'collaborationId' can not be null"); + } + + BoxCollaboration collaboration = new BoxCollaboration(boxConnection, collaborationId); + + return collaboration.getInfo(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Update collaboration information. + * + * @param collaborationId + * - the id of collaboration. + * @return The collaboration with updated information. + */ + public BoxCollaboration updateCollaborationInfo(String collaborationId, BoxCollaboration.Info info) { + try { + LOG.debug("Updating info for collaboration(id=" + collaborationId + ")"); + if (collaborationId == null) { + throw new IllegalArgumentException("Parameter 'collaborationId' can not be null"); + } + + BoxCollaboration collaboration = new BoxCollaboration(boxConnection, collaborationId); + + collaboration.updateInfo(info); + return collaboration; + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Delete collaboration. + * + * @param collaborationId + * - the id of comment to change. + * @param message + * - the new message for the comment. + * @return The comment with changed message. + */ + public void deleteCollaboration(String collaborationId) { + try { + LOG.debug("Deleting collaboration(id=" + collaborationId + ")"); + if (collaborationId == null) { + throw new IllegalArgumentException("Parameter 'collaborationId' can not be null"); + } + BoxCollaboration collaboration = new BoxCollaboration(boxConnection, collaborationId); + collaboration.delete(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Get all pending collaboration invites for the current user. + * + * @return A collection of pending collaboration information. + */ + public Collection<BoxCollaboration.Info> getPendingCollaborations() { + try { + + return BoxCollaboration.getPendingCollaborations(boxConnection); + + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/45335d1e/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2CommentsManager.java ---------------------------------------------------------------------- diff --git a/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2CommentsManager.java b/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2CommentsManager.java new file mode 100644 index 0000000..946f292 --- /dev/null +++ b/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2CommentsManager.java @@ -0,0 +1,205 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.box2.api; + +import java.util.List; + +import com.box.sdk.BoxAPIConnection; +import com.box.sdk.BoxAPIException; +import com.box.sdk.BoxComment; +import com.box.sdk.BoxFile; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Box2 Comments Manager + * + * <p> + * Provides operations to manage Box comments. + * + * @author <a href="mailto:[email protected]">William Collins</a> + * + */ +public class Box2CommentsManager { + + private static final Logger LOG = LoggerFactory.getLogger(Box2CommentsManager.class); + + /** + * Box connection to authenticated user account. + */ + private BoxAPIConnection boxConnection; + + /** + * Create comments manager to manage the comments of Box connection's + * authenticated user. + * + * @param boxConnection + * - Box connection to authenticated user account. + */ + public Box2CommentsManager(BoxAPIConnection boxConnection) { + this.boxConnection = boxConnection; + } + + /** + * Add comment to file. + * + * @param fileId + * - the id of file to rename. + * @param message + * - the comment's message. + * @return The commented file. + */ + public BoxFile addFileComment(String fileId, String message) { + try { + LOG.debug("Adding comment to file(id=" + fileId + ") to '" + message + "'"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (message == null) { + throw new IllegalArgumentException("Parameter 'message' can not be null"); + } + + BoxFile fileToCommentOn = new BoxFile(boxConnection, fileId); + fileToCommentOn.addComment(message); + return fileToCommentOn; + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Get a list of any comments on this file. + * + * @param fileId + * - the id of file. + * @return The list of comments on this file. + */ + public List<BoxComment.Info> getFileComments(String fileId) { + try { + LOG.debug("Getting comments of file(id=" + fileId + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + + BoxFile file = new BoxFile(boxConnection, fileId); + + return file.getComments(); + + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Get comment information. + * + * @param commentId + * - the id of comment. + * @return The comment information. + */ + public BoxComment.Info getCommentInfo(String commentId) { + try { + LOG.debug("Getting info for comment(id=" + commentId + ")"); + if (commentId == null) { + throw new IllegalArgumentException("Parameter 'commentId' can not be null"); + } + + BoxComment comment = new BoxComment(boxConnection, commentId); + + return comment.getInfo(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Reply to a comment. + * + * @param commentId + * - the id of comment to reply to. + * @param message + * - the message for the reply. + * @return The newly created reply comment. + */ + public BoxComment replyToComment(String commentId, String message) { + try { + LOG.debug("Replying to comment(id=" + commentId + ") with message=" + message); + if (commentId == null) { + throw new IllegalArgumentException("Parameter 'commentId' can not be null"); + } + if (message == null) { + throw new IllegalArgumentException("Parameter 'message' can not be null"); + } + BoxComment comment = new BoxComment(boxConnection, commentId); + return comment.reply(message).getResource(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Change comment message. + * + * @param commentId + * - the id of comment to change. + * @param message + * - the new message for the comment. + * @return The comment with changed message. + */ + public BoxComment changeCommentMessage(String commentId, String message) { + try { + LOG.debug("Changing comment(id=" + commentId + ") message=" + message); + if (commentId == null) { + throw new IllegalArgumentException("Parameter 'commentId' can not be null"); + } + if (message == null) { + throw new IllegalArgumentException("Parameter 'message' can not be null"); + } + BoxComment comment = new BoxComment(boxConnection, commentId); + return comment.changeMessage(message).getResource(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Delete comment. + * + * @param commentId + * - the id of comment to delete. + */ + public void deleteComment(String commentId) { + try { + LOG.debug("Deleting comment(id=" + commentId + ")"); + if (commentId == null) { + throw new IllegalArgumentException("Parameter 'commentId' can not be null"); + } + BoxComment comment = new BoxComment(boxConnection, commentId); + comment.delete(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/45335d1e/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2EventLogsManager.java ---------------------------------------------------------------------- diff --git a/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2EventLogsManager.java b/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2EventLogsManager.java new file mode 100644 index 0000000..a54c305 --- /dev/null +++ b/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2EventLogsManager.java @@ -0,0 +1,110 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.box2.api; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import com.box.sdk.BoxAPIConnection; +import com.box.sdk.BoxAPIException; +import com.box.sdk.BoxEvent; +import com.box.sdk.EventLog; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Box2 Event Logs Manager + * + * <p> + * Provides operations to read Box enterprise (admin) event logs. + * + * @author <a href="mailto:[email protected]">William Collins</a> + * + */ +public class Box2EventLogsManager { + + private static final Logger LOG = LoggerFactory.getLogger(Box2EventLogsManager.class); + + /** + * Box connection to authenticated user account. + */ + private BoxAPIConnection boxConnection; + + /** + * Create event logs manager to manage the event logs of Box connection's + * authenticated user. + * + * @param boxConnection + * - Box connection to authenticated user account. + */ + public Box2EventLogsManager(BoxAPIConnection boxConnection) { + this.boxConnection = boxConnection; + } + + /** + * Create an event stream with optional starting initial position and add + * listener that will be notified when an event is received. + * + * @param position + * - the starting position of the event stream. May be + * <code>null</code> in which case all events within bounds + * returned. + * @param after + * - the lower bound on the timestamp of the events returned. + * @param after + * - the upper bound on the timestamp of the events returned. + * @param types + * - an optional list of event types to filter by. + * + * @return A list of all the events that met the given criteria. + */ + public List<BoxEvent> getEnterpriseEvents(String position, Date after, Date before, BoxEvent.Type... types) { + try { + LOG.debug("Getting all enterprise events occuring between " + + (after == null ? after : SimpleDateFormat.getDateTimeInstance().format(after)) + " and " + + (before == null ? before : SimpleDateFormat.getDateTimeInstance().format(before)) + + (position == null ? position : (" starting at " + position))); + + if (after == null) { + throw new IllegalArgumentException("Parameter 'after' can not be null"); + } + if (before == null) { + throw new IllegalArgumentException("Parameter 'before' can not be null"); + } + + if (types == null) { + types = new BoxEvent.Type[0]; + } + + EventLog eventLog = EventLog.getEnterpriseEvents(boxConnection, position, after, before, types); + + List<BoxEvent> results = new ArrayList<BoxEvent>(); + for (BoxEvent event : eventLog) { + results.add(event); + } + + return results; + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/45335d1e/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2EventsManager.java ---------------------------------------------------------------------- diff --git a/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2EventsManager.java b/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2EventsManager.java new file mode 100644 index 0000000..7ef47f7 --- /dev/null +++ b/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2EventsManager.java @@ -0,0 +1,99 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.box2.api; + +import com.box.sdk.BoxAPIConnection; +import com.box.sdk.BoxAPIException; +import com.box.sdk.EventListener; +import com.box.sdk.EventStream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Box2 Events Manager + * + * <p> + * Provides operations to manage Box events. + * + * @author <a href="mailto:[email protected]">William Collins</a> + * + */ +public class Box2EventsManager { + + private static final Logger LOG = LoggerFactory.getLogger(Box2EventsManager.class); + + /** + * Box connection to authenticated user account. + */ + private BoxAPIConnection boxConnection; + + private EventStream eventStream; + + /** + * Create events manager to manage the events of Box connection's + * authenticated user. + * + * @param boxConnection + * - Box connection to authenticated user account. + */ + public Box2EventsManager(BoxAPIConnection boxConnection) { + this.boxConnection = boxConnection; + } + + /** + * Create an event stream with optional starting initial position and add + * listener that will be notified when an event is received. + * + * @param startingPosition + * - the starting position of the event stream. + * @param listener + * - the listener to add to event stream. + * + * @return The event stream. + */ + public void listen(EventListener listener, Long startingPosition) { + try { + LOG.debug("Listening for events with listener=" + listener + " at startingPosition=" + startingPosition); + + if (listener == null) { + LOG.debug("Parameter 'listener' is null: will not listen for events"); + return; + } + + if (startingPosition != null) { + eventStream = new EventStream(boxConnection, startingPosition); + } else { + eventStream = new EventStream(boxConnection); + } + + eventStream.addListener(listener); + + eventStream.start(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + public void stopListening() { + if (eventStream != null && eventStream.isStarted()) { + eventStream.stop(); + } + eventStream = null; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/45335d1e/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2FilesManager.java ---------------------------------------------------------------------- diff --git a/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2FilesManager.java b/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2FilesManager.java new file mode 100644 index 0000000..602a3c9 --- /dev/null +++ b/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2FilesManager.java @@ -0,0 +1,798 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.box2.api; + +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.Collection; +import java.util.Date; +import java.util.List; + +import com.box.sdk.BoxAPIConnection; +import com.box.sdk.BoxAPIException; +import com.box.sdk.BoxFile; +import com.box.sdk.BoxFileVersion; +import com.box.sdk.BoxFolder; +import com.box.sdk.BoxSharedLink; +import com.box.sdk.FileUploadParams; +import com.box.sdk.Metadata; +import com.box.sdk.ProgressListener; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Box2 Files Manager + * + * <p> + * Provides operations to manage Box files. + * + * @author <a href="mailto:[email protected]">William Collins</a> + * + */ +public class Box2FilesManager { + + private static final Logger LOG = LoggerFactory.getLogger(Box2FilesManager.class); + + /** + * Box connection to authenticated user account. + */ + private BoxAPIConnection boxConnection; + + /** + * Create files manager to manage the files of Box connection's + * authenticated user. + * + * @param boxConnection + * - Box connection to authenticated user account. + */ + public Box2FilesManager(BoxAPIConnection boxConnection) { + this.boxConnection = boxConnection; + } + + /** + * Get file information. + * + * @param fileId + * - the id of file. + * @param fields + * - the information fields to retrieve; if <code>null</code> all + * information fields are retrieved. + * @return The file information. + */ + public BoxFile.Info getFileInfo(String fileId, String... fields) { + try { + LOG.debug("Getting info for file(id=" + fileId + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + + BoxFile file = new BoxFile(boxConnection, fileId); + + if (fields == null || fields.length == 0) { + return file.getInfo(); + } else { + return file.getInfo(fields); + } + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Update file information. + * + * @param fileId + * - the id of file to update. + * @param info + * - the updated information + * @return The updated file. + */ + public BoxFile updateFileInfo(String fileId, BoxFile.Info info) { + try { + LOG.debug("Updating info for file(id=" + fileId + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (info == null) { + throw new IllegalArgumentException("Parameter 'info' can not be null"); + } + BoxFile file = new BoxFile(boxConnection, fileId); + file.updateInfo(info); + return file; + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Upload a new file to parent folder. + * + * @param parentFolderId + * - the id of parent folder. + * @param content + * - a stream containing contents of the file to upload. + * @param fileName + * the name to give the uploaded file. + * @param created + * - the content created date that will be given to the uploaded + * file. + * @param modified + * - the content modified date that will be given to the uploaded + * file. + * @param size + * - the size of the file's content used for monitoring the + * upload's progress. + * @param listener + * - a listener for monitoring the upload's progress. + * @return The uploaded file. + */ + public BoxFile uploadFile(String parentFolderId, InputStream content, String fileName, Date created, Date modified, + Long size, ProgressListener listener) { + try { + LOG.debug("Uploading file with name '" + fileName + "' to parent_folder(id=" + parentFolderId + ")"); + if (parentFolderId == null) { + throw new IllegalArgumentException("Parameter 'parentFolderId' can not be null"); + } + if (content == null) { + throw new IllegalArgumentException("Paramerer 'content' can not be null"); + } + if (fileName == null) { + throw new IllegalArgumentException("Paramerer 'fileName' can not be null"); + } + BoxFolder parentFolder = new BoxFolder(boxConnection, parentFolderId); + + FileUploadParams uploadParams = new FileUploadParams(); + uploadParams.setName(fileName); + uploadParams.setContent(content); + if (created != null) { + uploadParams.setCreated(created); + } + if (modified != null) { + uploadParams.setModified(modified); + } + if (size != null) { + uploadParams.setSize(size); + } + if (listener != null) { + uploadParams.setProgressListener(listener); + } + + return parentFolder.uploadFile(uploadParams).getResource(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Upload a new version of file. + * + * @param fileId + * - the id of file. + * @param fileContent + * - a stream containing contents of the file to upload. + * @param modified + * - the content modified date that will be given to the uploaded + * file. + * @param fileSize + * - the size of the file's content used for monitoring the + * upload's progress. + * @param listener + * - a listener for monitoring the upload's progress. + * @return The uploaded file. + */ + public BoxFile uploadNewFileVersion(String fileId, InputStream fileContent, Date modified, Long fileSize, + ProgressListener listener) { + try { + LOG.debug("Uploading new version of file(id=" + fileId + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (fileContent == null) { + throw new IllegalArgumentException("Paramerer 'fileContent' can not be null"); + } + + BoxFile file = new BoxFile(boxConnection, fileId); + + if (modified != null) { + if (fileSize != null && listener != null) { + file.uploadVersion(fileContent, modified, fileSize, listener); + } else { + file.uploadVersion(fileContent, modified); + } + } else { + file.uploadVersion(fileContent); + } + + return file; + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Get any previous versions of file. + * + * @param fileId + * - the id of file. + * @return The list of previous file versions. + */ + public Collection<BoxFileVersion> getFileVersions(String fileId) { + try { + LOG.debug("Getting versions of file(id=" + fileId + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + + BoxFile file = new BoxFile(boxConnection, fileId); + + return file.getVersions(); + + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Download a file. + * + * @param fileId + * - the id of file. + * @param output + * - the stream to which the file contents will be written. + * @param rangeStart + * - the byte offset in file at which to start the download; if + * <code>null</code> the entire contents of file will be + * downloaded. + * @param rangeEnd + * - the byte offset in file at which to stop the download; if + * <code>null</code> the entire contents of file will be + * downloaded. + * @param listener + * - a listener for monitoring the download's progress; if + * <code>null</code> the download's progress will not be + * monitored. + * @return The stream containing the contents of the downloaded file. + */ + public OutputStream downloadFile(String fileId, OutputStream output, Long rangeStart, Long rangeEnd, + ProgressListener listener) { + try { + LOG.debug("Downloading file(id=" + fileId + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (output == null) { + throw new IllegalArgumentException("Parameter 'output' can not be null"); + } + BoxFile file = new BoxFile(boxConnection, fileId); + + if (listener != null) { + if (rangeStart != null && rangeEnd != null) { + file.downloadRange(output, rangeStart, rangeEnd, listener); + } else { + file.download(output, listener); + } + } else { + if (rangeStart != null && rangeEnd != null) { + file.downloadRange(output, rangeStart, rangeEnd); + } else { + file.download(output); + } + } + return output; + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Download a previous version of file. + * + * @param fileId + * - the id of file. + * @param version + * - the version of file to download; initial version of file has + * value of <code>0</code>, second version of file is + * <code>1</code> and so on. + * @param output + * - the stream to which the version contents will be written. + * @param listener + * - a listener for monitoring the download's progress; if + * <code>null</code> the download's progress will not be + * monitored. + * @return The stream containing the contents of the downloaded file + * version. + */ + public OutputStream downloadPreviousFileVersion(String fileId, Integer version, OutputStream output, + ProgressListener listener) { + try { + LOG.debug("Downloading file(id=" + fileId + ", version=" + version + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (version == null) { + throw new IllegalArgumentException("Parameter 'version' can not be null"); + } + if (output == null) { + throw new IllegalArgumentException("Parameter 'output' can not be null"); + } + BoxFile file = new BoxFile(boxConnection, fileId); + + List<BoxFileVersion> fileVersions = (List<BoxFileVersion>) file.getVersions(); + BoxFileVersion fileVersion = fileVersions.get(version); + + if (listener != null) { + fileVersion.download(output, listener); + } else { + fileVersion.download(output); + } + return output; + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Promote a previous version of file. + * + * @param fileId + * - the id of file. + * @param version + * - the version of file to promote; initial version of file has + * value of <code>0</code>, second version of file is + * <code>1</code> and so on. + * @return The promoted version of file. + */ + public BoxFileVersion promoteFileVersion(String fileId, Integer version) { + try { + LOG.debug("Promoting file(id=" + fileId + ", version=" + version + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (version == null) { + throw new IllegalArgumentException("Parameter 'version' can not be null"); + } + BoxFile file = new BoxFile(boxConnection, fileId); + + List<BoxFileVersion> fileVersions = (List<BoxFileVersion>) file.getVersions(); + BoxFileVersion fileVersion = fileVersions.get(version); + + fileVersion.promote(); + return fileVersion; + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Copy file to destination folder while optionally giving it a new name. + * + * @param fileId + * - the id of file to copy. + * @param destinationFolderId + * - the id of the destination folder. + * @param newName + * - the new name for copied file; if <code>newName</code> is + * <code>null</code>, the copied file has same name as the + * original. + * @return The copied file. + */ + public BoxFile copyFile(String fileId, String destinationFolderId, String newName) { + try { + LOG.debug("Copying file(id=" + fileId + ") to destination_folder(id=" + destinationFolderId + ")" + + (newName == null ? "" : " with new name '" + newName + "'")); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (destinationFolderId == null) { + throw new IllegalArgumentException("Parameter 'destinationFolderId' can not be null"); + } + BoxFile fileToCopy = new BoxFile(boxConnection, fileId); + BoxFolder destinationFolder = new BoxFolder(boxConnection, destinationFolderId); + if (newName == null) { + return fileToCopy.copy(destinationFolder).getResource(); + } else { + return fileToCopy.copy(destinationFolder, newName).getResource(); + } + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Move file to destination folder while optionally giving it a new name. + * + * @param fileId + * - the id of file to move. + * @param destinationFolderId + * - the id of the destination folder. + * @param newName + * - the new name of moved file; if <code>newName</code> is + * <code>null</code>, the moved file has same name as the + * original. + * @return The moved file. + */ + public BoxFile moveFile(String fileId, String destinationFolderId, String newName) { + try { + LOG.debug("Moving file(id=" + fileId + ") to destination_folder(id=" + destinationFolderId + ")" + + (newName == null ? "" : " with new name '" + newName + "'")); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (destinationFolderId == null) { + throw new IllegalArgumentException("Parameter 'destinationFolderId' can not be null"); + } + BoxFile fileToMove = new BoxFile(boxConnection, fileId); + BoxFolder destinationFolder = new BoxFolder(boxConnection, destinationFolderId); + if (newName == null) { + return (BoxFile) fileToMove.move(destinationFolder).getResource(); + } else { + return (BoxFile) fileToMove.move(destinationFolder, newName).getResource(); + } + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Rename file giving it the name <code>newName</code> + * + * @param fileId + * - the id of file to rename. + * @param newFileName + * - the new name of file. + * @return The renamed file. + */ + public BoxFile renameFile(String fileId, String newFileName) { + try { + LOG.debug("Renaming file(id=" + fileId + ") to '" + newFileName + "'"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (newFileName == null) { + throw new IllegalArgumentException("Parameter 'newName' can not be null"); + } + BoxFile fileToRename = new BoxFile(boxConnection, fileId); + fileToRename.rename(newFileName); + return fileToRename; + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Delete the file. + * + * @param fileId + * - the id of file to delete. + */ + public void deleteFile(String fileId) { + try { + LOG.debug("Deleting file(id=" + fileId + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + BoxFile file = new BoxFile(boxConnection, fileId); + file.delete(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Delete a file version. + * + * @param fileId + * - the id of file with version to delete. + * @param version + * - the version of file to delete; initial version of file has + * value of <code>0</code>, second version of file is + * <code>1</code> and so on. + */ + public void deleteFileVersion(String fileId, Integer version) { + try { + LOG.debug("Deleting file(id=" + fileId + ", version=" + version + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (version == null) { + throw new IllegalArgumentException("Parameter 'version' can not be null"); + } + + BoxFile file = new BoxFile(boxConnection, fileId); + List<BoxFileVersion> versions = (List<BoxFileVersion>) file.getVersions(); + BoxFileVersion fileVersion = versions.get(version); + + fileVersion.delete(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Create a shared link to file. + * + * @param fileId + * - the id of the file to create shared link on. + * @param access + * - the access level of the shared link. + * @param unshareDate + * - the date and time at which time the created shared link will + * expire; if <code>unsharedDate</code> is <code>null</code> then + * a non-expiring link is created. + * @param permissions + * - the permissions of the created link; if + * <code>permissions</code> is <code>null</code> then the created + * shared link is create with default permissions. + * @return The created shared link. + */ + public BoxSharedLink createFileSharedLink(String fileId, BoxSharedLink.Access access, Date unshareDate, + BoxSharedLink.Permissions permissions) { + try { + LOG.debug("Creating shared link for file(id=" + fileId + ") with access=" + access + + (unshareDate == null ? "" + : " unsharedDate=" + SimpleDateFormat.getDateTimeInstance().format(unshareDate) + + " permissions=" + permissions)); + + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (access == null) { + throw new IllegalArgumentException("Parameter 'access' can not be null"); + } + + BoxFile file = new BoxFile(boxConnection, fileId); + return file.createSharedLink(access, unshareDate, permissions); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Get an expiring URL for downloading a file directly from Box. This can be + * user, for example, for sending as a redirect to a browser to cause the + * browser to download the file directly from Box. + * + * @param fileId + * - the id of file. + * @return The temporary download URL + */ + public URL getDownloadURL(String fileId) { + try { + LOG.debug("Getting download URL for file(id=" + fileId + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + + BoxFile file = new BoxFile(boxConnection, fileId); + + return file.getDownloadURL(); + + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Get an expiring URL for creating an embedded preview session. The URL + * will expire after 60 seconds and the preview session will expire after 60 + * minutes. + * + * @param fileId + * - the id of the file to get preview link on. + * @return The preview link. + */ + public URL getFilePreviewLink(String fileId) { + try { + LOG.debug("Getting preview link for file(id=" + fileId + ")"); + + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + + BoxFile file = new BoxFile(boxConnection, fileId); + return file.getPreviewLink(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Get an expiring URL for creating an embedded preview session. The URL + * will expire after 60 seconds and the preview session will expire after 60 + * minutes. + * + * @param fileId + * - the id of the file to get preview link on. + * @param fileType + * - either PNG of JPG. + * @param minWidth + * - minimum width. + * @param minHeight + * - minimum height. + * @param maxWidth + * - maximum width. + * @param maxHeight + * - maximum height. + * @return The byte array of the thumbnail image. + */ + public byte[] getFileThumbnail(String fileId, BoxFile.ThumbnailFileType fileType, Integer minWidth, + Integer minHeight, Integer maxWidth, Integer maxHeight) { + try { + LOG.debug("Getting thumbnail for file(id=" + fileId + ") fileType=" + fileType + " minWidth=" + minWidth + + " minHeight=" + minHeight + " maxWidth=" + maxWidth + " maxHeight=" + maxHeight); + + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (fileType == null) { + throw new IllegalArgumentException("Parameter 'fileType' can not be null"); + } + if (minWidth == null) { + throw new IllegalArgumentException("Parameter 'minWidth' can not be null"); + } + if (minHeight == null) { + throw new IllegalArgumentException("Parameter 'minHeight' can not be null"); + } + if (maxWidth == null) { + throw new IllegalArgumentException("Parameter 'maxWidth' can not be null"); + } + if (maxHeight == null) { + throw new IllegalArgumentException("Parameter 'maxHeight' can not be null"); + } + + BoxFile file = new BoxFile(boxConnection, fileId); + return file.getThumbnail(fileType, minWidth, minHeight, maxWidth, maxHeight); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Create metadata for file in either the global properties template or the + * specified template type. + * + * @param fileId + * - the id of the file to create metadata for. + * @param metadata + * - the new metadata values. + * @param typeName + * - the metadata template type name; if <code>null</code> the + * global properties template type is used. + * @return The metadata returned from the server. + */ + public Metadata createFileMetadata(String fileId, Metadata metadata, String typeName) { + try { + LOG.debug("Creating metadata for file(id=" + fileId + ")"); + + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (metadata == null) { + throw new IllegalArgumentException("Parameter 'metadata' can not be null"); + } + + BoxFile file = new BoxFile(boxConnection, fileId); + + if (typeName != null) { + return file.createMetadata(typeName, metadata); + } else { + return file.createMetadata(metadata); + } + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Gets the file properties metadata. + * + * @param fileId + * - the id of the file to retrieve metadata for. + * @param typeName + * - the metadata template type name; if <code>null</code> the + * global properties template type is used. + * @return The metadata returned from the server. + */ + public Metadata getFileMetadata(String fileId, String typeName) { + try { + LOG.debug("Get metadata for file(id=" + fileId + ")"); + + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + + BoxFile file = new BoxFile(boxConnection, fileId); + + if (typeName != null) { + return file.getMetadata(typeName); + } else { + return file.getMetadata(); + } + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + + } + + /** + * Update the file properties metadata. + * + * @param fileId + * - the id of file to delete. + * @param metadata + * - the new metadata values. + * @return The metadata returned from the server. + */ + public Metadata updateFileMetadata(String fileId, Metadata metadata) { + try { + LOG.debug("Updating metadata for file(id=" + fileId + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + if (metadata == null) { + throw new IllegalArgumentException("Parameter 'metadata' can not be null"); + } + BoxFile file = new BoxFile(boxConnection, fileId); + return file.updateMetadata(metadata); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Delete the file properties metadata. + * + * @param fileId + * - the id of file to delete. + */ + public void deleteFileMetadata(String fileId) { + try { + LOG.debug("Deleting metadata for file(id=" + fileId + ")"); + if (fileId == null) { + throw new IllegalArgumentException("Parameter 'fileId' can not be null"); + } + BoxFile file = new BoxFile(boxConnection, fileId); + file.deleteMetadata(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/45335d1e/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2FoldersManager.java ---------------------------------------------------------------------- diff --git a/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2FoldersManager.java b/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2FoldersManager.java new file mode 100644 index 0000000..4617fa2 --- /dev/null +++ b/components/camel-box2/camel-box2-api/src/main/java/org/apache/camel/component/box2/api/Box2FoldersManager.java @@ -0,0 +1,407 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.box2.api; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Date; + +import com.box.sdk.BoxAPIConnection; +import com.box.sdk.BoxAPIException; +import com.box.sdk.BoxFolder; +import com.box.sdk.BoxItem; +import com.box.sdk.BoxSharedLink; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Box2 Folders Manager + * + * <p> + * Provides operations to manage Box folders. + * + * @author <a href="mailto:[email protected]">William Collins</a> + * + */ +public class Box2FoldersManager { + + private static final Logger LOG = LoggerFactory.getLogger(Box2FoldersManager.class); + + /** + * Box connection to authenticated user account. + */ + private BoxAPIConnection boxConnection; + + /** + * Create folder manager to manage folders of Box connection's authenticated + * user. + * + * @param boxConnection + * - Box connection to authenticated user account. + */ + public Box2FoldersManager(BoxAPIConnection boxConnection) { + this.boxConnection = boxConnection; + } + + /** + * Return the root folder of authenticated user. + * + * @return The root folder of authenticated user. + */ + public BoxFolder getRootFolder() { + try { + LOG.debug("Getting root folder"); + return BoxFolder.getRootFolder(boxConnection); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Return the Box folder referenced by <code>path</code>. + * + * @param path + * - Sequence of Box folder names from root folder to returned + * folder. + * + * @return The Box folder referenced by <code>path</code> or + * <code>null</code> if folder is not found. + */ + public BoxFolder getFolder(String... path) { + try { + LOG.debug("Getting folder at path=" + Arrays.toString(path)); + + BoxFolder folder = BoxFolder.getRootFolder(boxConnection); + if (path == null || path.length == 0) { + // Return root folder if path is null or empty. + return folder; + } + + searchPath: for (int folderIndex = 0; folderIndex < path.length; folderIndex++) { + for (BoxItem.Info itemInfo : folder) { + if (itemInfo instanceof BoxFolder.Info && itemInfo.getName().equals(path[folderIndex])) { + folder = (BoxFolder) itemInfo.getResource(); + continue searchPath; + } + } + // Failed to find named folder in path: return null + return null; + } + return folder; + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Returns a specific range of child items in folder and specifies which + * fields of each item to retrieve. + * + * @param folderId + * - the id of folder. + * @param offset + * - the index of first child item to retrieve; if + * <code>null</code> all child items are retrieved. + * @param limit + * - the maximum number of children to retrieve after the offset; + * if <code>null</code> all child items are retrieved. + * @param fields + * - the item fields to retrieve for each child item; if + * <code>null</code> all item fields are retrieved. + * @return The Items in folder + */ + public Collection<BoxItem.Info> getFolderItems(String folderId, Long offset, Long limit, String... fields) { + try { + LOG.debug("Getting folder items in folder(id=" + folderId + ") at offset=" + offset + " and limit=" + limit + + " with fields=" + Arrays.toString(fields)); + if (folderId == null) { + throw new IllegalArgumentException("Parameter 'folderId' can not be null"); + } + BoxFolder folder = new BoxFolder(boxConnection, folderId); + if (fields == null) { + fields = new String[0]; + } + if (offset != null && limit != null) { + return folder.getChildrenRange(offset, limit, fields); + } else { + Collection<BoxItem.Info> folderItems = new ArrayList<BoxItem.Info>(); + Iterable<BoxItem.Info> iterable; + if (fields.length > 0) { + iterable = folder.getChildren(fields); + } else { + iterable = folder.getChildren(); + } + for (BoxItem.Info itemInfo : iterable) { + folderItems.add(itemInfo); + } + + return folderItems; + } + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Create a folder in parent folder with given <code>folderName</code>. + * + * @param parentFolderId + * - the id of parent folder. + * @param folderName + * the name of created folder. + * @return The created folder. + */ + public BoxFolder createFolder(String parentFolderId, String folderName) { + try { + LOG.debug("Creating folder with name '" + folderName + "' in parent_folder(id=" + parentFolderId + ")"); + if (parentFolderId == null) { + throw new IllegalArgumentException("Parameter 'parentFolderId' can not be null"); + } + if (folderName == null) { + throw new IllegalArgumentException("Paramerer 'folderName' can not be null"); + } + BoxFolder parentFolder = new BoxFolder(boxConnection, parentFolderId); + return parentFolder.createFolder(folderName).getResource(); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Copy folder to destination folder while optionally giving it a new name. + * + * @param folderId + * - the id of folder to copy. + * @param destinationFolderId + * - the id of the destination folder. + * @param newName + * - the new name for copied folder; if <code>newName</code> is + * <code>null</code>, the copied folder has same name as the + * original. + * @return The copied folder. + */ + public BoxFolder copyFolder(String folderId, String destinationFolderId, String newName) { + try { + LOG.debug("Copying folder(id=" + folderId + ") to destination_folder(id=" + destinationFolderId + ")" + + (newName == null ? "" : " with new name '" + newName + "'")); + if (folderId == null) { + throw new IllegalArgumentException("Parameter 'folderId' can not be null"); + } + if (destinationFolderId == null) { + throw new IllegalArgumentException("Parameter 'destinationFolderId' can not be null"); + } + BoxFolder folderToCopy = new BoxFolder(boxConnection, folderId); + BoxFolder destinationFolder = new BoxFolder(boxConnection, destinationFolderId); + if (newName == null) { + return folderToCopy.copy(destinationFolder).getResource(); + } else { + return folderToCopy.copy(destinationFolder, newName).getResource(); + } + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Move folder to destination folder while optionally giving it a new name. + * + * @param folderId + * - the id of folder to move. + * @param destinationFolderId + * - the id of the destination folder. + * @param newName + * - the new name of moved folder; if <code>newName</code> is + * <code>null</code>, the moved folder has same name as the + * original. + * @return The moved folder. + */ + public BoxFolder moveFolder(String folderId, String destinationFolderId, String newName) { + try { + LOG.debug("Moving folder(id=" + folderId + ") to destination_folder(id=" + destinationFolderId + ")" + + (newName == null ? "" : " with new name '" + newName + "'")); + if (folderId == null) { + throw new IllegalArgumentException("Parameter 'folderId' can not be null"); + } + if (destinationFolderId == null) { + throw new IllegalArgumentException("Parameter 'destinationFolderId' can not be null"); + } + BoxFolder folderToMove = new BoxFolder(boxConnection, folderId); + BoxFolder destinationFolder = new BoxFolder(boxConnection, destinationFolderId); + if (newName == null) { + return (BoxFolder) folderToMove.move(destinationFolder).getResource(); + } else { + return (BoxFolder) folderToMove.move(destinationFolder, newName).getResource(); + } + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Rename folder giving it the name <code>newName</code> + * + * @param folderId + * - the id of folder to rename. + * @param newFolderName + * - the new name of folder. + * @return The renamed folder. + */ + public BoxFolder renameFolder(String folderId, String newFolderName) { + try { + LOG.debug("Renaming folder(id=" + folderId + ") to '" + newFolderName + "'"); + if (folderId == null) { + throw new IllegalArgumentException("Parameter 'folderId' can not be null"); + } + if (newFolderName == null) { + throw new IllegalArgumentException("Parameter 'newFolderName' can not be null"); + } + BoxFolder folderToRename = new BoxFolder(boxConnection, folderId); + folderToRename.rename(newFolderName); + return folderToRename; + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Delete folder. + * + * @param folderId + * - the id of folder to delete. + */ + public void deleteFolder(String folderId) { + try { + LOG.debug("Deleting folder(id=" + folderId + ")"); + if (folderId == null) { + throw new IllegalArgumentException("Parameter 'folderId' can not be null"); + } + BoxFolder folder = new BoxFolder(boxConnection, folderId); + folder.delete(true); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Get folder information. + * + * @param folderId + * - the id of folder. + * @param fields + * - the information fields to retrieve; if <code>null</code> all + * information fields are retrieved. + * @return The folder information. + */ + public BoxFolder.Info getFolderInfo(String folderId, String... fields) { + try { + LOG.debug("Getting info for folder(id=" + folderId + ")"); + if (folderId == null) { + throw new IllegalArgumentException("Parameter 'folderId' can not be null"); + } + + BoxFolder folder = new BoxFolder(boxConnection, folderId); + + if (fields == null || fields.length == 0) { + return folder.getInfo(); + } else { + return folder.getInfo(fields); + } + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Update folder information. + * + * @param folderId + * - the id of folder to update. + * @param info + * - the updated information + * @return The updated folder. + */ + public BoxFolder updateFolderInfo(String folderId, BoxFolder.Info info) { + try { + LOG.debug("Updating info for folder(id=" + folderId + ")"); + if (folderId == null) { + throw new IllegalArgumentException("Parameter 'folderId' can not be null"); + } + if (info == null) { + throw new IllegalArgumentException("Parameter 'info' can not be null"); + } + BoxFolder folder = new BoxFolder(boxConnection, folderId); + folder.updateInfo(info); + return folder; + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + + /** + * Create a shared link to folder. + * + * @param folderId + * - the id of folder to create shared link on. + * @param access + * - the access level of the shared link. + * @param unshareDate + * - the date and time at which time the created shared link will + * expire; if <code>unsharedDate</code> is <code>null</code> then + * a non-expiring link is created. + * @param permissions + * - the permissions of the created link; if + * <code>permissions</code> is <code>null</code> then the created + * shared link is create with default permissions. + * @return The created shared link. + */ + public BoxSharedLink createFolderSharedLink(String folderId, BoxSharedLink.Access access, Date unshareDate, + BoxSharedLink.Permissions permissions) { + try { + LOG.debug("Creating shared link for folder(id=" + folderId + ") with access=" + access + + (unshareDate == null ? "" + : " unsharedDate=" + SimpleDateFormat.getDateTimeInstance().format(unshareDate) + + " permissions=" + permissions)); + + if (folderId == null) { + throw new IllegalArgumentException("Parameter 'folderId' can not be null"); + } + if (access == null) { + throw new IllegalArgumentException("Parameter 'access' can not be null"); + } + + BoxFolder folder = new BoxFolder(boxConnection, folderId); + return folder.createSharedLink(access, unshareDate, permissions); + } catch (BoxAPIException e) { + throw new RuntimeException( + String.format("Box API returned the error code %d\n\n%s", e.getResponseCode(), e.getResponse()), e); + } + } + +}
