Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.1.6
    • Fix Version/s: 3.0
    • Component/s: Core Interceptors
    • Labels:
      None

      Description

      Hello, I've developed for my current project a simple Cookie Interceptor. It's like the one in the distribution but different,

      It works by annotating in the action with a @Cookie(name="cookieName",timeToLive=100,path="xxx") a String property.

      The Interceptor (Which is also a PreResultListener) Scans the action. if it finds a @Cookie annotation it injects the cookie value into the action's property. When the action is executed, the interceptor's beforeResultMethod scans the action again and the values from properties annotated with @Cookie are saved in the response's cookies.

      This is the basic idea, than using just the getters and setters in the action you can retrieve and set a cookie..

      I would love to share this simple interceptor, i would love to see a little of my code added to your project ... what can i do for you to see the code and if it's something you like, submit it... thanks..

        Issue Links

          Activity

          carlo scarioni created issue -
          Hide
          Dave Newton added a comment - - edited

          If it was integrated into the existing interceptor that might be nice; I don't think I'd want two cookie interceptors.

          It would also have to support multiple cookies, either through a @Cookies annotation or by simply annotating setters instead of the action.

          Annotating the action isn't necessarily the best solution since some people use multiple action methods and might not always want the interceptor to fire (although the overhead is minimal, so maybe it doesn't matter).

          Feel free to attach a patch to this ticket. If you don't want to integrate the two interceptors I might take a look at it.

          Show
          Dave Newton added a comment - - edited If it was integrated into the existing interceptor that might be nice; I don't think I'd want two cookie interceptors. It would also have to support multiple cookies, either through a @Cookies annotation or by simply annotating setters instead of the action. Annotating the action isn't necessarily the best solution since some people use multiple action methods and might not always want the interceptor to fire (although the overhead is minimal, so maybe it doesn't matter). Feel free to attach a patch to this ticket. If you don't want to integrate the two interceptors I might take a look at it.
          Hide
          carlo scarioni added a comment -

          Hi, thanks for the quick answer, maybe my english is not perfect. The annotation works on String fields... and this field must be properties (have getters and setters). it's not a Type Annotation that annotates the action.. So the @Cookies for multiple cookies is not necessary. You simply annotate the field... that is how it works... i'll take another look at the existing interceptor to check if it can be integrated.
          Right now my Interceptor extends the Struts 2 Cookie interceptor.. and maintains the CookieAware behaviour. However the other behaviour (Injecting into value stack values based on cookies name) is kinda per-action behaviour... like this

          <action ... >
          <interceptor-ref name="cookie">
          <param name="cookiesName">cookie1, cookie2</param>
          <param name="cookiesValue">cookie1value, cookie2value</param>
          </interceptor-ref>
          ....
          </action>

          My interceptor on the other hand is defined in the interceptor stack.. and not customized per action.... I don't think that i can integrate this two behaviours... Bu i'll take a closer look and come to you later...

          Thanks again

          Show
          carlo scarioni added a comment - Hi, thanks for the quick answer, maybe my english is not perfect. The annotation works on String fields... and this field must be properties (have getters and setters). it's not a Type Annotation that annotates the action.. So the @Cookies for multiple cookies is not necessary. You simply annotate the field... that is how it works... i'll take another look at the existing interceptor to check if it can be integrated. Right now my Interceptor extends the Struts 2 Cookie interceptor.. and maintains the CookieAware behaviour. However the other behaviour (Injecting into value stack values based on cookies name) is kinda per-action behaviour... like this <action ... > <interceptor-ref name="cookie"> <param name="cookiesName">cookie1, cookie2</param> <param name="cookiesValue">cookie1value, cookie2value</param> </interceptor-ref> .... </action> My interceptor on the other hand is defined in the interceptor stack.. and not customized per action.... I don't think that i can integrate this two behaviours... Bu i'll take a closer look and come to you later... Thanks again
          carlo scarioni made changes -
          Field Original Value New Value
          Assignee carlo scarioni [ carlo81 ]
          carlo scarioni made changes -
          Status Open [ 1 ] In Progress [ 3 ]
          Hide
          carlo scarioni added a comment -

          Hi Dave,
          As i told you i don't think i can mix the full behaviour of the defaultcookie interceptor and mine... I attached (I don't know if this is the way to send you something) a Maven Project with nothing but my interceptor (And a testNG test, and the annotation) for you to take a look at. I hope you see it and contact me for any response you have. If by looking the code, you can think of a way of integrating the two interceptors don't hesitate to tell me..

          Thank you very much

          Show
          carlo scarioni added a comment - Hi Dave, As i told you i don't think i can mix the full behaviour of the defaultcookie interceptor and mine... I attached (I don't know if this is the way to send you something) a Maven Project with nothing but my interceptor (And a testNG test, and the annotation) for you to take a look at. I hope you see it and contact me for any response you have. If by looking the code, you can think of a way of integrating the two interceptors don't hesitate to tell me.. Thank you very much
          carlo scarioni made changes -
          Attachment struts-cookie-interceptor.tar.gz [ 14090 ]
          Hide
          Alex Siman added a comment -

          Hi,

          IMHO only reading cookie is not sufficient for real webapps. Definitely CooieInterceptor must have ability to add new cookie or remove existent.

          What about ideas from this proposal:
          http://www.nabble.com/Full-featured-cookie-utils-out-of-the-box-td25145716.html

          Show
          Alex Siman added a comment - Hi, IMHO only reading cookie is not sufficient for real webapps. Definitely CooieInterceptor must have ability to add new cookie or remove existent. What about ideas from this proposal: http://www.nabble.com/Full-featured-cookie-utils-out-of-the-box-td25145716.html
          Hide
          Dave Newton added a comment -

          I don't really agree with that, although it's possible I could be convinced. Adding or removing a cookie would almost always be action-specific, not a cross-cutting concern. In what scenarios do you see adding and removing cookies as being an application-wide concern?

          Show
          Dave Newton added a comment - I don't really agree with that, although it's possible I could be convinced. Adding or removing a cookie would almost always be action-specific, not a cross-cutting concern. In what scenarios do you see adding and removing cookies as being an application-wide concern?
          Hide
          Alex Siman added a comment -

          In "Remember me" feature I add "sid" cookie on login/register actions (action-specific) and remove "sid" cookie in AutoLoginInterceptor if "sid" has expired due to DB or was not found in DB (application-wide).

          Show
          Alex Siman added a comment - In "Remember me" feature I add "sid" cookie on login/register actions (action-specific) and remove "sid" cookie in AutoLoginInterceptor if "sid" has expired due to DB or was not found in DB (application-wide).
          Hide
          carlo scarioni added a comment -

          It actually does that two things...

          If you set the @Cookie annotated property in the action, when the action returns the beforeResult method is invoked on the interceptor and
          the new value from the action property will be setted on the cookie... If the property's value is null, the cookie's age will be setted to zero so it will be deleted from the browser...

          So you the interceptor do all of that...

          It injects the cookie to the action if it exists, and on return takes the value from the action's property (which can be the same value as before) and set it again on the cookie. In case of null, cookie is deleted...

          You can see the source code i attached..

          thanks

          Show
          carlo scarioni added a comment - It actually does that two things... If you set the @Cookie annotated property in the action, when the action returns the beforeResult method is invoked on the interceptor and the new value from the action property will be setted on the cookie... If the property's value is null, the cookie's age will be setted to zero so it will be deleted from the browser... So you the interceptor do all of that... It injects the cookie to the action if it exists, and on return takes the value from the action's property (which can be the same value as before) and set it again on the cookie. In case of null, cookie is deleted... You can see the source code i attached.. thanks
          Hide
          Alex Siman added a comment -

          Interesting approach to add/remove cookies.

          Why did you not use all properties of javax.servlet.http.Cookie in your Cookie annotation?
          http://www.docjar.com/html/api/javax/servlet/http/Cookie.java.html

          What if create Cookies annotation for Map<String, String> property?
          Concept example:

          class CookieAction

          { @Cookies( @Cookie(name="cookieName1", timeToLive=100, path="xxx"), @Cookie(name="cookieName2", timeToLive=200, path="yyy") ) private Map<String, String> twoCookies; @Cookies private Map<String, String> allCookies; // If (allCookies.get(key) == null) remove cookie. // If (allCookies == null || allCookies.size() < 1) remove all cookies. }

          What if make Cookie.name not required? Simply use property name as cookie name.

          Show
          Alex Siman added a comment - Interesting approach to add/remove cookies. Why did you not use all properties of javax.servlet.http.Cookie in your Cookie annotation? http://www.docjar.com/html/api/javax/servlet/http/Cookie.java.html What if create Cookies annotation for Map<String, String> property? Concept example: class CookieAction { @Cookies( @Cookie(name="cookieName1", timeToLive=100, path="xxx"), @Cookie(name="cookieName2", timeToLive=200, path="yyy") ) private Map<String, String> twoCookies; @Cookies private Map<String, String> allCookies; // If (allCookies.get(key) == null) remove cookie. // If (allCookies == null || allCookies.size() < 1) remove all cookies. } What if make Cookie.name not required? Simply use property name as cookie name.
          Hide
          carlo scarioni added a comment -

          To be honest, i didn't use all the properties because i didn't need them for the problem at hand.
          The @Cookies annotation for mapping multiple cookies to a Map property seems like a nice addition.
          To default the cookie name to the property name is a great convention..

          I can totally add these functionalities.
          So, do you think that the struts2 team wold be interested in this Inteceptor for the project. How can i figure it out.. I have long time been using OSS, and i'd really love to commit something to the community in return, even as little as this... .. to see a couple of lines of my code in a project like this would be awesome for me

          Show
          carlo scarioni added a comment - To be honest, i didn't use all the properties because i didn't need them for the problem at hand. The @Cookies annotation for mapping multiple cookies to a Map property seems like a nice addition. To default the cookie name to the property name is a great convention.. I can totally add these functionalities. So, do you think that the struts2 team wold be interested in this Inteceptor for the project. How can i figure it out.. I have long time been using OSS, and i'd really love to commit something to the community in return, even as little as this... .. to see a couple of lines of my code in a project like this would be awesome for me
          Jeff Turner made changes -
          Project Import Mon Feb 01 01:17:42 UTC 2010 [ 1264987062082 ]
          Maurizio Cucchiara made changes -
          Assignee carlo scarioni [ carlo81 ] Maurizio Cucchiara [ maurizio.cucchiara ]
          Maurizio Cucchiara made changes -
          Status In Progress [ 3 ] Open [ 1 ]
          Maurizio Cucchiara made changes -
          Link This issue relates to WW-3497 [ WW-3497 ]
          Lukasz Lenart made changes -
          Fix Version/s 3.x [ 12319158 ]
          Priority Major [ 3 ] Minor [ 4 ]
          Transition Time In Source Status Execution Times Last Executer Last Execution Date
          Open Open In Progress In Progress
          4h 36m 1 carlo scarioni 02/Sep/09 16:10
          In Progress In Progress Open Open
          503d 8h 5m 1 Maurizio Cucchiara 18/Jan/11 23:16

            People

            • Assignee:
              Maurizio Cucchiara
              Reporter:
              carlo scarioni
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:

                Development