Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* and/or LICENSE file distributed with this work for additional
* information regarding copyright ownership. The ASF licenses
* this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.ibatis.ognl;
import org.apache.ibatis.ognl.enhance.ExpressionAccessor;
import org.apache.ibatis.ognl.enhance.OgnlExpressionCompiler;
import org.apache.ibatis.ognl.security.OgnlSecurityManager;
import java.io.StringReader;
import java.lang.reflect.Member;
import java.lang.reflect.Modifier;
import java.util.Map;
/**
* This class provides static methods for parsing and interpreting OGNL expressions.
* The simplest use of the Ognl class is to get the value of an expression from an object, without
* extra context or pre-parsing.
*
* This will parse the expression given and evaluate it against the root object given, returning the
* result. If there is an error in the expression, such as the property is not found, the exception
* is encapsulated into an {@link OgnlException OgnlException}.
*
* Other more sophisticated uses of Ognl can pre-parse expressions. This provides two advantages: in
* the case of user-supplied expressions it allows you to catch parse errors before evaluation and
* it allows you to cache parsed expressions into an AST for better speed during repeated use. The
* pre-parsed expression is always returned as an Object to simplify use for programs
* that just wish to store the value for repeated use and do not care that it is an AST. If it does
* care it can always safely cast the value to an AST type.
*
* The Ognl class also takes a context map as one of the parameters to the set and get
* methods. This allows you to put your own variables into the available namespace for OGNL
* expressions. The default context contains only the #root and #context
* keys, which are required to be present. The addDefaultContext(Object, Map) method
* will alter an existing Map to put the defaults in. Here is an example that shows
* how to extract the documentName property out of the root object and append a
* string with the current user name in parens:
*
* private Map context = new HashMap(); public void setUserName(String value) {
* context.put("userName", value); } try { // get value using our own custom context map result =
* Ognl.getValue("documentName + \" (\" + ((#userName == null) ? \"<nobody>\" : #userName) +
* \")\"", context, root); } catch (OgnlException ex) { // Report error or recover }
*
*/
public abstract class Ognl {
private static volatile Integer expressionMaxLength = null;
private static volatile Boolean expressionMaxLengthFrozen = Boolean.FALSE;
/**
* Applies a maximum allowed length on OGNL expressions for security reasons.
*
* @param expressionMaxLength the OGNL expressions maximum allowed length. Use null (default) to disable this functionality.
* @throws SecurityException if the caller is inside OGNL expression itself.
* @throws IllegalStateException if the expression maximum allowed length is frozen.
* @throws IllegalArgumentException if the provided expressionMaxLength is < 0.
* @since 3.1.26
*/
public static synchronized void applyExpressionMaxLength(Integer expressionMaxLength) {
if (System.getSecurityManager() instanceof OgnlSecurityManager) {
throw new SecurityException("the OGNL expressions maximum allowed length is not accessible inside expression itself!");
}
if (expressionMaxLengthFrozen) {
throw new IllegalStateException("The OGNL expression maximum allowed length has been frozen and cannot be changed.");
}
if (expressionMaxLength != null && expressionMaxLength < 0) {
throw new IllegalArgumentException("The provided OGNL expression maximum allowed length, " + expressionMaxLength + ", is illegal.");
} else {
Ognl.expressionMaxLength = expressionMaxLength;
}
}
/**
* Freezes (prevents updates to) the maximum allowed length on OGNL expressions at the current value.
* This makes it clear to other OGNL callers that the value should not be changed.
*
* @throws SecurityException if the caller is inside OGNL expression itself.
* @since 3.1.26
*/
public static synchronized void freezeExpressionMaxLength() {
if (System.getSecurityManager() instanceof OgnlSecurityManager) {
throw new SecurityException("Freezing the OGNL expressions maximum allowed length is not accessible inside expression itself!");
}
Ognl.expressionMaxLengthFrozen = Boolean.TRUE;
}
/**
* Thaws (allows updates to) the maximum allowed length on OGNL expressions.
* This makes it clear to other OGNL callers that the value can (again) be changed.
*
* @throws SecurityException if the caller is inside OGNL expression itself.
* @since 3.1.26
*/
public static synchronized void thawExpressionMaxLength() {
if (System.getSecurityManager() instanceof OgnlSecurityManager) {
throw new SecurityException("Thawing the OGNL expressions maximum allowed length is not accessible inside expression itself!");
}
Ognl.expressionMaxLengthFrozen = Boolean.FALSE;
}
/**
* Parses the given OGNL expression and returns a tree representation of the expression that can
* be used by Ognl static methods.
*
* @param expression the OGNL expression to be parsed
* @return a tree representation of the expression
* @throws ExpressionSyntaxException if the expression is malformed
* @throws OgnlException if there is a pathological environmental problem
*/
public static Object parseExpression(String expression) throws OgnlException {
final Integer currentExpressionMaxLength = Ognl.expressionMaxLength; // Limit access to the volatile variable to a single operation
if (currentExpressionMaxLength != null && expression != null && expression.length() > currentExpressionMaxLength) {
throw new OgnlException("Parsing blocked due to security reasons!",
new SecurityException("This expression exceeded maximum allowed length: " + expression));
}
try {
assert expression != null;
OgnlParser parser = new OgnlParser(new StringReader(expression));
return parser.topLevelExpression();
} catch (ParseException | TokenMgrError e) {
throw new ExpressionSyntaxException(expression, e);
}
}
/**
* Parses and compiles the given expression using the {@link OgnlExpressionCompiler} returned
* from {@link OgnlRuntime#getCompiler()}.
*
* @param context The context to use.
* @param root The root object for the given expression.
* @param expression The expression to compile.
* @return The node with a compiled accessor set on {@link Node#getAccessor()} if compilation
* was successfull. In instances where compilation wasn't possible because of a partially null
* expression the {@link ExpressionAccessor} instance may be null and the compilation of this expression
* still possible at some as yet indertermined point in the future.
* @throws Exception If a compilation error occurs.
*/
public static Node compileExpression(OgnlContext context, Object root, String expression) throws Exception {
Node expr = (Node) Ognl.parseExpression(expression);
OgnlRuntime.compileExpression(context, expr, root);
return expr;
}
/**
* Creates and returns a new standard naming context for evaluating an OGNL expression.
*
* @param root the root of the object graph
* @return a new {@link OgnlContext} with the keys root and context set
* appropriately
*/
public static OgnlContext createDefaultContext(Object root) {
MemberAccess memberAccess = new AbstractMemberAccess() {
@Override
public boolean isAccessible(OgnlContext context, Object target, Member member, String propertyName) {
int modifiers = member.getModifiers();
return Modifier.isPublic(modifiers);
}
};
return addDefaultContext(root, memberAccess, null, null, null);
}
/**
* Creates and returns a new standard naming context for evaluating an OGNL expression.
*
* @param root the root of the object graph
* @return a new {@link OgnlContext} with the keys root and context set
* appropriately
*/
public static OgnlContext createDefaultContext(Object root, Map