Skip to content

Commit

Permalink
WIP - Merge branch 'master' into issue225_clean
Browse files Browse the repository at this point in the history
  • Loading branch information
otsakir committed Sep 21, 2017
2 parents 2f0fcd4 + 5bf9d11 commit 1eb598e
Show file tree
Hide file tree
Showing 37 changed files with 1,401 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package org.restcomm.connect.rvd;

import org.apache.http.impl.client.CloseableHttpClient;
import org.restcomm.connect.rvd.commons.http.CustomHttpClientBuilder;
import org.restcomm.connect.rvd.concurrency.ProjectRegistry;
import org.restcomm.connect.rvd.identity.AccountProvider;
Expand All @@ -36,6 +37,8 @@
public class ApplicationContext {
RvdConfiguration configuration;
CustomHttpClientBuilder httpClientBuilder;
CloseableHttpClient defaultHttpClient;
CloseableHttpClient externaltHttpClient;
AccountProvider accountProvider;
ProjectRegistry projectRegistry;
AggregateStats globalStats;
Expand Down Expand Up @@ -67,4 +70,12 @@ public AggregateStats getGlobalStats() {
public void setGlobalStats(AggregateStats globalStats) {
this.globalStats = globalStats;
}

public CloseableHttpClient getDefaultHttpClient() {
return defaultHttpClient;
}

public CloseableHttpClient getExternaltHttpClient() {
return externaltHttpClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package org.restcomm.connect.rvd;

import org.apache.http.impl.client.CloseableHttpClient;
import org.restcomm.connect.rvd.commons.http.CustomHttpClientBuilder;
import org.restcomm.connect.rvd.concurrency.ProjectRegistry;
import org.restcomm.connect.rvd.identity.AccountProvider;
Expand All @@ -30,6 +31,8 @@
public class ApplicationContextBuilder {
RvdConfiguration configuration;
CustomHttpClientBuilder httpClientBuilder;
CloseableHttpClient defaultHttpClient;
CloseableHttpClient externaltHttpClient;
AccountProvider accountProvider;
ProjectRegistry projectRegistry;

Expand All @@ -48,6 +51,15 @@ public ApplicationContextBuilder setAccountProvider(AccountProvider accountProvi
return this;
}

public ApplicationContextBuilder setDefaultHttpClient(CloseableHttpClient defaultHttpClient) {
this.defaultHttpClient = defaultHttpClient;
return this;
}
public ApplicationContextBuilder setExternalHttpClient(CloseableHttpClient httpClient) {
this.externaltHttpClient = httpClient;
return this;
}

public ApplicationContextBuilder setProjectRegistry(ProjectRegistry projectRegistry) {
this.projectRegistry = projectRegistry;
return this;
Expand All @@ -59,6 +71,8 @@ public ApplicationContext build() {
instance.httpClientBuilder = this.httpClientBuilder;
instance.accountProvider = this.accountProvider;
instance.projectRegistry = this.projectRegistry;
instance.defaultHttpClient = this.defaultHttpClient;
instance.externaltHttpClient = this.externaltHttpClient;
return instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,30 @@
import org.restcomm.connect.rvd.utils.RvdUtils;

import com.thoughtworks.xstream.XStream;
import java.util.List;
import org.restcomm.connect.rvd.configuration.RvdMaxPerHost;
import org.restcomm.connect.rvd.utils.XmlParser;

/**
* Configuration settings for RVD. Contains both static hardcoded and loaded values.
* Configuration settings for RVD. Contains both static hardcoded and loaded
* values.
*
* Besides hardcoded values, information form rvd.xml as well as proxied values from restcomm.xml
* are contained. It also provides vary basic logic so that default values are returned too.
* For example if 'videoSupport' configuration option is missing, it will return false (and not null).
* Besides hardcoded values, information form rvd.xml as well as proxied values
* from restcomm.xml are contained. It also provides vary basic logic so that
* default values are returned too. For example if 'videoSupport' configuration
* option is missing, it will return false (and not null).
*
* rvd.xml and restcomm.xml configuration options are applied in the following way:
* rvd.xml and restcomm.xml configuration options are applied in the following
* way:
*
* restcomm.xml based will be loaded first if available. Any option defined in rvd.xml will override
* these values if the option is defined (i.e.the XML element is there even if empty).
* restcomm.xml based will be loaded first if available. Any option defined in
* rvd.xml will override these values if the option is defined (i.e.the XML
* element is there even if empty).
*
* @author otsakir@gmail.com - Orestis Tsakiridis
*/
public class FileRvdConfiguration implements RvdConfiguration {

static Logger logger = RvdLoggers.local;
// these defaults are used when there are no values defined in the configuration files
private Integer maxMediaFileSize; // Maximum size allowed for media file uploads (in bytes). If set to null no limit is enforced
Expand Down Expand Up @@ -91,11 +98,12 @@ private void load() {
rvdConfig = loadRvdXmlConfig(contextRootPath + "WEB-INF/rvd.xml");
// workspaceBasePath option
String workspaceBasePath = contextRootPath + WORKSPACE_DIRECTORY_NAME;
if (rvdConfig.getWorkspaceLocation() != null && !"".equals(rvdConfig.getWorkspaceLocation()) ) {
if ( rvdConfig.getWorkspaceLocation().startsWith("/") )
if (rvdConfig.getWorkspaceLocation() != null && !"".equals(rvdConfig.getWorkspaceLocation())) {
if (rvdConfig.getWorkspaceLocation().startsWith("/")) {
workspaceBasePath = rvdConfig.getWorkspaceLocation(); // this is an absolute path
else
} else {
workspaceBasePath = contextRootPath + rvdConfig.getWorkspaceLocation(); // this is a relative path hooked under RVD context
}
}
this.workspaceBasePath = workspaceBasePath;
RvdLoggers.global.info("workspace located under " + workspaceBasePath);
Expand All @@ -116,57 +124,69 @@ private void load() {
// maxMediaFileSize
maxMediaFileSize = rvdConfig.getMaxMediaFileSize();
// sslMode
if (restcommConfig != null)
if (restcommConfig != null) {
sslMode = restcommConfig.getSslMode();
if (rvdConfig.getSslMode() != null)
}
if (rvdConfig.getSslMode() != null) {
sslMode = SslMode.valueOf(rvdConfig.getSslMode());
if (sslMode == null)
}
if (sslMode == null) {
sslMode = DEFAULT_SSL_MODE;
}
// hostnameOverride (hostname in restcomm.xml)
if (restcommConfig != null)
if (restcommConfig != null) {
hostnameOverride = restcommConfig.getHostname();
if (rvdConfig.getHostnameOverride() != null)
}
if (rvdConfig.getHostnameOverride() != null) {
hostnameOverride = rvdConfig.getHostnameOverride();
}
// useHostnameToResolveRelativeUrl
if (restcommConfig != null)
if (restcommConfig != null) {
useHostnameToResolveRelativeUrl = restcommConfig.getUseHostnameToResolveRelativeUrl();
if (rvdConfig.getUseHostnameToResolveRelativeUrl() != null)
}
if (rvdConfig.getUseHostnameToResolveRelativeUrl() != null) {
useHostnameToResolveRelativeUrl = rvdConfig.getUseHostnameToResolveRelativeUrl();
if (useHostnameToResolveRelativeUrl == null)
}
if (useHostnameToResolveRelativeUrl == null) {
useHostnameToResolveRelativeUrl = DEFAULT_USE_HOSTNAME_TO_RESOLVE_RELATIVE_URL;
}
// baseUrl
if (! RvdUtils.isEmpty(rvdConfig.getBaseUrl()) )
if (!RvdUtils.isEmpty(rvdConfig.getBaseUrl())) {
baseUrl = rvdConfig.getBaseUrl();
}
// useAbsoluteApplicationUrl
if (! RvdUtils.isEmpty(rvdConfig.useAbsoluteApplicationUrl()))
if (!RvdUtils.isEmpty(rvdConfig.useAbsoluteApplicationUrl())) {
useAbsoluteApplicationUrl = rvdConfig.useAbsoluteApplicationUrl();
else
} else {
useAbsoluteApplicationUrl = DEFAULT_USE_ABSOLUTE_APPLICATION_URL;
}
// ussd support
if ( RvdUtils.isEmpty(rvdConfig.getUssdSupport()) )
if (RvdUtils.isEmpty(rvdConfig.getUssdSupport())) {
ussdSupport = DEFAULT_USSD_SUPPORT;
else {
} else {
try {
ussdSupport = Boolean.parseBoolean(rvdConfig.getUssdSupport());
} catch ( Exception e) {
} catch (Exception e) {
ussdSupport = DEFAULT_USSD_SUPPORT;
logger.warn(LoggingHelper.buildMessage(RvdConfiguration.class,"load",null,"Error parsing rvd.xml:ussd/enabled option. Falling back to default: " + ussdSupport),e);
logger.warn(LoggingHelper.buildMessage(RvdConfiguration.class, "load", null, "Error parsing rvd.xml:ussd/enabled option. Falling back to default: " + ussdSupport), e);
}
}
// External service timeout
initExternalServiceTimeout();
// RVD instance id. If there is no value configured in rvd.xml, use a random setting
if (RvdUtils.isEmpty(rvdConfig.getInstanceId())) {
rvdInstanceId = UUID.randomUUID().toString().replace("-", "").substring(0,8);
} else
rvdInstanceId = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
} else {
rvdInstanceId = rvdConfig.getInstanceId();
}

// load whitelabeling configuration
loadWhitelabelConfig(contextRootPath + "WEB-INF/whitelabel.xml");
}

/**
* Loads rvd.xml into an RvdConfig. Returns null if the file is not found
*
* @param pathToXml
* @return
*/
Expand All @@ -175,27 +195,29 @@ public static RvdConfig loadRvdXmlConfig(String pathToXml) {
FileInputStream input = new FileInputStream(pathToXml);
XStream xstream = new XStream();
xstream.alias("rvd", RvdConfig.class);
xstream.alias("rvdMaxPerHost", RvdMaxPerHost.class);
xstream.omitField(RvdConfig.class, "corsWhitelist");
xstream.omitField(RvdConfig.class, "ussd");
xstream.registerConverter(new CustomIntegerConverter());
RvdConfig rvdConfig = (RvdConfig) xstream.fromXML( input );
RvdConfig rvdConfig = (RvdConfig) xstream.fromXML(input);
// read some more configuration options that xstream fails to read in a clean way
XmlParser xml = new XmlParser(pathToXml);
rvdConfig.setAllowedCorsOrigins(xml.getElementList("/rvd/corsWhitelist/origin"));
rvdConfig.setUssdSupport(xml.getElementContent("/rvd/ussdSupport"));
return rvdConfig;
} catch (FileNotFoundException e) {
logger.warn(LoggingHelper.buildMessage(RvdConfiguration.class,"loadRvdXmlConfig",null,"RVD configuration file not found: " + pathToXml));
logger.warn(LoggingHelper.buildMessage(RvdConfiguration.class, "loadRvdXmlConfig", null, "RVD configuration file not found: " + pathToXml));
return null;
} catch (XmlParserException e) {
logger.warn(LoggingHelper.buildMessage(RvdConfiguration.class,"loadRvdXmlConfig",null,"Error parsing RVD configuration file: " + pathToXml), e);
logger.warn(LoggingHelper.buildMessage(RvdConfiguration.class, "loadRvdXmlConfig", null, "Error parsing RVD configuration file: " + pathToXml), e);
return null;

}
}

/**
* Load configuration options from restcomm.xml that are needed by RVD. Return null in case of failure.
* Load configuration options from restcomm.xml that are needed by RVD.
* Return null in case of failure.
*
* @param pathToXml
* @return a valid RestcommConfig object or null
Expand All @@ -217,14 +239,16 @@ private void loadWhitelabelConfig(String pathToXml) {
try {
XmlParser xml = new XmlParser(pathToXml);
String value = xml.getElementContent("/whitelabel/welcomeMessage");
if (value != null)
if (value != null) {
welcomeMessage = value;
}
logger.info("Loaded whitelabeling information: " + pathToXml);
} catch (XmlParserException e) {
if ( e.getCause() instanceof FileNotFoundException)
if (e.getCause() instanceof FileNotFoundException) {
logger.info("No whitelabeling file found (" + pathToXml + "). Hardcoded defaults will be used.");
else
logger.error(LoggingHelper.buildMessage(RvdConfiguration.class,"loadWhitelabelConfig",null,"Error parsing whitelabeling configuration file: " + pathToXml), e);
} else {
logger.error(LoggingHelper.buildMessage(RvdConfiguration.class, "loadWhitelabelConfig", null, "Error parsing whitelabeling configuration file: " + pathToXml), e);
}
return;
}
}
Expand All @@ -250,13 +274,14 @@ public Integer getExternalServiceTimeout() {
}

private void initExternalServiceTimeout() {
if (externalServiceTimeout != null)
if (externalServiceTimeout != null) {
return;
}
if (rvdConfig != null && rvdConfig.getExternalServiceTimeout() != null && rvdConfig.getExternalServiceTimeout().trim().length() > 0) {
try {
this.externalServiceTimeout = Integer.parseInt(rvdConfig.getExternalServiceTimeout());
} catch (NumberFormatException e) {
logger.warn(LoggingHelper.buildMessage(getClass(),"getExternalServiceTimeout",null,"Cannot parse RVD ES timeout configuration setting. Will use default: " + DEFAULT_ES_TIMEOUT + (e.getMessage() != null ? " - " + e.getMessage() : "")));
logger.warn(LoggingHelper.buildMessage(getClass(), "getExternalServiceTimeout", null, "Cannot parse RVD ES timeout configuration setting. Will use default: " + DEFAULT_ES_TIMEOUT + (e.getMessage() != null ? " - " + e.getMessage() : "")));
this.externalServiceTimeout = DEFAULT_ES_TIMEOUT;
}
} else {
Expand All @@ -280,20 +305,24 @@ public URI getRestcommBaseUri() {
if (this.restcommBaseUri == null) {
// check rvd.xml override first
String rawUrl = rvdConfig.getRestcommBaseUrl();
if ( ! RvdUtils.isEmpty(rawUrl) ) {
if (!RvdUtils.isEmpty(rawUrl)) {
try {
URI uri = new URI(rawUrl);
if ( ! RvdUtils.isEmpty(uri.getScheme()) && !RvdUtils.isEmpty(uri.getHost()) )
if (!RvdUtils.isEmpty(uri.getScheme()) && !RvdUtils.isEmpty(uri.getHost())) {
this.restcommBaseUri = uri;
} catch (URISyntaxException e) { /* do nothing */}
}
} catch (URISyntaxException e) {
/* do nothing */
}
}
// if no override value in rvd.xml use the automatic way
if (this.restcommBaseUri == null) {
UriUtils uriUtils = new UriUtils(this);
try {
URI uri = new URI("/");
this.restcommBaseUri = uriUtils.resolve(uri);
} catch (URISyntaxException e) { /* we should never reach here */
} catch (URISyntaxException e) {
/* we should never reach here */
throw new IllegalStateException();
}
}
Expand Down Expand Up @@ -383,4 +412,68 @@ public String getWelcomeMessage() {
public String getRvdInstanceId() {
return rvdInstanceId;
}
}

@Override
public Integer getExternalServiceMaxConns() {
return (rvdConfig != null
&& rvdConfig.getExternalServiceMaxConns() != null)
? rvdConfig.getExternalServiceMaxConns() : RvdConfiguration.DEFAULT_ES_MAX_CONNS;
}

@Override
public Integer getExternalServiceMaxConnsPerRoute() {
return (rvdConfig != null
&& rvdConfig.getExternalServiceMaxConnsPerRoute() != null)
? rvdConfig.getExternalServiceMaxConnsPerRoute() : RvdConfiguration.DEFAULT_ES_MAX_CONNS_PER_ROUTE;
}

@Override
public Integer getExternalServiceTTL() {
return (rvdConfig != null
&& rvdConfig.getExternalServiceTTL() != null)
? rvdConfig.getExternalServiceTTL() : RvdConfiguration.DEFAULT_ES_TTL;
}

@Override
public Integer getDefaultHttpTimeout() {
return (rvdConfig != null
&& rvdConfig.getDefaultHttpTimeout() != null)
? rvdConfig.getDefaultHttpTimeout() : RvdConfiguration.DEFAULT_HTTP_TIMEOUT;
}

@Override
public Integer getDefaultHttpMaxConns() {
return (rvdConfig != null
&& rvdConfig.getDefaultHttpMaxConns() != null)
? rvdConfig.getDefaultHttpMaxConns() : RvdConfiguration.DEFAULT_HTTP_MAX_CONNS;
}

@Override
public Integer getDefaultHttpMaxConnsPerRoute() {
return (rvdConfig != null
&& rvdConfig.getDefaultHttpMaxConnsPerRoute() != null)
? rvdConfig.getDefaultHttpMaxConnsPerRoute() : RvdConfiguration.DEFAULT_HTTP_MAX_CONNS_PER_ROUTE;
}

@Override
public Integer getDefaultHttpTTL() {
return (rvdConfig != null
&& rvdConfig.getDefaultHttpTTL() != null)
? rvdConfig.getDefaultHttpTTL() : RvdConfiguration.DEFAULT_HTTP_TTL;
}

@Override
public List<RvdMaxPerHost> getExternalServiceMaxPerRoute() {
return (rvdConfig != null
&& rvdConfig.getExternalServicepMaxPerRoute() != null)
? rvdConfig.getExternalServicepMaxPerRoute() : null;
}

@Override
public List<RvdMaxPerHost> getDefaultHttpMaxPerRoute() {
return (rvdConfig != null
&& rvdConfig.getDefaultHttpMaxPerRoute() != null)
? rvdConfig.getDefaultHttpMaxPerRoute() : null; }


}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private String accessApi(final HashMap<String, String> params, final AccessApiAc
if (RvdUtils.isEmpty(identity.getEffectiveAuthorizationHeader()))
throw new ApplicationsApiSyncException("Could not determine credentials to access API.");
// create the client
RestcommClient client = new RestcommClient(restcommBaseUri,identity.getEffectiveAuthorizationHeader(),appContext.getHttpClientBuilder());
RestcommClient client = new RestcommClient(restcommBaseUri,identity.getEffectiveAuthorizationHeader(),appContext.getDefaultHttpClient());
String accountSid = identity.getAccountInfo().getSid();
RestcommApplicationResponse applicationResponse = null;
String applicationSid;
Expand Down

0 comments on commit 1eb598e

Please sign in to comment.