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

org.swisspush.gateleen.routing.Translator Maven / Gradle / Ivy

There is a newer version: 2.1.12
Show newest version
package org.swisspush.gateleen.routing;

import org.slf4j.Logger;
import org.swisspush.gateleen.core.util.StatusCodeTranslator;

import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * Enhances the {@link org.swisspush.gateleen.core.util.StatusCodeTranslator } with features
 * needed explicitly in the Router.
 * 
 * @author https://github.com/ljucam [Mario Aerni]
 */
public class Translator extends StatusCodeTranslator {

    /**
     * Checks if there exists a handling for the given statusCode in the
     * rules for this request.
     * 
     * @param statusCode the original status code
     * @param rule the rule matching the current request
     * @return the translated status or if no translation was carried out the original status code.
     */
    public static int translateStatusCode(int statusCode, Rule rule) {
        return translateStatusCode(statusCode, rule, null);
    }

    /**
     * Checks if there exists a handling for the given statusCode in the
     * rules for this request.
     * 
     * @param statusCode the original status code
     * @param rule the rule matching the current request
     * @param log extra configured logger
     * @return the translated status or if no translation was carried out the original status code.
     */
    public static int translateStatusCode(int statusCode, Rule rule, Logger log) {
        Integer translatedStatus = null;

        if (rule.getTranslateStatus() != null) {
            for (Map.Entry entry : rule.getTranslateStatus().entrySet()) {
                if (entry.getKey().matcher("" + statusCode).matches()) {
                    if (log != null) {
                        log.debug("Translated status {} to {}", statusCode, entry.getValue());
                    }
                    translatedStatus = entry.getValue();
                    break;
                }
            }
        }

        // if something is found, return found value else return original value
        return Objects.requireNonNullElse(translatedStatus, statusCode);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy