struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Lukasz Lenart (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (WW-4903) Dependency Mapping Exception When Using PrefixBasedActionProxyFactory
Date Mon, 18 Dec 2017 10:57:00 GMT

    [ https://issues.apache.org/jira/browse/WW-4903?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16294803#comment-16294803
] 

Lukasz Lenart edited comment on WW-4903 at 12/18/17 10:56 AM:
--------------------------------------------------------------

The simplest workaround is to use the below code to implement your own {{ActionProxyFactory}}

{code:java}
public class MyPrefixBasedActionProxyFactory extends DefaultActionProxyFactory {

    private static final Logger LOG = LogManager.getLogger(MyPrefixBasedActionProxyFactory.class);

    private Map<String, ActionProxyFactory> actionProxyFactories = new HashMap<>();

    @Inject
    public void setContainer(Container container) {
        this.container = container;
    }

    @Inject(StrutsConstants.PREFIX_BASED_MAPPER_CONFIGURATION)
    public void setPrefixBasedActionProxyFactories(String list) {
        if (list != null) {
            String[] factories = list.split(",");
            for (String factory : factories) {
                String[] thisFactory = factory.split(":");
                if (thisFactory.length == 2) {
                    String factoryPrefix = thisFactory[0].trim();
                    String factoryName = thisFactory[1].trim();
                    ActionProxyFactory obj = container.getInstance(ActionProxyFactory.class,
factoryName);
                    if (obj != null) {
                        actionProxyFactories.put(factoryPrefix, obj);
                    } else {
                        LOG.warn("Invalid PrefixBasedActionProxyFactory config entry: [{}]",
factory);
                    }
                }
            }
        }
    }

    public ActionProxy createActionProxy(String namespace, String actionName, String methodName,
                                         Map<String, Object> extraContext, boolean executeResult,
boolean cleanupContext) {

        String uri = namespace + (namespace.endsWith("/") ? actionName : "/" + actionName);
        for (int lastIndex = uri.lastIndexOf('/'); lastIndex > (-1); lastIndex = uri.lastIndexOf('/',
lastIndex - 1)) {
            String key = uri.substring(0, lastIndex);
            ActionProxyFactory actionProxyFactory = actionProxyFactories.get(key);
            if (actionProxyFactory != null) {
                LOG.debug("Using ActionProxyFactory [{}] for prefix [{}]", actionProxyFactory,
key);
                return actionProxyFactory.createActionProxy(namespace, actionName, methodName,
extraContext, executeResult, cleanupContext);
            } else {
                LOG.debug("No ActionProxyFactory defined for [{}]", key);
            }
        }
        LOG.debug("Cannot find any matching ActionProxyFactory, falling back to [{}]", super.getClass().getName());
        return super.createActionProxy(namespace, actionName, methodName, extraContext, executeResult,
cleanupContext);
    }

}
{code}

then register it in {{struts.xml}}:

{code:xml}
<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="myPrefix" class="com.company.MyPrefixBasedActionProxyFactory"/>
{code}

and use it in {{struts.xml}}

{code:xml}
<constant name="struts.actionProxyFactory" value="myPrefix"/>
{code}


was (Author: lukaszlenart):
The simplest workaround is to use the below code to implement your own {{ActionProxyFactory}}

{code:java}
public class MyPrefixBasedActionProxyFactory extends DefaultActionProxyFactory {

    private static final Logger LOG = LogManager.getLogger(MyPrefixBasedActionProxyFactory.class);

    private Map<String, ActionProxyFactory> actionProxyFactories = new HashMap<>();

    @Inject
    public void setContainer(Container container) {
        this.container = container;
    }

    @Inject(StrutsConstants.PREFIX_BASED_MAPPER_CONFIGURATION)
    public void setPrefixBasedActionProxyFactories(String list) {
        if (list != null) {
            String[] factories = list.split(",");
            for (String factory : factories) {
                String[] thisFactory = factory.split(":");
                if (thisFactory.length == 2) {
                    String factoryPrefix = thisFactory[0].trim();
                    String factoryName = thisFactory[1].trim();
                    ActionProxyFactory obj = container.getInstance(ActionProxyFactory.class,
factoryName);
                    if (obj != null) {
                        actionProxyFactories.put(factoryPrefix, obj);
                    } else {
                        LOG.warn("Invalid PrefixBasedActionProxyFactory config entry: [{}]",
factory);
                    }
                }
            }
        }
    }

    public ActionProxy createActionProxy(String namespace, String actionName, String methodName,
                                         Map<String, Object> extraContext, boolean executeResult,
boolean cleanupContext) {

        String uri = namespace + (namespace.endsWith("/") ? actionName : "/" + actionName);
        for (int lastIndex = uri.lastIndexOf('/'); lastIndex > (-1); lastIndex = uri.lastIndexOf('/',
lastIndex - 1)) {
            String key = uri.substring(0, lastIndex);
            ActionProxyFactory actionProxyFactory = actionProxyFactories.get(key);
            if (actionProxyFactory != null) {
                LOG.debug("Using ActionProxyFactory [{}] for prefix [{}]", actionProxyFactory,
key);
                return actionProxyFactory.createActionProxy(namespace, actionName, methodName,
extraContext, executeResult, cleanupContext);
            } else {
                LOG.debug("No ActionProxyFactory defined for [{}]", key);
            }
        }
        LOG.debug("Cannot find any matching ActionProxyFactory, falling back to [{}]", super.getClass().getName());
        return super.createActionProxy(namespace, actionName, methodName, extraContext, executeResult,
cleanupContext);
    }

}
{code}

then register it in {{struts.xml}}:

{code:xml}
<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="myPrefix" class="com.company.MyPrefixBasedActionProxyFactory"/>
{code}

and use it in {{struts.xml}}

{code:xml}
<constant name="struts.actionProxyFactory" value="prefix"/>
{code}

> Dependency Mapping Exception When Using PrefixBasedActionProxyFactory
> ---------------------------------------------------------------------
>
>                 Key: WW-4903
>                 URL: https://issues.apache.org/jira/browse/WW-4903
>             Project: Struts 2
>          Issue Type: Bug
>          Components: Plugin - REST
>            Reporter: aditya shrivastava
>            Assignee: Lukasz Lenart
>             Fix For: 2.5.15
>
>         Attachments: error_screen_shot.png
>
>
> I am using struts-rest plugin with REST & NON REST flow I am using STRUTS 2.3.34
version.
> To achieve this I am using following configuration.
> {code:java}
> <constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper"/>
> <constant name="struts.mapper.prefixMapping" value="/sm:rest,/pd:rest,:struts"/>
> <constant name="struts.actionProxyFactory" value="prefix"/>
> {code}
> When I start my tomcat & hit on url 
> https://localhost:8080/myapp
> I always get the exception 
> {code:java}
> HTTP Status 500 – Internal Server Error
> Type Exception Report
> Message java.lang.RuntimeException: com.opensymphony.xwork2.inject.DependencyException:
com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found
for dependency [type=com.opensymphony.xwork2.ActionProxyFactory, name='struts.actionProxyFactory']
in public void org.apache.struts2.impl.PrefixBasedActionProxyFactory.setActionProxyFactory(com.opensymphony.xwork2.ActionProxyFactory).
> Description The server encountered an unexpected condition that prevented it from fulfilling
the request.
> Exception
> java.lang.RuntimeException: java.lang.RuntimeException: com.opensymphony.xwork2.inject.DependencyException:
com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found
for dependency [type=com.opensymphony.xwork2.ActionProxyFactory, name='struts.actionProxyFactory']
in public void org.apache.struts2.impl.PrefixBasedActionProxyFactory.setActionProxyFactory(com.opensymphony.xwork2.ActionProxyFactory).
> 	com.opensymphony.xwork2.inject.ContainerBuilder$4.create(ContainerBuilder.java:132)
> 	com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:51)
> 	com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:514)
> 	com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:524)
> 	com.opensymphony.xwork2.inject.ContainerImpl$9.call(ContainerImpl.java:555)
> 	com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:584)
> 	com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:553)
> 	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:565)
> 	org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
> 	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
> 	com.elitecore.nvsmx.system.filter.PortalSecurityFilter.doFilter(PortalSecurityFilter.java:61)
> Root Cause
> java.lang.RuntimeException: com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException:
No mapping found for dependency [type=com.opensymphony.xwork2.ActionProxyFactory, name='struts.actionProxyFactory']
in public void org.apache.struts2.impl.PrefixBasedActionProxyFactory.setActionProxyFactory(com.opensymphony.xwork2.ActionProxyFactory).
> 	com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:502)
> 	com.opensymphony.xwork2.inject.ContainerImpl$7.call(ContainerImpl.java:539)
> 	com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:593)
> 	com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:537)
> 	com.opensymphony.xwork2.config.impl.LocatableFactory.create(LocatableFactory.java:32)
> 	com.opensymphony.xwork2.inject.ContainerBuilder$4.create(ContainerBuilder.java:130)
> 	com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:51)
> 	com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:514)
> 	com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:524)
> 	com.opensymphony.xwork2.inject.ContainerImpl$9.call(ContainerImpl.java:555)
> 	com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:584)
> 	com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:553)
> 	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:565)
> 	org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
> 	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
> 	com.elitecore.nvsmx.system.filter.PortalSecurityFilter.doFilter(PortalSecurityFilter.java:61)
> com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found
for dependency [type=com.opensymphony.xwork2.ActionProxyFactory, name='struts.actionProxyFactory']
in public void org.apache.struts2.impl.PrefixBasedActionProxyFactory.setActionProxyFactory(com.opensymphony.xwork2.ActionProxyFactory).
> 	com.opensymphony.xwork2.inject.ContainerImpl.createParameterInjector(ContainerImpl.java:239)
> 	com.opensymphony.xwork2.inject.ContainerImpl.getParametersInjectors(ContainerImpl.java:229)
> 	com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.<init>(ContainerImpl.java:293)
> 	com.opensymphony.xwork2.inject.ContainerImpl$3.create(ContainerImpl.java:117)
> 	com.opensymphony.xwork2.inject.ContainerImpl$3.create(ContainerImpl.java:114)
> 	com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMembers(ContainerImpl.java:141)
> 	com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMethods(ContainerImpl.java:113)
> 	com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:90)
> 	com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:71)
> 	com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:67)
> 	com.opensymphony.xwork2.inject.util.ReferenceCache$CallableCreate.call(ReferenceCache.java:150)
> 	java.util.concurrent.FutureTask.run(FutureTask.java:266)
> 	com.opensymphony.xwork2.inject.util.ReferenceCache.internalCreate(ReferenceCache.java:76)
> 	com.opensymphony.xwork2.inject.util.ReferenceCache.get(ReferenceCache.java:116)
> 	com.opensymphony.xwork2.inject.ContainerImpl$ConstructorInjector.<init>(ContainerImpl.java:356)
> 	com.opensymphony.xwork2.inject.ContainerImpl$5.create(ContainerImpl.java:311)
> 	com.opensymphony.xwork2.inject.ContainerImpl$5.create(ContainerImpl.java:307)
> 	com.opensymphony.xwork2.inject.util.ReferenceCache$CallableCreate.call(ReferenceCache.java:150)
> 	java.util.concurrent.FutureTask.run(FutureTask.java:266)
> 	com.opensymphony.xwork2.inject.util.ReferenceCache.internalCreate(ReferenceCache.java:76)
> 	com.opensymphony.xwork2.inject.util.ReferenceCache.get(ReferenceCache.java:116)
> 	com.opensymphony.xwork2.inject.ContainerImpl.getConstructor(ContainerImpl.java:607)
> 	com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:498)
> 	com.opensymphony.xwork2.inject.ContainerImpl$7.call(ContainerImpl.java:539)
> 	com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:593)
> 	com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:537)
> 	com.opensymphony.xwork2.config.impl.LocatableFactory.create(LocatableFactory.java:32)
> 	com.opensymphony.xwork2.inject.ContainerBuilder$4.create(ContainerBuilder.java:130)
> 	com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:51)
> 	com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:514)
> 	com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:524)
> 	com.opensymphony.xwork2.inject.ContainerImpl$9.call(ContainerImpl.java:555)
> 	com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:584)
> 	com.opensymphony.xwork2.inject.ContainerImpl.getInstance(ContainerImpl.java:553)
> 	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:565)
> 	org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
> 	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
> 	com.elitecore.nvsmx.system.filter.PortalSecurityFilter.doFilter(PortalSecurityFilter.java:61)
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message