Search in sources :

Example 56 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class ActionAutowiringInterceptor method intercept.

/**
 * <p>
 * Looks for the <code>ApplicationContext</code> under the attribute that the Spring listener sets in
 * the servlet context.  The configuration is done the first time here instead of in init() since the
 * <code>ActionContext</code> is not available during <code>Interceptor</code> initialization.
 * </p>
 *
 * <p>
 * Autowires the action to Spring beans and places the <code>ApplicationContext</code>
 * on the <code>ActionContext</code>
 * </p>
 *
 * <p>
 * TODO: Should this check to see if the <code>SpringObjectFactory</code> has already been configured instead of instantiating a new one?  Or is there a good reason for the interceptor to have it's own factory?
 * </p>
 *
 * @param invocation the action invocation
 * @throws Exception in case of any errors
 */
@Override
public String intercept(ActionInvocation invocation) throws Exception {
    if (!initialized) {
        ApplicationContext applicationContext = (ApplicationContext) ActionContext.getContext().getApplication().get(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        if (applicationContext == null) {
            LOG.warn("ApplicationContext could not be found.  Action classes will not be autowired.");
        } else {
            setApplicationContext(applicationContext);
            factory = new SpringObjectFactory();
            factory.setContainer(ActionContext.getContext().getContainer());
            factory.setApplicationContext(getApplicationContext());
            if (autowireStrategy != null) {
                factory.setAutowireStrategy(autowireStrategy);
            }
        }
        initialized = true;
    }
    if (factory != null) {
        Object bean = invocation.getAction();
        factory.autoWireBean(bean);
    }
    return invocation.invoke();
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SpringObjectFactory(com.opensymphony.xwork2.spring.SpringObjectFactory)

Example 57 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class InterceptorBuilder method constructInterceptorReference.

/**
 * Builds a list of interceptors referenced by the refName in the supplied PackageConfig (InterceptorMapping object).
 *
 * @param interceptorLocator interceptor locator
 * @param refName reference name
 * @param refParams reference parameters
 * @param location location
 * @param objectFactory object factory
 * @return list of interceptors referenced by the refName in the supplied PackageConfig (InterceptorMapping object).
 * @throws ConfigurationException in case of any configuration errors
 */
public static List<InterceptorMapping> constructInterceptorReference(InterceptorLocator interceptorLocator, String refName, Map<String, String> refParams, Location location, ObjectFactory objectFactory) throws ConfigurationException {
    Object referencedConfig = interceptorLocator.getInterceptorConfig(refName);
    List<InterceptorMapping> result = new ArrayList<>();
    if (referencedConfig == null) {
        throw new ConfigurationException("Unable to find interceptor class referenced by ref-name " + refName, location);
    } else {
        if (referencedConfig instanceof InterceptorConfig) {
            InterceptorConfig config = (InterceptorConfig) referencedConfig;
            Interceptor inter;
            try {
                inter = objectFactory.buildInterceptor(config, refParams);
                result.add(new InterceptorMapping(refName, inter, refParams));
            } catch (ConfigurationException ex) {
                LOG.warn(new ParameterizedMessage("Unable to load config class {} at {} probably due to a missing jar, which might be fine if you never plan to use the {} interceptor", config.getClassName(), ex.getLocation(), config.getName()), ex);
            }
        } else if (referencedConfig instanceof InterceptorStackConfig) {
            InterceptorStackConfig stackConfig = (InterceptorStackConfig) referencedConfig;
            if ((refParams != null) && (refParams.size() > 0)) {
                result = constructParameterizedInterceptorReferences(interceptorLocator, stackConfig, refParams, objectFactory);
            } else {
                result.addAll(stackConfig.getInterceptors());
            }
        } else {
            LOG.error("Got unexpected type for interceptor {}. Got {}", refName, referencedConfig);
        }
    }
    return result;
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) ArrayList(java.util.ArrayList) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor)

Example 58 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class XmlConfigurationProvider method loadInterceptorStacks.

protected void loadInterceptorStacks(Element element, PackageConfig.Builder context) throws ConfigurationException {
    NodeList interceptorStackList = element.getElementsByTagName("interceptor-stack");
    for (int i = 0; i < interceptorStackList.getLength(); i++) {
        Element interceptorStackElement = (Element) interceptorStackList.item(i);
        InterceptorStackConfig config = loadInterceptorStack(interceptorStackElement, context);
        context.addInterceptorStackConfig(config);
    }
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element)

Example 59 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class XmlConfigurationProvider method lookupInterceptorReference.

/**
 * Looks up the Interceptor Class from the interceptor-ref name and creates an instance, which is added to the
 * provided List, or, if this is a ref to a stack, it adds the Interceptor instances from the List to this stack.
 *
 * @param context               The PackageConfig to lookup the interceptor from
 * @param interceptorRefElement Element to pull interceptor ref data from
 * @return A list of Interceptor objects
 * @throws ConfigurationException in case of configuration errors
 */
private List<InterceptorMapping> lookupInterceptorReference(PackageConfig.Builder context, Element interceptorRefElement) throws ConfigurationException {
    String refName = interceptorRefElement.getAttribute("name");
    Map<String, String> refParams = XmlHelper.getParams(interceptorRefElement);
    Location loc = LocationUtils.getLocation(interceptorRefElement);
    return InterceptorBuilder.constructInterceptorReference(context, refName, refParams, loc, objectFactory);
}
Also used : Location(com.opensymphony.xwork2.util.location.Location)

Example 60 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class ScopeInterceptor method before.

protected void before(ActionInvocation invocation) throws Exception {
    invocation.addPreResultListener(this);
    Map<String, Object> session = ActionContext.getContext().getSession();
    if (session == null && autoCreateSession) {
        session = new SessionMap<>(ServletActionContext.getRequest());
        ActionContext.getContext().withSession(session);
    }
    if (session != null) {
        lock(session, invocation);
    }
    String key = getKey(invocation);
    Map<String, Object> application = ActionContext.getContext().getApplication();
    final ValueStack stack = ActionContext.getContext().getValueStack();
    LOG.debug("scope interceptor before");
    if (this.application != null)
        for (String string : this.application) {
            Object attribute = application.get(key + string);
            if (attribute != null) {
                LOG.debug("Application scoped variable set {} = {}", string, String.valueOf(attribute));
                stack.setValue(string, nullConvert(attribute));
            }
        }
    if (ActionContext.getContext().getParameters().get(sessionReset).isDefined()) {
        return;
    }
    if (reset) {
        return;
    }
    if (session == null) {
        LOG.debug("No HttpSession created... Cannot set session scoped variables");
        return;
    }
    if (this.session != null && (!"start".equals(type))) {
        for (String string : this.session) {
            Object attribute = session.get(key + string);
            if (attribute != null) {
                LOG.debug("Session scoped variable set {} = {}", string, String.valueOf(attribute));
                stack.setValue(string, nullConvert(attribute));
            }
        }
    }
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack)

Aggregations

ActionInvocation (com.opensymphony.xwork2.ActionInvocation)32 HashMap (java.util.HashMap)28 ActionContext (com.opensymphony.xwork2.ActionContext)20 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)19 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)19 ServletActionContext (org.apache.struts2.ServletActionContext)14 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)13 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)12 LinkedHashMap (java.util.LinkedHashMap)12 Map (java.util.Map)11 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)10 InterceptorConfig (com.opensymphony.xwork2.config.entities.InterceptorConfig)10 DefaultAcceptedPatternsChecker (com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker)10 DefaultExcludedPatternsChecker (com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)10 Cookie (javax.servlet.http.Cookie)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Mock (com.mockobjects.dynamic.Mock)8 ActionSupport (com.opensymphony.xwork2.ActionSupport)8