PDA

View Full Version : StateManager and cookie provider



AlessioUnibs
15 Dec 2010, 3:33 AM
Hi, i have gxt 2.2.0 and i'm using state manager instance. When i try to delete a cookie using StateManager.getProvider().clear(cookieName) with the default provider, it seems does anything because when i look at cookies whith firefox, i see that the cookie i want to delete is always in the cookies' list.
I also tried extend the cookie provider, and use the new class that extend the cookie provider to delete the cookie, in detail i have called the method clear_key in super class, but anything. Here is the code of extended cookie provider:
public class CookieProviderExtended extends CookieProvider {

/**
* COnstructor
* @param path
* @param expires
* @param domain
* @param secure
*/
public CookieProviderExtended(String path, Date expires, String domain,boolean secure) {
super(path, expires, domain, secure);
}


public void clear_key(String n){
super.clearKey(n);
}

}

Can someone help me please?
Thanks in advance

sven
15 Dec 2010, 3:35 AM
Works fine for me. What name do you pass in that method? Is that the correct one? Do you have a fully working testcase implementing EntryPoint?

AlessioUnibs
15 Dec 2010, 7:11 AM
Hi sven, i use this method as i said before sm.getProvider().clear(ETICH_CB_LUNEDI); where ETICH_CB_LUNEDI is a constant and it's value is "lunedi_cb_value". After the execution i still see the cookie. I post an image with the cookies' list and if you can see it, there is in folder localhost the cookie named "lunedi_cb_value".
23840

About a fully working testcase can you make me an example?
Thanks in advance

AlessioUnibs
15 Dec 2010, 9:17 AM
Hi, if i use StateManager instance to save the state in the cookie and i call directly gwt Cookies.removeCookie(name,path) when i need, all work fine. But in yhis way i bypass StateManager object, also based on class Cookies from gwt library. Maybe it isn't the best way to proceed or maybe it is strange that StateManager work only to save and not to remove cookies.

sven
15 Dec 2010, 9:27 AM
clearKey actually directly calls Cookies.removeCookie. It works without any problems for me. Cookie is gone.

Not that you need to reopen or refresh your cookie dialog after removing a cookie.

AlessioUnibs
15 Dec 2010, 9:41 AM
Yes i reopen my cookie dialog after the removing; now i have a question, maybe it is stupid, but clearKey is a protected method, abstract in class Provider and, as you said before, implemented in CookieProvider. But if i call StateManager.get().getProvider(). ... in eclipse autocompletion i don't see the method clearKey(). How i can call it?

I also see that clear (a public method) and clearKey are the same method.

sven
15 Dec 2010, 9:43 AM
Sorry to be not that clear on this.

You need to call clear on the provider. It internly will than call clearKey which will call Cookies.removeCookie

AlessioUnibs
15 Dec 2010, 9:47 AM
Maybe there is an error in the API documentation that i look at! I have to write this: StateManager.get().getProvider().clear("cookieName") ?

sven
15 Dec 2010, 9:49 AM
No thats correct. You need to pass into the name.

Please post a fully working testcase implementing EntryPoint. Are you sure that nothing is resetting the cookie?

AlessioUnibs
15 Dec 2010, 9:52 AM
This is my entry point class:

public class Prenotazioni implements EntryPoint{

//Oggetto dispatcher necessario per comunicare eventi ai controller
private Dispatcher dispatcher;

/**
* Metodo onModuleLoad(), necessario nella classe entry point
*
* @author Cecchi Alessio Giovanni
* @version 1.0
*/
public void onModuleLoad() {
/* Diamo all'applicazione il supporto all'History del browser
Se l'applicazione viene avviata con nessun History token, la si riporta ad uno stato "HOME".
*/

String initToken = History.getToken();
if (initToken.length() == 0) {
History.newItem(HistoryTokens.HOME);
}
/**Creazione dei diversi controller all'avvio dell'applicazione*/
dispatcher = Dispatcher.get();
dispatcher.addController(new LoginController());
dispatcher.addController(new FirstPageController());
dispatcher.addController(new SecondPageController());
dispatcher.addController(new Passo1Controller());
dispatcher.addController(new Passo2Controller());
dispatcher.addController(new ScritturaCorrettaDialogController());
dispatcher.addController(new DefinizionePassoSceltaGiornoController());
dispatcher.addController(new DefinizionePassoSceltaOrariController());
dispatcher.addController(new DefinizioneRichiestaDatiPersonaliController());
dispatcher.addController(new DatiUlterioriController());
dispatcher.addController(new DefinizionePassoDatiUtilitaController());
dispatcher.addController(new AltriDatiController());
dispatcher.addController(new DefinizioneRiepilogoEConfermaController());

/*All'avvio l'applicazione crea un evento di tipo Init (ad indicare la necessità di mostrare
* la prima pagina dell'applicazione) e attraverso il dispatcher lo passa al controller corretto
* */
dispatcher.dispatch(StartEvents.Init);


/*
* Meccanismo di history secondo cui al premere del bottone forward, se nello stack del browser c'è il token "PASSO1",
* viene caricata la seconda pagina dell'applicazione, tramite un evento lanciato dal dispatcher.
* */
History.addValueChangeHandler(new ValueChangeHandler<String>() {

@Override
public void onValueChange(ValueChangeEvent<String> event) {
String historyToken = event.getValue();
if(historyToken.equals(HistoryTokens.PASSO1)){
Dispatcher.forwardEvent(StartEvents.creaNuovoServizio);
}

}
});

}
}

