Skip to content

Commit 31b679d

Browse files
authored
Merge pull request #69 from kit-data-manager/dev_enhance_url_config
Extend URL configuration options
2 parents 70ee50e + 9f41b3a commit 31b679d

File tree

7 files changed

+56
-8
lines changed

7 files changed

+56
-8
lines changed

howtos/configuration.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ absorb: true
3030
| MultipleAnnotationPost | Is posting multiple annotations in one request possible. | true |
3131
| WapPort | The port under which the WAP service is reachable. This port is used for HTTP and HTTPS service. When 80 is set and a http service is used, the port is omitted. The same applies to HTTPS and port 443. This setting has influence on the root IRI and cannot be changed after a database has been created. For details refer to the Root Container section. | 80 |
3232
| RdfBackendImplementation | The qualifier of the used RDF backend implementation. The default backend is 'jena'. | jena |
33+
| WapBaseUrl | An override for the base URL. Only use to run behind proxy or in a container. | |
34+

howtos/root_container.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Example 4: Hostname=host1.example.org, EnableHttps=true, WapPort=443
2929
When using the installer (via --install or by starting the jar in an empty folder) it asks for this base configuration
3030
and shows its consequences on the root container IRI.
3131

32+
All of the above can be overwritten by using the `WapBaseUrl` property.
33+
This property is intended to be used only in scenarios where the server is reachable from a different URL
34+
and if this should or must be reflected in the database (reverse proxy, docker container).
35+
3236
If changing any of those parameters with an already running server is necessary, a deletion of the database is needed.
3337
It gets recreated on first startup after the configuration has been changed.
3438

