Search in sources :

Example 1 with AppendingStringBuffer

use of com.servoy.j2db.util.AppendingStringBuffer in project servoy-client by Servoy.

the class NGClient method initFromClientBrowserinformation.

private void initFromClientBrowserinformation() {
    Object retValue;
    try {
        retValue = this.getWebsocketSession().getClientService(NGClient.APPLICATION_SERVICE).executeServiceCall("getClientBrowserInformation", null);
    } catch (IOException e) {
        Debug.warn(e);
        return;
    }
    ClientInfo clientInfo = getClientInfo();
    if (clientInfo != null) {
        if (retValue instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) retValue;
            String url = jsonObject.optString("serverURL");
            if (url != null) {
                try {
                    serverURL = new URL(url);
                } catch (MalformedURLException e) {
                    Debug.error(e);
                }
            }
            String userAgent = jsonObject.optString("userAgent");
            if (userAgent != null) {
                clientInfo.addInfo("useragent:" + userAgent);
            }
            String platform = jsonObject.optString("platform");
            if (platform != null) {
                clientInfo.addInfo("platform:" + platform);
            }
            String remote_ipaddress = jsonObject.optString("remote_ipaddress");
            if (remote_ipaddress != null) {
                clientInfo.setHostAddress(remote_ipaddress);
            }
            String remote_host = jsonObject.optString("remote_host");
            if (remote_host != null) {
                clientInfo.setHostName(remote_host);
            }
            if (timeZone == null) {
                String clientTimeZone = jsonObject.optString("timeZone");
                if (clientTimeZone != null && clientTimeZone.length() > 0) {
                    timeZone = TimeZone.getTimeZone(clientTimeZone);
                } else {
                    String utc = jsonObject.optString("utcOffset");
                    if (utc != null) {
                        // apparently it is platform dependent on whether you get the
                        // offset in a decimal form or not. This parses the decimal
                        // form of the UTC offset, taking into account several
                        // possibilities
                        // such as getting the format in +2.5 or -1.2
                        int dotPos = utc.indexOf('.');
                        if (dotPos >= 0) {
                            String hours = utc.substring(0, dotPos);
                            String hourPart = utc.substring(dotPos + 1);
                            if (hours.startsWith("+")) {
                                hours = hours.substring(1);
                            }
                            int offsetHours = Integer.parseInt(hours);
                            int offsetMins = (int) (Double.parseDouble(hourPart) * 6);
                            // construct a GMT timezone offset string from the retrieved
                            // offset which can be parsed by the TimeZone class.
                            AppendingStringBuffer sb = new AppendingStringBuffer("GMT");
                            sb.append(offsetHours > 0 ? "+" : "-");
                            sb.append(Math.abs(offsetHours));
                            sb.append(":");
                            if (offsetMins < 10) {
                                sb.append("0");
                            }
                            sb.append(offsetMins);
                            timeZone = TimeZone.getTimeZone(sb.toString());
                        } else {
                            int offset = Integer.parseInt(utc);
                            if (offset < 0) {
                                utc = utc.substring(1);
                            }
                            timeZone = TimeZone.getTimeZone("GMT" + ((offset > 0) ? "+" : "-") + utc);
                        }
                        String dstOffset = jsonObject.optString("utcDstOffset");
                        if (timeZone != null && dstOffset != null) {
                            TimeZone dstTimeZone = null;
                            dotPos = dstOffset.indexOf('.');
                            if (dotPos >= 0) {
                                String hours = dstOffset.substring(0, dotPos);
                                String hourPart = dstOffset.substring(dotPos + 1);
                                if (hours.startsWith("+")) {
                                    hours = hours.substring(1);
                                }
                                int offsetHours = Integer.parseInt(hours);
                                int offsetMins = (int) (Double.parseDouble(hourPart) * 6);
                                // construct a GMT timezone offset string from the
                                // retrieved
                                // offset which can be parsed by the TimeZone class.
                                AppendingStringBuffer sb = new AppendingStringBuffer("GMT");
                                sb.append(offsetHours > 0 ? "+" : "-");
                                sb.append(Math.abs(offsetHours));
                                sb.append(":");
                                if (offsetMins < 10) {
                                    sb.append("0");
                                }
                                sb.append(offsetMins);
                                dstTimeZone = TimeZone.getTimeZone(sb.toString());
                            } else {
                                int offset = Integer.parseInt(dstOffset);
                                if (offset < 0) {
                                    dstOffset = dstOffset.substring(1);
                                }
                                dstTimeZone = TimeZone.getTimeZone("GMT" + ((offset > 0) ? "+" : "-") + dstOffset);
                            }
                            // the real time zone (1 January) try to combine the 2.
                            if (dstTimeZone != null && dstTimeZone.getRawOffset() != timeZone.getRawOffset()) {
                                int dstSaving = dstTimeZone.getRawOffset() - timeZone.getRawOffset();
                                String[] availableIDs = TimeZone.getAvailableIDs(timeZone.getRawOffset());
                                for (String availableID : availableIDs) {
                                    TimeZone zone = TimeZone.getTimeZone(availableID);
                                    if (zone.getDSTSavings() == dstSaving) {
                                        // this is a best guess... still the start and end of the DST should
                                        // be needed to know to be completely correct, or better yet
                                        // not just the GMT offset but the TimeZone ID should be transfered
                                        // from the browser.
                                        timeZone = zone;
                                        break;
                                    }
                                }
                            }
                            // if the timezone is really just the default of the server just use that one.
                            TimeZone dftZone = TimeZone.getDefault();
                            if (timeZone.getRawOffset() == dftZone.getRawOffset() && timeZone.getDSTSavings() == dftZone.getDSTSavings()) {
                                timeZone = dftZone;
                            }
                        }
                    }
                }
            }
            if (locale == null) {
                String browserLocale = jsonObject.optString("locale");
                if (browserLocale != null) {
                    String[] languageAndCountry = browserLocale.split("-");
                    if (languageAndCountry.length == 1) {
                        locale = new Locale(languageAndCountry[0]);
                    } else if (languageAndCountry.length == 2) {
                        locale = new Locale(languageAndCountry[0], languageAndCountry[1]);
                    }
                    clientInfo.addInfo("locale:" + locale);
                }
            }
        }
        if (timeZone != null) {
            clientInfo.setTimeZone(timeZone);
        }
        clientInfo.addInfo("session key: " + getWebsocketSession().getSessionKey());
        try {
            getClientHost().pushClientInfo(clientInfo.getClientId(), clientInfo);
        } catch (RemoteException e) {
            Debug.error(e);
        }
    }
}
Also used : Locale(java.util.Locale) MalformedURLException(java.net.MalformedURLException) AppendingStringBuffer(com.servoy.j2db.util.AppendingStringBuffer) IOException(java.io.IOException) URL(java.net.URL) TimeZone(java.util.TimeZone) JSONObject(org.json.JSONObject) BaseWebObject(org.sablo.BaseWebObject) JSONObject(org.json.JSONObject) ClientInfo(com.servoy.j2db.dataprocessing.ClientInfo) RemoteException(java.rmi.RemoteException)

Aggregations

ClientInfo (com.servoy.j2db.dataprocessing.ClientInfo)1 AppendingStringBuffer (com.servoy.j2db.util.AppendingStringBuffer)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 RemoteException (java.rmi.RemoteException)1 Locale (java.util.Locale)1 TimeZone (java.util.TimeZone)1 JSONObject (org.json.JSONObject)1 BaseWebObject (org.sablo.BaseWebObject)1