Search in sources :

Example 1 with NameFilter

use of org.wso2.carbon.identity.api.server.authenticators.v1.model.NameFilter in project identity-api-server by wso2.

the class ServerAuthenticatorManagementService method getAuthenticators.

/**
 * Retrieves the list of available authenticators.
 *
 * @param filter The filter string.
 * @param limit  The items per page. **Not supported at the moment.**
 * @param offset The offset to be used with the limit parameter. **Not supported at the moment.**
 * @return The list of authenticators
 */
public List<Authenticator> getAuthenticators(String filter, Integer limit, Integer offset) {
    handleNotImplementedCapabilities(limit, offset);
    try {
        String filterAuthenticatorName = null;
        String filterOperationForName = null;
        ArrayList<String> filterTagsList = null;
        int maximumItemPerPage = IdentityUtil.getMaximumItemPerPage();
        if (StringUtils.isNotBlank(filter)) {
            List<ExpressionNode> expressionNodes = getExpressionNodesForAuthenticator(filter);
            if (CollectionUtils.isNotEmpty(expressionNodes)) {
                NameFilter nameFilter = getFilterAuthenticatorNameAndOperation(expressionNodes);
                if (nameFilter != null) {
                    filterAuthenticatorName = nameFilter.getName();
                    filterOperationForName = nameFilter.getOperation();
                }
                filterTagsList = getFilterTagsList(expressionNodes);
            }
        }
        LocalAuthenticatorConfig[] localAuthenticatorConfigs = AuthenticatorsServiceHolder.getInstance().getApplicationManagementService().getAllLocalAuthenticators(ContextLoader.getTenantDomainFromContext());
        int localAuthenticatorsCount = localAuthenticatorConfigs.length;
        RequestPathAuthenticatorConfig[] requestPathAuthenticatorConfigs = new RequestPathAuthenticatorConfig[0];
        /* If there is no filter string available in the request, the request path authenticators are required to
            be fetched only if the  no. of local authenticators retrieved are less than the maximum items per page
            count as the no. of items returned in the response will be capped at the maximum items per page count. */
        if (StringUtils.isNotBlank(filter) || (StringUtils.isBlank(filter) && localAuthenticatorsCount < maximumItemPerPage)) {
            requestPathAuthenticatorConfigs = AuthenticatorsServiceHolder.getInstance().getApplicationManagementService().getAllRequestPathAuthenticators(ContextLoader.getTenantDomainFromContext());
        }
        List<String> requestedAttributeList = new ArrayList<>();
        requestedAttributeList.add(Constants.FEDERATED_AUTHENTICATORS);
        int idPCountToBeRetrieved = maximumItemPerPage - (localAuthenticatorsCount + requestPathAuthenticatorConfigs.length);
        List<IdentityProvider> identityProviders = null;
        /* If there is no filter string available in the request, the identity providers are required to
            be fetched only if the total of local authenticators and request path authenticators retrieved above is
            less than the maximum items per page count as the no. of items returned in the response will be capped
            at the maximum items per page count. */
        if (idPCountToBeRetrieved > 0 && StringUtils.isBlank(filter)) {
            IdpSearchResult idpSearchResult = AuthenticatorsServiceHolder.getInstance().getIdentityProviderManager().getIdPs(idPCountToBeRetrieved, null, null, null, null, ContextLoader.getTenantDomainFromContext(), requestedAttributeList);
            identityProviders = idpSearchResult.getIdPs();
        }
        return buildAuthenticatorsListResponse(filter, requestedAttributeList, filterAuthenticatorName, filterOperationForName, filterTagsList, localAuthenticatorConfigs, requestPathAuthenticatorConfigs, identityProviders);
    } catch (IdentityApplicationManagementException e) {
        throw handleApplicationMgtException(e, Constants.ErrorMessage.ERROR_CODE_ERROR_LISTING_AUTHENTICATORS, null);
    } catch (IdentityProviderManagementException e) {
        throw handleIdPException(e, Constants.ErrorMessage.ERROR_CODE_ERROR_LISTING_IDPS, null);
    }
}
Also used : IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) LocalAuthenticatorConfig(org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig) ArrayList(java.util.ArrayList) IdentityProvider(org.wso2.carbon.identity.application.common.model.IdentityProvider) IdpSearchResult(org.wso2.carbon.idp.mgt.model.IdpSearchResult) NameFilter(org.wso2.carbon.identity.api.server.authenticators.v1.model.NameFilter) ExpressionNode(org.wso2.carbon.identity.core.model.ExpressionNode) RequestPathAuthenticatorConfig(org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig) IdentityProviderManagementException(org.wso2.carbon.idp.mgt.IdentityProviderManagementException)

