struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jasper Rosenberg (JIRA)" <j...@apache.org>
Subject [jira] Created: (WW-1815) Make ActionMapper ParameterActions more easily extensible
Date Wed, 14 Mar 2007 14:56:51 GMT
Make ActionMapper ParameterActions more easily extensible
---------------------------------------------------------

                 Key: WW-1815
                 URL: https://issues.apache.org/struts/browse/WW-1815
             Project: Struts 2
          Issue Type: Improvement
    Affects Versions: 2.0.6
            Reporter: Jasper Rosenberg
            Priority: Minor


It would be great if DefaultActionMapper made it easy to add additional ParameterActions to
the prefixTrie.

All it would take is making ParameterAction protected, and then adding a method like:

protected void addParameterAction(String prefix, ParameterAction parameterAction) {
    prefixTrie.put(prefix, parameterAction);
}

We had the need to add our own prefix support for redirecting w/o appending the context, and,
just so you can feel my pain, here is what I currently have to do:

    /** Submission redirect parameter that doesn't automatically include context. */
    static final String REDIRECT_NO_CONTEXT_PREFIX = "redirect-nocontext:";

    public MyActionMapper() {
        try {
            /*
             * The list of ParameterActions is private, so we have
             * to bust through using reflection.
             */
            Field prefixTrieField = DefaultActionMapper.class.getDeclaredField("prefixTrie");
            prefixTrieField.setAccessible(true);
            PrefixTrie prefixTrie  = (PrefixTrie) prefixTrieField.get(this);
            Object sample = prefixTrie.get("action:");

            // The ParameterAction interface is package protected, so use a dynamic
            // proxy to get around it.
            Class[] interfaces = sample.getClass().getInterfaces();
            Object parameterActionProxy = 
                Proxy.newProxyInstance(
                    Thread.currentThread().getContextClassLoader(),
                    interfaces,
                    new InvocationHandler() {
                        public Object invoke(Object proxy, Method method, Object[] args)
                            throws Exception {
                            if (method.getName().equals("execute")) {
                                String key = (String) args[0];
                                ActionMapping mapping = (ActionMapping) args[1];
                                ServletRedirectResult redirect =
                                    new ServletRedirectResult();
                                redirect.setPrependServletContext(false);
                                redirect.setLocation(
                                    key.substring(
                                        REDIRECT_NO_CONTEXT_PREFIX.length()));
                                mapping.setResult(redirect);
                                return null;
                            } else {
                                throw new RuntimeException(
                                    "Unknown parameter action method: "
                                    + method.getName());
                            }                            
                        }
                    });
            
            prefixTrie.put(REDIRECT_NO_CONTEXT_PREFIX, parameterActionProxy);
        } catch (Exception e) {
            LogFactory.getLog(MyActionMapper.class).error(
                "Failed to add custom parameter action mapping.", e);
        }
    }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message