Skip to content

Commit 3884798

Browse files
Updated Spring sample to support Dropbox as file storage via it's API
1 parent b04dc4e commit 3884798

18 files changed

Lines changed: 745 additions & 187 deletions

File tree

Demos/Dropwizard/src/main/java/com/groupdocs/ui/comparison/provider/DropboxFilesProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void receiveFilesInputStream(String path, Consumer<InputStream> streamCon
8787

8888
@Override
8989
public void receiveFilesOutputStream(String path, Consumer<OutputStream> streamConsumer) throws ApiException {
90-
logger.debug("Request to Dropbox API to upload file with name'" + path + "'");
90+
logger.debug("Request to Dropbox API to upload file with name '" + path + "'");
9191

9292
try {
9393
final DbxUserFilesRequests filesRequests = client.files();

Demos/Spring/configuration.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ common:
5050
################################################
5151
comparison:
5252
# Files provider, where to get files to show them in browse dialog
53-
# Available values are `google` - using Google Drive API, `local` - using local directories
53+
# Available values are `google` - using Google Drive API, `dropbox` - using Dropbox API, `local` - using local directories
5454
filesProviderType: ${FILES_PROVIDER_TYPE:local}
5555
# Result pages preload
5656
# How many pages from a result document should be loaded, remaining pages will be loaded on page scrolling
@@ -97,4 +97,14 @@ google:
9797
# Default value is `8888`
9898
localServerReceiverPort: ${LOCAL_SERVER_RECEIVER_PORT:8888}
9999
# user ID or null if not using a persisted credential store
100-
authorizationUserId: user
100+
authorizationUserId: user
101+
################################################
102+
# Dropbox API files provider Configuration
103+
################################################
104+
dropbox:
105+
# The identifier that will be used as User-Agent in API requests
106+
clientIdentifier: ${CLIENT_IDENTIFIER:groupdocs/comparison-java}
107+
# Dropbox access token
108+
accessToken: ${ACCESS_TOKEN:}
109+
# Directory in which result files will be uploaded via Dropbox API, default value is `ResultFiles`.
110+
resultDirectory: ${RESULT_DIR:ResultFiles}

Demos/Spring/pom.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
<properties>
5454
<start-class>com.groupdocs.ui.common.Application</start-class>
5555
<java.version>1.8</java.version>
56+
<maven.compiler.source>${java.version}</maven.compiler.source>
57+
<maven.compiler.target>${java.version}</maven.compiler.target>
5658
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5759
</properties>
5860

@@ -149,6 +151,13 @@
149151
<version>1.32.1</version>
150152
</dependency>
151153

154+
<!-- Dropbox API -->
155+
<dependency>
156+
<groupId>com.dropbox.core</groupId>
157+
<artifactId>dropbox-core-sdk</artifactId>
158+
<version>4.0.1</version>
159+
</dependency>
160+
152161
<dependency>
153162
<groupId>junit</groupId>
154163
<artifactId>junit</artifactId>
@@ -180,10 +189,6 @@
180189
<groupId>org.apache.maven.plugins</groupId>
181190
<artifactId>maven-compiler-plugin</artifactId>
182191
<version>3.0</version>
183-
<configuration>
184-
<source>1.8</source>
185-
<target>1.8</target>
186-
</configuration>
187192
</plugin>
188193
<plugin>
189194
<groupId>com.github.eirslett</groupId>

Demos/Spring/src/main/java/com/groupdocs/ui/common/Application.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@
2222
//@EnableAutoConfiguration
2323
@SpringBootApplication(
2424
scanBasePackages = {
25-
"com.groupdocs.ui",
26-
// "com.groupdocs.ui.common",
27-
// "com.groupdocs.ui.comparison",
28-
// "com.groupdocs.ui.common.config",
29-
// "com.groupdocs.ui.comparison.config",
30-
// "com.groupdocs.ui.comparison.resources",
31-
// "com.groupdocs.ui.comparison.service"
25+
"com.groupdocs.ui"
3226
}
3327
)
3428
public class Application extends SpringBootServletInitializer {

Demos/Spring/src/main/java/com/groupdocs/ui/common/Defaults.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface Application {
88

99
interface Comparison {
1010
enum FilesProviderType {
11-
LOCAL, GOOGLE
11+
LOCAL, GOOGLE, DROPBOX
1212
}
1313

1414
int DEFAULT_PRELOAD_RESULT_PAGE_COUNT = 2;

Demos/Spring/src/main/java/com/groupdocs/ui/common/config/GlobalConfiguration.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.groupdocs.ui.common.config;
22

33
import com.groupdocs.ui.comparison.config.ComparisonConfiguration;
4+
import com.groupdocs.ui.comparison.config.DropboxProviderConfiguration;
45
import com.groupdocs.ui.comparison.config.GoogleProviderConfiguration;
56
import com.groupdocs.ui.comparison.config.LocalProviderConfiguration;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.stereotype.Component;
89

10+
/**
11+
* The type Global configuration.
12+
*/
913
@Component
1014
public class GlobalConfiguration {
1115

@@ -26,27 +30,59 @@ public class GlobalConfiguration {
2630

2731
@Autowired
2832
private GoogleProviderConfiguration google;
33+
@Autowired
34+
private DropboxProviderConfiguration dropbox;
2935

36+
/**
37+
* Gets server configuration
38+
*
39+
* @return the server configuration
40+
*/
3041
public ServerConfiguration getServer() {
3142
return server;
3243
}
3344

45+
/**
46+
* Sets server configuration
47+
*
48+
* @param server the server configuration
49+
*/
3450
public void setServer(ServerConfiguration server) {
3551
this.server = server;
3652
}
3753

54+
/**
55+
* Gets application configuration
56+
*
57+
* @return the application configuration
58+
*/
3859
public ApplicationConfiguration getApplication() {
3960
return application;
4061
}
4162

63+
/**
64+
* Sets application configuration
65+
*
66+
* @param application the application configuration
67+
*/
4268
public void setApplication(ApplicationConfiguration application) {
4369
this.application = application;
4470
}
4571

72+
/**
73+
* Gets common configuration
74+
*
75+
* @return the common configuration
76+
*/
4677
public CommonConfiguration getCommon() {
4778
return common;
4879
}
4980

81+
/**
82+
* Gets comparison configuration
83+
*
84+
* @return the comparison configuration
85+
*/
5086
public ComparisonConfiguration getComparison() {
5187
return comparison;
5288
}
@@ -60,6 +96,15 @@ public GoogleProviderConfiguration getGoogle() {
6096
return google;
6197
}
6298

99+
/**
100+
* Gets dropbox provider configuration
101+
*
102+
* @return the dropbox provider configuration
103+
*/
104+
public DropboxProviderConfiguration getDropbox() {
105+
return dropbox;
106+
}
107+
63108
/**
64109
* Get local provider configuration
65110
*
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.groupdocs.ui.common.exception;
2+
3+
/**
4+
* The type Api exception.
5+
*/
6+
public class ApiException extends TotalGroupDocsException {
7+
8+
/**
9+
* Instantiates a new Api exception.
10+
*
11+
* @param message the message
12+
*/
13+
public ApiException(String message) {
14+
super(message);
15+
}
16+
17+
/**
18+
* Instantiates a new Api exception.
19+
*
20+
* @param message the message
21+
* @param cause the cause
22+
*/
23+
public ApiException(String message, Throwable cause) {
24+
super(message, cause);
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.groupdocs.ui.common.exception;
2+
3+
/**
4+
* The type Api exception.
5+
*/
6+
public class LocalDiskException extends TotalGroupDocsException {
7+
8+
/**
9+
* Instantiates a new Api exception.
10+
*
11+
* @param message the message
12+
*/
13+
public LocalDiskException(String message) {
14+
super(message);
15+
}
16+
17+
/**
18+
* Instantiates a new Api exception.
19+
*
20+
* @param message the message
21+
* @param cause the cause
22+
*/
23+
public LocalDiskException(String message, Throwable cause) {
24+
super(message, cause);
25+
}
26+
}

Demos/Spring/src/main/java/com/groupdocs/ui/common/util/CachedPageStream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public InputStream createPageStream(int pageIndex) {
6969
}
7070
logger.debug("Creating cached page stream...");
7171
try {
72-
return Files.newInputStream(mCacheFiles.get(pageIndex).toFile().toPath());
72+
return Files.newInputStream(mCacheFiles.get(pageIndex));
7373
} catch (Exception e) {
7474
throw new TotalGroupDocsException("Cache exception occurred", e);
7575
}
@@ -79,7 +79,7 @@ public Stream<PageStream> stream() {
7979
logger.debug("Iterating cached pages stream...");
8080
return mCacheFiles.entrySet().stream().map(entry -> {
8181
try {
82-
return new PageStream(entry.getKey(), Files.newInputStream(entry.getValue().toFile().toPath()));
82+
return new PageStream(entry.getKey(), Files.newInputStream(entry.getValue()));
8383
} catch (Exception e) {
8484
throw new TotalGroupDocsException("Cache exception occurred", e);
8585
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.groupdocs.ui.common.util;
2+
3+
import com.groupdocs.ui.common.exception.TotalGroupDocsException;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.io.IOException;
8+
import java.nio.file.FileSystems;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
13+
public final class TempFilesManager {
14+
private static final Logger logger = LoggerFactory.getLogger(TempFilesManager.class);
15+
private static TempFilesManager INSTANCE;
16+
private final Path tempDirectoryAbsolutePath;
17+
18+
private TempFilesManager(Path tempDirectory) {
19+
final boolean isRelative = !tempDirectory.isAbsolute();
20+
if (isRelative) {
21+
tempDirectory = FileSystems.getDefault().getPath(tempDirectory.toString()).toAbsolutePath();
22+
}
23+
logger.debug("Temp directory is going to be '" + tempDirectory + "', creating it...");
24+
Path createdTempDirectory = tempDirectory;
25+
try {
26+
if (Files.notExists(tempDirectory)) {
27+
createdTempDirectory = Files.createDirectories(tempDirectory);
28+
}
29+
} catch (IOException e) {
30+
logger.error("Exception throws while creating temp directory: '" + tempDirectory + "'");
31+
throw new TotalGroupDocsException("Can't access temp directory", e);
32+
}
33+
this.tempDirectoryAbsolutePath = createdTempDirectory.toAbsolutePath();
34+
}
35+
36+
public static void createInstance(String tempDirectory) {
37+
if (tempDirectory == null) {
38+
throw new IllegalArgumentException("tempDirectory must not be null");
39+
}
40+
INSTANCE = new TempFilesManager(Paths.get(tempDirectory));
41+
}
42+
43+
public static TempFilesManager getInstance() {
44+
return INSTANCE;
45+
}
46+
47+
public Path createTempPath(String fileName) {
48+
49+
return tempDirectoryAbsolutePath.resolve(fileName);
50+
}
51+
}

0 commit comments

Comments
 (0)