Search in sources :

Example 11 with InterceptorConfig

use of com.opensymphony.xwork2.config.entities.InterceptorConfig in project struts by apache.

the class SpringObjectFactoryTest method testFallsBackToDefaultObjectFactoryInterceptorBuilding.

public void testFallsBackToDefaultObjectFactoryInterceptorBuilding() throws Exception {
    InterceptorConfig iConfig = new InterceptorConfig.Builder("timer", ModelDrivenInterceptor.class.getName()).build();
    Interceptor interceptor = objectFactory.buildInterceptor(iConfig, new HashMap<String, String>());
    assertEquals(ModelDrivenInterceptor.class, interceptor.getClass());
}
Also used : InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor) NoOpInterceptor(org.apache.struts2.interceptor.NoOpInterceptor) ModelDrivenInterceptor(com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor)

Example 12 with InterceptorConfig

use of com.opensymphony.xwork2.config.entities.InterceptorConfig 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 13 with InterceptorConfig

use of com.opensymphony.xwork2.config.entities.InterceptorConfig in project struts by apache.

the class PlexusObjectFactory method buildInterceptor.

/* (non-Javadoc)
     * @see com.opensymphony.xwork2.ObjectFactory#buildInterceptor(com.opensymphony.xwork2.config.entities.InterceptorConfig, java.util.Map)
     */
public Interceptor buildInterceptor(InterceptorConfig interceptorConfig, Map interceptorRefParams) throws ConfigurationException {
    String interceptorClassName = interceptorConfig.getClassName();
    Map thisInterceptorClassParams = interceptorConfig.getParams();
    Map params = (thisInterceptorClassParams == null) ? new HashMap() : new HashMap(thisInterceptorClassParams);
    params.putAll(interceptorRefParams);
    String message;
    Throwable cause;
    try {
        Map extraContext = new HashMap();
        extraContext.put(PLEXUS_COMPONENT_TYPE, Interceptor.class.getName());
        Interceptor interceptor = (Interceptor) buildBean(interceptorClassName, extraContext);
        reflectionProvider.setProperties(params, interceptor);
        interceptor.init();
        return interceptor;
    } catch (InstantiationException e) {
        cause = e;
        message = "Unable to instantiate an instance of Interceptor class [" + interceptorClassName + "].";
    } catch (IllegalAccessException e) {
        cause = e;
        message = "IllegalAccessException while attempting to instantiate an instance of Interceptor class [" + interceptorClassName + "].";
    } catch (ClassCastException e) {
        cause = e;
        message = "Class [" + interceptorClassName + "] does not implement com.opensymphony.xwork2.interceptor.Interceptor";
    } catch (Exception e) {
        cause = e;
        message = "Caught Exception while registering Interceptor class " + interceptorClassName;
    } catch (NoClassDefFoundError e) {
        cause = e;
        message = "Could not load class " + interceptorClassName + ". Perhaps it exists but certain dependencies are not available?";
    }
    throw new ConfigurationException(message, cause);
}
Also used : HashMap(java.util.HashMap) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Example 14 with InterceptorConfig

use of com.opensymphony.xwork2.config.entities.InterceptorConfig in project struts by apache.

the class XmlConfigurationProviderInterceptorsSpringTest method testInterceptorsLoadedFromSpringApplicationContext.

public void testInterceptorsLoadedFromSpringApplicationContext() throws ConfigurationException {
    sac.registerSingleton("noop-interceptor", NoOpInterceptor.class, new MutablePropertyValues());
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-interceptors-spring.xml";
    // Expect a ConfigurationException to be thrown if the interceptor reference
    // cannot be resolved
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map interceptorConfigs = pkg.getInterceptorConfigs();
    // assertions for size
    assertEquals(1, interceptorConfigs.size());
    // assertions for interceptors
    InterceptorConfig seen = (InterceptorConfig) interceptorConfigs.get("noop");
    assertEquals("noop-interceptor", seen.getClassName());
}
Also used : ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Map(java.util.Map) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 15 with InterceptorConfig

use of com.opensymphony.xwork2.config.entities.InterceptorConfig in project struts by apache.

the class InterceptorBuilderTest method testBuildInterceptor_2.

