From 0d050f023f4607d4c7d5967ad944f6a82e419122 Mon Sep 17 00:00:00 2001
From: Daniel Gerhardt <code@dgerhardt.net>
Date: Sat, 17 Jan 2015 16:02:41 +0100
Subject: [PATCH] Allow manually setting the API path for installations behind
 a proxy

---
 .../arsnova/controller/ConfigurationController.java   |  8 +++++++-
 .../de/thm/arsnova/controller/LoginController.java    | 11 +++++++++--
 src/main/resources/arsnova.properties.example         |  5 +++++
 src/test/resources/arsnova.properties.example         |  5 +++++
 4 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/src/main/java/de/thm/arsnova/controller/ConfigurationController.java b/src/main/java/de/thm/arsnova/controller/ConfigurationController.java
index 0fe5a45ce..badd966a5 100644
--- a/src/main/java/de/thm/arsnova/controller/ConfigurationController.java
+++ b/src/main/java/de/thm/arsnova/controller/ConfigurationController.java
@@ -44,6 +44,9 @@ public class ConfigurationController extends AbstractController {
 	public static final Logger LOGGER = LoggerFactory
 			.getLogger(ConfigurationController.class);
 
+	@Value("${api.path:}")
+	private String apiPath;
+
 	@Value("${customization.path}")
 	private String customizationPath;
 
@@ -114,7 +117,10 @@ public class ConfigurationController extends AbstractController {
 		HashMap<String, Boolean> features = new HashMap<String, Boolean>();
 
 		/* The API path could be unknown to the client in case the request was forwarded */
-		config.put("apiPath", request.getContextPath());
+		if ("".equals(apiPath)) {
+			apiPath = request.getContextPath();
+		}
+		config.put("apiPath", apiPath);
 
 		if (!"".equals(customizationPath)) {
 			config.put("customizationPath", customizationPath);
diff --git a/src/main/java/de/thm/arsnova/controller/LoginController.java b/src/main/java/de/thm/arsnova/controller/LoginController.java
index 8541728e2..27588e20a 100644
--- a/src/main/java/de/thm/arsnova/controller/LoginController.java
+++ b/src/main/java/de/thm/arsnova/controller/LoginController.java
@@ -71,6 +71,7 @@ public class LoginController extends AbstractController {
 	private static final int MAX_USERNAME_LENGTH = 15;
 	private static final int MAX_GUESTHASH_LENGTH = 10;
 
+	@Value("${api.path:}") private String apiPath;
 	@Value("${customization.path}") private String customizationPath;
 
 	@Value("${security.guest.enabled}") private String guestEnabled;
@@ -285,7 +286,10 @@ public class LoginController extends AbstractController {
 		request.getSession().invalidate();
 		SecurityContextHolder.clearContext();
 		if (auth instanceof CasAuthenticationToken) {
-			return new RedirectView("/j_spring_cas_security_logout", true);
+			if ("".equals(apiPath)) {
+				apiPath = request.getContextPath();
+			}
+			return new RedirectView(apiPath + "/j_spring_cas_security_logout", true);
 		}
 		return new RedirectView(request.getHeader("referer") != null ? request.getHeader("referer") : "/");
 	}
@@ -295,8 +299,11 @@ public class LoginController extends AbstractController {
 	public final List<ServiceDescription> getServices(final HttpServletRequest request) {
 		List<ServiceDescription> services = new ArrayList<ServiceDescription>();
 
+		if ("".equals(apiPath)) {
+			apiPath = request.getContextPath();
+		}
 		/* The first parameter is replaced by the backend, the second one by the frondend */
-		String dialogUrl = request.getContextPath() + "/auth/dialog?type={0}&successurl='{0}'";
+		String dialogUrl = apiPath + "/auth/dialog?type={0}&successurl='{0}'";
 
 		if ("true".equals(guestEnabled)) {
 			ServiceDescription sdesc = new ServiceDescription(
diff --git a/src/main/resources/arsnova.properties.example b/src/main/resources/arsnova.properties.example
index e1cbb2293..9cfaa64b1 100644
--- a/src/main/resources/arsnova.properties.example
+++ b/src/main/resources/arsnova.properties.example
@@ -5,6 +5,11 @@
 # http://localhost:8080 for development.
 root-url=https://example.com
 
+# The path where the ARSnova API is accessible by clients. By default, this path
+# is set to the context path of the backend. If you are running the backend
+# behind a proxy server, you might need to set this path manually.
+#api.path=/api
+
 # The context paths where the ARSnova modules have been deployed
 customization.path=/customization
 mobile.path=/mobile
diff --git a/src/test/resources/arsnova.properties.example b/src/test/resources/arsnova.properties.example
index e1cbb2293..9cfaa64b1 100644
--- a/src/test/resources/arsnova.properties.example
+++ b/src/test/resources/arsnova.properties.example
@@ -5,6 +5,11 @@
 # http://localhost:8080 for development.
 root-url=https://example.com
 
+# The path where the ARSnova API is accessible by clients. By default, this path
+# is set to the context path of the backend. If you are running the backend
+# behind a proxy server, you might need to set this path manually.
+#api.path=/api
+
 # The context paths where the ARSnova modules have been deployed
 customization.path=/customization
 mobile.path=/mobile
-- 
GitLab