Skip to content

Commit 9f9b263

Browse files
Updated Dropwizard sample to use new Comparison API and Comparison v22.3
Added cache, based on sessions
1 parent ce5470d commit 9f9b263

20 files changed

Lines changed: 574 additions & 365 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
deploy_key
2+
.idea
23

34
#License files
4-
*.lic
5+
*.lic

Demos/Dropwizard/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,16 @@ For all methods above you can adjust settings in `configuration.yml`. By default
9797
### Comparison configuration options
9898

9999
| Option | Type | Default value | Description |
100-
| ---------------------------------- | ------- |:-----------------:|:-------------------------------------------------------------------------------------------------------------------------------------------- |
100+
| ---------------------------------- | ------- |:-----------------:|:---------------------------------------------------------------------------------------------------------------------------------------------|
101101
| **`filesDirectory`** | String | `DocumentSamples` | Files directory path. Indicates where uploaded and predefined files are stored. It can be absolute or relative path |
102102
| **`fontsDirectory`** | String | | Path to custom fonts directory. |
103103
| **`defaultDocument`** | String | | Absolute path to default document that will be loaded automaticaly. |
104104
| **`preloadPageCount`** | Integer | `0` | Indicate how many pages from a document should be loaded, remaining pages will be loaded on page scrolling.Set `0` to load all pages at once |
105105
| **`multiComparing`** | String | `true` | Enable/disable multi comparing feature |
106+
| **`sessionTimeout`** | Integer | `600` | Timeout, after which session will expire. Session expiration will remove files that were cached for the user |
107+
108+
#### Caching details
109+
Cache files will be deleted when session expires (see sessionTimeout option). **Note:** cache files will be deleted for current user. For user, who left the site forever, cache files WILL NOT be deleted. They must be deleted manually, or additional functionality must be implemented to do it.
106110

107111
## License
108112
The MIT License (MIT).

Demos/Dropwizard/configuration.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ common:
4545
browse: true
4646
# Set false to disable right mouse click
4747
enableRightClick: true
48+
# Session timeout (cache live time) in seconds
49+
sessionTimeout: 600
4850

4951
################################################
5052
# GroupDocs.Comparison configurations

Demos/Dropwizard/pom.xml

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.groupdocs.ui</groupId>
88
<artifactId>comparison-dropwizard</artifactId>
9-
<version>1.8.17</version>
9+
<version>22.3</version>
1010
<packaging>jar</packaging>
1111

1212
<name>GroupDocs.Comparison Dropwizard</name>
@@ -20,39 +20,39 @@
2020
<dependency>
2121
<groupId>io.dropwizard</groupId>
2222
<artifactId>dropwizard-core</artifactId>
23-
<version>1.3.0</version>
23+
<version>2.0.28</version>
2424
</dependency>
2525
<dependency>
2626
<groupId>io.dropwizard</groupId>
2727
<artifactId>dropwizard-views-freemarker</artifactId>
28-
<version>1.3.0</version>
28+
<version>2.0.28</version>
2929
</dependency>
3030
<dependency>
3131
<groupId>io.dropwizard</groupId>
3232
<artifactId>dropwizard-assets</artifactId>
33-
<version>1.3.0</version>
33+
<version>2.0.28</version>
3434
</dependency>
3535
<dependency>
3636
<groupId>io.dropwizard</groupId>
3737
<artifactId>dropwizard-auth</artifactId>
38-
<version>1.3.0</version>
38+
<version>2.0.28</version>
3939
</dependency>
4040
<dependency>
4141
<groupId>io.dropwizard</groupId>
4242
<artifactId>dropwizard-forms</artifactId>
43-
<version>1.3.0</version>
43+
<version>2.0.28</version>
4444
</dependency>
4545
<dependency>
4646
<groupId>io.dropwizard</groupId>
4747
<artifactId>dropwizard-testing</artifactId>
48-
<version>1.3.7</version>
48+
<version>2.0.28</version>
4949
<scope>test</scope>
5050
</dependency>
5151
<dependency>
5252
<groupId>io.dropwizard</groupId>
5353
<artifactId>dropwizard-client</artifactId>
5454
<scope>test</scope>
55-
<version>1.3.7</version>
55+
<version>2.0.28</version>
5656
</dependency>
5757
<dependency>
5858
<groupId>org.glassfish.jersey.core</groupId>
@@ -62,12 +62,12 @@
6262
<dependency>
6363
<groupId>org.gitlab4j</groupId>
6464
<artifactId>gitlab4j-api</artifactId>
65-
<version>4.6.9</version>
65+
<version>4.17.0</version>
6666
</dependency>
6767
<dependency>
6868
<groupId>org.json</groupId>
6969
<artifactId>json</artifactId>
70-
<version>20180130</version>
70+
<version>20211205</version>
7171
</dependency>
7272
<dependency>
7373
<groupId>commons-io</groupId>
@@ -77,7 +77,7 @@
7777
<dependency>
7878
<groupId>com.groupdocs</groupId>
7979
<artifactId>groupdocs-comparison</artifactId>
80-
<version>19.6</version>
80+
<version>${project.version}</version>
8181
<type>jar</type>
8282
</dependency>
8383
<dependency>
@@ -88,7 +88,13 @@
8888
<dependency>
8989
<groupId>javax.activation</groupId>
9090
<artifactId>activation</artifactId>
91-
<version>1.1</version>
91+
<version>1.1.1</version>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.assertj</groupId>
95+
<artifactId>assertj-core</artifactId>
96+
<version>2.9.1</version>
97+
<scope>test</scope>
9298
</dependency>
9399
</dependencies>
94100

