Skip to content

Commit 0a88d05

Browse files
committed
Refactoring locale MessageBuilder
1 parent fa2b978 commit 0a88d05

File tree

7 files changed

+33
-288
lines changed

7 files changed

+33
-288
lines changed

knowage-core/src/main/java/it/eng/spagobi/commons/services/LoginActionByToken.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
package it.eng.spagobi.commons.services;
1919

2020
import java.io.IOException;
21-
import java.util.Locale;
2221

2322
import org.apache.commons.lang3.StringUtils;
2423
import org.apache.log4j.Logger;
2524
import org.json.JSONObject;
2625

27-
import it.eng.spago.base.Constants;
2826
import it.eng.spago.base.SourceBean;
2927
import it.eng.spago.configuration.ConfigSingleton;
3028
import it.eng.spago.security.IEngUserProfile;
@@ -35,7 +33,6 @@
3533
import it.eng.spagobi.commons.utilities.AuditLogUtilities;
3634
import it.eng.spagobi.commons.utilities.SpagoBIServiceExceptionHandler;
3735
import it.eng.spagobi.commons.utilities.UserUtilities;
38-
import it.eng.spagobi.commons.utilities.messages.MessageBuilder;
3936
import it.eng.spagobi.services.security.bo.SpagoBIUserProfile;
4037
import it.eng.spagobi.services.security.service.ISecurityServiceSupplier;
4138
import it.eng.spagobi.services.security.service.SecurityServiceSupplierFactory;
@@ -152,13 +149,6 @@ public void service(SourceBean request, SourceBean response) throws SpagoBIServi
152149
getHttpSession().setAttribute(SpagoBIConstants.BACK_URL, backUrl);
153150
}
154151

155-
// Propagate locale
156-
Locale locale = MessageBuilder.getBrowserLocaleFromSpago();
157-
logger.debug("User [" + userId + "] loacale has been set to [" + locale.getLanguage() + "/" + locale.getCountry() + "]");
158-
if (locale != null) {
159-
getSessionContainer().getPermanentContainer().setAttribute(Constants.USER_LANGUAGE, locale.getLanguage());
160-
getSessionContainer().getPermanentContainer().setAttribute(Constants.USER_COUNTRY, locale.getCountry());
161-
}
162152

163153
try {
164154
JSONObject results = new JSONObject();

knowage-core/src/main/java/it/eng/spagobi/commons/services/LoginActionWeb.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
package it.eng.spagobi.commons.services;
1919

2020
import java.io.IOException;
21-
import java.util.Locale;
2221

2322
import org.apache.commons.lang3.StringUtils;
2423
import org.apache.log4j.Logger;
2524
import org.json.JSONObject;
2625

27-
import it.eng.spago.base.Constants;
2826
import it.eng.spago.base.SourceBean;
2927
import it.eng.spago.configuration.ConfigSingleton;
3028
import it.eng.spago.security.IEngUserProfile;
@@ -35,7 +33,6 @@
3533
import it.eng.spagobi.commons.utilities.AuditLogUtilities;
3634
import it.eng.spagobi.commons.utilities.SpagoBIServiceExceptionHandler;
3735
import it.eng.spagobi.commons.utilities.UserUtilities;
38-
import it.eng.spagobi.commons.utilities.messages.MessageBuilder;
3936
import it.eng.spagobi.services.security.bo.SpagoBIUserProfile;
4037
import it.eng.spagobi.services.security.service.ISecurityServiceSupplier;
4138
import it.eng.spagobi.services.security.service.SecurityServiceSupplierFactory;
@@ -163,14 +160,6 @@ public void service(SourceBean request, SourceBean response) throws SpagoBIServi
163160
getHttpSession().setAttribute(SpagoBIConstants.BACK_URL, backUrl);
164161
}
165162

