Skip to content

Commit cda079a

Browse files
authored
Merge pull request #235 from jdev-org/issue-230-button-provider-img
#230 - Use static path to find provider button's img
2 parents b388a9e + e2099a6 commit cda079a

File tree

2 files changed

+110
-5
lines changed

2 files changed

+110
-5
lines changed

gateway/src/main/java/org/georchestra/gateway/app/LoginLogoutController.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.georchestra.gateway.app;
2020

21-
import java.nio.file.Path;
2221
import java.util.*;
2322

2423
import org.apache.commons.lang3.tuple.Pair;
@@ -27,7 +26,8 @@
2726
import org.springframework.beans.factory.annotation.Autowired;
2827
import org.springframework.beans.factory.annotation.Value;
2928
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties;
30-
import org.springframework.core.io.ClassPathResource;
29+
import org.springframework.boot.autoconfigure.web.WebProperties;
30+
import org.springframework.core.env.Environment;
3131
import org.springframework.stereotype.Controller;
3232
import org.springframework.ui.Model;
3333
import org.springframework.web.bind.annotation.GetMapping;
@@ -59,6 +59,10 @@
5959
@Controller
6060
public class LoginLogoutController {
6161

62+
private @Autowired(required = false) WebProperties webProperties;
63+
64+
private @Autowired(required = false) Environment environment;
65+
6266
/** Configuration properties for gateway security, including LDAP settings. */
6367
private @Autowired(required = false) GeorchestraGatewaySecurityConfigProperties georchestraGatewaySecurityConfigProperties;
6468

@@ -149,9 +153,21 @@ public String loginPage(@RequestParam Map<String, String> allRequestParams, Mode
149153
if (oauth2ClientConfig != null) {
150154
oauth2ClientConfig.getRegistration().forEach((key, value) -> {
151155
String clientName = Optional.ofNullable(value.getClientName()).orElse(key);
152-
String providerPath = Path.of("login/img/", key + ".png").toString();
153-
String logo = new ClassPathResource("static/" + providerPath).exists() ? providerPath
154-
: "login/img/default.png";
156+
String[] locations = webProperties.getResources().getStaticLocations();
157+
String staticPath = StaticResourcesUtils.computeStaticResourceWebPrefix(environment);
158+
// default logo
159+
String logo = "login/img/default.png";
160+
// provider logo
161+
String providerPath = "login/img/" + key + ".png";
162+
// loop over static locations
163+
for (String location : locations) {
164+
String base = location.endsWith("/") ? location : location + "/";
165+
if (StaticResourcesUtils.resourceExists(base + providerPath)) {
166+
// use logo if exists or use default
167+
logo = staticPath + providerPath;
168+
break;
169+
}
170+
}
155171
oauth2LoginLinks.put("/oauth2/authorization/" + key, Pair.of(clientName, logo));
156172
});
157173
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.georchestra.gateway.app;
2+
3+
import org.springframework.core.env.Environment;
4+
import org.springframework.core.io.ClassPathResource;
5+
import org.springframework.core.io.FileSystemResource;
6+
import org.springframework.core.io.Resource;
7+
import org.springframework.util.StringUtils;
8+
9+
/**
10+
* Utility methods for resolving and checking static resources, and for
11+
* computing the web prefix used to serve static content.
12+
*/
13+
public class StaticResourcesUtils {
14+
/**
15+
* Resolves a resource path into a Spring {@link Resource} instance.
16+
* <p>
17+
* Supported prefixes:
18+
* <ul>
19+
* <li><b>classpath:</b> resolved as a {@link ClassPathResource}</li>
20+
* <li><b>file:</b> resolved as a {@link FileSystemResource}</li>
21+
* <li>otherwise</li>
22+
* </ul>
23+
* If no prefix is provided, the path is interpreted as a filesystem path.
24+
*
25+
* @param path fully qualified resource path
26+
* @return the resolved {@link Resource}
27+
*/
28+
public static Resource resolveResource(String path) {
29+
if (path.startsWith("classpath:")) {
30+
return new ClassPathResource(path.substring(10));
31+
}
32+
if (path.startsWith("file:")) {
33+
return new FileSystemResource(path.substring(5));
34+
}
35+
return new FileSystemResource(path);
36+
}
37+
38+
/**
39+
* Checks whether a resource exists and is readable.
40+
* <p>
41+
* Resolution is delegated to {@link #resolveResource(String)}.
42+
*
43+
* @param path fully qualified resource path
44+
* @return {@code true} if the resource exists and can be read
45+
*/
46+
public static boolean resourceExists(String path) {
47+
return resolveResource(path).isReadable();
48+
}
49+
50+
/**
51+
* Computes the web prefix used for serving static resources, based on Spring
52+
* MVC or WebFlux configuration.
53+
* <p>
54+
* Reads:
55+
* <ul>
56+
* <li>{@code spring.webflux.static-path-pattern}</li>
57+
* <li>{@code spring.mvc.static-path-pattern}</li>
58+
* </ul>
59+
* If no pattern is defined, "/" is returned. The resulting prefix is normalized
60+
* to start and end with a slash, and the terminal "/**" pattern is removed if
61+
* present.
62+
*
63+
* @param environment the Spring {@link Environment}
64+
* @return the normalized static web prefix (for example "/static/")
65+
*/
66+
public static String computeStaticResourceWebPrefix(Environment environment) {
67+
String pattern = environment.getProperty("spring.webflux.static-path-pattern");
68+
if (!StringUtils.hasText(pattern)) {
69+
pattern = environment.getProperty("spring.mvc.static-path-pattern");
70+
}
71+
if (!StringUtils.hasText(pattern)) {
72+
return "/";
73+
}
74+
pattern = pattern.trim();
75+
if (!pattern.startsWith("/")) {
76+
pattern = "/" + pattern;
77+
}
78+
if (pattern.endsWith("/**")) {
79+
pattern = pattern.substring(0, pattern.length() - 2);
80+
}
81+
if (pattern.isEmpty() || "/".equals(pattern)) {
82+
return "/";
83+
}
84+
if (!pattern.endsWith("/")) {
85+
pattern = pattern + "/";
86+
}
87+
return pattern;
88+
}
89+
}

0 commit comments

Comments
 (0)