Skip to content

Commit 7e95d22

Browse files
Updated Dropwizard sample to support Dropbox as file storage via it's API
1 parent aa3e628 commit 7e95d22

15 files changed

Lines changed: 652 additions & 172 deletions

Demos/Dropwizard/configuration.yml

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

Demos/Dropwizard/pom.xml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<url>https://groupdocs.com</url>
1414

1515
<properties>
16+
<java.version>15</java.version>
17+
<maven.compiler.source>${java.version}</maven.compiler.source>
18+
<maven.compiler.target>${java.version}</maven.compiler.target>
1619
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1720
</properties>
1821

@@ -21,7 +24,7 @@
2124
<groupId>io.dropwizard</groupId>
2225
<artifactId>dropwizard-core</artifactId>
2326
<version>2.0.28</version>
24-
</dependency>
27+
</dependency>
2528
<dependency>
2629
<groupId>io.dropwizard</groupId>
2730
<artifactId>dropwizard-views-freemarker</artifactId>
@@ -85,6 +88,16 @@
8588
<artifactId>jaxb-api</artifactId>
8689
<version>2.3.0</version>
8790
</dependency>
91+
<dependency>
92+
<groupId>com.fasterxml.jackson.core</groupId>
93+
<artifactId>jackson-databind</artifactId>
94+
<version>2.13.3</version>
95+
</dependency>
96+
<dependency>
97+
<groupId>com.fasterxml.jackson.core</groupId>
98+
<artifactId>jackson-annotations</artifactId>
99+
<version>2.13.3</version>
100+
</dependency>
88101
<dependency>
89102
<groupId>javax.activation</groupId>
90103
<artifactId>activation</artifactId>
@@ -108,6 +121,13 @@
108121
<version>1.32.1</version>
109122
</dependency>
110123

124+
<!-- Dropbox API -->
125+
<dependency>
126+
<groupId>com.dropbox.core</groupId>
127+
<artifactId>dropbox-core-sdk</artifactId>
128+
<version>5.2.0</version>
129+
</dependency>
130+
111131
<dependency>
112132
<groupId>org.assertj</groupId>
113133
<artifactId>assertj-core</artifactId>
@@ -122,10 +142,6 @@
122142
<groupId>org.apache.maven.plugins</groupId>
123143
<artifactId>maven-compiler-plugin</artifactId>
124144
<version>3.0</version>
125-
<configuration>
126-
<source>1.8</source>
127-
<target>1.8</target>
128-
</configuration>
129145
</plugin>
130146
<plugin>
131147
<groupId>org.codehaus.mojo</groupId>