166-
// Propagate locale
167-
Locale locale = MessageBuilder.getBrowserLocaleFromSpago();
168-
logger.debug("User [" + usr + "] loacale has been set to [" + locale.getLanguage() + "/" + locale.getCountry() + "]");
169-
if (locale != null) {
170-
getSessionContainer().getPermanentContainer().setAttribute(Constants.USER_LANGUAGE, locale.getLanguage());
171-
getSessionContainer().getPermanentContainer().setAttribute(Constants.USER_COUNTRY, locale.getCountry());
172-
}
173-
174163
try {
175164
JSONObject results = new JSONObject();
176165
results.put("username", userId);

knowage-core/src/main/java/it/eng/spagobi/commons/services/LoginModule.java

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@
3030
import java.util.Iterator;
3131
import java.util.List;
3232
import java.util.Locale;
33-
import java.util.Locale.Builder;
3433
import java.util.Optional;
3534
import java.util.Properties;
3635
import java.util.stream.IntStream;
3736

3837
import javax.servlet.http.HttpServletRequest;
39-
import javax.servlet.http.HttpServletResponse;
4038
import javax.servlet.http.HttpSession;
4139

4240
import org.apache.commons.lang3.StringUtils;
@@ -49,9 +47,6 @@
4947
import it.eng.knowage.monitor.KnowageMonitorFactory;
5048
import it.eng.knowage.privacymanager.LoginEventBuilder;
5149
import it.eng.knowage.privacymanager.PrivacyManagerClient;
52-
import it.eng.spago.base.Constants;
53-
import it.eng.spago.base.RequestContainer;
54-
import it.eng.spago.base.SessionContainer;
5550
import it.eng.spago.base.SourceBean;
5651
import it.eng.spago.base.SourceBeanException;
5752
import it.eng.spago.dispatching.module.AbstractHttpModule;
@@ -72,7 +67,6 @@
7267
import it.eng.spagobi.commons.metadata.SbiExtRoles;
7368
import it.eng.spagobi.commons.metadata.SbiTenant;
7469
import it.eng.spagobi.commons.utilities.AuditLogUtilities;
75-
import it.eng.spagobi.commons.utilities.GeneralUtilities;
7670
import it.eng.spagobi.commons.utilities.HibernateSessionManager;
7771
import it.eng.spagobi.commons.utilities.SpagoBIUtilities;
7872
import it.eng.spagobi.commons.utilities.StringUtilities;
@@ -136,29 +130,11 @@ public void service(SourceBean request, SourceBean response) throws Exception {
136130

137131
IEngUserProfile profile = null;
138132

139-
RequestContainer reqCont = RequestContainer.getRequestContainer();
140-
SessionContainer sessCont = reqCont.getSessionContainer();
141-
SessionContainer permSess = sessCont.getPermanentContainer();
142-
143133
HttpServletRequest servletRequest = getHttpRequest();
144134
HttpSession httpSession = servletRequest.getSession();
145135

146136
String currTheme = ThemesManager.getDefaultTheme();
147137

148-
manageLocale(permSess);
149-
150-
String language = (String) permSess.getAttribute(Constants.USER_LANGUAGE);
151-
String country = (String) permSess.getAttribute(Constants.USER_COUNTRY);
152-
String script = permSess.getAttribute(USER_SCRIPT) != null ? (String) permSess.getAttribute(USER_SCRIPT) : null;
153-
Builder tmpLocale = new Builder().setLanguage(language).setRegion(country);
154-
if (StringUtils.isNotBlank(script)) {
155-
tmpLocale.setScript(script);
156-
}
157-
Locale browserLocale = tmpLocale.build();
158-
159-
String localeCookie = browserLocale.toLanguageTag();
160-
HttpServletResponse resp = getHttpResponse();
161-
162138
MessageBuilder msgBuilder = new MessageBuilder();
163139
Locale locale = msgBuilder.getLocale(servletRequest);
164140

@@ -552,28 +528,6 @@ private boolean isSSOActive() {
552528
return activeSoo;
553529
}
554530

555-
private void manageLocale(SessionContainer permSess) {
556-
// updates locale information on permanent container for Spago messages mechanism
557-
// search firstly if a default language is set on configuraiton file, else take browser from spago
558-
if (permSess.getAttribute(Constants.USER_LANGUAGE) == null || permSess.getAttribute(Constants.USER_COUNTRY) == null) {
559-
logger.debug("getting locale...");
560-
Locale locale = GeneralUtilities.getStartingDefaultLocale();
561-
if (locale == null) {
562-
locale = MessageBuilder.getBrowserLocaleFromSpago();
563-
} else {
564-
logger.debug("Locale " + locale.getLanguage() + " - " + locale.getCountry() + " taken as default from configuraiton file");
565-
}
566-
if (locale != null) {
567-
logger.debug("locale taken as default is " + locale.getLanguage() + " - " + locale.getCountry());
568-
permSess.setAttribute(Constants.USER_LANGUAGE, locale.getLanguage());
569-
permSess.setAttribute(Constants.USER_COUNTRY, locale.getCountry());
570-
permSess.setAttribute(USER_SCRIPT, locale.getScript());
571-
}
572-
} else {
573-
logger.debug("locale already found in session");
574-
}
575-
}
576-
577531
private HttpSession regenerateSession(HttpServletRequest request) {
578532

579533
HttpSession oldSession = request.getSession();

knowage/src/main/webapp/WEB-INF/jsp/commons/angular/sbiModule.jspf

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ sbiM.factory('sbiModule_config',function(){
4646
<%-- currTheme: '<%= currTheme %>', --%>
4747
<%-- supportedLocales: <%= GeneralUtilities.getSupportedLocalesAsJSONArray().toString() %>, --%>
4848
dateFormat: '<%= GeneralUtilities.getLocaleDateFormat(permanentSession) %>', // the date format localized according to user language and country
49-
localizedDateFormat: '<%= GeneralUtilities.getLocaleDateFormatForExtJs(permanentSession) %>', // the date format localized according to user language and country
50-
localizedTimestampFormat: '<%= GeneralUtilities.getLocaleDateFormatForExtJs(permanentSession) %> H:i:s', // the timestamp format localized according to user language and country
51-
clientServerDateFormat: '<%= GeneralUtilities.getServerDateFormatExtJs() %>', // the date format to be used when communicating with server
49+
clientServerDateFormat: '<%= GeneralUtilities.getServerDateFormatExtJs() %>', // the date format to be used when communicating with server
5250
clientServerTimestampFormat: '<%= GeneralUtilities.getServerTimestampFormatExtJs() %>', // the timestamp format to be used when communicating with server
5351
engineUrls:<%= aServiceResponse.getAttribute("engineUrls") %>,
5452
serverDateFormat: '<%= GeneralUtilities.getServerDateFormat() %>',
@@ -86,7 +84,7 @@ sbiM.factory('sbiModule_config',function(){
8684
});
8785

8886
sbiM.config(function($mdDateLocaleProvider) {
89-
this.localizedDateFormat= '<%= GeneralUtilities.getLocaleDateFormatForExtJs(permanentSession) %>'; // the date format localized according to user language and country
87+
this.localizedDateFormat= '<%= GeneralUtilities.getLocaleDateFormatForExtJs() %>'; // the date format localized according to user language and country
9088

9189
$mdDateLocaleProvider.parseDate = function(date) {
9290
if (typeof date == "string" && date.length > 0){

knowage/src/main/webapp/WEB-INF/jsp/commons/portlet_base410.jsp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
285285
contextName: '<%= KnowageSystemConfiguration.getKnowageContext() %>',
286286
adapterPath: '<%= KnowageSystemConfiguration.getKnowageContext() + GeneralUtilities.getSpagoAdapterHttpUrl() %>',
287287
supportedLocales: <%= GeneralUtilities.getSupportedLocalesAsJSONArray().toString() %>,
288-
// the date format localized according to user language and country
289-
localizedDateFormat: '<%= GeneralUtilities.getLocaleDateFormatForExtJs(permanentSession) %>',
290-
// the timestamp format localized according to user language and country
291-
localizedTimestampFormat: '<%= GeneralUtilities.getLocaleDateFormatForExtJs(permanentSession) %> H:i:s',
292288
// the date format to be used when communicating with server
293289
clientServerDateFormat: '<%= GeneralUtilities.getServerDateFormatExtJs() %>',
294290
// the timestamp format to be used when communicating with server

knowagedao/src/main/java/it/eng/spagobi/commons/utilities/GeneralUtilities.java

Lines changed: 10 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,9 @@
4646

4747
import it.eng.knowage.commons.security.KnowageSystemConfiguration;
4848
import it.eng.knowage.security.OwaspDefaultEncoderFactory;
49-
import it.eng.spago.base.RequestContainer;
50-
import it.eng.spago.base.SessionContainer;
5149
import it.eng.spago.security.IEngUserProfile;
5250
import it.eng.spagobi.commons.SingletonConfig;
5351
import it.eng.spagobi.commons.bo.Config;
54-
import it.eng.spagobi.commons.constants.SpagoBIConstants;
5552
import it.eng.spagobi.commons.dao.DAOFactory;
5653
import it.eng.spagobi.commons.dao.IConfigDAO;
5754
import it.eng.spagobi.commons.utilities.messages.IMessageBuilder;
@@ -136,15 +133,18 @@ private static String replaceInternationalizedMessages(String message, int start
136133
key = splitted[0].trim();
137134
if (splitted.length == 1) {
138135
String replacement = msgBuilder.getMessage(key, bundle);
139-
if (!replacement.equalsIgnoreCase(key))
136+
if (!replacement.equalsIgnoreCase(key)) {
140137
message = message.replaceAll("\\$\\{" + toBeReplaced + "\\}", replacement);
138+
}
141139
}
142140
if (splitted.length == 2) {
143-
if (splitted[1] != null && !splitted[1].trim().equals(""))
141+
if (splitted[1] != null && !splitted[1].trim().equals("")) {
144142
bundle = splitted[1].trim();
143+
}
145144
String replacement = msgBuilder.getMessage(key, bundle);
146-
if (!replacement.equalsIgnoreCase(key))
145+
if (!replacement.equalsIgnoreCase(key)) {
147146
message = message.replaceAll("\\$\\{" + toBeReplaced + "\\}", replacement);
147+
}
148148
}
149149
}
150150
startIndex = message.indexOf("${", endIndex);
@@ -229,26 +229,6 @@ public static String getSpagoAdapterHttpUrl() {
229229
return adapUrlStr;
230230
}
231231

232-
/**
233-
* Gets the default locale from SpagoBI configuration file, the behaviors is the same of getDefaultLocale() function, with difference that if not finds returns
234-
* null
235-
*
236-
* TODO : merge its behaviour with GetDefaultLocale (not done know cause today is release date). Gets the default locale.
237-
*
238-
* @return the default locale
239-
*/
240-
public static Locale getStartingDefaultLocale() {
241-
LOGGER.trace("Getting starting default locale");
242-
Locale locale = null;
243-
SingletonConfig config = SingletonConfig.getInstance();
244-
String languageConfig = config.getConfigValue("SPAGOBI.LANGUAGE_SUPPORTED.LANGUAGE.default");
245-
if (languageConfig != null) {
246-
locale = Locale.forLanguageTag(languageConfig);
247-
}
248-
LOGGER.trace("Locale is: {}", locale);
249-
return locale;
250-
}
251-
252232
/**
253233
* Gets the default locale.
254234
*
@@ -344,69 +324,6 @@ public static JSONArray getSupportedLocalesAsJSONArray() {
344324
return ret;
345325
}
346326

347-
public static Locale getCurrentLocale(RequestContainer requestContainer) {
348-
LOGGER.trace("Getting current locale from request");
349-
Locale ret = null;
350-
if (requestContainer != null) {
351-
SessionContainer permSession = requestContainer.getSessionContainer().getPermanentContainer();
352-
if (permSession != null) {
353-
String languageTag = (String) permSession.getAttribute(SpagoBIConstants.AF_LANGUAGE_TAG);
354-
if (StringUtils.isNotBlank(languageTag)) {
355-
ret = Locale.forLanguageTag(languageTag);
356-
} else {
357-
String language = (String) permSession.getAttribute(SpagoBIConstants.AF_LANGUAGE);
358-
String country = (String) permSession.getAttribute(SpagoBIConstants.AF_COUNTRY);
359-
String script = (String) permSession.getAttribute(SpagoBIConstants.AF_SCRIPT);
360-
361-
ret = new Builder().setLanguage(language).setRegion(country).setScript(script).build();
362-
}
363-
}
364-
}
365-
if (ret == null) {
366-
ret = getDefaultLocale();
367-
}
368-
LOGGER.trace("Current locale from request: {}", ret);
369-
return ret;
370-
}
371-
372-
public static String getLocaleDateFormat(SessionContainer permSess) {
373-
LOGGER.debug("Getting locale date format from session");
374-
String languageTag = (String) permSess.getAttribute(SpagoBIConstants.AF_LANGUAGE_TAG);
375-
// if a particular language is specified take the corrisponding date-format
376-
String ret = null;
377-
378-
if (StringUtils.isBlank(languageTag)) {
379-
380-
String language = (String) permSess.getAttribute(SpagoBIConstants.AF_LANGUAGE);
381-
String country = (String) permSess.getAttribute(SpagoBIConstants.AF_COUNTRY);
382-
String script = (String) permSess.getAttribute(SpagoBIConstants.AF_SCRIPT);
383-
384-
Locale locale = new Builder().setLanguage(language).setRegion(country).setScript(script).build();
385-
languageTag = locale.toLanguageTag();
386-
}
387-
if (languageTag != null) {
388-
ret = SingletonConfig.getInstance().getConfigValue("SPAGOBI.DATE-FORMAT-" + languageTag + ".format");
389-
}
390-
if (ret == null) {
391-
ret = SingletonConfig.getInstance().getConfigValue("SPAGOBI.DATE-FORMAT.format");
392-
}
393-
LOGGER.debug("Locale date format from session: {}", ret);
394-
return ret;
395-
396-
}
397-
398-
public static String getScriptFromLocale(SessionContainer permSess) {
399-
String toReturn = "";
400-
String script = (String) permSess.getAttribute(SpagoBIConstants.AF_SCRIPT);
401-
402-
if (StringUtils.isNotBlank(script)) {
403-
toReturn = script + "-";
404-
}
405-
406-
return toReturn;
407-
408-
}
409-
410327
public static String getLocaleDateFormat(Locale locale) {
411328
LOGGER.debug("Getting date format from locale");
412329
String ret = null;
@@ -423,34 +340,8 @@ public static String getLocaleDateFormat(Locale locale) {
423340

424341
}
425342

426-
public static String getLocaleDateFormatForExtJs(SessionContainer permSess) {
427-
LOGGER.debug("Getting date format from locale for ExtJS");
428-
String languageTag = (String) permSess.getAttribute(SpagoBIConstants.AF_LANGUAGE_TAG);
429-
String ret = null;
430-
431-
if (StringUtils.isBlank(languageTag)) {
432-
433-
String language = (String) permSess.getAttribute(SpagoBIConstants.AF_LANGUAGE);
434-
String country = (String) permSess.getAttribute(SpagoBIConstants.AF_COUNTRY);
435-
String script = (String) permSess.getAttribute(SpagoBIConstants.AF_SCRIPT);
436-
437-
Locale locale = new Builder().setLanguage(language).setRegion(country).setScript(script).build();
438-
languageTag = locale.toLanguageTag();
439-
}
440-
// if a particular language is specified take the corrisponding date-format
441-
if (languageTag != null) {
442-
ret = SingletonConfig.getInstance().getConfigValue("SPAGOBI.DATE-FORMAT-" + languageTag + ".format");
443-
}
444-
if (ret == null) {
445-
ret = SingletonConfig.getInstance().getConfigValue("SPAGOBI.DATE-FORMAT.extJsFormat");
446-
}
447-
if (ret == null) {
448-
LOGGER.warn("Locale date format for ExtJs not found, using d/m/Y as default");
449-
ret = "d/m/Y";
450-
}
451-
LOGGER.debug("Date format from locale for ExtJS: {}", ret);
452-
return ret;
453-
343+
public static String getLocaleDateFormatForExtJs() {
344+
return SingletonConfig.getInstance().getConfigValue("SPAGOBI.DATE-FORMAT.extJsFormat");
454345
}
455346

456347
public static String getServerDateFormat() {
@@ -815,7 +706,9 @@ private static String resolveRelativeUrlsForVue(String url) {
815706
LOGGER.debug("Url is relative");
816707
String domain = getServiceHostUrl();
817708
if (!isProduction)
709+
{
818710
domain = domain.replaceAll("8080", "3000"); // for testing purposes
711+
}
819712
LOGGER.debug("SpagoBI domain is " + domain);
820713
url = domain + url;
821714
LOGGER.debug("Absolute url is " + url);

0 commit comments

Comments
 (0)