All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xlcloud.logging.LoggingUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xlcloud.logging;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This class contains common logging utilities, like masking passwords.
 *
 * @author Krzysztof Szafrański, AMG.net
 */
public class LoggingUtils {

    private static final String TOKEN_ID = "[^/]*";
    
    public static final String MASKING_STRING = "****";
    
    private static final Pattern[] RESOURCES_TO_MASK = {
        Pattern.compile("(access-tokens/)(" + TOKEN_ID + ")(.*)")
    };
    
    public static String mask(String text) {
        return text == null ? null : MASKING_STRING;
    }

    public static String maskPartially(String text) {
        if (text == null) {
            return null;
        }
        int length = text.length();
        if (length <= 2) {
            return MASKING_STRING;
        } else if (length <= 6) {
            return maskPartially(text, 1);
        } else if (length <= 10) {
            return maskPartially(text, 3);
        } else if (length <= 20) {
            return maskPartially(text, 5);
        } else {
            return maskPartially(text, 10);
        }
    }
    
    private static String maskPartially(String text, int unmaskedEndsLength) {
        return text.substring(0, unmaskedEndsLength) + MASKING_STRING + text.substring(text.length() - unmaskedEndsLength, text.length());
    }
    
    public static String maskResource(String resource) {
        for (Pattern pattern : RESOURCES_TO_MASK) {
            Matcher matcher = pattern.matcher(resource);
            if (matcher.find()) {
                String maskedPart = matcher.group(2);
                if (!"*".equals(maskedPart) && !"-*-".equals(maskedPart)) {
                    maskedPart = maskPartially(maskedPart);
                }
                return matcher.replaceFirst("$1" + escapeForReplace(maskedPart) + "$3");
            }
        }
        return resource;
    }
    
    /**
     * $ and \ are treated differently by the replaceAll() and replaceFirst()
     * methods. This method escapes those symbols, so that the returned text can
     * be used as the replacement string in those methods.
     * 
     * @param text
     * @return
     */
    public static String escapeForReplace(String text) {
        if (text == null) {
            return null;
        }
        return text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\$", "\\\\\\$");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy