Search in sources :

Example 1 with PackageConfig

use of com.opensymphony.xwork2.config.entities.PackageConfig in project qi4j-sdk by Qi4j.

the class Qi4jCodebehindPackageProvider method loadPackageConfig.

/**
     * Finds or creates the package configuration for an Action class.
     *
     * The namespace annotation is honored, if found,
     * and the namespace is checked for a parent configuration.
     *
     * @param actionNamespace The configuration namespace
     * @param actionPackage   The Java package containing our Action classes
     * @param actionClass     The Action class instance
     *
     * @return PackageConfig object for the Action class
     */
protected PackageConfig.Builder loadPackageConfig(String actionNamespace, String actionPackage, Class actionClass) {
    PackageConfig.Builder parent = null;
    // Check for the @Namespace annotation
    if (actionClass != null) {
        Namespace ns = (Namespace) actionClass.getAnnotation(Namespace.class);
        if (ns != null) {
            parent = loadPackageConfig(actionNamespace, actionPackage, null);
            actionNamespace = ns.value();
            actionPackage = actionClass.getName();
        // See if the namespace has been overridden by the @Action annotation
        } else {
            org.apache.struts2.config.Action actionAnn = (org.apache.struts2.config.Action) actionClass.getAnnotation(org.apache.struts2.config.Action.class);
            if (actionAnn != null && !actionAnn.DEFAULT_NAMESPACE.equals(actionAnn.namespace())) {
                // we pass null as the namespace in case the parent package hasn't been loaded yet
                parent = loadPackageConfig(null, actionPackage, null);
                actionPackage = actionClass.getName();
            }
        }
    }
    PackageConfig.Builder pkgConfig = packageLoader.getPackage(actionPackage);
    if (pkgConfig == null) {
        pkgConfig = new PackageConfig.Builder(actionPackage);
        pkgConfig.namespace(actionNamespace);
        if (parent == null) {
            PackageConfig cfg = configuration.getPackageConfig(defaultParentPackage);
            if (cfg != null) {
                pkgConfig.addParent(cfg);
            } else {
                throw new ConfigurationException("ClasspathPackageProvider: Unable to locate default parent package: " + defaultParentPackage);
            }
        }
        packageLoader.registerPackage(pkgConfig);
    // if the parent package was first created by a child, ensure the namespace is correct
    } else if (pkgConfig.getNamespace() == null) {
        pkgConfig.namespace(actionNamespace);
    }
    if (parent != null) {
        packageLoader.registerChildToParent(pkgConfig, parent);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("class:" + actionClass + " parent:" + parent + " current:" + (pkgConfig != null ? pkgConfig.getName() : ""));
    }
    return pkgConfig;
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) org.apache.struts2.config(org.apache.struts2.config) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 2 with PackageConfig

use of com.opensymphony.xwork2.config.entities.PackageConfig in project dhis2-core by dhis2.

the class DefaultModuleManager method detectModules.

// -------------------------------------------------------------------------
// Module detection
// -------------------------------------------------------------------------
private synchronized void detectModules() {
    if (modulesDetected) {
        return;
    }
    I18n i18n = i18nManager.getI18n();
    for (PackageConfig packageConfig : getPackageConfigs()) {
        String name = packageConfig.getName();
        String namespace = packageConfig.getNamespace();
        String displayName = i18n.getString(name);
        log.debug("Package config: " + name + ", " + namespace);
        if (packageConfig.getAllActionConfigs().size() == 0) {
            log.debug("Ignoring action package with no actions: " + name);
            continue;
        }
        if (namespace == null || namespace.length() == 0) {
            throw new RuntimeException("Missing namespace in action package: " + name);
        }
        if (modulesByName.containsKey(name)) {
            throw new RuntimeException("Two action packages have the same name: " + name);
        }
        if (modulesByNamespace.containsKey(namespace)) {
            Module module = modulesByNamespace.get(namespace);
            throw new RuntimeException("These action packages have the same namespace: " + name + " and " + module.getName());
        }
        Module module = new Module(name, namespace);
        module.setDisplayName(displayName);
        modulesByName.put(name, module);
        modulesByNamespace.put(namespace, module);
        boolean include = !menuModuleExclusions.contains(name);
        if (packageConfig.getActionConfigs().containsKey(defaultActionName) && include) {
            module.setDefaultAction(".." + namespace + "/" + defaultActionName + ".action");
            menuModules.add(module);
            log.debug("Has default action: " + name);
        } else {
            log.debug("Doesn't have default action: " + name);
        }
    }
    Collections.sort(menuModules, moduleComparator);
    log.debug("Menu modules detected: " + menuModules);
    modulesDetected = true;
}
Also used : PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) I18n(org.hisp.dhis.i18n.I18n)

