Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to drop standard port when getting configuration value #6600

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.HttpsURL;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -157,6 +159,8 @@ protected Map<String, Object> initialValue() {
public static final String PEM_END_CERTIFICATE = "-----END CERTIFICATE-----";
private static final String APPLICATION_DOMAIN = "Application";
private static final String WORKFLOW_DOMAIN = "Workflow";
private static final String HTTP = "http";
private static final String HTTPS = "https";
private static Boolean groupsVsRolesSeparationImprovementsEnabled;
private static Boolean showLegacyRoleClaimOnGroupRoleSeparationEnabled;
private static String JAVAX_TRANSFORMER_PROP_VAL = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
Expand Down Expand Up @@ -191,15 +195,48 @@ public static void clearIdentityErrorMsg() {
}

/**
* Read configuration elements from the identity.xml
* Read configuration elements from the identity.xml.
*
* @param key Element Name as specified from the parent elements in the XML structure.
* To read the element value of b in {@code<a><b>text</b></a>}, the property
* To read the element value of b in {@code <a><b>text</b></a>}, the property
* name should be passed as "a.b"
* @return Element text value, "text" for the above element.
* @return Element text value, "text" for the above element with the placeholders replaced.
*/
public static String getProperty(String key) {

String strValue = getPropertyValue(key);
strValue = fillURLPlaceholders(strValue);
return strValue;
}

/**
* Read configuration elements from the identity.xml and
* drops the port if it is 443 for https and 80 for http.
*
* @param key Element name as specified from the parent elements in the XML structure.
* To read the element value of b in {@code <a><b>text</b></a>}, the property
* name should be passed as "a.b".
* @return The value of the element, which is "text" in the above element, with the placeholders
* replaced and the standard port dropped.
*/
public static String getPropertyWithoutStandardPort(String key) {

String strValue = getPropertyValue(key);
strValue = replacePortNumberPlaceholder(strValue, Boolean.TRUE);
strValue = fillURLPlaceholders(strValue);
return strValue;
}

/**
* Gets the property value corresponding to the given key from the identity.xml file.
*
* @param key Element name as specified from the parent elements in the XML structure.
* To read the element value of b in {@code <a><b>text</b></a>}, the property
* name should be passed as "a.b".
* @return The value of the element, which is "text" in the above element.
*/
private static String getPropertyValue(String key) {

Object value = configuration.get(key);
String strValue;

Expand All @@ -214,7 +251,6 @@ public static String getProperty(String key) {
} else {
strValue = String.valueOf(value);
}
strValue = fillURLPlaceholders(strValue);
return strValue;
}

Expand Down Expand Up @@ -966,6 +1002,48 @@ public static boolean isValidFileName(String fileName) {
return matcher.matches();
}

/**
* Replaces the port number placeholder with the actual port number for non-standard ports, and
* if the ports are standard ports, namely 443 for https and 80 for http, the port number is dropped.
*
* @param urlWithPlaceholders URL with the placeholders.
* @return URL with the port number placeholder replaced.
*/
public static String replacePortNumberPlaceholder(String urlWithPlaceholders, boolean dropStandardPort) {

if (StringUtils.contains(urlWithPlaceholders, IdentityConstants.CarbonPlaceholders.CARBON_PORT)) {

String mgtTransport = CarbonUtils.getManagementTransport();
AxisConfiguration axisConfiguration = IdentityCoreServiceComponent.getConfigurationContextService().
getServerConfigContext().getAxisConfiguration();

int mgtTransportProxyPort = CarbonUtils.getTransportProxyPort(axisConfiguration, mgtTransport);
String mgtTransportPort = Integer.toString(mgtTransportProxyPort);

if (mgtTransportProxyPort <= 0) {
if (StringUtils.equals(mgtTransport, HTTP)) {
mgtTransportPort = System.getProperty(
IdentityConstants.CarbonPlaceholders.CARBON_PORT_HTTP_PROPERTY);
} else {
mgtTransportPort = System.getProperty(
IdentityConstants.CarbonPlaceholders.CARBON_PORT_HTTPS_PROPERTY);
}
}

if (dropStandardPort && ((StringUtils.equals(mgtTransport, HTTP) &&
StringUtils.equals(mgtTransportPort, String.valueOf(HttpURL.DEFAULT_PORT))) ||
(StringUtils.equals(mgtTransport, HTTPS) &&
StringUtils.equals(mgtTransportPort, String.valueOf(HttpsURL.DEFAULT_PORT))))) {
urlWithPlaceholders = StringUtils.replace(urlWithPlaceholders, ":" +
IdentityConstants.CarbonPlaceholders.CARBON_PORT, StringUtils.EMPTY);
} else {
urlWithPlaceholders = StringUtils.replace(urlWithPlaceholders,
IdentityConstants.CarbonPlaceholders.CARBON_PORT, mgtTransportPort);
}
}
return urlWithPlaceholders;
}

/**
* Replace the placeholders with the related values in the URL.
*
Expand Down Expand Up @@ -998,28 +1076,7 @@ public static String fillURLPlaceholders(String urlWithPlaceholders) {
hostName);
}

if (StringUtils.contains(urlWithPlaceholders, IdentityConstants.CarbonPlaceholders.CARBON_PORT)) {

String mgtTransport = CarbonUtils.getManagementTransport();
AxisConfiguration axisConfiguration = IdentityCoreServiceComponent.getConfigurationContextService().
getServerConfigContext().getAxisConfiguration();

int mgtTransportProxyPort = CarbonUtils.getTransportProxyPort(axisConfiguration, mgtTransport);
String mgtTransportPort = Integer.toString(mgtTransportProxyPort);

if (mgtTransportProxyPort <= 0) {
if (StringUtils.equals(mgtTransport, "http")) {
mgtTransportPort = System.getProperty(
IdentityConstants.CarbonPlaceholders.CARBON_PORT_HTTP_PROPERTY);
} else {
mgtTransportPort = System.getProperty(
IdentityConstants.CarbonPlaceholders.CARBON_PORT_HTTPS_PROPERTY);
}
}

urlWithPlaceholders = StringUtils.replace(urlWithPlaceholders,
IdentityConstants.CarbonPlaceholders.CARBON_PORT, mgtTransportPort);
}
urlWithPlaceholders = replacePortNumberPlaceholder(urlWithPlaceholders, Boolean.FALSE);

if (StringUtils.contains(urlWithPlaceholders, IdentityConstants.CarbonPlaceholders.CARBON_PORT_HTTP)) {

Expand Down