@@ -238,9 +244,13 @@
238244
<url>http://repository.springsource.com/maven/bundles/external</url>
239245
</repository>
240246
<repository>
241-
<id>GroupDocsJavaAPI</id>
242-
<name>GroupDocs Java API</name>
243-
<url>http://artifact.groupdocs.com/repo/</url>
247+
<id>maven.groupdocs.com</id>
248+
<name>artifact.groupdocs.com-releases</name>
249+
<url>https://repository.groupdocs.com/repo/</url>
250+
</repository>
251+
<repository>
252+
<id>maven-central</id>
253+
<url>https://repo1.maven.org/maven2</url>
244254
</repository>
245255
</repositories>
246256

Demos/Dropwizard/src/main/java/com/groupdocs/ui/common/MainService.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.groupdocs.ui.common;
22

33
import com.google.common.collect.Sets;
4+
import com.groupdocs.ui.common.config.CommonConfiguration;
45
import com.groupdocs.ui.common.config.GlobalConfiguration;
56
import com.groupdocs.ui.common.exception.TotalGroupDocsExceptionMapper;
67
import com.groupdocs.ui.common.health.TemplateHealthCheck;
@@ -15,13 +16,17 @@
1516
import io.dropwizard.setup.Environment;
1617
import io.dropwizard.views.ViewBundle;
1718
import org.apache.commons.lang3.StringUtils;
19+
import org.eclipse.jetty.server.Handler;
20+
import org.eclipse.jetty.server.session.SessionHandler;
1821
import org.eclipse.jetty.servlets.CrossOriginFilter;
1922
import org.slf4j.Logger;
2023
import org.slf4j.LoggerFactory;
2124