Example 3 with PackageConfig

use of com.opensymphony.xwork2.config.entities.PackageConfig in project dhis2-core by dhis2.

the class DetectingSystemAuthoritiesProvider method getSystemAuthorities.

// -------------------------------------------------------------------------
// SystemAuthoritiesProvider implementation
// -------------------------------------------------------------------------
@Override
public Collection<String> getSystemAuthorities() {
    HashSet<String> authorities = new HashSet<>();
    Configuration configuration = Dispatcher.getInstance().getConfigurationManager().getConfiguration();
    for (PackageConfig packageConfig : configuration.getPackageConfigs().values()) {
        for (ActionConfig actionConfig : packageConfig.getActionConfigs().values()) {
            authorities.addAll(requiredAuthoritiesProvider.getAllAuthorities(actionConfig));
        }
    }
    return authorities;
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) Configuration(com.opensymphony.xwork2.config.Configuration) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) HashSet(java.util.HashSet)

Example 4 with PackageConfig

use of com.opensymphony.xwork2.config.entities.PackageConfig in project dhis2-core by dhis2.

the class SpringSecurityActionAccessResolver method hasAccess.

// -------------------------------------------------------------------------
// ActionAccessResolver implementation
// -------------------------------------------------------------------------
@Override
public boolean hasAccess(String module, String name) {
    // ---------------------------------------------------------------------
    // Get ObjectDefinitionSource
    // ---------------------------------------------------------------------
    Configuration config = Dispatcher.getInstance().getConfigurationManager().getConfiguration();
    PackageConfig packageConfig = config.getPackageConfig(module);
    if (packageConfig == null) {
        throw new IllegalArgumentException("Module doesn't exist: '" + module + "'");
    }
    ActionConfig actionConfig = packageConfig.getActionConfigs().get(name);
    if (actionConfig == null) {
        throw new IllegalArgumentException("Module " + module + " doesn't have an action named: '" + name + "'");
    }
    SecurityMetadataSource securityMetadataSource = requiredAuthoritiesProvider.createSecurityMetadataSource(actionConfig);
    // ---------------------------------------------------------------------
    // Test access
    // ---------------------------------------------------------------------
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    try {
        if (securityMetadataSource.getAttributes(actionConfig) != null) {
            if (authentication == null || !authentication.isAuthenticated()) {
                return false;
            }
            accessDecisionManager.decide(authentication, actionConfig, securityMetadataSource.getAttributes(actionConfig));
        }
        log.debug("Access to [" + module + ", " + name + "]: TRUE");
        return true;
    } catch (AccessDeniedException e) {
        log.debug("Access to [" + module + ", " + name + "]: FALSE (access denied)");
        return false;
    } catch (InsufficientAuthenticationException e) {
        log.debug("Access to [" + module + ", " + name + "]: FALSE (insufficient authentication)");
        return false;
    }
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Configuration(com.opensymphony.xwork2.config.Configuration) SecurityMetadataSource(org.springframework.security.access.SecurityMetadataSource) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) InsufficientAuthenticationException(org.springframework.security.authentication.InsufficientAuthenticationException) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 5 with PackageConfig

use of com.opensymphony.xwork2.config.entities.PackageConfig in project qi4j-sdk by Qi4j.

the class Qi4jCodebehindPackageProvider method processActionClass.

/**
     * Create a default action mapping for a class instance.
     *
     * The namespace annotation is honored, if found, otherwise
     * the Java package is converted into the namespace
     * by changing the dots (".") to slashes ("/").
     *
     * @param cls  Action or POJO instance to process
     * @param pkgs List of packages that were scanned for Actions
     */
