/* BridgeFix.java

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		2013/10/30 , Created by dennis
}}IS_NOTE

Copyright (C) 2013 Potix Corporation. All Rights Reserved.

{{IS_RIGHT
}}IS_RIGHT
*/
package org.zkoss.zssex.ui.impl;

import java.io.Serializable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.zkoss.util.Locales;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.WebApp;
import org.zkoss.zk.ui.http.I18Ns;
import org.zkoss.zk.ui.http.WebManager;
import org.zkoss.zkplus.embed.Bridge;

/**
 * // Workaround for ZK-1990 Can't get expected Local in Bridge
 * @author dennis
 *
 */
public class BridgeFix implements Serializable {
	private static final long serialVersionUID = 1299100229849991820L;
	
	Object _oldI18NObj;
	Bridge _bridge;
	HttpServletRequest _request;
	
	public Bridge start(ServletContext svlctx,
			HttpServletRequest request, HttpServletResponse response,
			Desktop desktop) {
		_request = request;
		Bridge bridge = Bridge.start(svlctx, request, response, desktop);
		if(Locales.getThreadLocal()==null){//it is possible to be fixed after 6.5.4
			//code refers Renders
			final Session sess = WebManager.getSession(svlctx, request);
			final WebApp wapp = sess.getWebApp();
			_oldI18NObj = I18Ns.setup(sess, request, response,
					wapp.getConfiguration().getResponseCharset());
		}
		return _bridge = bridge;
	}
	
	public String getResult(){
		return _bridge.getResult();
	}
	
	public void close(){
		if(_oldI18NObj!=null){
			I18Ns.cleanup(_request, _oldI18NObj);
			_oldI18NObj = null;
		}
		_bridge.close();
		_bridge = null;
		_request = null;
	}
}
