Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug 2750: filter and <vip> tag can support customized vipconfig name #2828

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 VMware, Inc.
* Copyright 2019-2024 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vipclient.i18n.filters;
Expand All @@ -23,9 +23,7 @@

import com.vmware.vipclient.i18n.I18nFactory;
import com.vmware.vipclient.i18n.VIPCfg;
import com.vmware.vipclient.i18n.base.cache.MessageCache;
import com.vmware.vipclient.i18n.base.instances.TranslationMessage;
import com.vmware.vipclient.i18n.exceptions.VIPClientInitException;
import com.vmware.vipclient.i18n.util.LocaleUtility;

/**
Expand All @@ -43,8 +41,8 @@
String locale = FilterUtils.getParamFromQuery(request, "locale");
logger.debug("locale: " + locale);
String messages = "{}";
if (!StringUtil.isEmpty(component) && !StringUtil.isEmpty(locale) && !LocaleUtility.isDefaultLocale(locale) && translation != null) {
Map<String, String> ctmap = translation.getMessages(LocaleUtility.fmtToMappedLocale(locale),
if (!StringUtil.isEmpty(component) && !StringUtil.isEmpty(locale) && !LocaleUtility.isDefaultLocale(locale)) {
Map<String, String> ctmap = translation.getStrings(LocaleUtility.fmtToMappedLocale(locale),
component);
if (ctmap != null) {
messages = JSONObject.toJSONString(ctmap);
Expand Down Expand Up @@ -74,17 +72,24 @@
private TranslationMessage translation;
private VIPCfg gc = VIPCfg.getInstance();

/**
* Here will create TranslationMessage instance, but create it requires I18nFactory instance created first, and creation of I18nFactory
* requires VIPCfg instance. Hence you must create VIPCfg instance and I18nFactory instance before this filter initialize, so recommend
* you create them at your service starts, that is in listener class that implements ServletContextListener, or will throw ServletException.
*
* Furthermore when initialize VIPCfg you had better rename your config file to avoid config loading error. You can write it in web.xml
* as <context-param> to avoid hardcoding the config file in code and make the whole application share the same config.
*
* @param filterConfig
* @throws ServletException
*/
public void init(FilterConfig filterConfig) throws ServletException {
if (gc.getVipService() == null) {
try {
gc.initialize("vipconfig");
} catch (VIPClientInitException e) {
logger.error(e.getMessage());
}
gc.initializeVIPService();
I18nFactory i18n = I18nFactory.getInstance();
try {
translation = (TranslationMessage) i18n.getMessageInstance(TranslationMessage.class);
} catch(NullPointerException e){
throw new ServletException("Haven't init I18nFactory, please init VIPCfg with your config first when your service starts" +

Check warning on line 91 in src/main/java/com/vmware/vipclient/i18n/filters/VIPComponentFilter.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/vmware/vipclient/i18n/filters/VIPComponentFilter.java#L90-L91

Added lines #L90 - L91 were not covered by tests
"(for example init VIPCfg in listener), then initialize I18nFactory with VIPCfg!", e);
}
gc.createTranslationCache(MessageCache.class);
I18nFactory i18n = I18nFactory.getInstance(gc);
translation = (TranslationMessage) i18n.getMessageInstance(TranslationMessage.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 VMware, Inc.
* Copyright 2019-2024 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vipclient.i18n.filters;
Expand All @@ -18,9 +18,7 @@

import com.vmware.vipclient.i18n.I18nFactory;
import com.vmware.vipclient.i18n.VIPCfg;
import com.vmware.vipclient.i18n.base.cache.FormattingCache;
import com.vmware.vipclient.i18n.base.instances.PatternMessage;
import com.vmware.vipclient.i18n.exceptions.VIPClientInitException;
import com.vmware.vipclient.i18n.exceptions.VIPJavaClientException;
import com.vmware.vipclient.i18n.util.StringUtil;
import org.json.simple.JSONObject;
Expand Down Expand Up @@ -68,18 +66,25 @@
// Do Nothing
}

/**
* Here will create TranslationMessage instance, but create it requires I18nFactory instance created first, and creation of I18nFactory
* requires VIPCfg instance. Hence you must create VIPCfg instance and I18nFactory instance before this filter initialize, so recommend
* you create them at your service starts, that is in listener class that implements ServletContextListener, or will throw ServletException.
*
* Furthermore when initialize VIPCfg you had better rename your config file to avoid config loading error. You can write it in web.xml
* as <context-param> to avoid hardcoding the config file in code and make the whole application share the same config.
*
* @param filterConfig
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
if (gc.getVipService() == null) {
try {
gc.initialize("vipconfig");
} catch (VIPClientInitException e) {
logger.error(e.getMessage());
}
gc.initializeVIPService();
I18nFactory i18n = I18nFactory.getInstance();
try {
patternMessage = (PatternMessage) i18n.getMessageInstance(PatternMessage.class);
} catch(NullPointerException e){
throw new ServletException("Haven't init I18nFactory, please init VIPCfg with your config first when your service starts" +

Check warning on line 86 in src/main/java/com/vmware/vipclient/i18n/filters/VIPPatternFilter.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/vmware/vipclient/i18n/filters/VIPPatternFilter.java#L85-L86

Added lines #L85 - L86 were not covered by tests
"(for example init VIPCfg in listener), then initialize I18nFactory with VIPCfg!", e);
}
gc.createFormattingCache(FormattingCache.class);
I18nFactory i18n = I18nFactory.getInstance(gc);
patternMessage = (PatternMessage) i18n.getMessageInstance(PatternMessage.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 VMware, Inc.
* Copyright 2019-2024 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vipclient.i18n.fmt.common;
Expand All @@ -18,10 +18,7 @@
import org.apache.taglibs.standard.tag.common.core.Util;

import com.vmware.vipclient.i18n.I18nFactory;
import com.vmware.vipclient.i18n.VIPCfg;
import com.vmware.vipclient.i18n.base.cache.MessageCache;
import com.vmware.vipclient.i18n.base.instances.TranslationMessage;
import com.vmware.vipclient.i18n.exceptions.VIPClientInitException;
import com.vmware.vipclient.i18n.util.LocaleUtility;

public class MessageSupport extends BodyTagSupport {
Expand All @@ -46,16 +43,6 @@
this.scope = 1;
this.keyAttrValue = null;
this.keySpecified = false;
VIPCfg gc = VIPCfg.getInstance();
try {
gc.initialize("vipconfig");
} catch (VIPClientInitException e) {

}
gc.initializeVIPService();
gc.createTranslationCache(MessageCache.class);
I18nFactory i18n = I18nFactory.getInstance(gc);
translation = (TranslationMessage) i18n.getMessageInstance(TranslationMessage.class);
}

public int doStartTag() throws JspException {
Expand All @@ -81,8 +68,14 @@
}
Locale locale = LocaleUtility.getLocale();
Object[] args = this.params.isEmpty() ? null : this.params.toArray();
String message = translation == null ? ""
: translation.getString2(component, bundle, locale, key, "TranslationCache", args);
try{
I18nFactory i18n = I18nFactory.getInstance();
translation = (TranslationMessage) i18n.getMessageInstance(TranslationMessage.class);
} catch(NullPointerException e){
throw new JspTagException("Haven't init I18nFactory, please init VIPCfg with your config first when your service starts" +

Check warning on line 75 in src/main/java/com/vmware/vipclient/i18n/fmt/common/MessageSupport.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/vmware/vipclient/i18n/fmt/common/MessageSupport.java#L72-L75

Added lines #L72 - L75 were not covered by tests
"(for example init VIPCfg in listener), then initialize I18nFactory with VIPCfg!", e);
}
String message = translation.getString2(component, bundle, locale, key, "TranslationCache", args);

Check warning on line 78 in src/main/java/com/vmware/vipclient/i18n/fmt/common/MessageSupport.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/vmware/vipclient/i18n/fmt/common/MessageSupport.java#L77-L78

Added lines #L77 - L78 were not covered by tests
if (this.var != null) {
this.pageContext.setAttribute(this.var, message, this.scope);
} else {
Expand Down
25 changes: 21 additions & 4 deletions src/test/java/com/vmware/vip/i18n/VIPComponentFilterTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/*
* Copyright 2019-2023 VMware, Inc.
* Copyright 2019-2024 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vip.i18n;

import com.vmware.vipclient.i18n.I18nFactory;
import com.vmware.vipclient.i18n.VIPCfg;
import com.vmware.vipclient.i18n.base.cache.MessageCache;
import com.vmware.vipclient.i18n.exceptions.VIPClientInitException;
import com.vmware.vipclient.i18n.filters.VIPComponentFilter;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -19,14 +23,27 @@
public class VIPComponentFilterTest extends BaseTestClass{
VIPComponentFilter componentFilter = new VIPComponentFilter();

//@Before
@Before
public void init() throws ServletException {
VIPCfg gc = VIPCfg.getInstance();
try {
gc.initialize("vipconfig");
} catch (VIPClientInitException e) {
logger.error(e.getMessage());
}
gc.createTranslationCache(MessageCache.class);
I18nFactory i18n = I18nFactory.getInstance(gc);

Check notice

Code scanning / Pmd (reported by Codacy)

Avoid unused local variables such as 'i18n'. Note test

Avoid unused local variables such as 'i18n'.

componentFilter.init(null);
}

//@Test
@Test
public void testDoFilter() throws IOException, ServletException {
String normalMsg = "var translation = {\"messages\" : {\"LeadTest\":\"[{0}] 测试警示\",\"table.host\":\"主机\",\"global_text_username\":\"用户名\",\"sample.plural.key1\":\"{0, plural, other{\\\"{1}\\\"上有#个文件。}}\"}, \"productName\" : \"JavaclientTest\", \"version\" : \"1.0.0\", \"vipServer\" : \"http://localhost:8099\", \"pseudo\" : \"false\", \"collectSource\" : \"false\"};";
String normalMsg = "var translation = {\"messages\" : {\"LeadTest\":\"[{0}] 测试警示\",\"table.host\":\"主机\",\"global_text_username\":\"用户名\"," +
"\"sample.plural.key1\":\"{0, plural, other{\\\"{1}\\\"上有#个文件。}}\"}," +
" \"productName\" : \"JavaclientTest\", \"version\" : \"1.0.0\", \"vipServer\" : \"http://localhost:8099\", " +
"\"pseudo\" : \"false\", \"collectSource\" : \"false\"};";

String errorMsg = "{\"code\":400, \"message\": \"Request parameter 'locale' is required!\"}";

String uri = "https://localhost/i18n/component/JAVA";
Expand Down
19 changes: 16 additions & 3 deletions src/test/java/com/vmware/vip/i18n/VIPPatternFilterTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/*
* Copyright 2019-2023 VMware, Inc.
* Copyright 2019-2024 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vip.i18n;

import com.vmware.vipclient.i18n.I18nFactory;
import com.vmware.vipclient.i18n.VIPCfg;
import com.vmware.vipclient.i18n.base.cache.FormattingCache;
import com.vmware.vipclient.i18n.exceptions.VIPClientInitException;
import com.vmware.vipclient.i18n.filters.VIPPatternFilter;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -18,12 +22,21 @@
public class VIPPatternFilterTest extends BaseTestClass{
VIPPatternFilter patternFilter = new VIPPatternFilter();

//@Before
@Before
public void init() throws ServletException {
VIPCfg gc = VIPCfg.getInstance();
try {
gc.initialize("vipconfig");
} catch (VIPClientInitException e) {
logger.error(e.getMessage());
}
gc.createFormattingCache(FormattingCache.class);
I18nFactory i18n = I18nFactory.getInstance(gc);

Check notice

Code scanning / Pmd (reported by Codacy)

Avoid unused local variables such as 'i18n'. Note test

Avoid unused local variables such as 'i18n'.

patternFilter.init(null);
}

//@Test
@Test
public void testDoFilter() throws IOException, ServletException {
String errorMsg = "{\"code\":400, \"message\": \"Request parameter 'locale' is required!\"}";

Expand Down
Loading