You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Gustavo Henrique <gu...@gmail.com> on 2010/04/01 15:41:36 UTC

Create cookie before redirect

Hi!
I need to create a cookie and redirect to page in other app, but when I try
redirect the cookie is not send in response:

Form loginForm = new StatelessForm("loginForm") {
      protected void onSubmit() {
          try {
              Customer authenticatedCustomer =
customerAuthentication.authenticateByLogin( getFormFieldValue("username"),
getFormFieldValue("password"));
              getWebRequestCycle().getWebResponse().addCookie( new
CustomerCookie( authenticatedCustomer, getDomainName() ) );
              getPage().getResponse().redirect("http://domain.com/myapp");
          } catch(CustomerAuthenticationException e) {
              add( new Label("customerAuthenticationFailure",
getString("customerAuthenticationFailure")));
          }
      }

      protected String getFormFieldValue(String field) {
          return get(field).getModelObjectAsString();
      }
}

Re: Create cookie before redirect

Posted by Pawel Koziorowski <pa...@gmail.com>.
Hi!
I slightly customized your code and it works fine. After redirecting I
checked http://localhost:8084/WicketTest/ (where is the code) domain
for CustomerCookie cookie and it existed and every time I click submit
a new cookie is added.

public class Index extends WebPage {

    public Index() {
        Form loginForm = new StatelessForm("loginForm") {

            @Override
            protected void onSubmit() {
                try {
                    Customer authenticatedCustomer =
authenticateByLogin(getFormFieldValue("username"),
getFormFieldValue("password"));

getWebRequestCycle().getWebResponse().addCookie(new
CustomerCookie(authenticatedCustomer, "Domain"));
                    getPage().getResponse().redirect("http://google.pl");
                } catch (Exception e) {

                }
            }

            protected String getFormFieldValue(String field) {
                return get(field).getDefaultModelObjectAsString();
            }

            private Customer authenticateByLogin(String
formFieldValue, String formFieldValue0) {
                return new Customer();
            }
        };

        TextField<String> user = new TextField<String>("username", new
Model<String>());
        PasswordTextField pass = new PasswordTextField("password", new
Model<String>());
        loginForm.add(user);
        loginForm.add(pass);
        add(loginForm);

    }
}

public class CustomerCookie extends Cookie{

    private Customer customer;
    private String domainName;

    public CustomerCookie(Customer customer, String domainName) {
        super(customer.toString(), domainName);
        this.customer = customer;
        this.domainName = domainName;
    }
}

public class Customer {

}


2010/4/1 Gustavo Henrique <gu...@gmail.com>:
> Hi!
> I need to create a cookie and redirect to page in other app, but when I try
> redirect the cookie is not send in response:
>
> Form loginForm = new StatelessForm("loginForm") {
>      protected void onSubmit() {
>          try {
>              Customer authenticatedCustomer =
> customerAuthentication.authenticateByLogin( getFormFieldValue("username"),
> getFormFieldValue("password"));
>              getWebRequestCycle().getWebResponse().addCookie( new
> CustomerCookie( authenticatedCustomer, getDomainName() ) );
>              getPage().getResponse().redirect("http://domain.com/myapp");
>          } catch(CustomerAuthenticationException e) {
>              add( new Label("customerAuthenticationFailure",
> getString("customerAuthenticationFailure")));
>          }
>      }
>
>      protected String getFormFieldValue(String field) {
>          return get(field).getModelObjectAsString();
>      }
> }
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org