Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Nov 15, 2023
1 parent e28d771 commit 11d1d85
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ private ApplicationConstants() {
// Console and My Account application names.
public static final String CONSOLE_APPLICATION_NAME = "Console";
public static final String MY_ACCOUNT_APPLICATION_NAME = "My Account";
public static final String MY_ACCOUNT_APP_PATH = "/myaccount";
public static final String CONSOLE_APP_PATH = "/console";
public static final String MY_ACCOUNT_APP_NAME = "myaccount";
public static final String CONSOLE_APP_NAME = "console";

/**
* Group the constants related to logs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import static org.wso2.carbon.identity.application.mgt.ApplicationConstants.CONSOLE_APP_PATH;
import static org.wso2.carbon.identity.application.mgt.ApplicationConstants.CONSOLE_APP_NAME;
import static org.wso2.carbon.identity.application.mgt.ApplicationConstants.ENABLE_APPLICATION_ROLE_VALIDATION_PROPERTY;
import static org.wso2.carbon.identity.application.mgt.ApplicationConstants.LogConstants.APP_OWNER;
import static org.wso2.carbon.identity.application.mgt.ApplicationConstants.LogConstants.DISABLE_LEGACY_AUDIT_LOGS_IN_APP_MGT_CONFIG;
import static org.wso2.carbon.identity.application.mgt.ApplicationConstants.LogConstants.INBOUND_AUTHENTICATION_CONFIG;
import static org.wso2.carbon.identity.application.mgt.ApplicationConstants.MY_ACCOUNT_APP_PATH;
import static org.wso2.carbon.identity.application.mgt.ApplicationConstants.MY_ACCOUNT_APP_NAME;
import static org.wso2.carbon.user.core.constants.UserCoreErrorConstants.ErrorMessages.ERROR_CODE_ROLE_ALREADY_EXISTS;
import static org.wso2.carbon.utils.CarbonUtils.isLegacyAuditLogsDisabled;

Expand Down Expand Up @@ -1039,14 +1039,13 @@ public static boolean isLegacyAuditLogsDisabledInAppMgt() {
* @return The URL which origin replaced placeholders.
* @throws URLBuilderException If any error occurs when building absolute public url without path.
*/
public static String replaceUrlOriginWithPlaceholders(String absoluteUrl) throws URLBuilderException {
public static String replaceUrlOriginWithPlaceholders(String applicationName, String absoluteUrl) throws URLBuilderException {

if (StringUtils.contains(absoluteUrl, CONSOLE_APP_PATH) ||
StringUtils.contains(absoluteUrl, MY_ACCOUNT_APP_PATH)) {
if (applicationName.equalsIgnoreCase(CONSOLE_APP_NAME) ||
applicationName.equalsIgnoreCase(MY_ACCOUNT_APP_NAME)) {
String origin = ServiceURLBuilder.create().build().getAbsolutePublicUrlWithoutPath();
absoluteUrl = StringUtils.replace(absoluteUrl, origin, BASE_URL_PLACEHOLDER);
}

return absoluteUrl;
}

Expand All @@ -1057,14 +1056,13 @@ public static String replaceUrlOriginWithPlaceholders(String absoluteUrl) throws
* @return The resolved URL from placeholders.
* @throws URLBuilderException If any error occurs when building absolute public url without path.
*/
public static String resolveOriginUrlFromPlaceholders(String absoluteUrl) throws URLBuilderException {
public static String resolveOriginUrlFromPlaceholders(String applicationName, String absoluteUrl) throws URLBuilderException {

if (StringUtils.contains(absoluteUrl, CONSOLE_APP_PATH) ||
StringUtils.contains(absoluteUrl, MY_ACCOUNT_APP_PATH)) {
if (applicationName.equalsIgnoreCase(CONSOLE_APP_NAME) ||
applicationName.equalsIgnoreCase(MY_ACCOUNT_APP_NAME)) {
String origin = ServiceURLBuilder.create().build().getAbsolutePublicUrlWithoutPath();
absoluteUrl = StringUtils.replace(absoluteUrl, BASE_URL_PLACEHOLDER, origin);
}

return absoluteUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ private ApplicationCreateResult persistBasicApplicationInformation(Connection co
storeAppPrepStmt.setString(10, resourceId);
storeAppPrepStmt.setString(11, application.getImageUrl());
storeAppPrepStmt.setString(12,
ApplicationMgtUtil.replaceUrlOriginWithPlaceholders(application.getAccessUrl()));
ApplicationMgtUtil.replaceUrlOriginWithPlaceholders(applicationName, application.getAccessUrl()));
storeAppPrepStmt.execute();

results = storeAppPrepStmt.getGeneratedKeys();
Expand Down Expand Up @@ -958,7 +958,7 @@ private void updateBasicApplicationData(ServiceProvider serviceProvider, Connect
statement.setString(ApplicationTableColumns.IS_DISCOVERABLE, isDiscoverable ? "1" : "0");
statement.setString(ApplicationTableColumns.IMAGE_URL, serviceProvider.getImageUrl());
statement.setString(ApplicationTableColumns.ACCESS_URL,
ApplicationMgtUtil.replaceUrlOriginWithPlaceholders(serviceProvider.getAccessUrl()));
ApplicationMgtUtil.replaceUrlOriginWithPlaceholders(serviceProvider.getApplicationName(), serviceProvider.getAccessUrl()));
if (isValidUserForOwnerUpdate) {
User owner = serviceProvider.getOwner();
statement.setString(ApplicationTableColumns.USERNAME, owner.getUserName());
Expand Down Expand Up @@ -1890,6 +1890,7 @@ private ServiceProvider getBasicApplicationData(String applicationName, Connecti
serviceProvider.setDescription(basicAppDataResultSet.getString(6));
serviceProvider.setImageUrl(basicAppDataResultSet.getString(ApplicationTableColumns.IMAGE_URL));
serviceProvider.setAccessUrl(ApplicationMgtUtil.resolveOriginUrlFromPlaceholders(
serviceProvider.getApplicationName(),
basicAppDataResultSet.getString(ApplicationTableColumns.ACCESS_URL)));
serviceProvider.setDiscoverable(getBooleanValue(basicAppDataResultSet.getString(ApplicationTableColumns
.IS_DISCOVERABLE)));
Expand Down Expand Up @@ -2350,6 +2351,7 @@ private ServiceProvider getBasicApplicationData(int appId, Connection connection
serviceProvider.setDescription(rs.getString(ApplicationTableColumns.DESCRIPTION));
serviceProvider.setImageUrl(rs.getString(ApplicationTableColumns.IMAGE_URL));
serviceProvider.setAccessUrl(ApplicationMgtUtil.resolveOriginUrlFromPlaceholders(
serviceProvider.getApplicationName(),
rs.getString(ApplicationTableColumns.ACCESS_URL)));
serviceProvider.setDiscoverable(getBooleanValue(rs.getString(ApplicationTableColumns.IS_DISCOVERABLE)));

Expand Down Expand Up @@ -5637,7 +5639,7 @@ private ApplicationBasicInfo buildApplicationBasicInfo(ResultSet appNameResultSe
basicInfo.setImageUrl(appNameResultSet.getString(ApplicationTableColumns.IMAGE_URL));

try {
basicInfo.setAccessUrl(ApplicationMgtUtil.resolveOriginUrlFromPlaceholders(
basicInfo.setAccessUrl(ApplicationMgtUtil.resolveOriginUrlFromPlaceholders(basicInfo.getApplicationName(),
appNameResultSet.getString(ApplicationTableColumns.ACCESS_URL)));
} catch (URLBuilderException e) {
throw new IdentityApplicationManagementException(
Expand Down Expand Up @@ -5677,7 +5679,7 @@ private ApplicationBasicInfo buildApplicationBasicInfoWithInboundConfig(ResultSe
basicInfo.setImageUrl(appNameResultSet.getString(ApplicationTableColumns.IMAGE_URL));

try {
basicInfo.setAccessUrl(ApplicationMgtUtil.resolveOriginUrlFromPlaceholders(
basicInfo.setAccessUrl(ApplicationMgtUtil.resolveOriginUrlFromPlaceholders(basicInfo.getApplicationName(),
appNameResultSet.getString(ApplicationTableColumns.ACCESS_URL)));
} catch (URLBuilderException e) {
throw new IdentityApplicationManagementException(
Expand Down

0 comments on commit 11d1d85

Please sign in to comment.