protected String safeGetTemplateName(Template template){
  if (template == null)   return null;
  String name=template.getName();
  if (name == null)   return null;
  String decodedName=name;
  if (decodedName.indexOf('%') != -1) {
    decodedName=StringUtil.replace(decodedName,"%2e",".",false,false);
    decodedName=StringUtil.replace(decodedName,"%2E",".",false,false);
    decodedName=StringUtil.replace(decodedName,"%2f","/",false,false);
    decodedName=StringUtil.replace(decodedName,"%2F","/",false,false);
    decodedName=StringUtil.replace(decodedName,"%5c","\\",false,false);
    decodedName=StringUtil.replace(decodedName,"%5C","\\",false,false);
  }
  int dotDotIdx=decodedName.indexOf("..");
  if (dotDotIdx != -1) {
    int before=dotDotIdx - 1 >= 0 ? decodedName.charAt(dotDotIdx - 1) : -1;
    int after=dotDotIdx + 2 < decodedName.length() ? decodedName.charAt(dotDotIdx + 2) : -1;
    if ((before == -1 || before == '/' || before == '\\') && (after == -1 || after == '/' || after == '\\')) {
      return null;
    }
  }
  return name.startsWith("/") ? name.substring(1) : name;
}

protected String safeGetTemplateName ( Template template ) { if ( template == null ) return null ; String name = template . getName ( ) ; if ( name == null ) return null ; String decodedName = name ; if ( decodedName . indexOf ( '%' ) != - 1 ) { decodedName = StringUtil . replace ( decodedName , "%2e" , "." , false , false ) ; decodedName = StringUtil . replace ( decodedName , "%2E" , "." , false , false ) ; decodedName = StringUtil . replace ( decodedName , "%2f" , "/" , false , false ) ; decodedName = StringUtil . replace ( decodedName , "%2F" , "/" , false , false ) ; decodedName = StringUtil . replace ( decodedName , "%5c" , "\\" , false , false ) ; decodedName = StringUtil . replace ( decodedName , "%5C" , "\\" , false , false ) ; } int dotDotIdx = decodedName . indexOf ( ".." ) ; if ( dotDotIdx != - 1 ) { int before = dotDotIdx - 1 >= 0 ? decodedName . charAt ( dotDotIdx - 1 ) : - 1 ; int after = dotDotIdx + 2 < decodedName . length ( ) ? decodedName . charAt ( dotDotIdx + 2 ) : - 1 ; if ( ( before == - 1 || before == '/' || before == '\\' ) && ( after == - 1 || after == '/' || after == '\\' ) ) { return null ; } } return name . startsWith ( "/" ) ? name . substring ( 1 ) : name ; }
extract the template name from the template object which will be matched against the trusted template names and pattern .
