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();
}
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;
}
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);
}
}
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);
}
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));
}
}
}
}
Aggregations