Example 2 with NameFilter

use of org.wso2.carbon.identity.api.server.authenticators.v1.model.NameFilter in project carbon-business-process by wso2.

the class ProcessManagementServiceSkeleton method processQuery.

/**
 * Query processes based on a {@link org.apache.ode.bpel.common.ProcessFilter} criteria. This is
 * implemented in memory rather than via database calls since the processes
 * are managed by the {@link org.apache.ode.bpel.iapi.ProcessStore} object and we don't want to make
 * this needlessly complicated.
 *
 * @param filter              process filter
 * @param tenantsProcessStore Current Tenant's process store
 * @return ProcessConf collection
 * @throws ProcessManagementException if an error occurred while processing query
 */
private Collection<ProcessConf> processQuery(ProcessFilter filter, TenantProcessStoreImpl tenantsProcessStore) throws ProcessManagementException {
    Map<QName, ProcessConfigurationImpl> processes = tenantsProcessStore.getProcessConfigMap();
    if (log.isDebugEnabled()) {
        for (Map.Entry<QName, ProcessConfigurationImpl> process : processes.entrySet()) {
            log.debug("Process " + process.getKey() + " in state " + process.getValue());
        }
    }
    Set<QName> pids = processes.keySet();
    // Name filter can be implemented using only the PIDs.
    if (filter != null && filter.getNameFilter() != null) {
        // adding escape sequences to [\^$.|?*+(){} characters
        String nameFilter = filter.getNameFilter().replace("\\", "\\\\").replace("]", "\\]").replace("[", "\\[").replace("^", "\\^").replace("$", "\\$").replace("|", "\\|").replace("?", "\\?").replace(".", "\\.").replace("+", "\\+").replace("(", "\\(").replace(")", "\\)").replace("{", "\\{").replace("}", "\\}").replace("*", ".*");
        final Pattern pattern = Pattern.compile(nameFilter + "(-\\d*)?");
        CollectionsX.remove_if(pids, new MemberOfFunction<QName>() {

            @Override
            public boolean isMember(QName o) {
                return !pattern.matcher(o.getLocalPart()).matches();
            }
        });
    }
    if (filter != null && filter.getNamespaceFilter() != null) {
        // adding escape sequences to [\^$.|?*+(){} characters
        String namespaceFilter = filter.getNamespaceFilter().replace("\\", "\\\\").replace("]", "\\]").replace("[", "\\[").replace("^", "\\^").replace("$", "\\$").replace("|", "\\|").replace("?", "\\?").replace(".", "\\.").replace("+", "\\+").replace("(", "\\(").replace(")", "\\)").replace("{", "\\{").replace("}", "\\}").replace("*", ".*");
        final Pattern pattern = Pattern.compile(namespaceFilter);
        CollectionsX.remove_if(pids, new MemberOfFunction<QName>() {

            @Override
            public boolean isMember(QName o) {
                String ns = o.getNamespaceURI() == null ? "" : o.getNamespaceURI();
                return !pattern.matcher(ns).matches();
            }
        });
    }
    // Now we need the process conf objects, we need to be
    // careful since someone could have deleted them by now
    List<ProcessConf> confs = new LinkedList<ProcessConf>();
    for (QName pid : pids) {
        ProcessConf pConf = tenantsProcessStore.getProcessConfiguration(pid);
        if (pConf != null) {
            confs.add(pConf);
        }
    }
    if (filter != null) {
        // Specific filter for deployment date.
        if (filter.getDeployedDateFilter() != null) {
            for (final String ddf : filter.getDeployedDateFilter()) {
                final Date dd;
                try {
                    dd = ISO8601DateParser.parse(Filter.getDateWithoutOp(ddf));
                } catch (ParseException e) {
                    // Should never happen.
                    String errMsg = "Exception while parsing date";
                    log.error(errMsg, e);
                    throw new ProcessManagementException(errMsg, e);
                }
                CollectionsX.remove_if(confs, new MemberOfFunction<ProcessConf>() {

                    @Override
                    public boolean isMember(ProcessConf o) {
                        if (ddf.startsWith("=")) {
                            return !o.getDeployDate().equals(dd);
                        }
                        if (ddf.startsWith("<=")) {
                            return o.getDeployDate().getTime() > dd.getTime();
                        }
                        if (ddf.startsWith(">=")) {
                            return o.getDeployDate().getTime() < dd.getTime();
                        }
                        if (ddf.startsWith("<")) {
                            return o.getDeployDate().getTime() >= dd.getTime();
                        }
                        return ddf.startsWith(">") && (o.getDeployDate().getTime() <= dd.getTime());
                    }
                });
            }
        }
        // Ordering
        if (filter.getOrders() != null) {
            ComparatorChain cChain = new ComparatorChain();
            for (String key : filter.getOrders()) {
                boolean ascending = true;
                String orderKey = key;
                if (key.startsWith("+") || key.startsWith("-")) {
                    orderKey = key.substring(1, key.length());
                    if (key.startsWith("-")) {
                        ascending = false;
                    }
                }
                Comparator c;
                if ("name".equals(orderKey)) {
                    c = new Comparator<ProcessConf>() {

                        public int compare(ProcessConf o1, ProcessConf o2) {
                            return o1.getProcessId().getLocalPart().compareTo(o2.getProcessId().getLocalPart());
                        }
                    };
                } else if ("namespace".equals(orderKey)) {
                    c = new Comparator<ProcessConf>() {

                        public int compare(ProcessConf o1, ProcessConf o2) {
                            String ns1 = o1.getProcessId().getNamespaceURI() == null ? "" : o1.getProcessId().getNamespaceURI();
                            String ns2 = o2.getProcessId().getNamespaceURI() == null ? "" : o2.getProcessId().getNamespaceURI();
                            return ns1.compareTo(ns2);
                        }
                    };
                } else if ("version".equals(orderKey)) {
                    c = new Comparator<ProcessConf>() {

                        public int compare(ProcessConf o1, ProcessConf o2) {
                            return (int) (o1.getVersion() - o2.getVersion());
                        }
                    };
                } else if ("deployed".equals(orderKey)) {
                    c = new Comparator<ProcessConf>() {

                        public int compare(ProcessConf o1, ProcessConf o2) {
                            return o1.getDeployDate().compareTo(o2.getDeployDate());
                        }
                    };
                } else if ("status".equals(orderKey)) {
                    c = new Comparator<ProcessConf>() {

                        public int compare(ProcessConf o1, ProcessConf o2) {
                            return o1.getState().compareTo(o2.getState());
                        }
                    };
                } else {
                    // unrecognized
                    if (log.isDebugEnabled()) {
                        log.debug("unrecognized order key" + orderKey);
                    }
                    continue;
                }
                cChain.addComparator(c, !ascending);
            }
            Collections.sort(confs, cChain);
        }
    }
    return confs;
}
Also used : Pattern(java.util.regex.Pattern) ComparatorChain(org.apache.commons.collections.comparators.ComparatorChain) QName(javax.xml.namespace.QName) ProcessConf(org.apache.ode.bpel.iapi.ProcessConf) ProcessConfigurationImpl(org.wso2.carbon.bpel.core.ode.integration.store.ProcessConfigurationImpl) LinkedList(java.util.LinkedList) Date(java.util.Date) ProcessManagementException(org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.ProcessManagementException) Comparator(java.util.Comparator) ParseException(java.text.ParseException) Map(java.util.Map)

Aggregations

ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 QName (javax.xml.namespace.QName)1 ComparatorChain (org.apache.commons.collections.comparators.ComparatorChain)1 ProcessConf (org.apache.ode.bpel.iapi.ProcessConf)1 ProcessConfigurationImpl (org.wso2.carbon.bpel.core.ode.integration.store.ProcessConfigurationImpl)1 ProcessManagementException (org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.ProcessManagementException)1 NameFilter (org.wso2.carbon.identity.api.server.authenticators.v1.model.NameFilter)1 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)1 IdentityProvider (org.wso2.carbon.identity.application.common.model.IdentityProvider)1 LocalAuthenticatorConfig (org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig)1 RequestPathAuthenticatorConfig (org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig)1 ExpressionNode (org.wso2.carbon.identity.core.model.ExpressionNode)1 IdentityProviderManagementException (org.wso2.carbon.idp.mgt.IdentityProviderManagementException)1 IdpSearchResult (org.wso2.carbon.idp.mgt.model.IdpSearchResult)1