'Check if any of the content of List present

I want to write a Java Spring boot code. If any one of the "aliasname" present in the list then it should inside the loop for checking sensitive fields. If any one of the aliasname (account_number, accountNumber, acct_num) is matching with sensitivefieldList, then it will be mask as per the requirements.

My code is as below,

First class -

public class SensitiveMaskedCharacterFieldsConfig {
    
    List<String> aliasname = new ArrayList<>(List.of("accountNumber", "acc_num", 
                             "acctNum"));
    String fieldname;
}

Second Class -

public class DisplaySensitiveMaskedCharacterFieldsConfig {
    
    protected List<SensitiveMaskedCharacterFieldsConfig> sensitivefields;
}

Third Class -

public class TokenizationFilter implements Filter {
    
    @Autowired
    DisplaySensitiveMaskedCharacterFieldsConfig config;
       
    public static final String CONTENT_TYPE = "application/json";
    public static final String HEADERS_CONTENT_TYPE = "Content-Type";
    String str = null;

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
      
      List<SensitiveMaskedCharacterFieldsConfig> sensitiveFields = config.getSensitivefields();
      List<String> sensitiveFieldsList = new ArrayList<>();
      for(SensitiveMaskedCharacterFieldsConfig charConfig: sensitiveFields) {
          sensitiveFieldsList.add(charConfig.getAliasname());
          sensitiveFieldsList.add(charConfig.getFieldname());
      }
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    
    ContentCachingResponseWrapper readWrapper = new ContentCachingResponseWrapper(httpResponse);
    HttpServletResponseWrapper writeWrapper = new HttpServletResponseWrapper(httpResponse);

    chain.doFilter(request, readWrapper);

    String content =
        new String(readWrapper.getContentAsByteArray(), readWrapper.getCharacterEncoding());
    
    sensitiveFieldsList.forEach(field -> {
        if (content.contains(field)){
            str = getStringManipulateSensitiveFields(field, content, writeWrapper);     
        }else {
            log.info("No sensitive fields found in current Object");
        }
      });
    
    log.info("str: " + str);
    log.info("Content: " + content);
    
    try {
            
      writeWrapper.getOutputStream().write(str != null ? str.getBytes() : content.getBytes() );

    } catch (JSONException e1) {
      e1.printStackTrace();
    }

  }
  
  public String getStringManipulateSensitiveFields(String key, String jsonString, HttpServletResponseWrapper writeWrapper) {

      String response = null;
      for (int index = jsonString.indexOf(key); index > 0 ; index = jsonString.indexOf(key,index + 1)) {
            int indexOf = index;
            indexOf = indexOf + key.length() + 3;
            int finalIndex=0;
            for (int i = indexOf; i < jsonString.length(); i++) {
              String charAt = jsonString.charAt(i)+"";
              if(charAt.equals(",") || charAt.equals("}")) {
                finalIndex = i-1;
                break;
              }
            }

            String sensitiveFieldValue = jsonString.substring(indexOf, finalIndex);
            if (tokenizationUrl != null) {
                String url = String.format("%s%s%s", tokenizationUrl, tokenizationEndpoint, sensitiveFieldValue);
                response = webClient.get().uri(url).header(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE)
                        .retrieve().bodyToMono(String.class)
                        .block();   
                if (response == null) {
                    log.error("Error occured to create or retrieve token for accountNumber - {}", sensitiveFieldValue);
                    throw log.throwing(new C360RuntimeException("Error occured to create token for accountNumber"));
                }
            }
            
            writeWrapper.addHeader("sensitive." + key + "." + sensitiveFieldValue,  response);

//            String maskedData = response;
//            jsonString = jsonString.replaceAll(sensitiveFieldValue, maskedData);
          }
      
      return jsonString;
    }
  

}

My Yaml file:

c360:
  config:
    sensitivefields:
      - aliasname: accountNumber
        fieldname: ACCOUNT_NUMBER


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source