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

Delay the config parser data read #268

Closed
Closed
Show file tree
Hide file tree
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 @@ -21,6 +21,10 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.identity.context.rewrite.bean.OrganizationRewriteContext;
import org.wso2.carbon.identity.context.rewrite.bean.RewriteContext;
import org.wso2.carbon.identity.core.util.IdentityConfigParser;
import org.wso2.carbon.identity.core.util.IdentityCoreInitializedEvent;
import org.wso2.carbon.identity.organization.management.service.OrganizationManagementInitialize;
import org.wso2.carbon.user.core.service.RealmService;
import org.osgi.service.component.annotations.Activate;
Expand All @@ -37,6 +41,16 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

import static org.wso2.carbon.identity.context.rewrite.constant.RewriteConstants.ORGANIZATION_PATH_PARAM;
import static org.wso2.carbon.identity.core.util.IdentityCoreConstants.ENABLE_TENANT_QUALIFIED_URLS;

@Component(
name = "identity.context.rewrite.valve.component",
Expand All @@ -51,6 +65,16 @@ protected void activate(ComponentContext context) {
log.debug("ContextRewriteValveServiceComponent is activated.");
}
loadPageNotFoundErrorPage();
ContextRewriteValveServiceComponentHolder.getInstance()
.setOrganizationRewriteContexts(getOrgContextsToRewrite());
// Initialize the tenant context rewrite valve.
ContextRewriteValveServiceComponentHolder.getInstance().setContextsToRewrite(getContextsToRewrite());
ContextRewriteValveServiceComponentHolder.getInstance()
.setContextListToOverwriteDispatch(getContextListToOverwriteDispatchLocation());
ContextRewriteValveServiceComponentHolder.getInstance()
.setIgnorePathListForOverwriteDispatch(getIgnorePathListForOverwriteDispatch());
ContextRewriteValveServiceComponentHolder.getInstance()
.setTenantQualifiedUrlsEnabled(isTenantQualifiedUrlsEnabled());
}

@Deactivate
Expand Down Expand Up @@ -100,6 +124,23 @@ protected void unsetOrganizationManagementEnablingService(
ContextRewriteValveServiceComponentHolder.getInstance().setOrganizationManagementEnable(null);
}

@Reference(
name = "identityCoreInitializedEventService",
service = IdentityCoreInitializedEvent.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetIdentityCoreInitializedEventService"
)
protected void setIdentityCoreInitializedEventService(IdentityCoreInitializedEvent identityCoreInitializedEvent) {
/* reference IdentityCoreInitializedEvent service to guarantee that this component will wait until identity core
is started */
}

protected void unsetIdentityCoreInitializedEventService(IdentityCoreInitializedEvent identityCoreInitializedEvent) {
/* reference IdentityCoreInitializedEvent service to guarantee that this component will wait until identity core
is started */
}

private void loadPageNotFoundErrorPage() {

String errorPage = "Page Not Found";
Expand All @@ -123,5 +164,124 @@ private void loadPageNotFoundErrorPage() {
}
ContextRewriteValveServiceComponentHolder.getInstance().setPageNotFoundErrorPage(errorPage);
}

private List<OrganizationRewriteContext> getOrgContextsToRewrite() {

List<OrganizationRewriteContext> organizationRewriteContexts = new ArrayList<>();
Map<String, Object> configuration = IdentityConfigParser.getInstance().getConfiguration();
Object webAppBasePathContexts = configuration.get("OrgContextsToRewrite.WebApp.Context.BasePath");
setOrganizationRewriteContexts(organizationRewriteContexts, webAppBasePathContexts, true);

Object webAppSubPathContexts = configuration.get("OrgContextsToRewrite.WebApp.Context.SubPaths.Path");
setSubPathContexts(organizationRewriteContexts, webAppSubPathContexts);

Object servletBasePathContexts = configuration.get("OrgContextsToRewrite.Servlet.Context");
setOrganizationRewriteContexts(organizationRewriteContexts, servletBasePathContexts, false);

return organizationRewriteContexts;
}

private void setOrganizationRewriteContexts(List<OrganizationRewriteContext> organizationRewriteContexts,
Object basePathContexts, boolean isWebApp) {

if (basePathContexts != null) {
if (basePathContexts instanceof ArrayList) {
for (String context : (ArrayList<String>) basePathContexts) {
organizationRewriteContexts.add(new OrganizationRewriteContext(isWebApp, context));
}
} else {
organizationRewriteContexts.add(new OrganizationRewriteContext(isWebApp,
basePathContexts.toString()));
}
}
}

private void setSubPathContexts(List<OrganizationRewriteContext> organizationRewriteContexts,
Object subPathContexts) {

if (subPathContexts != null) {
if (subPathContexts instanceof ArrayList) {
for (String subPath : (ArrayList<String>) subPathContexts) {
Optional<OrganizationRewriteContext> maybeOrgRewriteContext = organizationRewriteContexts.stream()
.filter(rewriteContext -> subPath.startsWith(rewriteContext.getContext()))
.max(Comparator.comparingInt(rewriteContext -> rewriteContext.getContext().length()));
maybeOrgRewriteContext.ifPresent(
organizationRewriteContext -> organizationRewriteContext.addSubPath(
Pattern.compile("^" + ORGANIZATION_PATH_PARAM + "([^/]+)" + subPath)));
}
}
}
}

private List<RewriteContext> getContextsToRewrite() {

List<RewriteContext> rewriteContexts = new ArrayList<>();
Map<String, Object> configuration = IdentityConfigParser.getInstance().getConfiguration();
Object webAppContexts = configuration.get("TenantContextsToRewrite.WebApp.Context");
if (webAppContexts != null) {
if (webAppContexts instanceof ArrayList) {
for (String context : (ArrayList<String>) webAppContexts) {
rewriteContexts.add(new RewriteContext(true, context));
}
} else {
rewriteContexts.add(new RewriteContext(true, webAppContexts.toString()));
}
}

Object servletContexts = configuration.get("TenantContextsToRewrite.Servlet.Context");
if (servletContexts != null) {
if (servletContexts instanceof ArrayList) {
for (String context : (ArrayList<String>) servletContexts) {
rewriteContexts.add(new RewriteContext(false, context));
}
} else {
rewriteContexts.add(new RewriteContext(false, servletContexts.toString()));
}
}
return rewriteContexts;
}

private boolean isTenantQualifiedUrlsEnabled() {

Map<String, Object> configuration = IdentityConfigParser.getInstance().getConfiguration();
String enableTenantQualifiedUrls = (String) configuration.get(ENABLE_TENANT_QUALIFIED_URLS);
return Boolean.parseBoolean(enableTenantQualifiedUrls);
}

/**
* Get context list to overwrite dispatch location.
*
* @return list of contexts.
*/
private List<String> getContextListToOverwriteDispatchLocation() {

return getConfigValues("TenantContextsToRewrite.OverwriteDispatch.Context");
}

/**
* Get path list to ignore for overwrite dispatch.
*
* @return list of paths.
*/
private List<String> getIgnorePathListForOverwriteDispatch() {

return getConfigValues("TenantContextsToRewrite.OverwriteDispatch.IgnorePath");
}