@@ -38,6 +42,6 @@ It gets recreated on first startup after the configuration has been changed.
3842
Using manual database manipulation, a conversion of the database to fit the new root container IRI can be achieved,
3943
but this is not implemented in the application. The easiest way to achieve this would be to have the database
4044
been backed up to NQUADS (which retains the named graphs) and then run a simple text replacement of old IRI ==> new IRI.
41-
This has never been tested and should be regarded as a good starting point at best.
45+
Also see [data migration guide](https://github.com/kit-data-manager/wap-server/wiki/Migrating-a-Server-Instance-and-its-Data) for details.
4246

4347
---

src/main/java/edu/kit/scc/dem/wapsrv/app/ConfigurationKeys.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,13 @@ public enum ConfigurationKeys {
111111
/**
112112
* @see WapServerConfig#fallbackValidation
113113
*/
114-
FallbackValidation
114+
FallbackValidation,
115+
/**
116+
* @see WapServerConfig#contextPath
117+
*/
118+
ContextPath,
119+
/**
120+
* @see WapServerConfig#proxiedBasePath
121+
*/
122+
ProxiedBasePath
115123
}

src/main/java/edu/kit/scc/dem/wapsrv/app/WapServerConfig.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.beans.factory.annotation.Value;
1414
import org.springframework.context.annotation.Configuration;
1515
import org.springframework.util.PathMatcher;
16+
import org.springframework.util.StringUtils;
1617
import org.springframework.web.servlet.config.annotation.CorsRegistry;
1718
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
1819
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@@ -90,6 +91,8 @@ public class WapServerConfig extends WebMvcConfigurationSupport{
9091
private static final String CORS_ALLOWED_ORIGINS_PATH_DEFAULT = "./cors_allowed_origins.conf";
9192
private static final boolean FALLBACK_VALIDATION_DEFAULT = true;
9293
private static final String RDF_BACKEND_IMPLEMENTATION_DEFAULT = "jena";
94+
private static final String CONTEXT_PATH_DEFAULT = "";
95+
private static final String PROXYBASEPATH_DEFAULT = "";
9396

9497
/**
9598
* The single instance of the configuration
@@ -245,6 +248,12 @@ public class WapServerConfig extends WebMvcConfigurationSupport{
245248
@Value("${RdfBackendImplementation:" + RDF_BACKEND_IMPLEMENTATION_DEFAULT + "}")
246249
private String rdfBackendImplementation;
247250

251+
@Value("${server.servlet.context-path:" + CONTEXT_PATH_DEFAULT + "}")
252+
private String contextPath = CONTEXT_PATH_DEFAULT;
253+
254+
@Value("${WapBaseUrl:" + PROXYBASEPATH_DEFAULT + "}")
255+
private String proxiedBasePath;
256+
248257
/**
249258
* The cors configuration to use
250259
*/
@@ -362,6 +371,8 @@ public static Properties getDefaultProperties(){
362371
props.put(ConfigurationKeys.SimpleFormatters.toString(), SIMPLE_FORMATTERS_DEFAULT);
363372
props.put(ConfigurationKeys.CorsAllowedOriginsPath.toString(), CORS_ALLOWED_ORIGINS_PATH_DEFAULT);
364373
props.put(ConfigurationKeys.FallbackValidation.toString(), FALLBACK_VALIDATION_DEFAULT + "");
374+
props.put(ConfigurationKeys.ContextPath.toString(), CONTEXT_PATH_DEFAULT);
375+
props.put(ConfigurationKeys.ProxiedBasePath.toString(), PROXYBASEPATH_DEFAULT);
365376
if(ConfigurationKeys.values().length != props.size()){
366377
throw new RuntimeException("Default properties and the ConfigurationKeys enum not in sync");
367378
}
@@ -715,6 +726,8 @@ public void updateConfig(Properties props){
715726
corsAllowedOriginsPath
716727
= getProperty(props, ConfigurationKeys.CorsAllowedOriginsPath, CORS_ALLOWED_ORIGINS_PATH_DEFAULT);
717728
fallbackValidation = getProperty(props, ConfigurationKeys.FallbackValidation, FALLBACK_VALIDATION_DEFAULT);
729+
contextPath = getProperty(props, ConfigurationKeys.ContextPath, CONTEXT_PATH_DEFAULT);
730+
proxiedBasePath = getProperty(props, ConfigurationKeys.ProxiedBasePath, PROXYBASEPATH_DEFAULT);
718731
}
719732

720733
private String getProperty(Properties newProps, ConfigurationKeys key, String defaultValue){
@@ -813,17 +826,18 @@ public boolean isRootWapUrl(String url){
813826
* @return The base url
814827
*/
815828
public String getBaseUrl(){
829+
if(StringUtils.hasText(proxiedBasePath))return proxiedBasePath;
816830
if(enableHttps){
817831
if(wapPort == 443){
818-
return "https://" + hostname;
832+
return "https://" + hostname + contextPath;
819833
} else{
820-
return "https://" + hostname + ":" + wapPort;
834+
return "https://" + hostname + ":" + wapPort + contextPath;
821835
}
822836
} else{
823837
if(wapPort == 80){
824-
return "http://" + hostname;
838+
return "http://" + hostname + contextPath;
825839
} else{
826-
return "http://" + hostname + ":" + wapPort;
840+
return "http://" + hostname + ":" + wapPort + contextPath;
827841
}
828842
}
829843
}

src/test/java/edu/kit/scc/dem/wapsrv/app/WapServerConfigTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,24 @@ final void testGetBaseUrl() {
453453
assertNotNull(actual, "Could not get base url from WapServerConfig after setting wapPort == 443.");
454454
expected = "https://localhost";
455455
assertEquals(expected, actual);
456+
// test context path == context
457+
paramProperties.setProperty(ConfigurationKeys.ContextPath.toString(), "/context");
458+
objWapServerConfig.updateConfig(paramProperties);
459+
460+
actual = null;
461+
actual = objWapServerConfig.getBaseUrl();
462+
assertNotNull(actual, "Could not get base url from WapServerConfig after setting context path.");
463+
expected = "https://localhost/context";
464+
assertEquals(expected, actual);
465+
// test WapBaseUrl = http://example.com
466+
paramProperties.setProperty(ConfigurationKeys.ProxiedBasePath.toString(), "http://example.com");
467+
objWapServerConfig.updateConfig(paramProperties);
468+
469+
actual = null;
470+
actual = objWapServerConfig.getBaseUrl();
471+
assertNotNull(actual, "Could not get base url from WapServerConfig after setting WapBaseUrl.");
472+
expected = "http://example.com";
473+
assertEquals(expected, actual);
456474
}
457475

458476
/**

webcontent/ExampleData.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class ExampleData {
5050
}
5151

5252
if (window.location.origin !== "null") {
53-
callback.settings["url"] = window.location.origin + "/webapp/" + "resources/examples/" + filename;
53+
let contextpath = !window.location.pathname.startsWith("/webapp") ? window.location.pathname.split("/webapp").at(0) : ""
54+
callback.settings["url"] = window.location.origin + contextpath + "/webapp/" + "resources/examples/" + filename;
5455
} else {
5556
return false;
5657
}

webcontent/Main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class Main {
5555
this.initEventHandler();
5656
//init value for targetUrl
5757
if (window.location.origin !== "null") {
58-
$("#targetUrl").val(window.location.origin + "/wap/");
58+
let contextpath = !window.location.pathname.startsWith("/webapp") ? window.location.pathname.split("/webapp").at(0) : ""
59+
$("#targetUrl").val(window.location.origin + contextpath + "/wap/");
5960
}
6061
}
6162

0 commit comments

Comments
 (0)