Demos/Dropwizard/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/Dropwizard/src/main/java/com/groupdocs/ui/common/MainService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.groupdocs.ui.common.config.GlobalConfiguration;
66
import com.groupdocs.ui.common.exception.TotalGroupDocsExceptionMapper;
77
import com.groupdocs.ui.common.health.TemplateHealthCheck;
8+
import com.groupdocs.ui.common.util.TempFilesManager;
89
import com.groupdocs.ui.comparison.resources.ComparisonResources;
910
import io.dropwizard.Application;
1011
import io.dropwizard.assets.AssetsBundle;
@@ -125,6 +126,8 @@ public void run(GlobalConfiguration globalConfiguration, Environment environment
125126

126127
final CommonConfiguration commonConfiguration = globalConfiguration.getCommon();
127128

129+
TempFilesManager.createInstance(globalConfiguration.getComparison().getTempDirectory());
130+
128131
final SessionHandler sessionHandler = new SessionHandler();
129132
sessionHandler.setMaxInactiveInterval(commonConfiguration.getSessionTimeout());
130133
sessionHandler.addEventListener(comparisonResources);

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import com.groupdocs.ui.comparison.config.ComparisonConfiguration;
5+
import com.groupdocs.ui.comparison.config.DropboxProviderConfiguration;
56
import com.groupdocs.ui.comparison.config.GoogleProviderConfiguration;
67
import com.groupdocs.ui.comparison.config.LocalProviderConfiguration;
78
import io.dropwizard.Configuration;
@@ -40,6 +41,10 @@ public class GlobalConfiguration extends Configuration {
4041
@JsonProperty
4142
private GoogleProviderConfiguration google;
4243

44+
@Valid
45+
@JsonProperty
46+
private DropboxProviderConfiguration dropbox;
47+
4348
/**
4449
* Constructor
4550
*/
@@ -50,6 +55,7 @@ public GlobalConfiguration() {
5055
comparison = new ComparisonConfiguration();
5156
local = new LocalProviderConfiguration();
5257
google = new GoogleProviderConfiguration();
58+
dropbox = new DropboxProviderConfiguration();
5359
}
5460

5561
/**
@@ -88,6 +94,15 @@ public ComparisonConfiguration getComparison() {
8894
return comparison;
8995
}
9096

97+
/**
98+
* Get local provider configuration
99+
*
100+
* @return local provider configuration
101+
*/
102+
public LocalProviderConfiguration getLocal() {
103+
return local;
104+
}
105+
91106
/**
92107
* Get google provider configuration
93108
*
@@ -98,12 +113,12 @@ public GoogleProviderConfiguration getGoogle() {
98113
}
99114

100115
/**
101-
* Get local provider configuration
116+
* Get dropbox provider configuration
102117
*
103-
* @return local provider configuration
118+
* @return dropbox provider configuration
104119
*/
105-
public LocalProviderConfiguration getLocal() {
106-
return local;
120+
public DropboxProviderConfiguration getDropbox() {
121+
return dropbox;
107122
}
108123
}
109124

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+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
12+
public final class TempFilesManager {
13+
private static final Logger logger = LoggerFactory.getLogger(TempFilesManager.class);
14+
private static TempFilesManager INSTANCE;
15+
private final Path tempDirectoryAbsolutePath;
16+
17+
private TempFilesManager(Path tempDirectory) {
18+
final boolean isRelative = !tempDirectory.isAbsolute();
19+
if (isRelative) {
20+
tempDirectory = FileSystems.getDefault().getPath(tempDirectory.toString()).toAbsolutePath();
21+
}
22+
logger.debug("Temp directory is going to be '" + tempDirectory + "', creating it...");
23+
Path createdTempDirectory = tempDirectory;
24+
try {
25+
if (Files.notExists(tempDirectory)) {
26+
createdTempDirectory = Files.createDirectories(tempDirectory);
27+
}
28+
} catch (IOException e) {
29+
logger.error("Exception throws while creating temp directory: '" + tempDirectory + "'");
30+
throw new TotalGroupDocsException("Can't access temp directory", e);
31+
}
32+
this.tempDirectoryAbsolutePath = createdTempDirectory.toAbsolutePath();
33+
}
34+
35+
public static void createInstance(String tempDirectory) {
36+
if (tempDirectory == null) {
37+
throw new IllegalArgumentException("tempDirectory must not be null");
38+
}
39+
INSTANCE = new TempFilesManager(Path.of(tempDirectory));
40+
}
41+
42+
public static TempFilesManager getInstance() {
43+
return INSTANCE;
44+
}
45+
46+
public java.nio.file.Path createTempPath(String fileName) {
47+
48+
return tempDirectoryAbsolutePath.resolve(fileName);
49+
}
50+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.groupdocs.ui.comparison.config;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import io.dropwizard.Configuration;
5+
import org.apache.commons.lang3.StringUtils;
6+
7+
import javax.validation.Valid;
8+
9+
/**
10+
* The type Dropbox provider configuration.
11+
*/
12+
public class DropboxProviderConfiguration extends Configuration {
13+
14+
@Valid
15+
@JsonProperty
16+
private String clientIdentifier;
17+
18+
@Valid
19+
@JsonProperty
20+
private String accessToken;
21+
22+
@Valid
23+
@JsonProperty
24+
private String resultDirectory;
25+
26+
/**
27+
* The identifier that will be used as User-Agent in API requests
28+
*
29+
* @return The identifier
30+
*/
31+
public String getClientIdentifier() {
32+
return StringUtils.isBlank(clientIdentifier) ? "groupdocs/comparison-java" : clientIdentifier;
33+
}
34+
35+
/**
36+
* The identifier that will be used as User-Agent in API requests
37+
*
38+
* @param clientIdentifier The identifier
39+
*/
40+
public void setClientIdentifier(String clientIdentifier) {
41+
this.clientIdentifier = clientIdentifier;
42+
}
43+
44+
/**
45+
* Dropbox access token
46+
*
47+
* @return access token
48+
*/
49+
public String getAccessToken() {
50+
return accessToken;
51+
}
52+
53+
/**
54+
* Dropbox access token
55+
*
56+
* @param accessToken access token
57+
*/
58+
public void setAccessToken(String accessToken) {
59+
this.accessToken = accessToken;
60+
}
61+
62+
/**
63+
* Directory in which result files will be uploaded via Dropbox API, default value is `ResultFiles`.
64+
*
65+
* @return the result directory
66+
*/
67+
public String getResultDirectory() {
68+
return StringUtils.isBlank(resultDirectory) ? "ResultFiles" : resultDirectory;
69+
}
70+
71+
/**
72+
* Directory in which result files will be uploaded via Dropbox API, default value is `ResultFiles`.
73+
*
74+
* @param resultDirectory the result directory
75+
*/
76+
public void setResultDirectory(String resultDirectory) {
77+
this.resultDirectory = resultDirectory;
78+
}
79+
80+
@Override
81+
public String toString() {
82+
return "DropboxProviderConfiguration{" +
83+
"clientIdentifier='" + getClientIdentifier() + '\'' +
84+
", accessToken='" + getAccessToken() + '\'' +
85+
", resultDirectory='" + getResultDirectory() + '\'' +
86+
'}';
87+
}
88+
}

0 commit comments

Comments
 (0)