From eeada6f508cc7705a8102a61a300706f8646c0f2 Mon Sep 17 00:00:00 2001 From: Kelven Yang Date: Mon, 16 May 2011 17:59:24 -0700 Subject: [PATCH] Letting console-proxy reflect back browser accept-language to help loading OS related keyboard --- console-proxy/js/ajaxviewer.js | 243 ++++++++++++------ .../consoleproxy/ConsoleProxyAjaxHandler.java | 30 ++- .../consoleproxy/ConsoleProxyViewer.java | 18 +- 3 files changed, 199 insertions(+), 92 deletions(-) diff --git a/console-proxy/js/ajaxviewer.js b/console-proxy/js/ajaxviewer.js index 19707a55d41..88d13862aea 100644 --- a/console-proxy/js/ajaxviewer.js +++ b/console-proxy/js/ajaxviewer.js @@ -53,6 +53,19 @@ StringBuilder.prototype = { } }; + +function getCurrentLanguage() { + if(acceptLanguages) { + var tokens = acceptLanguages.split(','); + if(tokens.length > 0) + return tokens[0]; + + return "en-us"; + } else { + return "en-us"; + } +} + ///////////////////////////////////////////////////////////////////////////// // class KeyboardMapper // @@ -435,84 +448,164 @@ AjaxViewer.prototype = { var charCodeMap = []; var shiftedCharCodeMap = []; - shiftedKeyCodeMap[50] = { code: 222, shift: 1 } ; // JP SHIFT + 2 -> " - shiftedCharCodeMap[64] = { code: 34, shift: 1 }; - - shiftedKeyCodeMap[54] = { code: 55, shift : 1 }; // JP SHIFT + 6 -> & - shiftedCharCodeMap[94] = { code: 38, shift : 1 }; - - shiftedKeyCodeMap[55] = { code: 222, shift : 0 }; // JP SHIFT + 7 -> ' - shiftedCharCodeMap[38] = { code: 39, shift : 0 }; - - shiftedKeyCodeMap[56] = { code: 57, shift : 1 }; // JP SHIFT + 8 -> ( - shiftedCharCodeMap[42] = { code: 40, shift : 1 }; - - shiftedKeyCodeMap[57] = { code: 48, shift : 1 }; // JP SHIFT + 9 -> ) - shiftedCharCodeMap[40] = { code: 41, shift : 1 }; - - shiftedKeyCodeMap[48] = { code: 192, shift : 1 }; // JP SHIFT + 0 -> ~ - shiftedCharCodeMap[41] = { code: 126, shift : 1 }; - - shiftedKeyCodeMap[109] = { code: 107, shift : 0 }; // JP SHIFT + (-=), keycode/charcode(109, 95) from Firefox - shiftedCharCodeMap[95] = { code: 61, shift : 0 }; - - shiftedKeyCodeMap[189] = { code: 107, shift : 0 }; // JP SHIFT + (-=), keycode/charcode(109, 95) from Chrome/Safari/MSIE - shiftedCharCodeMap[95] = { code: 61, shift : 0 }; - - if($.browser.mozilla) { - keyCodeMap[107] = { code: 107, shift : 1, defer : true }; // JP NUM +, keycode/charcode (107, 43) from Firefox - charCodeMap[43] = { code: 43, shift : 1, keyCode: 107 }; - charCodeMap[61] = { code: 94, shift : 1, keyCode: 54 }; // JP (-^), keycode/charcode (107, 61) from Firefox + var currentLanguage = getCurrentLanguage(); + if(currentLanguage == 'ja') { + shiftedKeyCodeMap[50] = { code: 222, shift: 1, defer: true }; // JP SHIFT + 2 -> " + shiftedCharCodeMap[34] = { code: 34, shift : 1, keyCode: 222 }; - shiftedKeyCodeMap[107] = { code: 109, shift : 1 }; // JP SHIFT + (-^) - shiftedCharCodeMap[43] = { code: 95, shift : 1 }; - } else { - keyCodeMap[187] = { code: 54, shift: 1}; // JP -^ - charCodeMap[61] = { code: 94, shift: 1}; + shiftedKeyCodeMap[54] = { code: 55, shift : 1 }; // JP SHIFT + 6 -> & + shiftedCharCodeMap[94] = { code: 38, shift : 1 }; - shiftedKeyCodeMap[187] = { code: 109, shift : 1 }; // JP SHIFT + (-^) - shiftedCharCodeMap[43] = { code: 95, shift : 1 }; - } - - shiftedKeyCodeMap[255] = { code: 220, shift : 1, charCode: 124 }; // JP (|-, key before backspace) - - keyCodeMap[219] = { code: 50, shift : 1 }; // JP @` - charCodeMap[91] = { code: 64, shift : 1 }; - shiftedKeyCodeMap[219] = { code: 192, shift : 0 }; // JP SHIFT + (@`) - shiftedCharCodeMap[123] = { code: 96, shift : 0 }; - - keyCodeMap[221] = { code: 219, shift : 0 }; // JP [{ - charCodeMap[93] = { code: 91, shift : 0 }; - shiftedKeyCodeMap[221] = { code: 219, shift : 1 }; - shiftedCharCodeMap[125] = { code: 123, shift : 1 }; - - if($.browser.mozilla) { - shiftedKeyCodeMap[59] = { code: 107, shift : 1 }; // JP ;+ - shiftedCharCodeMap[58] = { code: 43, shift : 1 }; + shiftedKeyCodeMap[55] = { code: 222, shift : 0 }; // JP SHIFT + 7 -> ' + shiftedCharCodeMap[38] = { code: 39, shift : 0 }; + + shiftedKeyCodeMap[56] = { code: 57, shift : 1 }; // JP SHIFT + 8 -> ( + shiftedCharCodeMap[42] = { code: 40, shift : 1 }; + + shiftedKeyCodeMap[57] = { code: 48, shift : 1 }; // JP SHIFT + 9 -> ) + shiftedCharCodeMap[40] = { code: 41, shift : 1 }; + + shiftedKeyCodeMap[48] = { code: 192, shift : 1 }; // JP SHIFT + 0 -> ~ + shiftedCharCodeMap[41] = { code: 126, shift : 1 }; + + shiftedKeyCodeMap[109] = { code: 107, shift : 0 }; // JP SHIFT + (-=), keycode/charcode(109, 95) from Firefox + shiftedCharCodeMap[95] = { code: 61, shift : 0 }; + + shiftedKeyCodeMap[189] = { code: 107, shift : 0 }; // JP SHIFT + (-=), keycode/charcode(109, 95) from Chrome/Safari/MSIE + shiftedCharCodeMap[95] = { code: 61, shift : 0 }; + + if($.browser.mozilla) { + keyCodeMap[107] = { code: 107, shift : 1, defer : true }; // JP NUM +, keycode/charcode (107, 43) from Firefox + charCodeMap[43] = { code: 43, shift : 1, keyCode: 107 }; + charCodeMap[61] = { code: 94, shift : 1, keyCode: 54 }; // JP (-^), keycode/charcode (107, 61) from Firefox + + shiftedKeyCodeMap[107] = { code: 109, shift : 1 }; // JP SHIFT + (-^) + shiftedCharCodeMap[43] = { code: 95, shift : 1 }; + } else { + keyCodeMap[187] = { code: 54, shift: 1}; // JP -^ + charCodeMap[61] = { code: 94, shift: 1}; + + shiftedKeyCodeMap[187] = { code: 109, shift : 1 }; // JP SHIFT + (-^) + shiftedCharCodeMap[43] = { code: 95, shift : 1 }; + } + + shiftedKeyCodeMap[255] = { code: 220, shift : 1, charCode: 124 }; // JP (|-, key before backspace) + + keyCodeMap[219] = { code: 50, shift : 1 }; // JP @` + charCodeMap[91] = { code: 64, shift : 1 }; + shiftedKeyCodeMap[219] = { code: 192, shift : 0 }; // JP SHIFT + (@`) + shiftedCharCodeMap[123] = { code: 96, shift : 0 }; + + keyCodeMap[221] = { code: 219, shift : 0 }; // JP [{ + charCodeMap[93] = { code: 91, shift : 0 }; + shiftedKeyCodeMap[221] = { code: 219, shift : 1 }; + shiftedCharCodeMap[125] = { code: 123, shift : 1 }; + + if($.browser.mozilla) { + shiftedKeyCodeMap[59] = { code: 107, shift : 1 }; // JP ;+ + shiftedCharCodeMap[58] = { code: 43, shift : 1 }; + } else { + shiftedKeyCodeMap[186] = { code: 107, shift : 1 }; // JP ;+ + shiftedCharCodeMap[58] = { code: 43, shift : 1 }; + } + + keyCodeMap[222] = { code: 59, shift : 1 }; // JP :* + charCodeMap[39] = { code: 58, shift : 1 }; + shiftedKeyCodeMap[222] = { code: 56, shift : 1 }; + // shiftedCharCodeMap[34] = { code: 42, shift : 1 }; + + keyCodeMap[220] = { code: 221, shift : 0 }; // JP ]} + charCodeMap[92] = { code: 93, shift : 0 }; + shiftedKeyCodeMap[220] = { code: 221, shift : 1 }; + shiftedCharCodeMap[124] = { code: 125, shift : 1 }; + + keyCodeMap[193] = { code: 220, shift : 0, charCode: 92 }; // JP \-| + shiftedKeyCodeMap[193] = { code: 109, shift : 0, charCode : 45 }; + + keyCodeMap[106] = { code: 56, shift : 1 }; // JP NUM * + charCodeMap[42] = { code: 42, shift : 1 }; + + keyCodeMap[110] = { code: 190, shift : 0 }; // JP NUM . + charCodeMap[46] = { code: 46, shift : 0 }; } else { - shiftedKeyCodeMap[186] = { code: 107, shift : 1 }; // JP ;+ - shiftedCharCodeMap[58] = { code: 43, shift : 1 }; + shiftedKeyCodeMap[50] = { code: 222, shift: 1 } ; // JP SHIFT + 2 -> " + shiftedCharCodeMap[64] = { code: 34, shift: 1 }; + + shiftedKeyCodeMap[54] = { code: 55, shift : 1 }; // JP SHIFT + 6 -> & + shiftedCharCodeMap[94] = { code: 38, shift : 1 }; + + shiftedKeyCodeMap[55] = { code: 222, shift : 0 }; // JP SHIFT + 7 -> ' + shiftedCharCodeMap[38] = { code: 39, shift : 0 }; + + shiftedKeyCodeMap[56] = { code: 57, shift : 1 }; // JP SHIFT + 8 -> ( + shiftedCharCodeMap[42] = { code: 40, shift : 1 }; + + shiftedKeyCodeMap[57] = { code: 48, shift : 1 }; // JP SHIFT + 9 -> ) + shiftedCharCodeMap[40] = { code: 41, shift : 1 }; + + shiftedKeyCodeMap[48] = { code: 192, shift : 1 }; // JP SHIFT + 0 -> ~ + shiftedCharCodeMap[41] = { code: 126, shift : 1 }; + + shiftedKeyCodeMap[109] = { code: 107, shift : 0 }; // JP SHIFT + (-=), keycode/charcode(109, 95) from Firefox + shiftedCharCodeMap[95] = { code: 61, shift : 0 }; + + shiftedKeyCodeMap[189] = { code: 107, shift : 0 }; // JP SHIFT + (-=), keycode/charcode(109, 95) from Chrome/Safari/MSIE + shiftedCharCodeMap[95] = { code: 61, shift : 0 }; + + if($.browser.mozilla) { + keyCodeMap[107] = { code: 107, shift : 1, defer : true }; // JP NUM +, keycode/charcode (107, 43) from Firefox + charCodeMap[43] = { code: 43, shift : 1, keyCode: 107 }; + charCodeMap[61] = { code: 94, shift : 1, keyCode: 54 }; // JP (-^), keycode/charcode (107, 61) from Firefox + + shiftedKeyCodeMap[107] = { code: 109, shift : 1 }; // JP SHIFT + (-^) + shiftedCharCodeMap[43] = { code: 95, shift : 1 }; + } else { + keyCodeMap[187] = { code: 54, shift: 1}; // JP -^ + charCodeMap[61] = { code: 94, shift: 1}; + + shiftedKeyCodeMap[187] = { code: 109, shift : 1 }; // JP SHIFT + (-^) + shiftedCharCodeMap[43] = { code: 95, shift : 1 }; + } + + shiftedKeyCodeMap[255] = { code: 220, shift : 1, charCode: 124 }; // JP (|-, key before backspace) + + keyCodeMap[219] = { code: 50, shift : 1 }; // JP @` + charCodeMap[91] = { code: 64, shift : 1 }; + shiftedKeyCodeMap[219] = { code: 192, shift : 0 }; // JP SHIFT + (@`) + shiftedCharCodeMap[123] = { code: 96, shift : 0 }; + + keyCodeMap[221] = { code: 219, shift : 0 }; // JP [{ + charCodeMap[93] = { code: 91, shift : 0 }; + shiftedKeyCodeMap[221] = { code: 219, shift : 1 }; + shiftedCharCodeMap[125] = { code: 123, shift : 1 }; + + if($.browser.mozilla) { + shiftedKeyCodeMap[59] = { code: 107, shift : 1 }; // JP ;+ + shiftedCharCodeMap[58] = { code: 43, shift : 1 }; + } else { + shiftedKeyCodeMap[186] = { code: 107, shift : 1 }; // JP ;+ + shiftedCharCodeMap[58] = { code: 43, shift : 1 }; + } + + keyCodeMap[222] = { code: 59, shift : 1 }; // JP :* + charCodeMap[39] = { code: 58, shift : 1 }; + shiftedKeyCodeMap[222] = { code: 56, shift : 1 }; + shiftedCharCodeMap[34] = { code: 42, shift : 1 }; + + keyCodeMap[220] = { code: 221, shift : 0 }; // JP ]} + charCodeMap[92] = { code: 93, shift : 0 }; + shiftedKeyCodeMap[220] = { code: 221, shift : 1 }; + shiftedCharCodeMap[124] = { code: 125, shift : 1 }; + + keyCodeMap[193] = { code: 220, shift : 0, charCode: 92 }; // JP \-| + shiftedKeyCodeMap[193] = { code: 109, shift : 0, charCode : 45 }; + + keyCodeMap[106] = { code: 56, shift : 1 }; // JP NUM * + charCodeMap[42] = { code: 42, shift : 1 }; + + keyCodeMap[110] = { code: 190, shift : 0 }; // JP NUM . + charCodeMap[46] = { code: 46, shift : 0 }; } - - keyCodeMap[222] = { code: 59, shift : 1 }; // JP :* - charCodeMap[39] = { code: 58, shift : 1 }; - shiftedKeyCodeMap[222] = { code: 56, shift : 1 }; - shiftedCharCodeMap[34] = { code: 42, shift : 1 }; - - keyCodeMap[220] = { code: 221, shift : 0 }; // JP ]} - charCodeMap[92] = { code: 93, shift : 0 }; - shiftedKeyCodeMap[220] = { code: 221, shift : 1 }; - shiftedCharCodeMap[124] = { code: 125, shift : 1 }; - - keyCodeMap[193] = { code: 220, shift : 0, charCode: 92 }; // JP \-| - shiftedKeyCodeMap[193] = { code: 109, shift : 0, charCode : 45 }; - - keyCodeMap[106] = { code: 56, shift : 1 }; // JP NUM * - charCodeMap[42] = { code: 42, shift : 1 }; - - keyCodeMap[110] = { code: 190, shift : 0 }; // JP NUM . - charCodeMap[46] = { code: 46, shift : 0 }; - this.keyboardMappers[AjaxViewer.KEYBOARD_TYPE_JAPANESE] = new KeyboardMapper(false, keyCodeMap, shiftedKeyCodeMap, charCodeMap, shiftedCharCodeMap); }, @@ -578,6 +671,7 @@ AjaxViewer.prototype = { if(!g_logger.isOpen()) { g_logger.enable(true); g_logger.open(); + g_logger.log(Logger.LEVEL_SYS, "Accept languages: " + acceptLanguages + ", current language: " + getCurrentLanguage()); } else { g_logger.close(); } @@ -669,6 +763,7 @@ AjaxViewer.prototype = { if(!g_logger.isOpen()) { g_logger.enable(true); g_logger.open(); + g_logger.log(Logger.LEVEL_SYS, "Accept languages: " + acceptLanguages + ", current language: " + getCurrentLanguage()); } else { g_logger.close(); } diff --git a/console-proxy/src/com/cloud/consoleproxy/ConsoleProxyAjaxHandler.java b/console-proxy/src/com/cloud/consoleproxy/ConsoleProxyAjaxHandler.java index 1d9aa7fe944..b1c3f7359cb 100644 --- a/console-proxy/src/com/cloud/consoleproxy/ConsoleProxyAjaxHandler.java +++ b/console-proxy/src/com/cloud/consoleproxy/ConsoleProxyAjaxHandler.java @@ -18,19 +18,20 @@ package com.cloud.consoleproxy; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.URLDecoder; -import java.util.HashMap; -import java.util.Map; - +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.URLDecoder; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import com.cloud.console.Logger; -import com.sun.net.httpserver.Headers; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; public class ConsoleProxyAjaxHandler implements HttpHandler { private static final Logger s_logger = Logger.getLogger(ConsoleProxyAjaxHandler.class); @@ -388,8 +389,9 @@ public class ConsoleProxyAjaxHandler implements HttpHandler { } } - private void handleClientStart(HttpExchange t, ConsoleProxyViewer viewer, String title) throws IOException { - String response = viewer.onAjaxClientStart(title); + private void handleClientStart(HttpExchange t, ConsoleProxyViewer viewer, String title) throws IOException { + List languages = t.getRequestHeaders().get("Accept-Language"); + String response = viewer.onAjaxClientStart(title, languages); Headers hds = t.getResponseHeaders(); hds.set("Content-Type", "text/html"); diff --git a/console-proxy/src/com/cloud/consoleproxy/ConsoleProxyViewer.java b/console-proxy/src/com/cloud/consoleproxy/ConsoleProxyViewer.java index 7bef0b1ace4..4585b57387d 100644 --- a/console-proxy/src/com/cloud/consoleproxy/ConsoleProxyViewer.java +++ b/console-proxy/src/com/cloud/consoleproxy/ConsoleProxyViewer.java @@ -921,7 +921,7 @@ public class ConsoleProxyViewer implements java.lang.Runnable, RfbViewer, RfbPro "