private List<String> getConfigValues(String elementPath) {

Map<String, Object> configuration = IdentityConfigParser.getInstance().getConfiguration();
Object elements = configuration.get(elementPath);
if (elements != null) {
List<String> configValues = new ArrayList<>();
if (elements instanceof List) {
configValues.addAll((List<String>) elements);
} else {
configValues.add(elements.toString());
}
return configValues;
}
return Collections.emptyList();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.context.rewrite.bean.OrganizationRewriteContext;
import org.wso2.carbon.identity.context.rewrite.bean.RewriteContext;
import org.wso2.carbon.identity.organization.management.service.OrganizationManagementInitialize;
import org.wso2.carbon.user.core.service.RealmService;

import java.util.List;

public class ContextRewriteValveServiceComponentHolder {

private static ContextRewriteValveServiceComponentHolder instance = new ContextRewriteValveServiceComponentHolder();
Expand All @@ -31,6 +35,12 @@ public class ContextRewriteValveServiceComponentHolder {
private static final Log log = LogFactory.getLog(ContextRewriteValveServiceComponentHolder.class);
private boolean isOrganizationManagementEnable;

private List<OrganizationRewriteContext> organizationRewriteContexts;
private List<RewriteContext> contextsToRewrite;
private List<String> contextListToOverwriteDispatch;
private List<String> ignorePathListForOverwriteDispatch;
private boolean isTenantQualifiedUrlsEnabled;

private ContextRewriteValveServiceComponentHolder() {

}
Expand Down Expand Up @@ -91,4 +101,55 @@ public void setOrganizationManagementEnable(
isOrganizationManagementEnable = organizationManagementInitializeService.isOrganizationManagementEnabled();
}
}

public List<OrganizationRewriteContext> getOrganizationRewriteContexts() {

return organizationRewriteContexts;
}

public void setOrganizationRewriteContexts(
List<OrganizationRewriteContext> organizationRewriteContexts) {

this.organizationRewriteContexts = organizationRewriteContexts;
}

public List<RewriteContext> getContextsToRewrite() {

return contextsToRewrite;
}

public void setContextsToRewrite(List<RewriteContext> contextsToRewrite) {

this.contextsToRewrite = contextsToRewrite;
}

public List<String> getContextListToOverwriteDispatch() {

return contextListToOverwriteDispatch;
}

public void setContextListToOverwriteDispatch(List<String> contextListToOverwriteDispatch) {

this.contextListToOverwriteDispatch = contextListToOverwriteDispatch;
}

public List<String> getIgnorePathListForOverwriteDispatch() {

return ignorePathListForOverwriteDispatch;
}

public void setIgnorePathListForOverwriteDispatch(List<String> ignorePathListForOverwriteDispatch) {

this.ignorePathListForOverwriteDispatch = ignorePathListForOverwriteDispatch;
}

public boolean isTenantQualifiedUrlsEnabled() {

return isTenantQualifiedUrlsEnabled;
}

public void setTenantQualifiedUrlsEnabled(boolean tenantQualifiedUrlsEnabled) {

this.isTenantQualifiedUrlsEnabled = tenantQualifiedUrlsEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@
*/
public class OrganizationContextRewriteValve extends ValveBase {

private static List<OrganizationRewriteContext> orgContextsToRewrite;

@Override
protected synchronized void startInternal() throws LifecycleException {

super.startInternal();
orgContextsToRewrite = getOrgContextsToRewrite();
}

@Override
Expand All @@ -85,7 +83,8 @@ public void invoke(Request request, Response response) throws IOException, Servl

if (ContextRewriteValveServiceComponentHolder.getInstance().isOrganizationManagementEnabled() &&
StringUtils.startsWith(requestURI, ORGANIZATION_PATH_PARAM)) {
for (OrganizationRewriteContext organizationRewriteContext : orgContextsToRewrite) {
for (OrganizationRewriteContext organizationRewriteContext
: ContextRewriteValveServiceComponentHolder.getInstance().getOrganizationRewriteContexts()) {
Pattern orgPattern = organizationRewriteContext.getOrgContextPattern();
if (orgPattern.matcher(requestURI).find() || orgPattern.matcher(requestURI + "/").find()) {
subPathsConfigured = false;
Expand Down Expand Up @@ -158,52 +157,4 @@ private void unsetMDCThreadLocals() {
MDC.remove(TENANT_DOMAIN);
MDC.remove(TENANT_ID);
}

private List<OrganizationRewriteContext> getOrgContextsToRewrite() {

List<OrganizationRewriteContext> organizationRewriteContexts = new ArrayList<>();
Map<String, Object> configuration = IdentityConfigParser.getInstance().getConfiguration();
Object webAppBasePathContexts = configuration.get("OrgContextsToRewrite.WebApp.Context.BasePath");
setOrganizationRewriteContexts(organizationRewriteContexts, webAppBasePathContexts, true);

Object webAppSubPathContexts = configuration.get("OrgContextsToRewrite.WebApp.Context.SubPaths.Path");
setSubPathContexts(organizationRewriteContexts, webAppSubPathContexts);

Object servletBasePathContexts = configuration.get("OrgContextsToRewrite.Servlet.Context");
setOrganizationRewriteContexts(organizationRewriteContexts, servletBasePathContexts, false);

return organizationRewriteContexts;
}

private void setOrganizationRewriteContexts(List<OrganizationRewriteContext> organizationRewriteContexts,
Object basePathContexts, boolean isWebApp) {

if (basePathContexts != null) {
if (basePathContexts instanceof ArrayList) {
for (String context : (ArrayList<String>) basePathContexts) {
organizationRewriteContexts.add(new OrganizationRewriteContext(isWebApp, context));
}
} else {
organizationRewriteContexts.add(new OrganizationRewriteContext(isWebApp,
basePathContexts.toString()));
}
}
}

private void setSubPathContexts(List<OrganizationRewriteContext> organizationRewriteContexts,
Object subPathContexts) {

if (subPathContexts != null) {
if (subPathContexts instanceof ArrayList) {
for (String subPath : (ArrayList<String>) subPathContexts) {
Optional<OrganizationRewriteContext> maybeOrgRewriteContext = organizationRewriteContexts.stream()
.filter(rewriteContext -> subPath.startsWith(rewriteContext.getContext()))
.max(Comparator.comparingInt(rewriteContext -> rewriteContext.getContext().length()));
maybeOrgRewriteContext.ifPresent(
organizationRewriteContext -> organizationRewriteContext.addSubPath(
Pattern.compile("^" + ORGANIZATION_PATH_PARAM + "([^/]+)" + subPath)));
}
}
}
}
}
Loading
Loading