|
| 1 | +package org.owasp.esapi.tags; |
| 2 | + |
| 3 | +import java.io.UnsupportedEncodingException; |
| 4 | + |
| 5 | +import org.owasp.esapi.ESAPI; |
| 6 | +import org.owasp.esapi.Encoder; |
| 7 | +import org.owasp.esapi.errors.EncodingException; |
| 8 | + |
| 9 | +/** |
| 10 | + * Static encoder methods for JSP EL expression functions. |
| 11 | + */ |
| 12 | +public class ELEncodeFunctions |
| 13 | +{ |
| 14 | + private static final String DEFAULT_ENCODING = "UTF-8"; |
| 15 | + |
| 16 | + /** |
| 17 | + * Private constructor as this class shouldn't need to be |
| 18 | + * instantiated. |
| 19 | + */ |
| 20 | + private ELEncodeFunctions() |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Base64 encode a string. UTF-8 is used to encode the string and no line wrapping is performed. |
| 26 | + * @param str The string to encode. |
| 27 | + * @return The base64 encoded String. |
| 28 | + * @see Encoder#encodeForBase64(byte[],boolean) |
| 29 | + * @throws UnsupportedEncodingException if UTF-8 is an unsupported character set. This should not happen as UTF-8 is required to be supported by the JVM spec. |
| 30 | + */ |
| 31 | + public static String encodeForBase64(String str) throws UnsupportedEncodingException |
| 32 | + { |
| 33 | + return encodeForBase64Charset(DEFAULT_ENCODING, str); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Base64 encode a string with line wrapping. UTF-8 is used to encode the string and lines are wrapped at 64 characters.. |
| 38 | + * @param str The string to encode. |
| 39 | + * @return The base64 encoded String. |
| 40 | + * @see Encoder#encodeForBase64(byte[],boolean) |
| 41 | + * @throws UnsupportedEncodingException if UTF-8 is an unsupported character set. This should not happen as UTF-8 is required to be supported by the JVM spec. |
| 42 | + */ |
| 43 | + public static String encodeForBase64Wrap(String str) throws UnsupportedEncodingException |
| 44 | + { |
| 45 | + return encodeForBase64CharsetWrap(DEFAULT_ENCODING, str); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Base64 encode a string after converting to bytes using the specified character set. No line wrapping is performed. |
| 50 | + * @param charset The character set used to convert str to bytes. |
| 51 | + * @param str The string to encode. |
| 52 | + * @return The base64 encoded String. |
| 53 | + * @see Encoder#encodeForBase64(byte[],boolean) |
| 54 | + * @throws UnsupportedEncodingException if charset is an unsupported character set. |
| 55 | + */ |
| 56 | + public static String encodeForBase64Charset(String charset, String str) throws UnsupportedEncodingException |
| 57 | + { |
| 58 | + return ESAPI.encoder().encodeForBase64(str.getBytes(charset), false); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Base64 encode a string after converting to bytes using the specified character set and wrapping lines. Lines are wrapped at 64 characters. |
| 63 | + * @param charset The character set used to convert str to bytes. |
| 64 | + * @param str The string to encode. |
| 65 | + * @return The base64 encoded String. |
| 66 | + * @see Encoder#encodeForBase64(byte[],boolean) |
| 67 | + * @throws UnsupportedEncodingException if charset is an unsupported character set. |
| 68 | + */ |
| 69 | + public static String encodeForBase64CharsetWrap(String charset, String str) throws UnsupportedEncodingException |
| 70 | + { |
| 71 | + return ESAPI.encoder().encodeForBase64(str.getBytes(charset), true); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Encode string for use in CSS. |
| 76 | + * @param str The string to encode. |
| 77 | + * @return str encoded for use in CSS. |
| 78 | + * @see Encoder#encodeForCSS(String) |
| 79 | + */ |
| 80 | + public static String encodeForCSS(String str) |
| 81 | + { |
| 82 | + return ESAPI.encoder().encodeForCSS(str); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Encode string for use in HTML. |
| 87 | + * @param str The string to encode. |
| 88 | + * @return str encoded for use in HTML. |
| 89 | + * @see Encoder#encodeForHTML(String) |
| 90 | + */ |
| 91 | + public static String encodeForHTML(String str) |
| 92 | + { |
| 93 | + return ESAPI.encoder().encodeForHTML(str); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Encode string for use in a HTML attribute. |
| 98 | + * @param str The string to encode. |
| 99 | + * @return str encoded for use in HTML attribute. |
| 100 | + * @see Encoder#encodeForHTMLAttribute(String) |
| 101 | + */ |
| 102 | + public static String encodeForHTMLAttribute(String str) |
| 103 | + { |
| 104 | + return ESAPI.encoder().encodeForHTMLAttribute(str); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Encode string for use in JavaScript. |
| 109 | + * @param str The string to encode. |
| 110 | + * @return str encoded for use in JavaScript. |
| 111 | + * @see Encoder#encodeForJavaScript(String) |
| 112 | + */ |
| 113 | + public static String encodeForJavaScript(String str) |
| 114 | + { |
| 115 | + return ESAPI.encoder().encodeForJavaScript(str); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Encode string for use in a URL. |
| 120 | + * @param str The string to encode. |
| 121 | + * @return str encoded for use in a URL. |
| 122 | + * @see Encoder#encodeForURL(String) |
| 123 | + */ |
| 124 | + public static String encodeForURL(String str) throws EncodingException |
| 125 | + { |
| 126 | + return ESAPI.encoder().encodeForURL(str); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Encode string for use in VBScript. |
| 131 | + * @param str The string to encode. |
| 132 | + * @return str encoded for use in VBScript. |
| 133 | + * @see Encoder#encodeForVBScript(String) |
| 134 | + */ |
| 135 | + public static String encodeForVBScript(String str) |
| 136 | + { |
| 137 | + return ESAPI.encoder().encodeForVBScript(str); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Encode string for use in XML. |
| 142 | + * @param str The string to encode. |
| 143 | + * @return str encoded for use in XML. |
| 144 | + * @see Encoder#encodeForXML(String) |
| 145 | + */ |
| 146 | + public static String encodeForXML(String str) |
| 147 | + { |
| 148 | + return ESAPI.encoder().encodeForXML(str); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Encode string for use in a XML attribute. |
| 153 | + * @param str The string to encode. |
| 154 | + * @return str encoded for use in XML attribute. |
| 155 | + * @see Encoder#encodeForXMLAttribute(String) |
| 156 | + */ |
| 157 | + public static String encodeForXMLAttribute(String str) |
| 158 | + { |
| 159 | + return ESAPI.encoder().encodeForXMLAttribute(str); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Encode string for use in XPath. |
| 164 | + * @param str The string to encode. |
| 165 | + * @return str encoded for use in XPath. |
| 166 | + * @see Encoder#encodeForXPath(String) |
| 167 | + */ |
| 168 | + public static String encodeForXPath(String str) |
| 169 | + { |
| 170 | + return ESAPI.encoder().encodeForXPath(str); |
| 171 | + } |
| 172 | +} |
0 commit comments