diff --git a/core/src/main/java/com/opensymphony/xwork2/ActionSupport.java b/core/src/main/java/com/opensymphony/xwork2/ActionSupport.java index ab1a18099a..be9cc29cad 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ActionSupport.java +++ b/core/src/main/java/com/opensymphony/xwork2/ActionSupport.java @@ -18,342 +18,11 @@ */ package com.opensymphony.xwork2; -import com.opensymphony.xwork2.conversion.impl.ConversionData; -import com.opensymphony.xwork2.inject.Container; -import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.interceptor.ValidationAware; -import com.opensymphony.xwork2.util.ValueStack; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.struts2.StrutsConstants; - -import java.io.Serializable; -import java.util.*; /** - * Provides a default implementation for the most common actions. - * See the documentation for all the interfaces this class implements for more detailed information. + * @deprecated since 6.7.0, use {@link org.apache.struts2.ActionSupport} instead. */ -public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable { - - private static final Logger LOG = LogManager.getLogger(ActionSupport.class); - - private final ValidationAwareSupport validationAware = new ValidationAwareSupport(); - - private transient TextProvider textProvider; - private transient LocaleProvider localeProvider; - - protected Container container; - - @Override - public void setActionErrors(Collection errorMessages) { - validationAware.setActionErrors(errorMessages); - } - - @Override - public Collection getActionErrors() { - return validationAware.getActionErrors(); - } - - @Override - public void setActionMessages(Collection messages) { - validationAware.setActionMessages(messages); - } - - @Override - public Collection getActionMessages() { - return validationAware.getActionMessages(); - } - - @Override - public void setFieldErrors(Map> errorMap) { - validationAware.setFieldErrors(errorMap); - } - - @Override - public Map> getFieldErrors() { - return validationAware.getFieldErrors(); - } - - @Override - public Locale getLocale() { - return getLocaleProvider().getLocale(); - } - - @Override - public boolean isValidLocaleString(String localeStr) { - return getLocaleProvider().isValidLocaleString(localeStr); - } - - @Override - public boolean isValidLocale(Locale locale) { - return getLocaleProvider().isValidLocale(locale); - } - - @Override - public Locale toLocale(String localeStr) { - return getLocaleProvider().toLocale(localeStr); - } - - @Override - public boolean hasKey(String key) { - return getTextProvider().hasKey(key); - } - - @Override - public String getText(String aTextName) { - return getTextProvider().getText(aTextName); - } - - @Override - public String getText(String aTextName, String defaultValue) { - return getTextProvider().getText(aTextName, defaultValue); - } - - @Override - public String getText(String aTextName, String defaultValue, String obj) { - return getTextProvider().getText(aTextName, defaultValue, obj); - } - - @Override - public String getText(String aTextName, List args) { - return getTextProvider().getText(aTextName, args); - } - - @Override - public String getText(String key, String[] args) { - return getTextProvider().getText(key, args); - } - - @Override - public String getText(String aTextName, String defaultValue, List args) { - return getTextProvider().getText(aTextName, defaultValue, args); - } - - @Override - public String getText(String key, String defaultValue, String[] args) { - return getTextProvider().getText(key, defaultValue, args); - } - - @Override - public String getText(String key, String defaultValue, List args, ValueStack stack) { - return getTextProvider().getText(key, defaultValue, args, stack); - } - - @Override - public String getText(String key, String defaultValue, String[] args, ValueStack stack) { - return getTextProvider().getText(key, defaultValue, args, stack); - } - - /** - * Dedicated method to support I10N and conversion errors - * - * @param key message which contains formatting string - * @param expr that should be formatted - * @return formatted expr with format specified by key - */ - public String getFormatted(String key, String expr) { - Map conversionErrors = ActionContext.getContext().getConversionErrors(); - if (conversionErrors.containsKey(expr)) { - String[] vals = (String[]) conversionErrors.get(expr).getValue(); - return vals[0]; - } else { - final ValueStack valueStack = ActionContext.getContext().getValueStack(); - final Object val = valueStack.findValue(expr); - return getText(key, Arrays.asList(val)); - } - } - - @Override - public ResourceBundle getTexts() { - return getTextProvider().getTexts(); - } - - @Override - public ResourceBundle getTexts(String aBundleName) { - return getTextProvider().getTexts(aBundleName); - } - - @Override - public void addActionError(String anErrorMessage) { - validationAware.addActionError(anErrorMessage); - } - - @Override - public void addActionMessage(String aMessage) { - validationAware.addActionMessage(aMessage); - } - - @Override - public void addFieldError(String fieldName, String errorMessage) { - validationAware.addFieldError(fieldName, errorMessage); - } - - public String input() throws Exception { - return INPUT; - } - - /** - * A default implementation that does nothing an returns "success". - * - *

- * Subclasses should override this method to provide their business logic. - *

- * - *

- * See also {@link com.opensymphony.xwork2.Action#execute()}. - *

- * - * @return returns {@link #SUCCESS} - * @throws Exception can be thrown by subclasses. - */ - @Override - public String execute() throws Exception { - return SUCCESS; - } - - @Override - public boolean hasActionErrors() { - return validationAware.hasActionErrors(); - } - - @Override - public boolean hasActionMessages() { - return validationAware.hasActionMessages(); - } - - @Override - public boolean hasErrors() { - return validationAware.hasErrors(); - } - - @Override - public boolean hasFieldErrors() { - return validationAware.hasFieldErrors(); - } - - /** - * Clears field errors. Useful for Continuations and other situations - * where you might want to clear parts of the state on the same action. - */ - public void clearFieldErrors() { - validationAware.clearFieldErrors(); - } - - /** - * Clears action errors. Useful for Continuations and other situations - * where you might want to clear parts of the state on the same action. - */ - public void clearActionErrors() { - validationAware.clearActionErrors(); - } - - /** - * Clears messages. Useful for Continuations and other situations - * where you might want to clear parts of the state on the same action. - */ - public void clearMessages() { - validationAware.clearMessages(); - } - - /** - * Clears all errors. Useful for Continuations and other situations - * where you might want to clear parts of the state on the same action. - */ - public void clearErrors() { - validationAware.clearErrors(); - } - - /** - * Clears all errors and messages. Useful for Continuations and other situations - * where you might want to clear parts of the state on the same action. - */ - public void clearErrorsAndMessages() { - validationAware.clearErrorsAndMessages(); - } - - /** - * A default implementation that validates nothing. - * Subclasses should override this method to provide validations. - */ - @Override - public void validate() { - // A default implementation that validates nothing - } - - @Override - public Object clone() throws CloneNotSupportedException { - return super.clone(); - } - - /** - * - * Stops the action invocation immediately (by throwing a PauseException) and causes the action invocation to return - * the specified result, such as {@link #SUCCESS}, {@link #INPUT}, etc. - * - *

- * The next time this action is invoked (and using the same continuation ID), the method will resume immediately - * after where this method was called, with the entire call stack in the execute method restored. - *

- * - *

- * Note: this method can only be called within the {@link #execute()} method. - *

- * - * - * - * @param result the result to return - the same type of return value in the {@link #execute()} method. - */ - public void pause(String result) { - } - - /** - * If called first time it will create {@link com.opensymphony.xwork2.TextProviderFactory}, - * inject dependency (if {@link com.opensymphony.xwork2.inject.Container} is accesible) into in, - * then will create new {@link com.opensymphony.xwork2.TextProvider} and store it in a field - * for future references and at the returns reference to that field - * - * @return reference to field with TextProvider - */ - protected TextProvider getTextProvider() { - if (textProvider == null) { - final TextProviderFactory tpf = getContainer().getInstance(TextProviderFactory.class); - textProvider = tpf.createInstance(getClass()); - } - return textProvider; - } - - protected LocaleProvider getLocaleProvider() { - if (localeProvider == null) { - final LocaleProviderFactory localeProviderFactory = getContainer().getInstance(LocaleProviderFactory.class); - localeProvider = localeProviderFactory.createLocaleProvider(); - } - return localeProvider; - } - - /** - * TODO: This a temporary solution, maybe we should consider stop injecting container into beans - */ - protected Container getContainer() { - if (container == null) { - container = ActionContext.getContext().getContainer(); - if (container != null) { - boolean devMode = Boolean.parseBoolean(container.getInstance(String.class, StrutsConstants.STRUTS_DEVMODE)); - if (devMode) { - LOG.warn("Container is null, action was created manually? Fallback to ActionContext"); - } else { - LOG.debug("Container is null, action was created manually? Fallback to ActionContext"); - } - } else { - LOG.warn("Container is null, action was created out of ActionContext scope?!?"); - } - } - return container; - } - - @Inject - public void setContainer(Container container) { - this.container = container; - } - +@Deprecated +public class ActionSupport extends org.apache.struts2.ActionSupport implements Action, Validateable, ValidationAware { } diff --git a/core/src/main/java/com/opensymphony/xwork2/config/entities/InterceptorMapping.java b/core/src/main/java/com/opensymphony/xwork2/config/entities/InterceptorMapping.java index 260ae325b1..6625bc7a13 100644 --- a/core/src/main/java/com/opensymphony/xwork2/config/entities/InterceptorMapping.java +++ b/core/src/main/java/com/opensymphony/xwork2/config/entities/InterceptorMapping.java @@ -36,8 +36,16 @@ public class InterceptorMapping implements Serializable { private Interceptor interceptor; private final Map params; + public InterceptorMapping(String name, org.apache.struts2.interceptor.Interceptor interceptor) { + this(name, Interceptor.adapt(interceptor)); + } + + public InterceptorMapping(String name, org.apache.struts2.interceptor.Interceptor interceptor, Map params) { + this(name, Interceptor.adapt(interceptor), params); + } + public InterceptorMapping(String name, Interceptor interceptor) { - this(name, interceptor, new HashMap()); + this(name, interceptor, new HashMap<>()); } public InterceptorMapping(String name, Interceptor interceptor, Map params) { diff --git a/core/src/main/java/com/opensymphony/xwork2/interceptor/AbstractInterceptor.java b/core/src/main/java/com/opensymphony/xwork2/interceptor/AbstractInterceptor.java index 21e459c291..69c6674621 100644 --- a/core/src/main/java/com/opensymphony/xwork2/interceptor/AbstractInterceptor.java +++ b/core/src/main/java/com/opensymphony/xwork2/interceptor/AbstractInterceptor.java @@ -21,41 +21,23 @@ import com.opensymphony.xwork2.ActionInvocation; /** - * Provides default implementations of optional lifecycle methods + * @deprecated since 6.7.0, use {@link org.apache.struts2.interceptor.AbstractInterceptor} instead. */ -public abstract class AbstractInterceptor implements ConditionalInterceptor { - - private boolean disabled; - - /** - * Does nothing - */ - public void init() { - } - - /** - * Does nothing - */ - public void destroy() { - } +@Deprecated +public abstract class AbstractInterceptor extends org.apache.struts2.interceptor.AbstractInterceptor implements ConditionalInterceptor { /** * Override to handle interception */ public abstract String intercept(ActionInvocation invocation) throws Exception; - /** - * Allows to skip executing a given interceptor, just define {@code true} - * or use other way to override interceptor's parameters, see - * docs. - * @param disable if set to true, execution of a given interceptor will be skipped. - */ - public void setDisabled(String disable) { - this.disabled = Boolean.parseBoolean(disable); + @Override + public String intercept(org.apache.struts2.ActionInvocation invocation) throws Exception { + return intercept(ActionInvocation.adapt(invocation)); } @Override public boolean shouldIntercept(ActionInvocation invocation) { - return !this.disabled; + return shouldIntercept((org.apache.struts2.ActionInvocation) invocation); } } diff --git a/core/src/main/java/com/opensymphony/xwork2/interceptor/MethodFilterInterceptor.java b/core/src/main/java/com/opensymphony/xwork2/interceptor/MethodFilterInterceptor.java index e96951cfa1..bcce3da12d 100644 --- a/core/src/main/java/com/opensymphony/xwork2/interceptor/MethodFilterInterceptor.java +++ b/core/src/main/java/com/opensymphony/xwork2/interceptor/MethodFilterInterceptor.java @@ -31,56 +31,59 @@ * *

* MethodFilterInterceptor is an abstract Interceptor used as - * a base class for interceptors that will filter execution based on method + * a base class for interceptors that will filter execution based on method * names according to specified included/excluded method lists. - * + * *

- * + * * Settable parameters are as follows: - * + * *
    *
  • excludeMethods - method names to be excluded from interceptor processing
  • *
  • includeMethods - method names to be included in interceptor processing
  • *
- * + * *

- * - * NOTE: If method name are available in both includeMethods and - * excludeMethods, it will be considered as an included method: + * + * NOTE: If method name are available in both includeMethods and + * excludeMethods, it will be considered as an included method: * includeMethods takes precedence over excludeMethods. - * + * *

- * + * * Interceptors that extends this capability include: - * + * *
    *
  • TokenInterceptor
  • *
  • TokenSessionStoreInterceptor
  • *
  • DefaultWorkflowInterceptor
  • *
  • ValidationInterceptor
  • *
- * + * * - * + * * @author Alexandru Popescu * @author Rainer Hermanns - * + * * @see org.apache.struts2.interceptor.TokenInterceptor * @see org.apache.struts2.interceptor.TokenSessionStoreInterceptor * @see com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor * @see com.opensymphony.xwork2.validator.ValidationInterceptor + * + * @deprecated since 6.7.0, use {@link org.apache.struts2.interceptor.MethodFilterInterceptor} instead. */ +@Deprecated public abstract class MethodFilterInterceptor extends AbstractInterceptor { private static final Logger LOG = LogManager.getLogger(MethodFilterInterceptor.class); - + protected Set excludeMethods = Collections.emptySet(); protected Set includeMethods = Collections.emptySet(); public void setExcludeMethods(String excludeMethods) { this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods); } - + public Set getExcludeMethodsSet() { return excludeMethods; } @@ -88,7 +91,7 @@ public Set getExcludeMethodsSet() { public void setIncludeMethods(String includeMethods) { this.includeMethods = TextParseUtil.commaDelimitedStringToSet(includeMethods); } - + public Set getIncludeMethodsSet() { return includeMethods; } @@ -97,7 +100,7 @@ public Set getIncludeMethodsSet() { public String intercept(ActionInvocation invocation) throws Exception { if (applyInterceptor(invocation)) { return doIntercept(invocation); - } + } return invocation.invoke(); } @@ -110,14 +113,14 @@ protected boolean applyInterceptor(ActionInvocation invocation) { } return applyMethod; } - + /** * Subclasses must override to implement the interceptor logic. - * + * * @param invocation the action invocation * @return the result of invocation * @throws Exception in case of any errors */ protected abstract String doIntercept(ActionInvocation invocation) throws Exception; - + } diff --git a/core/src/main/java/com/opensymphony/xwork2/interceptor/MethodFilterInterceptorUtil.java b/core/src/main/java/com/opensymphony/xwork2/interceptor/MethodFilterInterceptorUtil.java index beacb87844..7e1a2c4348 100644 --- a/core/src/main/java/com/opensymphony/xwork2/interceptor/MethodFilterInterceptorUtil.java +++ b/core/src/main/java/com/opensymphony/xwork2/interceptor/MethodFilterInterceptorUtil.java @@ -18,131 +18,9 @@ */ package com.opensymphony.xwork2.interceptor; -import com.opensymphony.xwork2.util.TextParseUtil; -import com.opensymphony.xwork2.util.WildcardHelper; - -import java.util.HashMap; -import java.util.Set; - /** - * Utility class contains common methods used by - * {@link com.opensymphony.xwork2.interceptor.MethodFilterInterceptor}. - * - * @author tm_jee + * @deprecated since 6.7.0, use {@link org.apache.struts2.interceptor.MethodFilterInterceptorUtil} instead. */ -public class MethodFilterInterceptorUtil { - - /** - * Static method to decide if the specified method should be - * apply (not filtered) depending on the set of excludeMethods and - * includeMethods. - * - *
    - *
  • - * includeMethods takes precedence over excludeMethods - *
  • - *
- * Note: Supports wildcard listings in includeMethods/excludeMethods - * - * @param excludeMethods list of methods to exclude. - * @param includeMethods list of methods to include. - * @param method the specified method to check - * @return true if the method should be applied. - */ - public static boolean applyMethod(Set excludeMethods, Set includeMethods, String method) { - - // quick check to see if any actual pattern matching is needed - boolean needsPatternMatch = false; - for (String includeMethod : includeMethods) { - if (!"*".equals(includeMethod) && includeMethod.contains("*")) { - needsPatternMatch = true; - break; - } - } - - for (String excludeMethod : excludeMethods) { - if (!"*".equals(excludeMethod) && excludeMethod.contains("*")) { - needsPatternMatch = true; - break; - } - } - - // this section will try to honor the original logic, while - // still allowing for wildcards later - if (!needsPatternMatch && (includeMethods.contains("*") || includeMethods.size() == 0) ) { - if (excludeMethods != null - && excludeMethods.contains(method) - && !includeMethods.contains(method) ) { - return false; - } - } - - // test the methods using pattern matching - WildcardHelper wildcard = new WildcardHelper(); - String methodCopy ; - if (method == null ) { // no method specified - methodCopy = ""; - } - else { - methodCopy = new String(method); - } - for (String pattern : includeMethods) { - if (pattern.contains("*")) { - int[] compiledPattern = wildcard.compilePattern(pattern); - HashMap matchedPatterns = new HashMap<>(); - boolean matches = wildcard.match(matchedPatterns, methodCopy, compiledPattern); - if (matches) { - return true; // run it, includeMethods takes precedence - } - } - else { - if (pattern.equals(methodCopy)) { - return true; // run it, includeMethods takes precedence - } - } - } - if (excludeMethods.contains("*") ) { - return false; - } - - // CHECK ME: Previous implementation used include method - for ( String pattern : excludeMethods) { - if (pattern.contains("*")) { - int[] compiledPattern = wildcard.compilePattern(pattern); - HashMap matchedPatterns = new HashMap<>(); - boolean matches = wildcard.match(matchedPatterns, methodCopy, compiledPattern); - if (matches) { - // if found, and wasn't included earlier, don't run it - return false; - } - } - else { - if (pattern.equals(methodCopy)) { - // if found, and wasn't included earlier, don't run it - return false; - } - } - } - - - // default fall-back from before changes - return includeMethods.size() == 0 || includeMethods.contains(method) || includeMethods.contains("*"); - } - - /** - * Same as {@link #applyMethod(Set, Set, String)}, except that excludeMethods - * and includeMethods are supplied as comma separated string. - * - * @param excludeMethods comma seperated string of methods to exclude. - * @param includeMethods comma seperated string of methods to include. - * @param method the specified method to check - * @return true if the method should be applied. - */ - public static boolean applyMethod(String excludeMethods, String includeMethods, String method) { - Set includeMethodsSet = TextParseUtil.commaDelimitedStringToSet(includeMethods == null? "" : includeMethods); - Set excludeMethodsSet = TextParseUtil.commaDelimitedStringToSet(excludeMethods == null? "" : excludeMethods); - - return applyMethod(excludeMethodsSet, includeMethodsSet, method); - } - +@Deprecated +public class MethodFilterInterceptorUtil extends org.apache.struts2.interceptor.MethodFilterInterceptorUtil { } diff --git a/core/src/main/java/org/apache/struts2/ActionSupport.java b/core/src/main/java/org/apache/struts2/ActionSupport.java new file mode 100644 index 0000000000..04c513f3a2 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/ActionSupport.java @@ -0,0 +1,368 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE 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.struts2; + +import com.opensymphony.xwork2.LocaleProvider; +import com.opensymphony.xwork2.LocaleProviderFactory; +import com.opensymphony.xwork2.TextProvider; +import com.opensymphony.xwork2.TextProviderFactory; +import com.opensymphony.xwork2.ValidationAwareSupport; +import com.opensymphony.xwork2.conversion.impl.ConversionData; +import com.opensymphony.xwork2.inject.Container; +import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.ValueStack; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.struts2.interceptor.ValidationAware; + +import java.io.Serializable; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.ResourceBundle; + +/** + * Provides a default implementation for the most common actions. + * See the documentation for all the interfaces this class implements for more detailed information. + */ +public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable { + + private static final Logger LOG = LogManager.getLogger(ActionSupport.class); + + private final ValidationAwareSupport validationAware = new ValidationAwareSupport(); + + private transient TextProvider textProvider; + private transient LocaleProvider localeProvider; + + protected Container container; + + @Override + public void setActionErrors(Collection errorMessages) { + validationAware.setActionErrors(errorMessages); + } + + @Override + public Collection getActionErrors() { + return validationAware.getActionErrors(); + } + + @Override + public void setActionMessages(Collection messages) { + validationAware.setActionMessages(messages); + } + + @Override + public Collection getActionMessages() { + return validationAware.getActionMessages(); + } + + @Override + public void setFieldErrors(Map> errorMap) { + validationAware.setFieldErrors(errorMap); + } + + @Override + public Map> getFieldErrors() { + return validationAware.getFieldErrors(); + } + + @Override + public Locale getLocale() { + return getLocaleProvider().getLocale(); + } + + @Override + public boolean isValidLocaleString(String localeStr) { + return getLocaleProvider().isValidLocaleString(localeStr); + } + + @Override + public boolean isValidLocale(Locale locale) { + return getLocaleProvider().isValidLocale(locale); + } + + @Override + public Locale toLocale(String localeStr) { + return getLocaleProvider().toLocale(localeStr); + } + + @Override + public boolean hasKey(String key) { + return getTextProvider().hasKey(key); + } + + @Override + public String getText(String aTextName) { + return getTextProvider().getText(aTextName); + } + + @Override + public String getText(String aTextName, String defaultValue) { + return getTextProvider().getText(aTextName, defaultValue); + } + + @Override + public String getText(String aTextName, String defaultValue, String obj) { + return getTextProvider().getText(aTextName, defaultValue, obj); + } + + @Override + public String getText(String aTextName, List args) { + return getTextProvider().getText(aTextName, args); + } + + @Override + public String getText(String key, String[] args) { + return getTextProvider().getText(key, args); + } + + @Override + public String getText(String aTextName, String defaultValue, List args) { + return getTextProvider().getText(aTextName, defaultValue, args); + } + + @Override + public String getText(String key, String defaultValue, String[] args) { + return getTextProvider().getText(key, defaultValue, args); + } + + @Override + public String getText(String key, String defaultValue, List args, ValueStack stack) { + return getTextProvider().getText(key, defaultValue, args, stack); + } + + @Override + public String getText(String key, String defaultValue, String[] args, ValueStack stack) { + return getTextProvider().getText(key, defaultValue, args, stack); + } + + /** + * Dedicated method to support I10N and conversion errors + * + * @param key message which contains formatting string + * @param expr that should be formatted + * @return formatted expr with format specified by key + */ + public String getFormatted(String key, String expr) { + Map conversionErrors = ActionContext.getContext().getConversionErrors(); + if (conversionErrors.containsKey(expr)) { + String[] vals = (String[]) conversionErrors.get(expr).getValue(); + return vals[0]; + } else { + final ValueStack valueStack = ValueStack.adapt(ActionContext.getContext().getValueStack()); + final Object val = valueStack.findValue(expr); + return getText(key, Arrays.asList(val)); + } + } + + @Override + public ResourceBundle getTexts() { + return getTextProvider().getTexts(); + } + + @Override + public ResourceBundle getTexts(String aBundleName) { + return getTextProvider().getTexts(aBundleName); + } + + @Override + public void addActionError(String anErrorMessage) { + validationAware.addActionError(anErrorMessage); + } + + @Override + public void addActionMessage(String aMessage) { + validationAware.addActionMessage(aMessage); + } + + @Override + public void addFieldError(String fieldName, String errorMessage) { + validationAware.addFieldError(fieldName, errorMessage); + } + + public String input() throws Exception { + return INPUT; + } + + /** + * A default implementation that does nothing an returns "success". + * + *

+ * Subclasses should override this method to provide their business logic. + *

+ * + *

+ * See also {@link Action#execute()}. + *

+ * + * @return returns {@link #SUCCESS} + * @throws Exception can be thrown by subclasses. + */ + @Override + public String execute() throws Exception { + return SUCCESS; + } + + @Override + public boolean hasActionErrors() { + return validationAware.hasActionErrors(); + } + + @Override + public boolean hasActionMessages() { + return validationAware.hasActionMessages(); + } + + @Override + public boolean hasErrors() { + return validationAware.hasErrors(); + } + + @Override + public boolean hasFieldErrors() { + return validationAware.hasFieldErrors(); + } + + /** + * Clears field errors. Useful for Continuations and other situations + * where you might want to clear parts of the state on the same action. + */ + public void clearFieldErrors() { + validationAware.clearFieldErrors(); + } + + /** + * Clears action errors. Useful for Continuations and other situations + * where you might want to clear parts of the state on the same action. + */ + public void clearActionErrors() { + validationAware.clearActionErrors(); + } + + /** + * Clears messages. Useful for Continuations and other situations + * where you might want to clear parts of the state on the same action. + */ + public void clearMessages() { + validationAware.clearMessages(); + } + + /** + * Clears all errors. Useful for Continuations and other situations + * where you might want to clear parts of the state on the same action. + */ + public void clearErrors() { + validationAware.clearErrors(); + } + + /** + * Clears all errors and messages. Useful for Continuations and other situations + * where you might want to clear parts of the state on the same action. + */ + public void clearErrorsAndMessages() { + validationAware.clearErrorsAndMessages(); + } + + /** + * A default implementation that validates nothing. + * Subclasses should override this method to provide validations. + */ + @Override + public void validate() { + // A default implementation that validates nothing + } + + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + /** + * + * Stops the action invocation immediately (by throwing a PauseException) and causes the action invocation to return + * the specified result, such as {@link #SUCCESS}, {@link #INPUT}, etc. + * + *

+ * The next time this action is invoked (and using the same continuation ID), the method will resume immediately + * after where this method was called, with the entire call stack in the execute method restored. + *

+ * + *

+ * Note: this method can only be called within the {@link #execute()} method. + *

+ * + * + * + * @param result the result to return - the same type of return value in the {@link #execute()} method. + */ + public void pause(String result) { + } + + /** + * If called first time it will create {@link TextProviderFactory}, + * inject dependency (if {@link Container} is accesible) into in, + * then will create new {@link TextProvider} and store it in a field + * for future references and at the returns reference to that field + * + * @return reference to field with TextProvider + */ + protected TextProvider getTextProvider() { + if (textProvider == null) { + final TextProviderFactory tpf = getContainer().getInstance(TextProviderFactory.class); + textProvider = tpf.createInstance(getClass()); + } + return textProvider; + } + + protected LocaleProvider getLocaleProvider() { + if (localeProvider == null) { + final LocaleProviderFactory localeProviderFactory = getContainer().getInstance(LocaleProviderFactory.class); + localeProvider = localeProviderFactory.createLocaleProvider(); + } + return localeProvider; + } + + /** + * TODO: This a temporary solution, maybe we should consider stop injecting container into beans + */ + protected Container getContainer() { + if (container == null) { + container = ActionContext.getContext().getContainer(); + if (container != null) { + boolean devMode = Boolean.parseBoolean(container.getInstance(String.class, StrutsConstants.STRUTS_DEVMODE)); + if (devMode) { + LOG.warn("Container is null, action was created manually? Fallback to ActionContext"); + } else { + LOG.debug("Container is null, action was created manually? Fallback to ActionContext"); + } + } else { + LOG.warn("Container is null, action was created out of ActionContext scope?!?"); + } + } + return container; + } + + @Inject + public void setContainer(Container container) { + this.container = container; + } + +} diff --git a/core/src/main/java/org/apache/struts2/interceptor/AbstractInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/AbstractInterceptor.java new file mode 100644 index 0000000000..ddb48a0d79 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/interceptor/AbstractInterceptor.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE 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.struts2.interceptor; + +import org.apache.struts2.ActionInvocation; + +/** + * Provides default implementations of optional lifecycle methods + */ +public abstract class AbstractInterceptor implements ConditionalInterceptor { + + private boolean disabled; + + /** + * Does nothing + */ + public void init() { + } + + /** + * Does nothing + */ + public void destroy() { + } + + /** + * Override to handle interception + */ + public abstract String intercept(ActionInvocation invocation) throws Exception; + + /** + * Allows to skip executing a given interceptor, just define {@code true} + * or use other way to override interceptor's parameters, see + * docs. + * @param disable if set to true, execution of a given interceptor will be skipped. + */ + public void setDisabled(String disable) { + this.disabled = Boolean.parseBoolean(disable); + } + + @Override + public boolean shouldIntercept(ActionInvocation invocation) { + return !this.disabled; + } +} diff --git a/core/src/main/java/org/apache/struts2/interceptor/MethodFilterInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/MethodFilterInterceptor.java new file mode 100644 index 0000000000..1ffe682619 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/interceptor/MethodFilterInterceptor.java @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE 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.struts2.interceptor; + +import com.opensymphony.xwork2.util.TextParseUtil; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.struts2.ActionInvocation; + +import java.util.Collections; +import java.util.Set; + +/** + * + * + *

+ * MethodFilterInterceptor is an abstract Interceptor used as + * a base class for interceptors that will filter execution based on method + * names according to specified included/excluded method lists. + * + *

+ * + * Settable parameters are as follows: + * + *
    + *
  • excludeMethods - method names to be excluded from interceptor processing
  • + *
  • includeMethods - method names to be included in interceptor processing
  • + *
+ * + *

+ * + * NOTE: If method name are available in both includeMethods and + * excludeMethods, it will be considered as an included method: + * includeMethods takes precedence over excludeMethods. + * + *

+ * + * Interceptors that extends this capability include: + * + *
    + *
  • TokenInterceptor
  • + *
  • TokenSessionStoreInterceptor
  • + *
  • DefaultWorkflowInterceptor
  • + *
  • ValidationInterceptor
  • + *
+ * + * + * + * @author Alexandru Popescu + * @author Rainer Hermanns + * + * @see TokenInterceptor + * @see TokenSessionStoreInterceptor + * @see com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor + * @see com.opensymphony.xwork2.validator.ValidationInterceptor + */ +public abstract class MethodFilterInterceptor extends AbstractInterceptor { + + private static final Logger LOG = LogManager.getLogger(MethodFilterInterceptor.class); + + protected Set excludeMethods = Collections.emptySet(); + protected Set includeMethods = Collections.emptySet(); + + public void setExcludeMethods(String excludeMethods) { + this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods); + } + + public Set getExcludeMethodsSet() { + return excludeMethods; + } + + public void setIncludeMethods(String includeMethods) { + this.includeMethods = TextParseUtil.commaDelimitedStringToSet(includeMethods); + } + + public Set getIncludeMethodsSet() { + return includeMethods; + } + + @Override + public String intercept(ActionInvocation invocation) throws Exception { + if (applyInterceptor(invocation)) { + return doIntercept(invocation); + } + return invocation.invoke(); + } + + protected boolean applyInterceptor(ActionInvocation invocation) { + String method = invocation.getProxy().getMethod(); + // ValidationInterceptor + boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method); + if (!applyMethod) { + LOG.debug("Skipping Interceptor... Method [{}] found in exclude list.", method); + } + return applyMethod; + } + + /** + * Subclasses must override to implement the interceptor logic. + * + * @param invocation the action invocation + * @return the result of invocation + * @throws Exception in case of any errors + */ + protected abstract String doIntercept(ActionInvocation invocation) throws Exception; + +} diff --git a/core/src/main/java/org/apache/struts2/interceptor/MethodFilterInterceptorUtil.java b/core/src/main/java/org/apache/struts2/interceptor/MethodFilterInterceptorUtil.java new file mode 100644 index 0000000000..2a43ba2ffa --- /dev/null +++ b/core/src/main/java/org/apache/struts2/interceptor/MethodFilterInterceptorUtil.java @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE 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.struts2.interceptor; + +import com.opensymphony.xwork2.util.TextParseUtil; +import com.opensymphony.xwork2.util.WildcardHelper; + +import java.util.HashMap; +import java.util.Set; + +/** + * Utility class contains common methods used by + * {@link MethodFilterInterceptor}. + * + * @author tm_jee + */ +public class MethodFilterInterceptorUtil { + + /** + * Static method to decide if the specified method should be + * apply (not filtered) depending on the set of excludeMethods and + * includeMethods. + * + *
    + *
  • + * includeMethods takes precedence over excludeMethods + *
  • + *
+ * Note: Supports wildcard listings in includeMethods/excludeMethods + * + * @param excludeMethods list of methods to exclude. + * @param includeMethods list of methods to include. + * @param method the specified method to check + * @return true if the method should be applied. + */ + public static boolean applyMethod(Set excludeMethods, Set includeMethods, String method) { + + // quick check to see if any actual pattern matching is needed + boolean needsPatternMatch = false; + for (String includeMethod : includeMethods) { + if (!"*".equals(includeMethod) && includeMethod.contains("*")) { + needsPatternMatch = true; + break; + } + } + + for (String excludeMethod : excludeMethods) { + if (!"*".equals(excludeMethod) && excludeMethod.contains("*")) { + needsPatternMatch = true; + break; + } + } + + // this section will try to honor the original logic, while + // still allowing for wildcards later + if (!needsPatternMatch && (includeMethods.contains("*") || includeMethods.size() == 0) ) { + if (excludeMethods != null + && excludeMethods.contains(method) + && !includeMethods.contains(method) ) { + return false; + } + } + + // test the methods using pattern matching + WildcardHelper wildcard = new WildcardHelper(); + String methodCopy ; + if (method == null ) { // no method specified + methodCopy = ""; + } + else { + methodCopy = new String(method); + } + for (String pattern : includeMethods) { + if (pattern.contains("*")) { + int[] compiledPattern = wildcard.compilePattern(pattern); + HashMap matchedPatterns = new HashMap<>(); + boolean matches = wildcard.match(matchedPatterns, methodCopy, compiledPattern); + if (matches) { + return true; // run it, includeMethods takes precedence + } + } + else { + if (pattern.equals(methodCopy)) { + return true; // run it, includeMethods takes precedence + } + } + } + if (excludeMethods.contains("*") ) { + return false; + } + + // CHECK ME: Previous implementation used include method + for ( String pattern : excludeMethods) { + if (pattern.contains("*")) { + int[] compiledPattern = wildcard.compilePattern(pattern); + HashMap matchedPatterns = new HashMap<>(); + boolean matches = wildcard.match(matchedPatterns, methodCopy, compiledPattern); + if (matches) { + // if found, and wasn't included earlier, don't run it + return false; + } + } + else { + if (pattern.equals(methodCopy)) { + // if found, and wasn't included earlier, don't run it + return false; + } + } + } + + + // default fall-back from before changes + return includeMethods.size() == 0 || includeMethods.contains(method) || includeMethods.contains("*"); + } + + /** + * Same as {@link #applyMethod(Set, Set, String)}, except that excludeMethods + * and includeMethods are supplied as comma separated string. + * + * @param excludeMethods comma seperated string of methods to exclude. + * @param includeMethods comma seperated string of methods to include. + * @param method the specified method to check + * @return true if the method should be applied. + */ + public static boolean applyMethod(String excludeMethods, String includeMethods, String method) { + Set includeMethodsSet = TextParseUtil.commaDelimitedStringToSet(includeMethods == null? "" : includeMethods); + Set excludeMethodsSet = TextParseUtil.commaDelimitedStringToSet(excludeMethods == null? "" : excludeMethods); + + return applyMethod(excludeMethodsSet, includeMethodsSet, method); + } + +} diff --git a/core/src/test/java/com/opensymphony/xwork2/config/providers/ConfigurationProviderOgnlAllowlistTest.java b/core/src/test/java/com/opensymphony/xwork2/config/providers/ConfigurationProviderOgnlAllowlistTest.java index 2379216bc5..51d2f96f27 100644 --- a/core/src/test/java/com/opensymphony/xwork2/config/providers/ConfigurationProviderOgnlAllowlistTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/config/providers/ConfigurationProviderOgnlAllowlistTest.java @@ -50,6 +50,7 @@ public void allowlist() throws Exception { Class.forName("java.io.Serializable"), Class.forName("com.opensymphony.xwork2.mock.MockResult"), Class.forName("com.opensymphony.xwork2.interceptor.ConditionalInterceptor"), + Class.forName("org.apache.struts2.ActionSupport"), Class.forName("com.opensymphony.xwork2.ActionSupport"), Class.forName("com.opensymphony.xwork2.ActionChainResult"), Class.forName("com.opensymphony.xwork2.TextProvider"), @@ -60,6 +61,7 @@ public void allowlist() throws Exception { Class.forName("com.opensymphony.xwork2.mock.MockInterceptor"), Class.forName("com.opensymphony.xwork2.Action"), Class.forName("com.opensymphony.xwork2.interceptor.AbstractInterceptor"), + Class.forName("org.apache.struts2.interceptor.AbstractInterceptor"), Class.forName("com.opensymphony.xwork2.Result"), Class.forName("com.opensymphony.xwork2.SimpleAction"), Class.forName("org.apache.struts2.interceptor.Interceptor"), @@ -82,6 +84,7 @@ public void allowlist_1only() throws Exception { Class.forName("java.io.Serializable"), Class.forName("com.opensymphony.xwork2.mock.MockResult"), Class.forName("com.opensymphony.xwork2.interceptor.ConditionalInterceptor"), + Class.forName("org.apache.struts2.ActionSupport"), Class.forName("com.opensymphony.xwork2.ActionSupport"), Class.forName("com.opensymphony.xwork2.TextProvider"), Class.forName("com.opensymphony.xwork2.interceptor.Interceptor"), @@ -90,6 +93,7 @@ public void allowlist_1only() throws Exception { Class.forName("com.opensymphony.xwork2.mock.MockInterceptor"), Class.forName("com.opensymphony.xwork2.Action"), Class.forName("com.opensymphony.xwork2.interceptor.AbstractInterceptor"), + Class.forName("org.apache.struts2.interceptor.AbstractInterceptor"), Class.forName("com.opensymphony.xwork2.Result"), Class.forName("com.opensymphony.xwork2.SimpleAction"), Class.forName("org.apache.struts2.interceptor.Interceptor"), @@ -111,6 +115,7 @@ public void allowlist_2only() throws Exception { Class.forName("com.opensymphony.xwork2.LocaleProvider"), Class.forName("java.io.Serializable"), Class.forName("com.opensymphony.xwork2.interceptor.ConditionalInterceptor"), + Class.forName("org.apache.struts2.ActionSupport"), Class.forName("com.opensymphony.xwork2.ActionSupport"), Class.forName("com.opensymphony.xwork2.ActionChainResult"), Class.forName("com.opensymphony.xwork2.TextProvider"), @@ -120,6 +125,7 @@ public void allowlist_2only() throws Exception { Class.forName("com.opensymphony.xwork2.Validateable"), Class.forName("com.opensymphony.xwork2.Action"), Class.forName("com.opensymphony.xwork2.interceptor.AbstractInterceptor"), + Class.forName("org.apache.struts2.interceptor.AbstractInterceptor"), Class.forName("com.opensymphony.xwork2.Result"), Class.forName("org.apache.struts2.interceptor.Interceptor"), Class.forName("org.apache.struts2.interceptor.ConditionalInterceptor"),