Skip to content

Commit

Permalink
Introducing possiblity to define the server baseUrl using environment…
Browse files Browse the repository at this point in the history
… variable (#213)

Co-authored-by: Jad El-khoury <jad.el.khoury@scania.com>
Co-authored-by: Andrew Berezovskyi <andriib@kth.se>
  • Loading branch information
3 people authored Dec 29, 2021
1 parent a8c8f45 commit f559afc
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 23 deletions.
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Lyo Designer Changelog

## [Unreleased]

### Added

### Changed

- `LYO_BASE` is replaced with `LYO_BASEURL`. To override the Base URL of the adaptor, use the `baseurl` property. This release makes the use of this property consistent and check the `LYO_BASEURL` environment variable instead of `LYO_BASE`.

### Deprecated

### Removed

### Fixed

---

**Template**

## [Unreleased]

### Added

### Changed

### Deprecated

### Removed

### Fixed


> See https://keepachangelog.com/en/1.0.0/ for more info.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ package [javaClassPackageNameForAdaptorServletListener(anAdaptorInterface) /];

import java.net.MalformedURLException;
import java.util.NoSuchElementException;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import org.slf4j.Logger;
Expand All @@ -68,12 +69,7 @@ import [javaClassFullNameForAdaptorManager(anAdaptorInterface) /];
/**
* During the initialization of this ServletListener, the base URI for the OSLC resources produced by this server is configured through the OSLC4J method setPublicURI().
* <p>
* A number of approaches can be used to set this base URI:
* <ol>
* <li>Set a system environment property of name "LYO_BASE".</li>
* <li>In the web.xml file, set a {@code context-param} property, with name "[javaClassPackageNameForAdaptorServletListener(anAdaptorInterface) /].baseurl}</li>
* <li>Override the value of the DEFAULT_BASE constant.</li>
* </ol>
* See getConfigurationProperty() for the different alternatives to set this base URI.
*/
public class [javaClassNameForAdaptorServletListener(anAdaptorInterface) /] implements ServletContextListener {
private static final Logger logger = LoggerFactory.getLogger([javaClassNameForAdaptorServletListener(anAdaptorInterface)/].class);
Expand All @@ -89,15 +85,17 @@ public class [javaClassNameForAdaptorServletListener(anAdaptorInterface) /] impl
public void contextInitialized(final ServletContextEvent servletContextEvent)
{
//These are default values. You can modify any of them early in this method.
String basePathEnvKey = "LYO_BASE";
String basePathContextPropertyKey = String.format("%s.%s", [javaClassNameForAdaptorServletListener(anAdaptorInterface) /].class.getPackage().getName(), "baseurl");
String basePathKey = "baseurl";
String fallbackBase = "http://localhost:8080";
String servletName = "JAX-RS Servlet";

// [protected ('contextInitialized_init')]
// [/protected]

String baseUrl = generateBasePath(servletContextEvent, basePathEnvKey, basePathContextPropertyKey, fallbackBase);
ServletContext servletContext = servletContextEvent.getServletContext();
String basePathProperty = getConfigurationProperty(basePathKey, fallbackBase, servletContext, [javaClassNameForAdaptorServletListener(anAdaptorInterface) /].class);
UriBuilder builder = UriBuilder.fromUri(basePathProperty);
String baseUrl = builder.path(servletContext.getContextPath()).build().toString();
String servletUrlPattern = "services/";
try {
servletUrlPattern = getServletUrlPattern(servletContextEvent, servletName);
Expand Down Expand Up @@ -140,28 +138,69 @@ public class [javaClassNameForAdaptorServletListener(anAdaptorInterface) /] impl
// [protected ('class_methods')]
// [/protected]

private static String generateBasePath(final ServletContextEvent servletContextEvent, String basePathEnvKey, String basePathContextPropertyKey, String fallbackBase) {
String base = getBasePathFromEnvironment(basePathEnvKey).orElseGet(() -> getBasePathFromContext(servletContextEvent, basePathContextPropertyKey).orElseGet(() -> fallbackBase));
UriBuilder builder = UriBuilder.fromUri(base);
return builder.path(servletContextEvent.getServletContext().getContextPath()).build().toString();
/**
* For a property 'scheme', this is the lookup priority:
* <p>
* <ol>
* <li>LYO_SCHEME env variable</li>
* <li>%pkg_name%.scheme JVM property, e.g. org.eclipse.lyo.oslc4j.core.servlet.scheme</li>
* <li>%pkg_name%.scheme Servlet Context parameter, e.g. org.eclipse.lyo.oslc4j.core.servlet.scheme</li>
* </ol>
* @param key property key name
* @param defaultValue default String value
* @param klass Class of the ServletListener
* @return value, if found, from ENV, JVM, or Servlet Context (in this order)
*/
private static String getConfigurationProperty(String key, String defaultValue, final ServletContext servletContext, Class klass) {
String value = getConfigurationPropertyFromEnvironment(generateEnvironmentKey(key))
.orElseGet(() -> getConfigurationPropertyFromSystemProperties(generateFullyQualifiedKey(klass, key))
.orElseGet(() -> getConfigurationPropertyFromContext(servletContext, generateFullyQualifiedKey(klass, key))
.orElse(defaultValue)));
return value;
}

private static Optional<String> getBasePathFromEnvironment(String basePathEnvKey) {
/**
* property name 'scheme' would become "org.eclipse.lyo.oslc4j.core.servlet.scheme"
*/
private static String generateFullyQualifiedKey(Class klass, String key) {
return klass.getPackage().getName() + '.' + key;
}

/**
* web.xml property ending in '.scheme' would become "LYO_SCHEME"
*/
private static String generateEnvironmentKey(String key) {
return "LYO_" + key.toUpperCase(Locale.ROOT).replace('.', '_');
}

private static Optional<String> getConfigurationPropertyFromEnvironment(String basePathEnvKey) {
final Map<String, String> env = System.getenv();
if(env.containsKey(basePathEnvKey)) {
logger.info("Found {} env variable", basePathEnvKey);
return Optional.of(env.get(basePathEnvKey));
if (!env.containsKey(basePathEnvKey)) {
logger.debug("ENV variable '{}' not defined", basePathEnvKey);
return Optional.empty();
}
return Optional.empty();
logger.info("Found {} env variable", basePathEnvKey);
return Optional.of(env.get(basePathEnvKey));
}

private static Optional<String> getBasePathFromContext(final ServletContextEvent servletContextEvent, String basePathContextPropertyKey) {
final ServletContext servletContext = servletContextEvent.getServletContext();
String base = servletContext.getInitParameter(basePathContextPropertyKey);
if (base == null || base.trim().isEmpty()) {
private static Optional<String> getConfigurationPropertyFromSystemProperties(String basePathContextPropertyKey) {
String value = System.getProperty(basePathContextPropertyKey);
if (value == null || value.trim().isEmpty()) {
logger.debug("System (JVM) property '{}' not defined", basePathContextPropertyKey);
return Optional.empty();
}
logger.info("Found {} System (JVM) property", basePathContextPropertyKey);
return Optional.of(value);
}

private static Optional<String> getConfigurationPropertyFromContext(final ServletContext servletContext, String basePathContextPropertyKey) {
String value = servletContext.getInitParameter(basePathContextPropertyKey);
if (value == null || value.trim().isEmpty()) {
logger.debug("Servlet Context parameter '{}' not defined", basePathContextPropertyKey);
return Optional.empty();
}
return Optional.of(base);
logger.info("Found {} context parameter", basePathContextPropertyKey);
return Optional.of(value);
}

private static String getServletUrlPattern(final ServletContextEvent servletContextEvent, String servletName) throws Exception {
Expand Down

0 comments on commit f559afc

Please sign in to comment.