2225
import javax.imageio.ImageIO;
2326
import javax.servlet.DispatcherType;
2427
import javax.servlet.FilterRegistration;
28+
import javax.servlet.http.HttpSessionEvent;
29+
import javax.servlet.http.HttpSessionListener;
2530
import java.io.File;
2631
import java.util.EnumSet;
2732
import java.util.HashSet;
@@ -100,6 +105,7 @@ public void run(GlobalConfiguration globalConfiguration, Environment environment
100105
// Enable CORS headers
101106
final FilterRegistration.Dynamic cors = environment.servlets().addFilter("CORS", CrossOriginFilter.class);
102107

108+
103109
// Configure CORS parameters
104110
cors.setInitParameter("allowedOrigins", "*");
105111
cors.setInitParameter("allowedHeaders", "X-Requested-With,Content-Type,Accept,Origin");
@@ -109,7 +115,8 @@ public void run(GlobalConfiguration globalConfiguration, Environment environment
109115
cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
110116

111117
// Initiate resources (web pages)
112-
environment.jersey().register(new ComparisonResources(globalConfiguration));
118+
final ComparisonResources comparisonResources = new ComparisonResources(globalConfiguration);
119+
environment.jersey().register(comparisonResources);
113120

114121
// Add custom exception mapper
115122
environment.jersey().register(new TotalGroupDocsExceptionMapper());
@@ -118,5 +125,12 @@ public void run(GlobalConfiguration globalConfiguration, Environment environment
118125
// TODO: implement health check
119126
final TemplateHealthCheck healthCheck = new TemplateHealthCheck("");
120127
environment.healthChecks().register("HealthCheck", healthCheck);
128+
129+
final CommonConfiguration commonConfiguration = globalConfiguration.getCommon();
130+
131+
final SessionHandler sessionHandler = new SessionHandler();
132+
sessionHandler.setMaxInactiveInterval(commonConfiguration.getSessionTimeout());
133+
sessionHandler.addEventListener(comparisonResources);
134+
environment.servlets().setSessionHandler(sessionHandler);
121135
}
122136
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public class CommonConfiguration extends Configuration {
4040
@JsonProperty
4141
private boolean enableRightClick;
4242

43+
@Valid
44+
@JsonProperty
45+
private int sessionTimeout;
46+
4347
public boolean isPageSelector() {
4448
return pageSelector;
4549
}
@@ -95,4 +99,12 @@ public boolean isEnableRightClick() {
9599
public void setEnableRightClick(boolean enableRightClick) {
96100
this.enableRightClick = enableRightClick;
97101
}
102+
103+
public int getSessionTimeout() {
104+
return sessionTimeout;
105+
}
106+
107+
public void setSessionTimeout(int sessionTimeout) {
108+
this.sessionTimeout = sessionTimeout;
109+
}
98110
}

Demos/Dropwizard/src/main/java/com/groupdocs/ui/common/entity/web/request/FileTreeRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package com.groupdocs.ui.common.entity.web.request;
22

3+
import org.apache.commons.lang3.StringUtils;
4+
35
public class FileTreeRequest {
46
private String path;
57

8+
public boolean isNotEmpty() {
9+
return StringUtils.isNotEmpty(path) && StringUtils.isNotBlank(path);
10+
}
11+
612
public String getPath() {
713
return path;
814
}

Demos/Dropwizard/src/main/java/com/groupdocs/ui/common/entity/web/request/LoadDocumentRequest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.groupdocs.ui.common.entity.web.request;
22

3+
import org.apache.commons.lang3.StringUtils;
4+
35
public class LoadDocumentRequest {
46

57
private String guid;
@@ -14,7 +16,7 @@ public void setGuid(String guid) {
1416
}
1517

1618
public String getPassword() {
17-
return password;
19+
return StringUtils.isEmpty(password) || StringUtils.isBlank(password) ? null : password;
1820
}
1921

2022
public void setPassword(String password) {
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.groupdocs.ui.common.util;
2+
3+
import com.groupdocs.comparison.common.delegates.Delegates;
4+
import com.groupdocs.ui.common.exception.TotalGroupDocsException;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.io.*;
9+
import java.nio.file.Path;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
import java.util.stream.Stream;
13+
14+
public class CachedPageStream implements Delegates.CreatePageStream {
15+
private static final Logger logger = LoggerFactory.getLogger(CachedPageStream.class);
16+
17+
private final SessionCache mSessionCache;
18+
private final String mGuid;
19+
private final Map<Integer, Path> mCacheFiles;
20+
21+
public CachedPageStream(SessionCache sessionCache, String guid) {
22+
if (sessionCache == null) {
23+
throw new IllegalArgumentException("Session cache is null!");
24+
}
25+
mSessionCache = sessionCache;
26+
mGuid = guid;
27+
mCacheFiles = new HashMap<>();
28+
}
29+
30+
public boolean isCached() {
31+
if (mCacheFiles.isEmpty()) {
32+
for (int pageNumber = 1; pageNumber < Integer.MAX_VALUE; pageNumber++) {
33+
final String fileName = createFileName(mGuid, pageNumber);
34+
if (mSessionCache.isCacheEntryExist(fileName)) {
35+
mCacheFiles.put(pageNumber - 1, mSessionCache.getCacheEntry(fileName));
36+
} else {
37+
break;
38+
}
39+
}
40+
}
41+
42+
final boolean isCached = !mCacheFiles.isEmpty();
43+
44+
logger.error("File with guid='" + mGuid + "' " + (isCached ? "is cached" : "is NOT cached"));
45+
return isCached;
46+
}
47+
48+
@Override
49+
public OutputStream invoke(int pageNumber) {
50+
try {
51+
final String fileName = createFileName(mGuid, pageNumber);
52+
final Path cacheEntry = mSessionCache.createCacheEntry(fileName);
53+
mCacheFiles.put(pageNumber - 1 /* to pageIndex */, cacheEntry);
54+
return new FileOutputStream(cacheEntry.toFile());
55+
} catch (FileNotFoundException e) {
56+
logger.error("Exception occurred while creating cache file", e);
57+
throw new TotalGroupDocsException("Exception occurred while creating cache file", e);
58+
}
59+
}
60+
61+
public int getPagesCount() {
62+
return mCacheFiles.size();
63+
}
64+
65+
public InputStream createPageStream(int pageIndex) {
66+
if (!mCacheFiles.containsKey(pageIndex)) {
67+
throw new IllegalArgumentException("Incorrect page index");
68+
}
69+
try {
70+
return new FileInputStream(mCacheFiles.get(pageIndex).toFile());
71+
} catch (Exception e) {
72+
logger.error("Exception occurred while creating page stream", e);
73+
throw new TotalGroupDocsException("Exception occurred while creating page stream", e);
74+
}
75+
}
76+
77+
public Stream<PageStream> stream() {
78+
return mCacheFiles.entrySet().stream().map(entry -> {
79+
try {
80+
return new PageStream(entry.getKey(), new FileInputStream(entry.getValue().toFile()));
81+
} catch (Exception e) {
82+
logger.error("Exception occurred while iterating cache files", e);
83+
throw new TotalGroupDocsException("Exception occurred while iterating cache files", e);
84+
}
85+
});
86+
87+
}
88+
89+
private static String createFileName(String guid, int pageNumber) {
90+
return "gd_" + guid.hashCode() + "_page_" + pageNumber + ".cache";
91+
}
92+
93+
public static class PageStream {
94+
public final int pageIndex;
95+
public final InputStream pageStream;
96+
97+
public PageStream(int pageIndex, InputStream pageStream) {
98+
this.pageIndex = pageIndex;
99+
this.pageStream = pageStream;
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)