2019-07-18 12:35:36 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET)
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package ch.ethz.seb.sebserver.webservice;
|
|
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.UnknownHostException;
|
2019-09-09 09:33:55 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
2019-07-18 12:35:36 +02:00
|
|
|
|
2019-07-22 10:36:23 +02:00
|
|
|
import org.apache.commons.lang3.BooleanUtils;
|
2019-07-18 12:35:36 +02:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2019-09-09 09:33:55 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2019-07-18 12:35:36 +02:00
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
|
2019-07-22 10:36:23 +02:00
|
|
|
import ch.ethz.seb.sebserver.gbl.Constants;
|
2019-07-18 12:35:36 +02:00
|
|
|
import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile;
|
|
|
|
|
|
|
|
@Lazy
|
|
|
|
@Service
|
|
|
|
@WebServiceProfile
|
|
|
|
public class WebserviceInfo {
|
|
|
|
|
2019-09-09 09:33:55 +02:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(WebserviceInfo.class);
|
|
|
|
|
2019-08-27 09:13:33 +02:00
|
|
|
private static final String WEB_SERVICE_TEST_PROPERTY = "sebserver.test.property";
|
2019-07-18 12:35:36 +02:00
|
|
|
private static final String WEB_SERVICE_SERVER_NAME_KEY = "sebserver.webservice.http.server.name";
|
|
|
|
private static final String WEB_SERVICE_HTTP_SCHEME_KEY = "sebserver.webservice.http.scheme";
|
2019-10-17 16:53:33 +02:00
|
|
|
private static final String WEB_SERVICE_HTTP_PORT = "sebserver.webservice.http.port";
|
2019-07-18 12:35:36 +02:00
|
|
|
private static final String WEB_SERVICE_HOST_ADDRESS_KEY = "server.address";
|
|
|
|
private static final String WEB_SERVICE_SERVER_PORT_KEY = "server.port";
|
|
|
|
private static final String WEB_SERVICE_EXAM_API_DISCOVERY_ENDPOINT_KEY =
|
|
|
|
"sebserver.webservice.api.exam.endpoint.discovery";
|
2019-09-09 09:33:55 +02:00
|
|
|
private static final String WEB_SERVICE_EXTERNAL_ADDRESS_ALIAS = "sebserver.webservice.lms.address.alias";
|
2019-07-18 12:35:36 +02:00
|
|
|
|
2019-08-27 09:13:33 +02:00
|
|
|
private final String testProperty;
|
2019-07-18 12:35:36 +02:00
|
|
|
private final String httpScheme;
|
|
|
|
private final String hostAddress; // internal
|
2019-10-17 16:53:33 +02:00
|
|
|
private final String webserverName; // external
|
|
|
|
private final String serverPort; // internal
|
|
|
|
private final String webserverPort; // external
|
2019-07-18 12:35:36 +02:00
|
|
|
private final String discoveryEndpoint;
|
|
|
|
|
|
|
|
private final String serverURLPrefix;
|
2019-07-22 10:36:23 +02:00
|
|
|
private final boolean isDistributed;
|
|
|
|
|
2019-09-09 09:33:55 +02:00
|
|
|
private Map<String, String> externalAddressAlias;
|
|
|
|
|
2019-07-18 12:35:36 +02:00
|
|
|
public WebserviceInfo(final Environment environment) {
|
2019-08-27 09:13:33 +02:00
|
|
|
this.testProperty = environment.getProperty(WEB_SERVICE_TEST_PROPERTY, "NOT_AVAILABLE");
|
2019-07-18 12:35:36 +02:00
|
|
|
this.httpScheme = environment.getRequiredProperty(WEB_SERVICE_HTTP_SCHEME_KEY);
|
|
|
|
this.hostAddress = environment.getRequiredProperty(WEB_SERVICE_HOST_ADDRESS_KEY);
|
2019-10-17 16:53:33 +02:00
|
|
|
this.webserverName = environment.getProperty(WEB_SERVICE_SERVER_NAME_KEY, "");
|
2019-07-18 12:35:36 +02:00
|
|
|
this.serverPort = environment.getRequiredProperty(WEB_SERVICE_SERVER_PORT_KEY);
|
2019-10-17 16:53:33 +02:00
|
|
|
this.webserverPort = environment.getProperty(WEB_SERVICE_HTTP_PORT);
|
2019-07-18 12:35:36 +02:00
|
|
|
this.discoveryEndpoint = environment.getRequiredProperty(WEB_SERVICE_EXAM_API_DISCOVERY_ENDPOINT_KEY);
|
|
|
|
|
2019-10-17 16:53:33 +02:00
|
|
|
final UriComponentsBuilder builder = UriComponentsBuilder.newInstance()
|
2019-07-18 12:35:36 +02:00
|
|
|
.scheme(this.httpScheme)
|
2019-10-17 16:53:33 +02:00
|
|
|
.host((StringUtils.isNotBlank(this.webserverName))
|
|
|
|
? this.webserverName
|
|
|
|
: this.hostAddress);
|
|
|
|
if (StringUtils.isNotBlank(this.webserverPort)) {
|
|
|
|
builder.port(this.webserverPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.serverURLPrefix = builder.toUriString();
|
2019-07-22 10:36:23 +02:00
|
|
|
|
|
|
|
this.isDistributed = BooleanUtils.toBoolean(environment.getProperty(
|
|
|
|
"sebserver.webservice.distributed",
|
|
|
|
Constants.FALSE_STRING));
|
2019-09-09 09:33:55 +02:00
|
|
|
|
|
|
|
final String addressAlias = environment.getProperty(WEB_SERVICE_EXTERNAL_ADDRESS_ALIAS, "");
|
|
|
|
if (StringUtils.isNotBlank(addressAlias)) {
|
|
|
|
try {
|
|
|
|
final String[] aliass = StringUtils.split(addressAlias, Constants.LIST_SEPARATOR);
|
|
|
|
final Map<String, String> mapping = new LinkedHashMap<>();
|
|
|
|
for (final String alias : aliass) {
|
|
|
|
final String[] nameValue =
|
|
|
|
StringUtils.split(alias, Constants.FORM_URL_ENCODED_NAME_VALUE_SEPARATOR);
|
|
|
|
mapping.put(nameValue[0], nameValue[1]);
|
|
|
|
}
|
|
|
|
this.externalAddressAlias = Collections.unmodifiableMap(mapping);
|
|
|
|
} catch (final Exception e) {
|
|
|
|
log.error("Failed to parse sebserver.webservice.lms.address.alias: ", e);
|
|
|
|
this.externalAddressAlias = Collections.emptyMap();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.externalAddressAlias = Collections.emptyMap();
|
|
|
|
}
|
2019-07-18 12:35:36 +02:00
|
|
|
}
|
|
|
|
|
2019-08-27 09:13:33 +02:00
|
|
|
public String getTestProperty() {
|
|
|
|
return this.testProperty;
|
|
|
|
}
|
|
|
|
|
2019-07-18 12:35:36 +02:00
|
|
|
public String getHttpScheme() {
|
|
|
|
return this.httpScheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getHostAddress() {
|
|
|
|
return this.hostAddress;
|
|
|
|
}
|
|
|
|
|
2019-10-17 16:53:33 +02:00
|
|
|
public String getWebserviceDomainName() {
|
|
|
|
return this.webserverName;
|
2019-07-18 12:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getServerPort() {
|
|
|
|
return this.serverPort;
|
|
|
|
}
|
|
|
|
|
2019-10-17 16:53:33 +02:00
|
|
|
public String getServerExternalPort() {
|
|
|
|
return this.webserverPort;
|
|
|
|
}
|
|
|
|
|
2019-07-18 12:35:36 +02:00
|
|
|
public String getDiscoveryEndpoint() {
|
|
|
|
return this.discoveryEndpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getDiscoveryEndpointAddress() {
|
|
|
|
return this.serverURLPrefix + this.discoveryEndpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getLocalHostName() {
|
|
|
|
try {
|
|
|
|
return InetAddress.getLocalHost().getHostName();
|
|
|
|
} catch (final UnknownHostException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getLocalHostAddress() {
|
|
|
|
try {
|
|
|
|
return InetAddress.getLocalHost().getHostAddress();
|
|
|
|
} catch (final UnknownHostException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getLoopbackHostName() {
|
|
|
|
return InetAddress.getLoopbackAddress().getHostName();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getLoopbackHostAddress() {
|
|
|
|
return InetAddress.getLoopbackAddress().getHostAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the server URL prefix in form of;
|
|
|
|
* [scheme{http|https}]://[server-address{DNS-name|IP}]:[port]
|
|
|
|
*
|
|
|
|
* E.g.: https://seb.server.ch:8080
|
|
|
|
*
|
|
|
|
* @return the server URL prefix */
|
|
|
|
public String getServerURL() {
|
|
|
|
return this.serverURLPrefix;
|
|
|
|
}
|
|
|
|
|
2019-07-22 10:36:23 +02:00
|
|
|
public boolean isDistributed() {
|
|
|
|
return this.isDistributed;
|
|
|
|
}
|
|
|
|
|
2019-09-09 09:33:55 +02:00
|
|
|
public Map<String, String> getExternalAddressAlias() {
|
|
|
|
return this.externalAddressAlias;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getExternalAddressAlias(final String internalAddress) {
|
|
|
|
return this.externalAddressAlias
|
|
|
|
.entrySet()
|
|
|
|
.stream()
|
|
|
|
.filter(entry -> internalAddress.contains(entry.getKey()))
|
|
|
|
.findFirst()
|
|
|
|
.map(entry -> entry.getValue())
|
|
|
|
.orElse(null);
|
|
|
|
}
|
|
|
|
|
2019-07-18 12:35:36 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
final StringBuilder builder = new StringBuilder();
|
2019-08-27 09:13:33 +02:00
|
|
|
builder.append("WebserviceInfo [testProperty=");
|
|
|
|
builder.append(this.testProperty);
|
|
|
|
builder.append(", httpScheme=");
|
2019-07-18 12:35:36 +02:00
|
|
|
builder.append(this.httpScheme);
|
|
|
|
builder.append(", hostAddress=");
|
|
|
|
builder.append(this.hostAddress);
|
2019-10-17 16:53:33 +02:00
|
|
|
builder.append(", webserverName=");
|
|
|
|
builder.append(this.webserverName);
|
2019-07-18 12:35:36 +02:00
|
|
|
builder.append(", serverPort=");
|
|
|
|
builder.append(this.serverPort);
|
2019-10-17 16:53:33 +02:00
|
|
|
builder.append(", webserverPort=");
|
|
|
|
builder.append(this.webserverPort);
|
2019-07-18 12:35:36 +02:00
|
|
|
builder.append(", discoveryEndpoint=");
|
|
|
|
builder.append(this.discoveryEndpoint);
|
|
|
|
builder.append(", serverURLPrefix=");
|
|
|
|
builder.append(this.serverURLPrefix);
|
2019-08-27 09:13:33 +02:00
|
|
|
builder.append(", isDistributed=");
|
|
|
|
builder.append(this.isDistributed);
|
2019-10-17 16:53:33 +02:00
|
|
|
builder.append(", externalAddressAlias=");
|
|
|
|
builder.append(this.externalAddressAlias);
|
2019-07-18 12:35:36 +02:00
|
|
|
builder.append("]");
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|