Skip to content

Commit 7f89e7b

Browse files
Fixed bugs with svg and woff2 resources loading
1 parent 94052af commit 7f89e7b

File tree

7 files changed

+33
-13
lines changed

7 files changed

+33
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ User Interface for GroupDocs.Viewer for Java. API for easily integrating a docum
2121
<dependency>
2222
<groupId>com.groupdocs</groupId>
2323
<artifactId>groupdocs-viewer-ui</artifactId>
24-
<version>24.12.1</version>
24+
<version>24.12.2</version>
2525
</dependency>
2626

2727
<dependency>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.groupdocs</groupId>
88
<artifactId>groupdocs-viewer-ui</artifactId>
9-
<version>24.12.1</version>
9+
<version>24.12.2</version>
1010
<packaging>jar</packaging>
1111

1212
<name>GroupDocs.Viewer for Java User Interface</name>

res/simplified-pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<artifactId>groupdocs-viewer-ui</artifactId>
77
<name>GroupDocs.Viewer for Java User Interface</name>
88
<url>https://github.com/groupdocs-viewer/GroupDocs.Viewer-for-Java-UI</url>
9-
<version>24.12.1</version>
9+
<version>24.12.2</version>
1010
<description>
1111
GroupDocs.Viewer UI is a rich UI interface that designed to work in conjunction with GroupDocs.Viewer for Java to display most popular file and document formats in a browser.
1212
To integrate GroupDocs.Viewer UI in your Java project you just need to configure Viewer API and handle requests using specific endpoint handler provided in GroupDocs.Viewer.UI.

samples/groupdocs-viewer-java-ui-spring-boot/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>com.example</groupId>
1212
<artifactId>groupdocs-viewer-java-ui-spring-boot</artifactId>
13-
<version>24.12.1</version>
13+
<version>24.12.2</version>
1414
<name>demo</name>
1515
<description>Demo project for Spring Boot</description>
1616
<properties>

src/main/java/com/groupdocs/viewerui/handler/CommonViewerEndpointHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,9 @@ private UiResource prepareUiResourceForResponse(String requestUrl) throws IOExce
543543
}
544544
resourcePath = Paths.get(uiPath).normalize().relativize(Paths.get(path)).toString();
545545
}
546+
if (resourcePath.contains("\\")) {
547+
resourcePath = resourcePath.replaceAll("\\\\", "/");
548+
}
546549

547550
if (resourcePath.startsWith("/")) {
548551
resourcePath = resourcePath.substring(1);

src/main/java/com/groupdocs/viewerui/ui/core/DefaultContentTypeDetector.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.groupdocs.viewerui.ui.core;
22

3-
import java.io.IOException;
4-
import java.net.FileNameMap;
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
56
import java.net.URLConnection;
67
import java.nio.file.Files;
78
import java.nio.file.Path;
89
import java.nio.file.Paths;
910

1011
public class DefaultContentTypeDetector implements IContentTypeDetector {
12+
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultContentTypeDetector.class);
13+
1114
/**
1215
* Detects the content type of the given resource name.
1316
*
@@ -16,16 +19,22 @@ public class DefaultContentTypeDetector implements IContentTypeDetector {
1619
*/
1720
@Override
1821
public String detect(String resourceName) {
19-
FileNameMap fileNameMap = URLConnection.getFileNameMap();
20-
String contentType = fileNameMap.getContentTypeFor(resourceName);
22+
String contentType = URLConnection.guessContentTypeFromName(resourceName);
2123
if (contentType == null) {
22-
Path path = Paths.get(resourceName);
23-
try {
24-
contentType = Files.probeContentType(path);
25-
} catch (IOException e) {
26-
return "application/octet-stream";
24+
if (resourceName.endsWith(".woff") || resourceName.endsWith(".woff2")) {
25+
contentType = "application/x-font-woff";
26+
} else {
27+
try {
28+
Path path = Paths.get(resourceName);
29+
contentType = Files.probeContentType(path);
30+
} catch (Exception e2) {
31+
LOGGER.trace("Content type for '{}' was not detected, using 'application/octet-stream'", resourceName);
32+
}
2733
}
2834
}
35+
if (contentType == null) {
36+
contentType = "application/octet-stream";
37+
}
2938

3039
return contentType;
3140
}

src/main/java/com/groupdocs/viewerui/ui/core/UiEmbeddedResourcesReader.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.groupdocs.viewerui.Keys;
44
import com.groupdocs.viewerui.exception.ViewerUiException;
55
import com.groupdocs.viewerui.ui.core.extensions.StreamExtensions;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
68

79
import java.io.IOException;
810
import java.io.InputStream;
@@ -11,13 +13,18 @@
1113
import java.util.Optional;
1214

1315
public class UiEmbeddedResourcesReader implements IUiResourcesReader {
16+
private static final Logger LOGGER = LoggerFactory.getLogger(UiEmbeddedResourcesReader.class);
1417

1518
private static final String BASIC_RESOURCE_PATH = "com/groupdocs/viewerui";
1619
private List<UiResource> _cachedUiResources = null;
1720
private IContentTypeDetector _contentTypeDetector;
1821

1922
@Override
2023
public UiResource getUiResource(String resourceName) throws IOException {
24+
if (resourceName.contains("\\")) {
25+
LOGGER.error("Resource name is not valid (contains '\\' character): {}", resourceName);
26+
throw new ViewerUiException("Resource name is not valid (contains '\\' character): " + resourceName);
27+
}
2128
if (_cachedUiResources == null) {
2229
_cachedUiResources = new ArrayList<>();
2330
final UiResource uiResource = loadAndAddUiResource(getClass().getClassLoader(), resourceName, getContentTypeDetector());
@@ -39,6 +46,7 @@ private static UiResource loadAndAddUiResource(ClassLoader classLoader, String r
3946
try (final InputStream resourceAsStream = classLoader
4047
.getResourceAsStream(BASIC_RESOURCE_PATH + "/" + resourceName)) {
4148
if (resourceAsStream == null) {
49+
LOGGER.error("Failed loading UI resource (not found): {}", BASIC_RESOURCE_PATH + "/" + resourceName);
4250
throw new ViewerUiException(
4351
"Resource with name '" + Keys.GROUPDOCSVIEWERUI_MAIN_UI_RESOURCE + "' was not found");
4452
}

0 commit comments

Comments
 (0)