sven
15 Dec 2010, 9:53 AM
That is not a testcase i can run. Please post a testcase without any other depencies so that everybody can run it

sven
15 Dec 2010, 9:58 AM
class Test implements EntryPoint {

public void onModuleLoad() {
StateManager.get().setProvider(new CookieProvider("/", null, null, false));
StateManager.get().set("name", "test");
System.out.println(StateManager.get().getString("name"));
StateManager.get().getProvider().clear("name");
System.out.println(StateManager.get().getString("name"));

}

}

This prints out
test
null

Which is expected

AlessioUnibs
15 Dec 2010, 10:00 AM
I hope this is correct because in my Entry point class i only dispatch events.


public class PrenotazioniTest {

@Test
public void testOnModuleLoad() {
String initToken = History.getToken();
if (initToken.length() == 0) {
History.newItem(HistoryTokens.HOME);
}
/**Creazione dei diversi controller all'avvio dell'applicazione*/
Dispatcher dispatcher = Dispatcher.get();

History.addValueChangeHandler(new ValueChangeHandler<String>() {

@Override
public void onValueChange(ValueChangeEvent<String> event) {
String historyToken = event.getValue();
if(historyToken.equals(HistoryTokens.PASSO1)){

}

}
});

}

}

sven
15 Dec 2010, 10:01 AM
I hope this is correct because in my Entry point class i only dispatch events.


public class PrenotazioniTest {

@Test
public void testOnModuleLoad() {
String initToken = History.getToken();
if (initToken.length() == 0) {
History.newItem(HistoryTokens.HOME);
}
/**Creazione dei diversi controller all'avvio dell'applicazione*/
Dispatcher dispatcher = Dispatcher.get();

History.addValueChangeHandler(new ValueChangeHandler<String>() {

@Override
public void onValueChange(ValueChangeEvent<String> event) {
String historyToken = event.getValue();
if(historyToken.equals(HistoryTokens.PASSO1)){

}

}
});

}

}

That class now does not show anything that is related to cookies. Also how should i run it?

AlessioUnibs
15 Dec 2010, 10:04 AM
It is a junit test case:

public class PrenotazioniTest {

@Test
public void testOnModuleLoad() {

StateManager.get().set("name", "test");
System.out.println(StateManager.get().getString("name"));
StateManager.get().getProvider().clear("name");
System.out.println(StateManager.get().getString("name"));
}

}

sven
15 Dec 2010, 10:06 AM
So yes, now you copied my code which runs fine in firefox against latest GWT 2.1.0 and GXT latest.

AlessioUnibs
15 Dec 2010, 10:11 AM
I execute the test case with eclipse gwt junit test tool, but i have a null pointer exception at line StateManager.get().set("name","test"); maybe i not initialize something. I'm sorry for truble i cause you.

sven
15 Dec 2010, 10:14 AM
You need to set a Provider. Also you should test this in a real browser, and not htmlunit. Not everything will work in htmlunit

AlessioUnibs
15 Dec 2010, 10:20 AM
I made it, but when i look at eclipse console (where system.out.print show output), i saw that cookie isn't deleted. it print test test

AlessioUnibs
15 Dec 2010, 10:28 AM
The CookieProvider clear method in the gxt 2.2.0 library source code that i'm using is:
public void clear(String name) {
Cookies.removeCookie(name);
}

And my mozzilla firefox is: Mozilla/5.0 (Windows; U; Windows NT 6.1; it; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13

sven
15 Dec 2010, 10:30 AM
Thats the problem;)

it looks like this in latest SVN
public void clear(String name) {
Cookies.removeCookie(name, path);
}

AlessioUnibs
15 Dec 2010, 11:12 AM
So i try to update the gxt library. I use the 2.2.0 actually!

sven
15 Dec 2010, 11:13 AM
Thats the latest official, you need to wait for 2.2.2 or override the CookieProvider file yoruself

AlessioUnibs
15 Dec 2010, 11:17 AM
Ok, thanks for suggestions!! I'll post here if i solve the problem!

AlessioUnibs
15 Dec 2010, 1:38 PM
Hi, i downloaded also the gxt 2.2.1, but i only confirmed what you said before: i need the version 2.2.2 or i need to override the cookieProvider. Do you know when the new version will be avaiable?

I thank you for help me!
Good evening