Skip to content

Commit

Permalink
add the Mult version key API (#2661)
Browse files Browse the repository at this point in the history
* add multi version key feature

* add validate

* add the all version

* add part success

* add the status

* code format
  • Loading branch information
tigershi authored Sep 18, 2023
1 parent 1cc470c commit b924535
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 219 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,17 @@ public APIResponseDTO getVersionNameList(
HttpServletRequest request) throws Exception {
return super.getVersionList(productName);
}

@Operation(summary = APIOperation.PRODUCT_MULTI_VERSION_KEY_VALUE, description = APIOperation.PRODUCT_MULTI_VERSION_KEY_NOTES)
@RequestMapping(value = APIV2.KEY_MULTI_VERSION_TRANSLATION_GET, method = RequestMethod.GET, produces = {API.API_CHARSET})
@ResponseStatus(HttpStatus.OK)
public APIResponseDTO getMultiVersionKey(
@Parameter(name = APIParamName.PRODUCT_NAME, required = true, description = APIParamValue.PRODUCT_NAME) @PathVariable(APIParamName.PRODUCT_NAME) String productName,
@Parameter(name = APIParamName.VERSIONS, required = true, description = APIParamValue.VERSIONS) @RequestParam(value = APIParamName.VERSIONS) String versions,
@Parameter(name = APIParamName.LOCALE, required = true, description = APIParamValue.LOCALE) @RequestParam(value = APIParamName.LOCALE) String locale,
@Parameter(name = APIParamName.COMPONENT, required = true, description = APIParamValue.COMPONENT) @RequestParam(APIParamName.COMPONENT) String component,
@Parameter(name = APIParamName.KEY, required = true, description = APIParamValue.KEY) @RequestParam(APIParamName.KEY) String key,
HttpServletRequest request) throws Exception {
return super.getVersionsTransByGet(productName, versions, component, locale, key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@ public class APIOperation {

public static final String IMAGE_COUNTRY_FLAG_VALUE = "Get the country flag svg image";
public static final String IMAGE_COUNTRY_FLAG_NOTES = "Get the country flag svg image with region and scale";
public static final String PRODUCT_MULTI_VERSION_KEY_VALUE = "Get a key's request versions all translation";
public static final String PRODUCT_MULTI_VERSION_KEY_NOTES = "Get a key's all translation in specific version list";
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class APIParamName {
public final static String PRODUCT_NAME = "productName";
public final static String VERSION = "version";
public final static String VERSIONS = "versions";
public final static String VERSION2 = "version:.+";
public final static String COMPONENT = "component";
public final static String COMPONENTS = "components";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class APIParamValue {
public final static String PRODUCT_NAME = "product name";
public final static String VERSION = "translation version";
public final static String VERSIONS = "a String contains multiple translation version, separated by commas.";
public final static String COMPONENT = "component name";
public final static String COMPONENTS = "a String contains multiple components, separated by commas. e.g. 'cim, common, cpa, cpu'";
public final static String LOCALE = "locale String. e.g. 'en-US'";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class APIV2 {
public static final String PRODUCT_VERSION_LIST_GET = API_TRANSLATOIN + "/products/{"+APIParamName.PRODUCT_NAME+"}/versionlist";
public static final String PRODUCT_TRANSLATION_SYNC_PUT = API_TRANSLATOIN +"/products/{"+APIParamName.PRODUCT_NAME+"}/versions/{"+APIParamName.VERSION2+"}/synch";
// key-based
public static final String KEY_MULTI_VERSION_TRANSLATION_GET = API_TRANSLATOIN + "/products/{"+APIParamName.PRODUCT_NAME+"}/multiVersionKey";
public static final String KEY_TRANSLATION_GET = API_TRANSLATOIN + "/products/{"+APIParamName.PRODUCT_NAME+"}/versions/{"+APIParamName.VERSION2+"}/locales/{"+APIParamName.LOCALE+"}/components/{"+APIParamName.COMPONENT+"}/keys/{"+APIParamName.KEY2+"}";
public static final String KEY_TRANSLATION_POST = API_TRANSLATOIN + "/products/{"+APIParamName.PRODUCT_NAME+"}/versions/{"+APIParamName.VERSION2+"}/locales/{"+APIParamName.LOCALE+"}/components/{"+APIParamName.COMPONENT+"}/keys/{"+APIParamName.KEY2+"}";
public static final String KEY_SET_POST = API_TRANSLATOIN + "/products/{"+APIParamName.PRODUCT_NAME+"}/versions/{"+APIParamName.VERSION2+"}/locales/{"+APIParamName.LOCALE+"}/components/{"+APIParamName.COMPONENT+"}/keys";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void validate() throws ValidationException {
validateAppId(request);
validateProductname(request);
validateVersion(request);
validateVersions(request);
validateComponent(request);
validateComponents(request);
validateKey(request);
Expand Down Expand Up @@ -109,6 +110,20 @@ private void validateVersion(HttpServletRequest request)
throw new ValidationException(ValidationMsg.VERSION_NOT_VALIDE);
}
}
private void validateVersions(HttpServletRequest request)
throws ValidationException {
Map<String, String> pathVariables = (Map<String, String>) request
.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
String versions = pathVariables.get(APIParamName.VERSIONS) == null ? request
.getParameter(APIParamName.VERSIONS) : pathVariables
.get(APIParamName.VERSIONS);
if (StringUtils.isEmpty(versions)) {
return;
}
if (!RegExpValidatorUtils.IsAllOrNumberDotAndComm(versions)) {
throw new ValidationException(ValidationMsg.VERSIONS_NOT_VALIDE);
}
}

@SuppressWarnings("unchecked")
private void validateComponent(HttpServletRequest request)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 VMware, Inc.
* Copyright 2019-2023 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
package com.vmware.vip.core.messages.service.string;
Expand All @@ -9,6 +9,8 @@
import com.vmware.vip.core.messages.exception.L3APIException;
import com.vmware.vip.core.messages.service.singlecomponent.ComponentMessagesDTO;

import java.util.List;

/**
* This class handles the translation by String.
*
Expand Down Expand Up @@ -37,10 +39,21 @@ public interface IStringService {
* If the translation is cached, get it directly;
* otherwise get it from local bundle.
*
* @param c
* @param compMsg
* @param keyArr
* @return
* @throws L3APIException
*/
public SingleComponentDTO getMultKeyTranslation(ComponentMessagesDTO compMsg, String[] keyArr)throws L3APIException;

/**
* Get multiple version key translation
* This method use the one component cache
* @param compMsg
* @param versionList
* @param key
* @return
* @throws L3APIException
*/
public List<StringBasedDTO> getMultiVersionKeyTranslation(ComponentMessagesDTO compMsg, List<String> versionList, String key)throws L3APIException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.springframework.util.StringUtils;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -45,7 +47,7 @@ public class StringService implements IStringService {
* If the translation is cached, get it directly, otherwise get it from
* local bundle.
*
* @param componentMessagesDTO
* @param comDTO
* the object of ComponentMessagesDTO.
* @param key
* The unique identify for source in component's resource file.
Expand Down Expand Up @@ -215,4 +217,44 @@ public SingleComponentDTO getMultKeyTranslation(ComponentMessagesDTO compMsg, St
return null;
}
}

/**
* Get translation of multi version key.
*
* @param compMsg
* the object of ComponentMessagesDTO.
* @param versionList
* the list of version string.
* @param key
* The unique identify for source in component's resource file.
*/
@Override
public List<StringBasedDTO> getMultiVersionKeyTranslation(ComponentMessagesDTO compMsg, List<String> versionList, String key) throws L3APIException {
List<StringBasedDTO> stringBasedDTOs = new ArrayList<>();
for(String versionStr : versionList){
compMsg.setVersion(versionStr.trim());
try{
ComponentMessagesDTO compData = singleComponentService.getComponentTranslation(compMsg);
String translation = null;
if(compData.getMessages()!= null && (translation = (String)((Map)compData.getMessages()).get(key)) != null){
StringBasedDTO stringBasedDTO = new StringBasedDTO();
stringBasedDTO.setProductName(compMsg.getProductName());
stringBasedDTO.setVersion(compMsg.getVersion());
stringBasedDTO.setComponent(compMsg.getComponent());
stringBasedDTO.setLocale(compMsg.getLocale());
stringBasedDTO.setKey(key);
stringBasedDTO.setTranslation(translation);
stringBasedDTO.setStatus(ConstantsMsg.SOURCE_IS_NOT_PROVIDE);
stringBasedDTOs.add(stringBasedDTO);
}
}catch (Exception e){
logger.error(e.getMessage(), e);
}

}
if (stringBasedDTOs.size() == 0){
throw new L3APIException(ConstantsMsg.TRANS_IS_NOT_FOUND);
}
return stringBasedDTOs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public class ConstantsKeys {
public static final String FLAGS = "flags";
public static final String SVG = "svg";
public static final String JSON = "json";
public static final String ALL = "all";

public final static Map<String,String> IMAGE_TYPE_MAP = Map.of("svg", "image/svg+xml", "json", "application/json;charset=utf-8");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public final class ValidationMsg {
public final static String PRODUCTNAME_NOT_VALIDE = "Incorrect produtName(only allows letter and number)";
public final static String VERSION_NOT_VALIDE = "Incorrect version(only allows number and dot, e.g. 1.2.0)";
public final static String VERSIONS_NOT_VALIDE = "Incorrect versions(only allows all or number, dot, comm. e.g. 1.2.0,2.2.0)";
public final static String COMPONENT_NOT_VALIDE = "Incorrect component(only allows letter, number, dot, underline, dashline)";
public final static String COMPONENTS_NOT_VALIDE = "Incorrect components(only allows letter, number, comma, dot, underline, dashline)";
public final static String KEY_NOT_VALIDE = "Incorrect key(only allows standard ASCII char)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public static boolean IsNumberAndDot(String str) {
return match(regex, str);
}

/*
* is number dot and comm
*/
public static boolean IsAllOrNumberDotAndComm(String str) {
String regex = "^(all|[0-9\\.\\,]+)$";
return match(regex, str);
}
/*
* is number and dot and length is (5,10)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private String getRequestId(HttpServletRequest request, List<String> headerNames
public void validate(HttpServletRequest request) throws ValidationException {
validateProductname(request);
validateVersion(request);
validateVersions(request);
validateComponent(request);
validateComponents(request);
validateKey(request);
Expand Down Expand Up @@ -154,6 +155,21 @@ private void validateVersion(HttpServletRequest request)
}
}

private void validateVersions(HttpServletRequest request)
throws ValidationException {
Map<String, String> pathVariables = (Map<String, String>) request
.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
String versions = pathVariables.get(APIParamName.VERSIONS) == null ? request
.getParameter(APIParamName.VERSIONS) : pathVariables
.get(APIParamName.VERSIONS);
if (org.apache.commons.lang3.StringUtils.isEmpty(versions)) {
return;
}
if (!RegExpValidatorUtils.IsAllOrNumberDotAndComm(versions)) {
throw new ValidationException(ValidationMsg.VERSIONS_NOT_VALIDE);
}
}

@SuppressWarnings("unchecked")
private void validateComponent(HttpServletRequest request)
throws ValidationException {
Expand Down

0 comments on commit b924535

Please sign in to comment.