Vaadin cookies internationally

I welcome!

Faced with the problem.
I’m trying to save russian login in a cookie file,


...
		Cookie cookie = new Cookie("introGisLoginCookie", login + ":" + password);
		cookie.setMaxAge(3600);
		response.addCookie(cookie);		

...

I get an error -


Control character in cookie value or attribute

Does anyone know how to solve this problem?
I would greatly appreciate your help! :smiley:

Cheers,
Thx

How to be, for example, if you want to save a login -百度文 ? :smiley:
Does anyone know? :smiley:

Actually, the first part of the problem is solved -


...
		Cookie cookie = null;
		try {
			login = URLEncoder.encode(new String(login.getBytes("windows-1251")));
		} catch (UnsupportedEncodingException e) {
			LOG.warn("Stack Trace:", e);
		}
		
		cookie = new Cookie("introGisLoginCookie" + login, login + ":" + password);
		cookie.setMaxAge(3600);
		
		response.addCookie(cookie);		

...

When i read -


...
        HashMap<String, String> hMap = readCookies();
        if(hMap != null && hMap.size() != 0) {
        	Set keys = hMap.keySet();
        	for(Object o : keys) {
        			String item = o.toString(); 
        				   item = java.net.URLDecoder.decode(item);  
        		loginComboBox.addItem(item);
        	}
        }

...

If my code is somewhat inaccurate, I will be grateful for corrections. :smiley:

From the documentation, you should encode it in base64 ( make sure to use something like org.apache.commons.codec.binary and NOT the old sun encoder)

setValue

public void setValue(java.lang.String newValue)
Assigns a new value to a cookie after the cookie is created.

If you use a binary value, you may want to use BASE64 encoding
.

With Version 0 cookies, values should not contain white space, brackets, parentheses, equals signs, commas, double quotes, slashes, question marks, at signs, colons, and semicolons. Empty values may not behave the same way on all browsers.

Parameters:
newValue - a String specifying the new value
See Also:
getValue(), Cookie

I welcome you, Jean-François Lamy!

Thank you very much for the info!:smiley:

I rewrote some method of recording settings -


...
	private void saveAuthInfoInCookies(String login, String password) {
		login = URLEncoder.encode(login);
		Cookie cookie = new Cookie("introGisLoginCookie" + login, login + ":" + password);
		cookie.setMaxAge(3600);
		
		response.addCookie(cookie);		
	}

...

And when I read, respectively -


...
						while(stringTokenizer.hasMoreTokens()) {
							login = java.net.URLDecoder.decode(stringTokenizer.nextToken());
							password = stringTokenizer.nextToken();
							
							authPairs.put(login, password);
					    }

...

So fill combobox-


...
        HashMap<String, String> hMap = readCookies();
        if(hMap != null && hMap.size() != 0) {
        	Set keys = hMap.keySet();
        	for(Object o : keys) 
        		loginComboBox.addItem(o.toString());
        }

...

And listener -


...
 	public LoginComboBoxValueChangeListener(HashMap<String, String> hMap, 
 			TextField passwordTextField, ComboBox loginComboBox) {
 		this.hMap = hMap;
 		this.passwordTextField = passwordTextField;
 		this.loginComboBox = loginComboBox; 
	}
	@Override
	public void valueChange(ValueChangeEvent event) {
		String login = event.getProperty().toString();
		String password = new String("");
		
        if(hMap != null && hMap.size() != 0) {
        	Set keys = hMap.keySet();
        	for(Object o : keys) {
        		String keyValue = o.toString();        		
        		if(login != null && login.equals(keyValue)) { 
        			password = hMap.get(o.toString());
                	break;
        		}        		
        	}
			loginComboBox.setValue(login);
        	passwordTextField.setValue(password);
        }
	}

...

Seems to work.:smiley:

You are asking for trouble by using URL encoding/decoding. You never know when the cookie can be added to a URL by some component, and then things will break. I do suggest you use Base64…

I welcome you, Jean-François Lamy!

Well, I’ll follow your suggestion. :smiley:

Cheers,
Thx