Jahia Dev Forum > Creating a Custom...

0 (0 Good)
0 (0 Bad)

Creating a Custom Annotation for Jahia Actions

by  sashok »  2013/07/22 08:39

Hello Everyone,

 

I have just started developping modules and templates for Jahia 6.6 and I saw that all actions are defined in the META-INF/spring/<module-name>.xml as <bean> tags.

I am not sure if there are better ways to let Spring load our actions in the context. So I have decided to implement my custom @JahiaAction annotation.

This is how the end result looks like:

@JahiaAction(name="formComponentAction1", requireAuthenticatedUser=true)
public class FormComponentAction extends org.jahia.bin.Action ..{

class body...

}

 

And this is how it is done:

META-INF/spring/<module-name>.xml

<context:component-scan base-package="org.jahia.modules.actions" />
<bean class="org.jahia.modules.actions.MyBeanPostProcessor"/>

org.jahia.modules.actions.MyBeanPostProcessor

public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        if(bean.getClass().getAnnotation(JahiaAction.class)!=null){
            String name = bean.getClass().getAnnotation(JahiaAction.class).name();
            if(StringUtils.isEmpty(name)){
                name = beanName;
            }
            boolean requireAuthenticatedUser = bean.getClass().getAnnotation(JahiaAction.class).requireAuthenticatedUser();
            Action jahiaActionBean = (Action) bean;
            jahiaActionBean.setName(name);
            jahiaActionBean.setRequireAuthenticatedUser(requireAuthenticatedUser);
        }
        
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        return bean;
    }
}

org.jahia.modules.actions.JahiaAction

/**
 * Annotation qui permet au Spring de récuperer
 * les beans controlleurs et de les configurer avec
 * les parametres necessaires.
 *
 * {@link Action}
 * @author KIRILOV Alexandre
 *
 */
@Component
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface JahiaAction{
    /**
     * Le nom du controlleur utilisé par Jahia.
     * Default - le nom de la class
     * ex: si on met pas le nom
     * class MyController; name: myController
     *
     * @see Action#setName(String)
     */
    String name() default "";
    /**
     * La variable requireAuthenticatedUser utilisé
     * par Jahia. Default - false
     *
     * @see Action#setRequireAuthenticatedUser(boolean)
     */
    boolean requireAuthenticatedUser() default false;
}

 

I hope this helps !

 

Best Regards,