protected void processActionClass(Class<?> cls, String[] pkgs) {
    String name = cls.getName();
    String actionPackage = cls.getPackage().getName();
    String actionNamespace = null;
    String actionName = null;
    org.apache.struts2.config.Action actionAnn = (org.apache.struts2.config.Action) cls.getAnnotation(org.apache.struts2.config.Action.class);
    if (actionAnn != null) {
        actionName = actionAnn.name();
        if (actionAnn.namespace().equals(org.apache.struts2.config.Action.DEFAULT_NAMESPACE)) {
            actionNamespace = "";
        } else {
            actionNamespace = actionAnn.namespace();
        }
    } else {
        for (String pkg : pkgs) {
            if (name.startsWith(pkg)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("ClasspathPackageProvider: Processing class " + name);
                }
                name = name.substring(pkg.length() + 1);
                actionNamespace = "";
                actionName = name;
                int pos = name.lastIndexOf('.');
                if (pos > -1) {
                    actionNamespace = "/" + name.substring(0, pos).replace('.', '/');
                    actionName = name.substring(pos + 1);
                }
                break;
            }
        }
        // Truncate Action suffix if found
        if (actionName.endsWith(getClassSuffix())) {
            actionName = actionName.substring(0, actionName.length() - getClassSuffix().length());
        }
        // Force initial letter of action to lowercase, if desired
        if ((forceLowerCase) && (actionName.length() > 1)) {
            int lowerPos = actionName.lastIndexOf('/') + 1;
            StringBuilder sb = new StringBuilder();
            sb.append(actionName.substring(0, lowerPos));
            sb.append(Character.toLowerCase(actionName.charAt(lowerPos)));
            sb.append(actionName.substring(lowerPos + 1));
            actionName = sb.toString();
        }
    }
    PackageConfig.Builder pkgConfig = loadPackageConfig(actionNamespace, actionPackage, cls);
    // In case the package changed due to namespace annotation processing
    if (!actionPackage.equals(pkgConfig.getName())) {
        actionPackage = pkgConfig.getName();
    }
    Annotation annotation = cls.getAnnotation(ParentPackage.class);
    if (annotation != null) {
        String parent = ((ParentPackage) annotation).value()[0];
        PackageConfig parentPkg = configuration.getPackageConfig(parent);
        if (parentPkg == null) {
            throw new ConfigurationException("ClasspathPackageProvider: Unable to locate parent package " + parent, annotation);
        }
        pkgConfig.addParent(parentPkg);
        if (!isNotEmpty(pkgConfig.getNamespace()) && isNotEmpty(parentPkg.getNamespace())) {
            pkgConfig.namespace(parentPkg.getNamespace());
        }
    }
    ResultTypeConfig defaultResultType = packageLoader.getDefaultResultType(pkgConfig);
    ActionConfig actionConfig = new ActionConfig.Builder(actionPackage, actionName, cls.getName()).addResultConfigs(new ResultMap<String, ResultConfig>(cls, actionName, defaultResultType)).build();
    pkgConfig.addActionConfig(actionName, actionConfig);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) Annotation(java.lang.annotation.Annotation) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ResultTypeConfig(com.opensymphony.xwork2.config.entities.ResultTypeConfig) org.apache.struts2.config(org.apache.struts2.config)

Aggregations

PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)6 Configuration (com.opensymphony.xwork2.config.Configuration)3 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)3 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)2 org.apache.struts2.config (org.apache.struts2.config)2 ResultTypeConfig (com.opensymphony.xwork2.config.entities.ResultTypeConfig)1 Annotation (java.lang.annotation.Annotation)1 HashSet (java.util.HashSet)1 I18n (org.hisp.dhis.i18n.I18n)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1 SecurityMetadataSource (org.springframework.security.access.SecurityMetadataSource)1 InsufficientAuthenticationException (org.springframework.security.authentication.InsufficientAuthenticationException)1 Authentication (org.springframework.security.core.Authentication)1 SecurityContext (org.springframework.security.core.context.SecurityContext)1