Skip to content

Commit

Permalink
Added caching to resource reader, validation check to Config and othe…
Browse files Browse the repository at this point in the history
…r minor fixes

Updated GroupDocs.Viewer for Java to v24.2
  • Loading branch information
oleksii-unnamed committed Mar 4, 2024
1 parent d8c519a commit 903c6dd
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 160 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-viewer-ui</artifactId>
<version>23.11</version>
<version>24.2</version>
<packaging>jar</packaging>

<name>GroupDocs.Viewer for Java User Interface</name>
Expand Down Expand Up @@ -54,7 +54,7 @@
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-viewer</artifactId>
<version>23.11</version>
<version>24.2</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions samples/groupdocs-viewer-java-ui-spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.example</groupId>
<artifactId>groupdocs-viewer-java-ui-spring-boot</artifactId>
<version>23.11</version>
<version>24.2</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
Expand All @@ -37,7 +37,7 @@
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-viewer</artifactId>
<version>23.11</version>
<version>24.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public DemoApplication(JakartaViewerEndpointHandler endpointHandler) {
}

@RequestMapping("/")
String home() {
return "<html><body><p>groupdocs-viewer-java-ui-spring-boot</p><p><a href=\"/viewer/\">Open GroupDocs.Viewer page</a></p><body/></html>";
public String home(HttpServletRequest request) {
return "<html><body><p>groupdocs-viewer-java-ui-spring-boot</p><p><a href=\"" + request.getContextPath() + "/viewer/\">Open GroupDocs.Viewer page</a></p><body/></html>";
}

@GetMapping({ ViewerConfiguration.VIEWER_UI_PATH, ViewerConfiguration.VIEWER_UI_PATH + "/**",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void init() {
_viewerEndpointHandler = JakartaViewerEndpointHandler
.setupGroupDocsViewer((viewerConfig, config) -> {
viewerConfig.setViewerType(ViewerType.PNG);
// viewerConfig.setLicensePath("GroupDocs.Viewer.Product.Family.lic");

config.setPreloadPageCount(2);
config.setBaseUrl("http://localhost:8080");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,49 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class UiEmbeddedResourcesReader implements IUiResourcesReader {

private static final String BASIC_RESOURCE_PATH = "com/groupdocs/viewerui";
private static final String BASIC_RESOURCE_PATH = "com/groupdocs/viewerui";
private List<UiResource> _cachedUiResources = null;

@Override
public UiResource getUiResource(String resourceName) throws IOException {
final ClassLoader classLoader = getClass().getClassLoader();
@Override
public UiResource getUiResource(String resourceName) throws IOException {
if (_cachedUiResources == null) {
_cachedUiResources = new ArrayList<>();
final UiResource uiResource = loadAndAddUiResource(getClass().getClassLoader(), resourceName);
_cachedUiResources.add(uiResource);
return uiResource;
} else {
final Optional<UiResource> firstResource = _cachedUiResources.stream().filter(uiResource -> resourceName.equals(uiResource.getFileName())).findFirst();
if (firstResource.isPresent()) {
return firstResource.get();
} else {
final UiResource uiResource = loadAndAddUiResource(getClass().getClassLoader(), resourceName);
_cachedUiResources.add(uiResource);
return uiResource;
}
}
}

private static UiResource loadAndAddUiResource(ClassLoader classLoader, String resourceName) throws IOException {
try (final InputStream resourceAsStream = classLoader
.getResourceAsStream(BASIC_RESOURCE_PATH + "/" + resourceName)) {
.getResourceAsStream(BASIC_RESOURCE_PATH + "/" + resourceName)) {
if (resourceAsStream == null) {
throw new ViewerUiException(
"Resource with name '" + Keys.GROUPDOCSVIEWERUI_MAIN_UI_RESOURCE + "' was not found");
}
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String mimeType = fileNameMap.getContentTypeFor(resourceName);
final String resourceContent = new String(StreamExtensions.toByteArray(resourceAsStream), StandardCharsets.UTF_8);
return UiResource.create(resourceName, resourceContent, mimeType);
final UiResource uiResource = UiResource.create(resourceName, resourceContent, mimeType);
return uiResource;
}
}

}
292 changes: 146 additions & 146 deletions src/main/resources/com/groupdocs/viewerui/main.js

Large diffs are not rendered by default.

0 comments on commit 903c6dd

Please sign in to comment.