KIRILOV Alexander

 

  • Creating a Custom Annotation for Jahia Actions
    2013/07/22 08:39

    sashok <p> Hello Everyone,</p> <p> &nbsp;</p> <p> I have just started developping modules and templates for Jahia 6.6 and I saw that all actions are defined in the META-INF/spring/&lt;module-name&gt;.xml as &lt;bean&gt; tags.</p> <p> I am not sure if there are better ways to let Spring load our actions in the context. So I have decided to implement my custom @JahiaAction annotation.</p> <p> This is how the end result looks like:</p> <p> <em>@JahiaAction(name=&quot;formComponentAction1&quot;, requireAuthenticatedUser=true)<br /> public class FormComponentAction extends org.jahia.bin.Action ..{</em></p> <p> <em>class body...</em></p> <p> <em>}</em></p> <p> &nbsp;</p> <p> And this is how it is done:</p> <p> <strong>META-INF/spring/&lt;module-name&gt;.xml</strong></p> <p> <em>&lt;context:component-scan base-package=&quot;org.jahia.modules.actions&quot; /&gt;<br /> &lt;bean class=&quot;org.jahia.modules.actions.MyBeanPostProcessor&quot;/&gt;</em></p> <p> <strong>org.jahia.modules.actions.MyBeanPostProcessor</strong></p> <p> <em>public class MyBeanPostProcessor implements BeanPostProcessor {<br /> <br /> &nbsp;&nbsp; &nbsp;@Override<br /> &nbsp;&nbsp; &nbsp;public Object postProcessBeforeInitialization(Object bean, String beanName)<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;throws BeansException {<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(bean.getClass().getAnnotation(JahiaAction.class)!=null){<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String name = bean.getClass().getAnnotation(JahiaAction.class).name();<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(StringUtils.isEmpty(name)){<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;name = beanName;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;boolean requireAuthenticatedUser = bean.getClass().getAnnotation(JahiaAction.class).requireAuthenticatedUser();<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Action jahiaActionBean = (Action) bean;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;jahiaActionBean.setName(name);<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;jahiaActionBean.setRequireAuthenticatedUser(requireAuthenticatedUser);<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return bean;<br /> &nbsp;&nbsp; &nbsp;}<br /> <br /> &nbsp;&nbsp; &nbsp;@Override<br /> &nbsp;&nbsp; &nbsp;public Object postProcessAfterInitialization(Object bean, String beanName)<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;throws BeansException {<br /> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return bean;<br /> &nbsp;&nbsp; &nbsp;}<br /> }</em></p> <p> <strong>org.jahia.modules.actions.JahiaAction</strong></p> <p> <em>/**<br /> &nbsp;* Annotation qui permet au Spring de r&eacute;cuperer<br /> &nbsp;* les beans controlleurs et de les configurer avec<br /> &nbsp;* les parametres necessaires.<br /> &nbsp;*<br /> &nbsp;* {@link Action}<br /> &nbsp;* @author KIRILOV Alexandre<br /> &nbsp;*<br /> &nbsp;*/<br /> @Component<br /> @Target(ElementType.TYPE)<br /> @Retention(RetentionPolicy.RUNTIME)<br /> public @interface JahiaAction{<br /> &nbsp;&nbsp; &nbsp;/**<br /> &nbsp;&nbsp; &nbsp; * Le nom du controlleur utilis&eacute; par Jahia.<br /> &nbsp;&nbsp; &nbsp; * Default - le nom de la class<br /> &nbsp;&nbsp; &nbsp; * ex: si on met pas le nom<br /> &nbsp;&nbsp; &nbsp; * class MyController; name: myController<br /> &nbsp;&nbsp; &nbsp; *<br /> &nbsp;&nbsp; &nbsp; * @see Action#setName(String)<br /> &nbsp;&nbsp; &nbsp; */<br /> &nbsp;&nbsp; &nbsp;String name() default &quot;&quot;;<br /> &nbsp;&nbsp; &nbsp;/**<br /> &nbsp;&nbsp; &nbsp; * La variable requireAuthenticatedUser utilis&eacute;<br /> &nbsp;&nbsp; &nbsp; * par Jahia. Default - false<br /> &nbsp;&nbsp; &nbsp; *<br /> &nbsp;&nbsp; &nbsp; * @see Action#setRequireAuthenticatedUser(boolean)<br /> &nbsp;&nbsp; &nbsp; */<br /> &nbsp;&nbsp; &nbsp;boolean requireAuthenticatedUser() default false;<br /> }</em></p> <p> &nbsp;</p> <p> I hope this helps !</p> <p> &nbsp;</p> <p> Best Regards,</p> <p> KIRILOV Alexander</p> <p> &nbsp;</p>

  • Number of messages  14
    Registration date Jul 22, 2013
    0 (0 Good)
    0 (0 Bad)

    Re: Creating a Custom Annotation for Jahia Actions

    by  shuber »  2013/07/24 08:30

    Thanks a lot for sharing this, it is a very cool integration indeed ! Is this something you'd like to contribute to Jahia eventually ?

    Best regards,

      Serge... 

    Serge Huber (shuber)

    Number of messages  271
    Registration date
    0 (0 Good)
    0 (0 Bad)

    Re: Re: Creating a Custom Annotation for Jahia Actions

    by  sashok »  2013/07/24 09:42

    Hello Serge,

    Yes, I would be happy to contribute this to Jahia. Can you please tell me what the procedure is ?

    Regards,

    Alexander

     

    Thanks a lot for sharing this, it is a very cool integration indeed ! Is this something you'd like to contribute to Jahia eventually ?

    Best regards,

      Serge... 

  • Re: Re: Creating a Custom Annotation for Jahia Actions
    2013/07/24 09:42

    sashok <p> Hello Serge,</p> <p> Yes, I would be happy to contribute this to Jahia. Can you please tell me what the procedure is ?</p> <p> Regards,</p> <p> Alexander</p> <p> &nbsp;</p> <blockquote> <p> Thanks a lot for sharing this, it is a very cool integration indeed ! Is this something you&#39;d like to contribute to Jahia eventually ?</p> <p> Best regards,</p> <p> &nbsp; Serge...&nbsp;</p> </blockquote>

  • Number of messages  14
    Registration date Jul 24, 2013
    0 (0 Good)
    0 (0 Bad)

    Re: Re: Re: Creating a Custom Annotation for Jahia Actions

    by  faissah »  2013/07/30 14:44

    Hello Alexander,

    Thanks you for contributing to Jahia! You would be the first contributor from the community so we are looking at the correct procedure to do it. We will come back to you as soon as we figure out such procedure.

    Thanks again,

    Fabrice

    Fabrice  (faissah)

    Number of messages  264
    Registration date
    0 (0 Good)
    0 (0 Bad)

    Re: Re: Re: Creating a Custom Annotation for Jahia Actions

    by  shuber »  2013/08/21 07:29

    Hello Alexander, 

    Sorry for the delay, with the holidays it took a little longer to get in touch with the persons involved :) 

    For contributions, as we need to make sure there are no legal issues, we first need you to sign a contributor agreement, that is similar to what is used at the Apache Foundation. You can either contribute as an individual (assuming you didn't do the code for a company), or as a company. Here are the two contracts: 

    Individual:

     

     
    Then the easiest is simply to submit a patch file with your changes on our public Jira here : 
    http://jira.jahia.org
     
    Let me know if you have any questions.
     
    Best regards,
      Serge Huber.

    Serge Huber (shuber)

    Number of messages  271
    Registration date
    0 (0 Good)
    0 (0 Bad)

    Re: Re: Re: Re: Creating a Custom Annotation for Jahia Actions

    by  sashok »  2016/02/26 09:34

    Bump

     

    Can we see this implemented into Jahia soon please :) ? I never had the time to see all the contractual stuff you have provided me with..

    Is it possible to commit it on your github repo ?

     

    Best Regards,

    Alexander KIRILOV

  • Re: Re: Re: Re: Creating a Custom Annotation for Jahia Actions
    2016/02/26 09:34

    sashok <p>Bump</p> <p>&nbsp;</p> <p>Can we see this implemented into Jahia soon please :) ? I never had the time to see all the contractual stuff you have provided me with..</p> <p>Is it possible to commit it on your github repo ?</p> <p>&nbsp;</p> <p>Best Regards,</p> <p>Alexander KIRILOV</p>

  • Number of messages  14
    Registration date Feb 26, 2016
    0 (0 Good)
    0 (0 Bad)

    подарок своими руками лучший подарок

    by  WilliamCok »  2017/09/03 12:41

    подарок своими руками лучший подарок http://cllon.ru
    WilliamCok
    Number of messages  1
    Not registered
    Contact
    Share
    Feedback

    Get in touch

    Whether you are a current user or if you are just evaluating Jahia, we are here to help.

    Contact us

    Share this page