/**
 * Try to test this
 * <interceptor-ref name="interceptorStack1">
 * <param name="interceptorStack2.interceptor1.param1">interceptor1_value1</param>
 * <param name="interceptorStack2.interceptor1.param2">interceptor1_value2</param>
 * <param name="interceptorStack3.interceptor2.param1">interceptor2_value1</param>
 * <param name="interceptorStack3.interceptor2.param2">interceptor2_value2</param>
 * </interceptor-ref>
 *
 * @throws Exception
 */
public void testBuildInterceptor_2() throws Exception {
    InterceptorStackConfig interceptorStackConfig1 = new InterceptorStackConfig.Builder("interceptorStack1").build();
    InterceptorStackConfig interceptorStackConfig2 = new InterceptorStackConfig.Builder("interceptorStack2").build();
    InterceptorStackConfig interceptorStackConfig3 = new InterceptorStackConfig.Builder("interceptorStack3").build();
    InterceptorConfig interceptorConfig1 = new InterceptorConfig.Builder("interceptor1", "com.opensymphony.xwork2.config.providers.InterceptorBuilderTest$MockInterceptor1").build();
    InterceptorConfig interceptorConfig2 = new InterceptorConfig.Builder("interceptor2", "com.opensymphony.xwork2.config.providers.InterceptorBuilderTest$MockInterceptor2").build();
    PackageConfig packageConfig = new PackageConfig.Builder("package1").namespace("/namspace").addInterceptorConfig(interceptorConfig1).addInterceptorConfig(interceptorConfig2).addInterceptorStackConfig(interceptorStackConfig1).addInterceptorStackConfig(interceptorStackConfig2).addInterceptorStackConfig(interceptorStackConfig3).build();
    List interceptorMappings = InterceptorBuilder.constructInterceptorReference(packageConfig, "interceptorStack1", new LinkedHashMap<String, String>() {

        private static final long serialVersionUID = -5819935102242042570L;

        {
            put("interceptorStack2.interceptor1.param1", "interceptor1_value1");
            put("interceptorStack2.interceptor1.param2", "interceptor1_value2");
            put("interceptorStack3.interceptor2.param1", "interceptor2_value1");
            put("interceptorStack3.interceptor2.param2", "interceptor2_value2");
        }
    }, null, objectFactory);
    assertEquals(interceptorMappings.size(), 2);
    assertEquals(((InterceptorMapping) interceptorMappings.get(0)).getName(), "interceptor1");
    assertNotNull(((InterceptorMapping) interceptorMappings.get(0)).getInterceptor());
    assertEquals(((InterceptorMapping) interceptorMappings.get(0)).getInterceptor().getClass(), MockInterceptor1.class);
    assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(0)).getInterceptor()).getParam1(), "interceptor1_value1");
    assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(0)).getInterceptor()).getParam2(), "interceptor1_value2");
    assertEquals(((InterceptorMapping) interceptorMappings.get(1)).getName(), "interceptor2");
    assertNotNull(((InterceptorMapping) interceptorMappings.get(1)).getInterceptor());
    assertEquals(((InterceptorMapping) interceptorMappings.get(1)).getInterceptor().getClass(), MockInterceptor2.class);
    assertEquals(((MockInterceptor2) ((InterceptorMapping) interceptorMappings.get(1)).getInterceptor()).getParam1(), "interceptor2_value1");
    assertEquals(((MockInterceptor2) ((InterceptorMapping) interceptorMappings.get(1)).getInterceptor()).getParam2(), "interceptor2_value2");
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) List(java.util.List) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Aggregations

InterceptorConfig (com.opensymphony.xwork2.config.entities.InterceptorConfig)11 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)7 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)7 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)6 Interceptor (com.opensymphony.xwork2.interceptor.Interceptor)6 List (java.util.List)5 Map (java.util.Map)5 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)4 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)3 HashMap (java.util.HashMap)3 ModelDrivenInterceptor (com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor)2 ArrayList (java.util.ArrayList)2 SimpleAction (com.opensymphony.xwork2.SimpleAction)1 Configuration (com.opensymphony.xwork2.config.Configuration)1 DefaultConfiguration (com.opensymphony.xwork2.config.impl.DefaultConfiguration)1 WithLazyParams (com.opensymphony.xwork2.interceptor.WithLazyParams)1 MockInterceptor (com.opensymphony.xwork2.mock.MockInterceptor)1 MockResult (com.opensymphony.xwork2.mock.MockResult)1 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)1 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)1