Skip to content

Commit 79842f9

Browse files
committed
Cache contextpath
1 parent 00d187f commit 79842f9

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

src/main/java/eu/openanalytics/containerproxy/api/ProxyRouteController.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
import eu.openanalytics.containerproxy.service.ProxyService;
2525
import eu.openanalytics.containerproxy.service.UserService;
2626
import eu.openanalytics.containerproxy.util.ProxyMappingManager;
27-
import eu.openanalytics.containerproxy.util.SessionHelper;
28-
import org.springframework.core.env.Environment;
27+
import eu.openanalytics.containerproxy.util.ContextPathHelper;
2928
import org.springframework.stereotype.Controller;
3029
import org.springframework.web.bind.annotation.RequestMapping;
3130

@@ -45,15 +44,12 @@ public class ProxyRouteController extends BaseController {
4544
@Inject
4645
private ProxyMappingManager mappingManager;
4746

48-
@Inject
49-
private Environment environment;
50-
5147
@RequestMapping(value="/api/route/**")
5248
public void route(HttpServletRequest request, HttpServletResponse response) {
5349
try {
5450
// Ensure that the caller is the owner of the target proxy.
5551
boolean hasAccess = false;
56-
String baseURL = SessionHelper.getContextPath(environment, true) + "api/route/";
52+
String baseURL = ContextPathHelper.withEndingSlash() + "api/route/";
5753
String mapping = request.getRequestURI().substring(baseURL.length());
5854
String proxyId = mappingManager.getProxyId(mapping);
5955
if (proxyId != null) {

src/main/java/eu/openanalytics/containerproxy/auth/impl/OpenIDAuthenticationBackend.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import eu.openanalytics.containerproxy.auth.impl.oidc.OpenIdReAuthorizeFilter;
2525
import eu.openanalytics.containerproxy.spec.expression.SpecExpressionContext;
2626
import eu.openanalytics.containerproxy.spec.expression.SpecExpressionResolver;
27-
import eu.openanalytics.containerproxy.util.SessionHelper;
27+
import eu.openanalytics.containerproxy.util.ContextPathHelper;
2828
import net.minidev.json.JSONArray;
2929
import net.minidev.json.parser.JSONParser;
3030
import net.minidev.json.parser.ParseException;
@@ -161,7 +161,7 @@ public void configureAuthenticationManagerBuilder(AuthenticationManagerBuilder a
161161
}
162162

163163
public String getLoginRedirectURI() {
164-
return SessionHelper.getContextPath(environment, false)
164+
return ContextPathHelper.withoutEndingSlash()
165165
+ OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI
166166
+ "/" + REG_ID;
167167
}

src/main/java/eu/openanalytics/containerproxy/auth/impl/SAMLAuthenticationBackend.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import eu.openanalytics.containerproxy.auth.IAuthenticationBackend;
2424
import eu.openanalytics.containerproxy.auth.impl.saml.AuthenticationFailureHandler;
2525
import eu.openanalytics.containerproxy.auth.impl.saml.Saml2MetadataFilter;
26-
import eu.openanalytics.containerproxy.util.SessionHelper;
26+
import eu.openanalytics.containerproxy.util.ContextPathHelper;
2727
import org.springframework.beans.factory.annotation.Autowired;
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2929
import org.springframework.core.env.Environment;
@@ -158,7 +158,7 @@ public static String determineLogoutSuccessURL(Environment environment) {
158158
}
159159

160160
public String getLoginRedirectURI() {
161-
return SessionHelper.getContextPath(environment, false)
161+
return ContextPathHelper.withoutEndingSlash()
162162
+ "/saml2/authenticate/"
163163
+ REG_ID;
164164
}

src/main/java/eu/openanalytics/containerproxy/util/SessionHelper.java renamed to src/main/java/eu/openanalytics/containerproxy/util/ContextPathHelper.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,37 @@
2020
*/
2121
package eu.openanalytics.containerproxy.util;
2222

23+
import org.springframework.beans.factory.annotation.Autowired;
2324
import org.springframework.core.env.Environment;
25+
import org.springframework.stereotype.Component;
2426

25-
public class SessionHelper {
27+
@Component
28+
public class ContextPathHelper {
2629

27-
/**
28-
* Get the context path that has been configured for this instance.
29-
*
30-
* @param environment The Spring environment containing the context-path setting.
31-
* @param endWithSlash True to always end the context path with a slash.
32-
* @return The instance's context path, may be empty, never null.
33-
*/
34-
public static String getContextPath(Environment environment, boolean endWithSlash) {
30+
private static String contextPathWithoutSlash = null;
31+
private static String contextPathWithSlash = null;
32+
33+
@Autowired
34+
public void setEnvironment(Environment environment){
35+
contextPathWithSlash = getContextPath(environment, true);
36+
contextPathWithoutSlash = getContextPath(environment, false);
37+
}
38+
39+
public static String withEndingSlash() {
40+
return contextPathWithSlash;
41+
}
42+
43+
public static String withoutEndingSlash() {
44+
return contextPathWithoutSlash;
45+
}
46+
47+
private static String getContextPath(Environment environment, boolean endWithSlash) {
3548
String contextPath = environment.getProperty("server.servlet.context-path");
3649
if (contextPath == null || contextPath.trim().equals("/") || contextPath.trim().isEmpty()) return endWithSlash ? "/" : "";
37-
50+
3851
if (!contextPath.startsWith("/")) contextPath = "/" + contextPath;
3952
if (endWithSlash && !contextPath.endsWith("/")) contextPath += "/";
40-
53+
4154
return contextPath;
4255
}
4356

0 commit comments

Comments
 (0)