Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

@RequiresCookie / @SetsCookie #1771

Description

@marcbiaggini

Hi Everyone, I would like to understand a little bit more about this annotations. My specific case is related to requires and sets Cookies annotations. I´ve created my Interface as below:

@Rest(rootUrl = "http://myURL/api/myaccount", converters = { MappingJackson2HttpMessageConverter.class , FormHttpMessageConverter.class})
public interface MyAccount extends RestClientErrorHandling {

  @Get("/get")
  @RequiresCookie({"_aecp_dev","_aecp_dev_user"})
  UserResponse getMyAccountInfo();

  @Get("/get_user")
  @RequiresCookie({"_aecp_dev","_aecp_dev_user"})
  UserResponse getUserInfo();

  @Post("/login")
  @Headers(@Header(name ="Content-Type", value="application/x-www-form-urlencoded"))
  @SetsCookie({"_aecp_dev","_aecp_dev_user"})
  GenericResponse logIn(@Body MultiValueMap formParams);

void setCookie(String name, String value);
  String getCookie(String name);
}

Basically my problem comes when I´m trying to call the login Mehod, it supposed that once that it was called, this mothod must to set those cookies "_aecp_dev" and "_aecp_dev_user". But instead, the cookie with the key "_aecp_dev" is getting set with the value of the key "aecp_dev_user", due to in the MyAccount generated class exist this function inside logIn mehod:

 List<String> allCookies = response.getHeaders().get("Set-Cookie");

 if (allCookies!= null) {

    for (String rawCookie: allCookies) {
                    for (String thisCookieName: requestedCookies) {
                     /***In this line is were the problem appears cuz both keys start with the same name***/
                        if (rawCookie.startsWith(thisCookieName)) {
                            int valueEnd = rawCookie.indexOf(';');
                            if (valueEnd == -1) {
                                valueEnd = rawCookie.length();
                            }
                            availableCookies.put(thisCookieName, rawCookie.substring((rawCookie.indexOf("=")+ 1), valueEnd));
                            break;
                        }
                    }
                }

Any ideas? what I´m doing worng?

Thanks

Activity

  1. marcbiaggini commented on May 13, 2016

    @marcbiaggini
    Author

    Issue solved:

    1.) First Approach:

    the problem was that both Keys starts with the same name, and the return of those cookies from server, was ovewriting the values for one of those cookies and null for the other, so to avoid that I put in the first position, the cookie with longest String length and in the second position, a cookie with shorter key as Bellow:

    /*Before I change*/
    @SetsCookie({"_aecp_dev","_aecp_dev_user"})
      GenericResponse logIn(@Body MultiValueMap formParams);
    
    /*After I change*/
    @SetsCookie({"_aecp_dev_user","_aecp_dev"})
      GenericResponse logIn(@Body MultiValueMap formParams);

    2.) Second Approach:

    In the auto gerated Class" myInterface_" once the part for setsallcookies it´s created you must to add another condition to build the cookie:

    if (rawCookie.startsWith(thisCookieName) && rawCookie.substring(rawCookie.indexOf(thisCookieName+ 1)).equals("="))
  2. dodgex commented on May 16, 2016

    @dodgex
    Member

    maybe its simplier to do if (rawCookie.startsWith(thisCookieName + "="))?

  3. marcbiaggini commented on May 18, 2016

    @marcbiaggini
    Author

    I agreed @dodgex , for the second approach is a better solution then I proposed.

  4. WonderCsabo commented on May 18, 2016

    @WonderCsabo
    Member

    @marcbiaggini wanna contribute the fix?

  5. marcbiaggini commented on May 18, 2016

    @marcbiaggini
    Author

    yeah for sure. How can I do it?

  6. WonderCsabo commented on May 18, 2016

    @WonderCsabo
    Member

    Check out this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions