Search in sources :

Example 6 with Evaluated

use of com.opensymphony.xwork2.util.Evaluated in project struts by apache.

the class HttpHeaderResult method execute.

/**
 * Sets the optional HTTP response status code and also re-sets HTTP headers after they've
 * been optionally evaluated against the ValueStack.
 *
 * @param invocation an encapsulation of the action execution state.
 * @throws Exception if an error occurs when re-setting the headers.
 */
public void execute(ActionInvocation invocation) throws Exception {
    if (invocation == null) {
        throw new IllegalArgumentException("Invocation cannot be null!");
    }
    HttpServletResponse response = invocation.getInvocationContext().getServletResponse();
    ValueStack stack = invocation.getStack();
    if (status != -1) {
        response.setStatus(status);
    }
    if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            String value = entry.getValue();
            String finalValue = parse ? TextParseUtil.translateVariables(value, stack) : value;
            response.addHeader(entry.getKey(), finalValue);
        }
    }
    if (status == -1 && error != null) {
        int errorCode = -1;
        try {
            errorCode = Integer.parseInt(parse ? TextParseUtil.translateVariables(error, stack) : error);
        } catch (Exception e) {
            LOG.error("Cannot parse errorCode [{}] value as Integer!", error, e);
        }
        if (errorCode != -1) {
            if (errorMessage != null) {
                String finalMessage = parse ? TextParseUtil.translateVariables(errorMessage, stack) : errorMessage;
                response.sendError(errorCode, finalMessage);
            } else {
                response.sendError(errorCode);
            }
        }
    }
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) HttpServletResponse(javax.servlet.http.HttpServletResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with Evaluated

use of com.opensymphony.xwork2.util.Evaluated in project struts by apache.

the class AliasInterceptor method intercept.

@Override
public String intercept(ActionInvocation invocation) throws Exception {
    ActionConfig config = invocation.getProxy().getConfig();
    ActionContext ac = invocation.getInvocationContext();
    Object action = invocation.getAction();
    // get the action's parameters
    final Map<String, String> parameters = config.getParams();
    if (parameters.containsKey(aliasesKey)) {
        String aliasExpression = parameters.get(aliasesKey);
        ValueStack stack = ac.getValueStack();
        Object obj = stack.findValue(aliasExpression);
        if (obj instanceof Map) {
            // get secure stack
            ValueStack newStack = valueStackFactory.createValueStack(stack);
            boolean clearableStack = newStack instanceof ClearableValueStack;
            if (clearableStack) {
                // if the stack's context can be cleared, do that to prevent OGNL
                // from having access to objects in the stack, see XW-641
                ((ClearableValueStack) newStack).clearContextValues();
                Map<String, Object> context = newStack.getContext();
                ReflectionContextState.setCreatingNullObjects(context, true);
                ReflectionContextState.setDenyMethodExecution(context, true);
                ReflectionContextState.setReportingConversionErrors(context, true);
                // keep locale from original context
                newStack.getActionContext().withLocale(stack.getActionContext().getLocale());
            }
            // override
            Map aliases = (Map) obj;
            for (Object o : aliases.entrySet()) {
                Map.Entry entry = (Map.Entry) o;
                String name = entry.getKey().toString();
                if (isNotAcceptableExpression(name)) {
                    continue;
                }
                String alias = (String) entry.getValue();
                if (isNotAcceptableExpression(alias)) {
                    continue;
                }
                Evaluated value = new Evaluated(stack.findValue(name));
                if (!value.isDefined()) {
                    // workaround
                    HttpParameters contextParameters = ActionContext.getContext().getParameters();
                    if (null != contextParameters) {
                        Parameter param = contextParameters.get(name);
                        if (param.isDefined()) {
                            value = new Evaluated(param.getValue());
                        }
                    }
                }
                if (value.isDefined()) {
                    try {
                        newStack.setValue(alias, value.get());
                    } catch (RuntimeException e) {
                        if (devMode) {
                            String developerNotification = localizedTextProvider.findText(ParametersInterceptor.class, "devmode.notification", ActionContext.getContext().getLocale(), "Developer Notification:\n{0}", new Object[] { "Unexpected Exception caught setting '" + entry.getKey() + "' on '" + action.getClass() + ": " + e.getMessage() });
                            LOG.error(developerNotification);
                            if (action instanceof ValidationAware) {
                                ((ValidationAware) action).addActionMessage(developerNotification);
                            }
                        }
                    }
                }
            }
            if (clearableStack) {
                stack.getActionContext().withConversionErrors(newStack.getActionContext().getConversionErrors());
            }
        } else {
            LOG.debug("invalid alias expression: {}", aliasesKey);
        }
    }
    return invocation.invoke();
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ClearableValueStack(com.opensymphony.xwork2.util.ClearableValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) HttpParameters(org.apache.struts2.dispatcher.HttpParameters) Evaluated(com.opensymphony.xwork2.util.Evaluated) ActionContext(com.opensymphony.xwork2.ActionContext) ClearableValueStack(com.opensymphony.xwork2.util.ClearableValueStack) Parameter(org.apache.struts2.dispatcher.Parameter) Map(java.util.Map)

Example 8 with Evaluated

use of com.opensymphony.xwork2.util.Evaluated in project struts by apache.

the class StrutsLocalizedTextProvider method findText.

/**
 * <p>
 * Finds a localized text message for the given key, aTextName. Both the key and the message
 * itself is evaluated as required.  The following algorithm is used to find the requested
 * message:
 * </p>
 *
 * <ol>
 * <li>If {@link #searchDefaultBundlesFirst} is <code>true</code>, look for the message in the default resource bundles first.</li>
 * <li>Look for message in aClass' class hierarchy.
 * <ol>
 * <li>Look for the message in a resource bundle for aClass</li>
 * <li>If not found, look for the message in a resource bundle for any implemented interface</li>
 * <li>If not found, traverse up the Class' hierarchy and repeat from the first sub-step</li>
 * </ol></li>
 * <li>If not found and aClass is a {@link ModelDriven} Action, then look for message in
 * the model's class hierarchy (repeat sub-steps listed above).</li>
 * <li>If not found, look for message in child property.  This is determined by evaluating
 * the message key as an OGNL expression.  For example, if the key is
 * <i>user.address.state</i>, then it will attempt to see if "user" can be resolved into an
 * object.  If so, repeat the entire process from the beginning with the object's class as
 * aClass and "address.state" as the message key.</li>
 * <li>If not found, look for the message in aClass' package hierarchy.</li>
 * <li>If still not found, look for the message in the default resource bundles
 * (Note: the lookup is not repeated again if {@link #searchDefaultBundlesFirst} was <code>true</code>).</li>
 * <li>Return defaultMessage</li>
 * </ol>
 *
 * <p>
 * When looking for the message, if the key indexes a collection (e.g. user.phone[0]) and a
 * message for that specific key cannot be found, the general form will also be looked up
 * (i.e. user.phone[*]).
 * </p>
 *
 * <p>
 * If a message is found, it will also be interpolated.  Anything within <code>${...}</code>
 * will be treated as an OGNL expression and evaluated as such.
 * </p>
 *
 * <p>
 * If a message is <b>not</b> found a DEBUG level log warning will be logged.
 * </p>
 *
 * @param aClass         the class whose name to use as the start point for the search
 * @param aTextName      the key to find the text message for
 * @param locale         the locale the message should be for
 * @param defaultMessage the message to be returned if no text message can be found in any
 *                       resource bundle
 * @param args           arguments
 * @param valueStack     the value stack to use to evaluate expressions instead of the
 *                       one in the ActionContext ThreadLocal
 * @return the localized text, or null if none can be found and no defaultMessage is provided
 */
@Override
public String findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args, ValueStack valueStack) {
    String indexedTextName = null;
    if (aTextName == null) {
        LOG.warn("Trying to find text with null key!");
        aTextName = "";
    }
    // calculate indexedTextName (collection[*]) if applicable
    if (aTextName.contains("[")) {
        int i = -1;
        indexedTextName = aTextName;
        while ((i = indexedTextName.indexOf('[', i + 1)) != -1) {
            int j = indexedTextName.indexOf(']', i);
            String a = indexedTextName.substring(0, i);
            String b = indexedTextName.substring(j);
            indexedTextName = a + "[*" + b;
        }
    }
    // Allow for and track an early lookup for the message in the default resource bundles first, before searching the class hierarchy.
    // The early lookup is only performed when the text provider has been configured to do so, otherwise follow the standard processing order.
    boolean performedInitialDefaultBundlesMessageLookup = false;
    GetDefaultMessageReturnArg result = null;
    // If search default bundles first is set true, call alternative logic first.
    if (searchDefaultBundlesFirst) {
        result = getDefaultMessageWithAlternateKey(aTextName, indexedTextName, locale, valueStack, args, defaultMessage);
        performedInitialDefaultBundlesMessageLookup = true;
        if (!unableToFindTextForKey(result)) {
            // Found a message in the default resource bundles for aTextName or indexedTextName.
            return result.message;
        }
    }
    // search up class hierarchy
    String msg = findMessage(aClass, aTextName, indexedTextName, locale, args, null, valueStack);
    if (msg != null) {
        return msg;
    }
    if (ModelDriven.class.isAssignableFrom(aClass)) {
        ActionContext context = ActionContext.getContext();
        // search up model's class hierarchy
        ActionInvocation actionInvocation = context.getActionInvocation();
        // ActionInvocation may be null if we're being run from a Sitemesh filter, so we won't get model texts if this is null
        if (actionInvocation != null) {
            Object action = actionInvocation.getAction();
            if (action instanceof ModelDriven) {
                Object model = ((ModelDriven) action).getModel();
                if (model != null) {
                    msg = findMessage(model.getClass(), aTextName, indexedTextName, locale, args, null, valueStack);
                    if (msg != null) {
                        return msg;
                    }
                }
            }
        }
    }
    // nothing still? alright, search the package hierarchy now
    for (Class clazz = aClass; (clazz != null) && !clazz.equals(Object.class); clazz = clazz.getSuperclass()) {
        String basePackageName = clazz.getName();
        while (basePackageName.lastIndexOf('.') != -1) {
            basePackageName = basePackageName.substring(0, basePackageName.lastIndexOf('.'));
            String packageName = basePackageName + ".package";
            msg = getMessage(packageName, locale, aTextName, valueStack, args);
            if (msg != null) {
                return msg;
            }
            if (indexedTextName != null) {
                msg = getMessage(packageName, locale, indexedTextName, valueStack, args);
                if (msg != null) {
                    return msg;
                }
            }
        }
    }
    // see if it's a child property
    int idx = aTextName.indexOf('.');
    if (idx != -1) {
        String newKey = null;
        String prop = null;
        if (aTextName.startsWith(XWorkConverter.CONVERSION_ERROR_PROPERTY_PREFIX)) {
            idx = aTextName.indexOf('.', XWorkConverter.CONVERSION_ERROR_PROPERTY_PREFIX.length());
            if (idx != -1) {
                prop = aTextName.substring(XWorkConverter.CONVERSION_ERROR_PROPERTY_PREFIX.length(), idx);
                newKey = XWorkConverter.CONVERSION_ERROR_PROPERTY_PREFIX + aTextName.substring(idx + 1);
            }
        } else {
            prop = aTextName.substring(0, idx);
            newKey = aTextName.substring(idx + 1);
        }
        if (prop != null) {
            Object obj = valueStack.findValue(prop);
            try {
                Object actionObj = ReflectionProviderFactory.getInstance().getRealTarget(prop, valueStack.getContext(), valueStack.getRoot());
                if (actionObj != null) {
                    PropertyDescriptor propertyDescriptor = ReflectionProviderFactory.getInstance().getPropertyDescriptor(actionObj.getClass(), prop);
                    if (propertyDescriptor != null) {
                        Class clazz = propertyDescriptor.getPropertyType();
                        if (clazz != null) {
                            if (obj != null) {
                                valueStack.push(obj);
                            }
                            msg = findText(clazz, newKey, locale, null, args);
                            if (obj != null) {
                                valueStack.pop();
                            }
                            if (msg != null) {
                                return msg;
                            }
                        }
                    }
                }
            } catch (Exception e) {
                LOG.debug("unable to find property {}", prop, e);
            }
        }
    }
    // so we check first to avoid repeating the same operation twice.
    if (!performedInitialDefaultBundlesMessageLookup) {
        result = getDefaultMessageWithAlternateKey(aTextName, indexedTextName, locale, valueStack, args, defaultMessage);
    }
    // could we find the text, if not log a warn
    if (unableToFindTextForKey(result) && LOG.isDebugEnabled()) {
        String warn = "Unable to find text for key '" + aTextName + "' ";
        if (indexedTextName != null) {
            warn += " or indexed key '" + indexedTextName + "' ";
        }
        warn += "in class '" + aClass.getName() + "' and locale '" + locale + "'";
        LOG.debug(warn);
    }
    return result != null ? result.message : null;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ModelDriven(com.opensymphony.xwork2.ModelDriven) ActionContext(com.opensymphony.xwork2.ActionContext)

Aggregations

ValueStack (com.opensymphony.xwork2.util.ValueStack)3 Map (java.util.Map)3 ActionContext (com.opensymphony.xwork2.ActionContext)2 TextProvider (com.opensymphony.xwork2.TextProvider)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 BooleanAttribute (com.agiletec.aps.system.common.entity.model.attribute.BooleanAttribute)1 Content (com.agiletec.plugins.jacms.aps.system.services.content.model.Content)1 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)1 ActionSupport (com.opensymphony.xwork2.ActionSupport)1 ModelDriven (com.opensymphony.xwork2.ModelDriven)1 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)1 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)1 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)1 ClearableValueStack (com.opensymphony.xwork2.util.ClearableValueStack)1 Evaluated (com.opensymphony.xwork2.util.Evaluated)1 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)1 PropertyDescriptor (java.beans.PropertyDescriptor)1