com.atlassian.bamboo.util
Class BambooStringUtils

java.lang.Object
  extended by com.atlassian.bamboo.util.BambooStringUtils

public class BambooStringUtils
extends Object


Field Summary
static char CHAR_APOSTROPHE
           
static char CHAR_BACKSLASH
           
static char CHAR_DOUBLE_QUOTE
           
static char CHAR_ELLIPSIS
           
static char CHAR_NONE
           
static char[] XSS_RELATED_CHARACTERS
           
static String XSS_RELATED_CHARACTERS_FIND_STR
           
 
Method Summary
static boolean containsDefinedXssRelatedCharacters(String string, char[] characters)
          Deprecated. since 5.0 please use containsRelatedCharacters
static boolean containsRelatedCharacters(String string, char[] characters)
           
static boolean containsRelaxedXssRelatedCharacters(String string)
           
static boolean containsShellInjectionRelatedCharacters(String string)
           
static com.google.common.base.Predicate<String> containsString(String string)
           
static boolean containsXssRelatedCharacters(String string)
          Returns true if the supplied string contains characters that could be used to deface a page or trigger an XSS exploit (characters: "&'<>\).
static String dos2unix(String s)
          Changes Dos-style line endings to Unix style line endings
static String encodeHtmlWithTagWhiteList(String string)
          Escapes string but preserves few defined tags.
static com.google.common.base.Function<String,String> encodeHtmlWithTagWhiteListFunction()
           
static String forceAlphaNumeric(String str)
           
static String forceValidHtmlId(String id)
          According to the HTML standard, ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
static String formatTemplate(String urlTemplate, Map<String,String> variableValues)
          Expands variable references in urlTemplate with variableValues.
static String getFirstDefinedXssRelatedCharacter(String string, char[] characters)
          Deprecated. since 5.0 please use containsRelatedCharacters
static String getFirstRelatedCharacter(String string, char[] characters)
           
static String getFirstRelaxedXssRelatedCharacter(String string)
           
static String getFirstShellInjectionRelatedCharacters(String string)
           
static String getFirstXssRelatedCharacter(String string)
          Retrieves first unsafe character from supplied string
static boolean in(String needle, String... haystacks)
           
static int indexOf(String haystack, String needle, int n)
          Returns n-th occurence of string (index is 0-based)
static int indexOfRegex(String haystack, String needle, int occurence)
          Returns n-th occurence of regex (index is 0-based)
static Iterable<String> lineIterator(String string)
           
static String nullIfBlank(String string)
           
static String preventInlining(String constant)
          This is to prevent inlining.
static String remove(String quotedVarValue, int index)
          Removes a character at specified index from string
static com.google.common.base.Function<String,String> replaceAll(Pattern pattern, String replacement)
           
static com.google.common.base.Function<String,String> replaceAll(String regex, String replacement)
           
static boolean startsWithAny(String string, Collection<String> searchStrings)
           
static com.google.common.base.Function<String,String> stripFullyEnclosingQuotes()
           
static String toCamelCase(String string, char separator)
          Returns the string in camel case.
static String toCommandLineString(Iterable<String> commandArgs)
          Joins supplied argument into a valid command line, wrapping arguments in single quotes if needed.
static com.google.common.base.Function<String,String> toUpperCase()
           
static String truncateAndAddEllipsis(String string, int maxLength)
          Truncates a string to specified length.
static String unquote(String quotedVarValue, boolean processBackslashes)
          Removes quotes from variable using shell rules.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CHAR_NONE

public static final char CHAR_NONE
See Also:
Constant Field Values

CHAR_APOSTROPHE

public static final char CHAR_APOSTROPHE
See Also:
Constant Field Values

CHAR_BACKSLASH

public static final char CHAR_BACKSLASH
See Also:
Constant Field Values

CHAR_DOUBLE_QUOTE

public static final char CHAR_DOUBLE_QUOTE
See Also:
Constant Field Values

CHAR_ELLIPSIS

public static final char CHAR_ELLIPSIS
See Also:
Constant Field Values

XSS_RELATED_CHARACTERS

public static final char[] XSS_RELATED_CHARACTERS

XSS_RELATED_CHARACTERS_FIND_STR

public static final String XSS_RELATED_CHARACTERS_FIND_STR
See Also:
Constant Field Values
Method Detail

forceAlphaNumeric

@NotNull
public static String forceAlphaNumeric(@Nullable
                                               String str)

containsXssRelatedCharacters

public static boolean containsXssRelatedCharacters(@Nullable
                                                   String string)
Returns true if the supplied string contains characters that could be used to deface a page or trigger an XSS exploit (characters: "&'<>\). Should be used in addition to normal escaping of output, not instead of it.

Parameters:
string - string to check
Returns:
true if string contains unsafe characters, false otherwise

getFirstXssRelatedCharacter

@NotNull
public static String getFirstXssRelatedCharacter(@Nullable
                                                         String string)
Retrieves first unsafe character from supplied string

Parameters:
string - the string to check
Returns:
string with the first encountered unsafe character.

containsRelaxedXssRelatedCharacters

public static boolean containsRelaxedXssRelatedCharacters(@Nullable
                                                          String string)

getFirstRelaxedXssRelatedCharacter

@NotNull
public static String getFirstRelaxedXssRelatedCharacter(@Nullable
                                                                String string)

containsShellInjectionRelatedCharacters

public static boolean containsShellInjectionRelatedCharacters(@Nullable
                                                              String string)

getFirstShellInjectionRelatedCharacters

public static String getFirstShellInjectionRelatedCharacters(@Nullable
                                                             String string)

containsDefinedXssRelatedCharacters

@Deprecated
public static boolean containsDefinedXssRelatedCharacters(@Nullable
                                                                     String string,
                                                                     @NotNull
                                                                     char[] characters)
Deprecated. since 5.0 please use containsRelatedCharacters


containsRelatedCharacters

public static boolean containsRelatedCharacters(@Nullable
                                                String string,
                                                @NotNull
                                                char[] characters)

getFirstDefinedXssRelatedCharacter

@Deprecated
public static String getFirstDefinedXssRelatedCharacter(@Nullable
                                                                   String string,
                                                                   @NotNull
                                                                   char[] characters)
Deprecated. since 5.0 please use containsRelatedCharacters


getFirstRelatedCharacter

@NotNull
public static String getFirstRelatedCharacter(@Nullable
                                                      String string,
                                                      @NotNull
                                                      char[] characters)

forceValidHtmlId

@NotNull
public static String forceValidHtmlId(@NotNull
                                              String id)
According to the HTML standard, ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). This method replaces all invalid characters in the supplied id with an underscore. It does not enforce the "must begin with a letter" rule. There are two functions that do it, one in FreeMarker functions.ftl and one in BambooStringUtils. They MUST be kept in sync.


lineIterator

public static Iterable<String> lineIterator(String string)
Returns:
an iterable that makes it possible to iterate over lines with Windows or Unix endings

containsString

public static com.google.common.base.Predicate<String> containsString(@NotNull
                                                                      String string)

indexOf

public static int indexOf(String haystack,
                          String needle,
                          int n)
Returns n-th occurence of string (index is 0-based)

Parameters:
haystack - search here
needle - search for
n - n-the cocurence to return
Returns:
-1 if not found, else the index

indexOfRegex

public static int indexOfRegex(String haystack,
                               String needle,
                               int occurence)
Returns n-th occurence of regex (index is 0-based)

Parameters:
haystack - search here
needle - search for
occurence - n-the occurence to return
Returns:
-1 if not found, else the index

startsWithAny

public static boolean startsWithAny(String string,
                                    Collection<String> searchStrings)
See Also:
StringUtils.startsWithAny(String, String[])

nullIfBlank

@Nullable
public static String nullIfBlank(@Nullable
                                          String string)

encodeHtmlWithTagWhiteList

public static String encodeHtmlWithTagWhiteList(String string)
Escapes string but preserves few defined tags. Allows some html formatting inside strings while preventing XSS. Allowed tags are: <br /> <i> <b> <p> <div> <a>. Properties are not allowed except 'href' in anchor tag.

Parameters:
string -
Returns:
html escaped string with white listed tags preserved

encodeHtmlWithTagWhiteListFunction

public static com.google.common.base.Function<String,String> encodeHtmlWithTagWhiteListFunction()

unquote

@NotNull
public static String unquote(@NotNull
                                     String quotedVarValue,
                                     boolean processBackslashes)
Removes quotes from variable using shell rules.

Parameters:
quotedVarValue -
processBackslashes - whether backslash should be treated as a special character or not
Returns:

in

public static boolean in(String needle,
                         String... haystacks)

remove

@NotNull
public static String remove(@NotNull
                                    String quotedVarValue,
                                    int index)
Removes a character at specified index from string

Parameters:
quotedVarValue -
index -
Returns:

truncateAndAddEllipsis

@Nullable
public static String truncateAndAddEllipsis(@Nullable
                                                     String string,
                                                     int maxLength)
Truncates a string to specified length. If truncation causes actual information loss an ellipsis is appended to the end of the string.

Parameters:
string - string
maxLength - maximum length of the string
Returns:
truncated string

stripFullyEnclosingQuotes

public static com.google.common.base.Function<String,String> stripFullyEnclosingQuotes()

toUpperCase

public static com.google.common.base.Function<String,String> toUpperCase()

replaceAll

public static com.google.common.base.Function<String,String> replaceAll(Pattern pattern,
                                                                        String replacement)

replaceAll

public static com.google.common.base.Function<String,String> replaceAll(String regex,
                                                                        String replacement)

dos2unix

@NotNull
public static String dos2unix(@NotNull
                                      String s)
Changes Dos-style line endings to Unix style line endings


toCamelCase

public static String toCamelCase(@NotNull
                                 String string,
                                 char separator)
Returns the string in camel case. Com.atlassian.bamboo.somePlugin becomes comAtlassianBambooSomePlugin

Parameters:
string -
separator -
Returns:

toCommandLineString

public static String toCommandLineString(@NotNull
                                         Iterable<String> commandArgs)
Joins supplied argument into a valid command line, wrapping arguments in single quotes if needed.


formatTemplate

public static String formatTemplate(String urlTemplate,
                                    Map<String,String> variableValues)
Expands variable references in urlTemplate with variableValues.

The variable references look like "${foo}" and will be replaced with the value matching the that key from variableValues. The names may not contain '}' characters but are otherwise unconstrained. Variables which do not have associated values in the request are unmodified.

Parameters:
urlTemplate - the template to expand.
variableValues - the variable values to use when expanding the template
Returns:
the template expanded as detailed above.

preventInlining

public static String preventInlining(String constant)
This is to prevent inlining. Please see https://extranet.atlassian.com/pages/viewpage.action?pageId=1850409006 for details.

Parameters:
constant - The constant to return.
Returns:
the given constant.


Copyright © 2015 Atlassian Software Systems Pty Ltd. All rights reserved.