"; } - public String onAjaxClientStart(String title) { + public String onAjaxClientStart(String title, List languages) { if(!waitForViewerReady()) return onAjaxClientConnectFailed(); @@ -1002,11 +1002,21 @@ public class ConsoleProxyViewer implements java.lang.Runnable, RfbViewer, RfbPro */ return getAjaxViewerPageContent(sbTileSequence.toString(), imgUrl, updateUrl, width, height, tileWidth, tileHeight, title, - ConsoleProxy.keyboardType == ConsoleProxy.KEYBOARD_RAW); + ConsoleProxy.keyboardType == ConsoleProxy.KEYBOARD_RAW, languages); } private String getAjaxViewerPageContent(String tileSequence, String imgUrl, String updateUrl, int width, - int height, int tileWidth, int tileHeight, String title, boolean rawKeyboard) { + int height, int tileWidth, int tileHeight, String title, boolean rawKeyboard, List languages) { + + StringBuffer sbLanguages = new StringBuffer(""); + if(languages != null) { + for(String lang : languages) { + if(sbLanguages.length() > 0) { + sbLanguages.append(","); + } + sbLanguages.append(lang); + } + } String[] content = new String[] { "", @@ -1047,7 +1057,7 @@ public class ConsoleProxyViewer implements java.lang.Runnable, RfbViewer, RfbPro "", "
", "