Search in sources :

Example 11 with ParseException

use of org.springframework.expression.ParseException in project engine by craftercms.

the class ConfigAwareUrlAccessRestrictionCheckingProcessor method getUrlRestrictions.

@Override
@SuppressWarnings("unchecked")
protected Map<String, Expression> getUrlRestrictions() {
    Callback<Map<String, Expression>> callback = new Callback<Map<String, Expression>>() {

        @Override
        public Map<String, Expression> execute() {
            HierarchicalConfiguration config = ConfigUtils.getCurrentConfig();
            Map<String, Expression> customRestrictions = null;
            if (config != null) {
                List<HierarchicalConfiguration> restrictionsConfig = config.configurationsAt(URL_RESTRICTION_KEY);
                if (CollectionUtils.isNotEmpty(restrictionsConfig)) {
                    customRestrictions = new LinkedHashMap<>(restrictionsConfig.size());
                    ExpressionParser parser = new SpelExpressionParser();
                    for (HierarchicalConfiguration restrictionConfig : restrictionsConfig) {
                        String url = restrictionConfig.getString(URL_RESTRICTION_URL_KEY);
                        String expression = restrictionConfig.getString(URL_RESTRICTION_EXPRESSION_KEY);
                        if (StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(expression)) {
                            try {
                                customRestrictions.put(url, parser.parseExpression(expression));
                            } catch (ParseException e) {
                                throw new ConfigurationException(expression + " is not a valid SpEL expression", e);
                            }
                        }
                    }
                }
            }
            if (customRestrictions != null) {
                return customRestrictions;
            } else {
                return urlRestrictions;
            }
        }
    };
    SiteContext siteContext = SiteContext.getCurrent();
    if (siteContext != null) {
        return cacheTemplate.getObject(siteContext.getContext(), callback, URL_RESTRICTIONS_CACHE_KEY);
    } else {
        return urlRestrictions;
    }
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) Callback(org.craftercms.commons.lang.Callback) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ConfigurationException(org.craftercms.engine.exception.ConfigurationException) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ParseException(org.springframework.expression.ParseException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 12 with ParseException

use of org.springframework.expression.ParseException in project spring-security by spring-projects.

the class ExpressionBasedAnnotationAttributeFactory method createPostInvocationAttribute.

public PostInvocationAttribute createPostInvocationAttribute(String postFilterAttribute, String postAuthorizeAttribute) {
    try {
        ExpressionParser parser = getParser();
        Expression postAuthorizeExpression = postAuthorizeAttribute == null ? null : parser.parseExpression(postAuthorizeAttribute);
        Expression postFilterExpression = postFilterAttribute == null ? null : parser.parseExpression(postFilterAttribute);
        if (postFilterExpression != null || postAuthorizeExpression != null) {
            return new PostInvocationExpressionAttribute(postFilterExpression, postAuthorizeExpression);
        }
    } catch (ParseException e) {
        throw new IllegalArgumentException("Failed to parse expression '" + e.getExpressionString() + "'", e);
    }
    return null;
}
Also used : Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) ParseException(org.springframework.expression.ParseException)

Example 13 with ParseException

use of org.springframework.expression.ParseException in project spring-security by spring-projects.

the class ExpressionBasedFilterInvocationSecurityMetadataSource method processMap.

private static LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> processMap(LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> requestMap, ExpressionParser parser) {
    Assert.notNull(parser, "SecurityExpressionHandler returned a null parser object");
    LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> requestToExpressionAttributesMap = new LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>>(requestMap);
    for (Map.Entry<RequestMatcher, Collection<ConfigAttribute>> entry : requestMap.entrySet()) {
        RequestMatcher request = entry.getKey();
        Assert.isTrue(entry.getValue().size() == 1, "Expected a single expression attribute for " + request);
        ArrayList<ConfigAttribute> attributes = new ArrayList<ConfigAttribute>(1);
        String expression = entry.getValue().toArray(new ConfigAttribute[1])[0].getAttribute();
        logger.debug("Adding web access control expression '" + expression + "', for " + request);
        AbstractVariableEvaluationContextPostProcessor postProcessor = createPostProcessor(request);
        try {
            attributes.add(new WebExpressionConfigAttribute(parser.parseExpression(expression), postProcessor));
        } catch (ParseException e) {
            throw new IllegalArgumentException("Failed to parse expression '" + expression + "'");
        }
        requestToExpressionAttributesMap.put(request, attributes);
    }
    return requestToExpressionAttributesMap;
}
Also used : RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) ConfigAttribute(org.springframework.security.access.ConfigAttribute) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Collection(java.util.Collection) ParseException(org.springframework.expression.ParseException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 14 with ParseException

use of org.springframework.expression.ParseException in project spring-security by spring-projects.

the class AbstractAuthorizeTag method authorizeUsingAccessExpression.

/**
	 * Make an authorization decision based on a Spring EL expression. See the
	 * "Expression-Based Access Control" chapter in Spring Security for details on what
	 * expressions can be used.
	 *
	 * @return the result of the authorization decision
	 * @throws IOException
	 */
public boolean authorizeUsingAccessExpression() throws IOException {
    if (SecurityContextHolder.getContext().getAuthentication() == null) {
        return false;
    }
    SecurityExpressionHandler<FilterInvocation> handler = getExpressionHandler();
    Expression accessExpression;
    try {
        accessExpression = handler.getExpressionParser().parseExpression(getAccess());
    } catch (ParseException e) {
        IOException ioException = new IOException();
        ioException.initCause(e);
        throw ioException;
    }
    return ExpressionUtils.evaluateAsBoolean(accessExpression, createExpressionEvaluationContext(handler));
}
Also used : Expression(org.springframework.expression.Expression) FilterInvocation(org.springframework.security.web.FilterInvocation) ParseException(org.springframework.expression.ParseException) IOException(java.io.IOException)

Example 15 with ParseException

use of org.springframework.expression.ParseException in project scheduling by ow2-proactive.

the class SPELParserValidator method createValidator.

@Override
protected Validator<String> createValidator(String model, Converter<String> converter) throws ModelSyntaxException {
    String spelRegexp = "^" + SPEL_TYPE_REGEXP + LEFT_DELIMITER_REGEXP + "(.+)" + RIGHT_DELIMITER_REGEXP + "$";
    String spelString = parseAndGetOneGroup(model, spelRegexp);
    try {
        return new SpelValidator(spelString);
    } catch (ParseException e) {
        throw new ModelSyntaxException(e.getMessage(), e);
    }
}
Also used : ModelSyntaxException(org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException) SpelValidator(org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.SpelValidator) ParseException(org.springframework.expression.ParseException)

Aggregations

ParseException (org.springframework.expression.ParseException)15 Expression (org.springframework.expression.Expression)11 EvaluationException (org.springframework.expression.EvaluationException)5 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)5 Test (org.junit.Test)4 ExpressionParser (org.springframework.expression.ExpressionParser)3 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)1 Callback (org.craftercms.commons.lang.Callback)1 ConfigurationException (org.craftercms.engine.exception.ConfigurationException)1 SiteContext (org.craftercms.engine.service.context.SiteContext)1 ModelSyntaxException (org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException)1 SpelValidator (org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.SpelValidator)1 CompositeStringExpression (org.springframework.expression.common.CompositeStringExpression)1 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)1