Skip to content

Commit

Permalink
Make a workaround for Spring boot run as jar
Browse files Browse the repository at this point in the history
Fixes #8705
  • Loading branch information
Denis Anisimov committed Aug 4, 2020
1 parent d8e9274 commit 7e67099
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions flow-server/src/main/java/com/vaadin/flow/server/PwaRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@
import java.io.InputStreamReader;
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.vaadin.flow.server.frontend.FrontendUtils;
import org.slf4j.LoggerFactory;

import com.vaadin.flow.server.startup.ApplicationRouteRegistry;

import elemental.json.Json;
import elemental.json.JsonArray;
import elemental.json.JsonObject;
import org.slf4j.LoggerFactory;

/**
* Registry for PWA data.
Expand All @@ -59,6 +60,8 @@
* @since 1.2
*/
public class PwaRegistry implements Serializable {

private static final String META_INF_RESOURCES = "/META-INF/resources";
private static final String HEADLESS_PROPERTY = "java.awt.headless";
private static final String APPLE_STARTUP_IMAGE = "apple-touch-startup-image";
private static final String APPLE_IMAGE_MEDIA = "(device-width: %dpx) and (device-height: %dpx) "
Expand Down Expand Up @@ -96,18 +99,20 @@ public PwaRegistry(PWA pwa, ServletContext servletContext)

// Build pwa elements only if they are enabled
if (pwaConfiguration.isEnabled()) {
URL logo = servletContext
.getResource(pwaConfiguration.relIconPath());
URL offlinePage = servletContext
.getResource(pwaConfiguration.relOfflinePath());
URL logo = getUrl(servletContext, pwaConfiguration.relIconPath());

URL offlinePage = getUrl(servletContext,
pwaConfiguration.relOfflinePath());
// Load base logo from servlet context if available
// fall back to local image if unavailable
BufferedImage baseImage = getBaseImage(logo);

if (baseImage == null) {
LoggerFactory.getLogger(PwaRegistry.class).error("Image is not found or can't be loaded: " + logo);
LoggerFactory.getLogger(PwaRegistry.class).error(
"Image is not found or can't be loaded: " + logo);
} else {
// Pick top-left pixel as fill color if needed for image resizing
// Pick top-left pixel as fill color if needed for image
// resizing
int bgColor = baseImage.getRGB(0, 0);

// initialize icons
Expand All @@ -127,6 +132,19 @@ public PwaRegistry(PWA pwa, ServletContext servletContext)
}
}

private URL getUrl(ServletContext context, String path)
throws MalformedURLException {
URL logo = context.getResource(path);
if (logo == null) {
// this is a workaround specific for Spring default static resources
// location: see #8705
String cpPath = path.startsWith("/") ? META_INF_RESOURCES + path
: META_INF_RESOURCES + "/" + path;
logo = PwaRegistry.class.getResource(cpPath);
}
return logo;
}

private List<PwaIcon> initializeIcons(BufferedImage baseImage,
int bgColor) {
for (PwaIcon icon : getIconTemplates(pwaConfiguration.getIconPath())) {
Expand Down

0 comments on commit 7e67099

Please sign in to comment.