Guest User

Untitled

a guest
Dec 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. package br.com.caelum.fj91.seguranca;
  2.  
  3. import java.beans.FeatureDescriptor;
  4. import java.util.Iterator;
  5.  
  6. import javax.el.ELContext;
  7. import javax.el.ELResolver;
  8. import javax.servlet.ServletContext;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.jsp.JspContext;
  11. import javax.servlet.jsp.JspFactory;
  12.  
  13. import org.springframework.boot.web.servlet.ServletContextInitializer;
  14. import org.springframework.context.annotation.Configuration;
  15.  
  16. @Configuration
  17. public class EscapeXmlELResolverListener implements ServletContextInitializer {
  18.  
  19. @Override
  20. public void onStartup(ServletContext servletContext) throws ServletException {
  21. JspFactory.getDefaultFactory().getJspApplicationContext(servletContext)
  22. .addELResolver(new EscapeXmlELResolver());
  23. }
  24.  
  25. static class EscapeXmlELResolver extends ELResolver {
  26.  
  27. /** pageContext attribute name for flag to enable XML escaping */
  28. static final String ESCAPE_XML_ATTRIBUTE = EscapeXmlELResolver.class.getName() + ".escapeXml";
  29.  
  30. private ThreadLocal<Boolean> excludeMe = new ThreadLocal<Boolean>() {
  31. @Override
  32. protected Boolean initialValue() {
  33. return Boolean.FALSE;
  34. }
  35. };
  36.  
  37. @Override
  38. public Class<?> getCommonPropertyType(ELContext context, Object base) {
  39. return null;
  40. }
  41.  
  42. @Override
  43. public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
  44. return null;
  45. }
  46.  
  47. @Override
  48. public Class<?> getType(ELContext context, Object base, Object property) {
  49. return null;
  50. }
  51.  
  52. @Override
  53. public Object getValue(ELContext context, Object base, Object property) {
  54. JspContext pageContext = (JspContext) context.getContext(JspContext.class);
  55. Boolean escapeXml = (Boolean) pageContext.getAttribute(ESCAPE_XML_ATTRIBUTE);
  56. if (escapeXml != null && !escapeXml) {
  57. return null;
  58. }
  59.  
  60. try {
  61. if (excludeMe.get()) {
  62. return null;
  63. }
  64.  
  65. // This resolver is in the original resolver chain. To prevent
  66. // infinite recursion, set a flag to prevent this resolver from
  67. // invoking the original resolver chain again when its turn in
  68. // the
  69. // chain comes around.
  70. excludeMe.set(Boolean.TRUE);
  71. Object value = context.getELResolver().getValue(context, base, property);
  72.  
  73. if (value instanceof String) {
  74. value = EscapeXml.escape((String) value);
  75. }
  76. return value;
  77.  
  78. } finally {
  79. excludeMe.remove();
  80. }
  81. }
  82.  
  83. @Override
  84. public boolean isReadOnly(ELContext context, Object base, Object property) {
  85. return true;
  86. }
  87.  
  88. @Override
  89. public void setValue(ELContext context, Object base, Object property, Object value) {
  90. }
  91. }
  92.  
  93. static class EscapeXml {
  94.  
  95. private static final String[] ESCAPES;
  96.  
  97. static {
  98. int size = '>' + 1; // '>' is the largest escaped value
  99. ESCAPES = new String[size];
  100. ESCAPES['<'] = "<";
  101. ESCAPES['>'] = ">";
  102. ESCAPES['&'] = "&";
  103. ESCAPES['\''] = "&#039;";
  104. ESCAPES['"'] = """;
  105. }
  106.  
  107. private static String getEscape(char c) {
  108. if (c < ESCAPES.length) {
  109. return ESCAPES[c];
  110. } else {
  111. return null;
  112. }
  113. }
  114.  
  115. /**
  116. * Escape a string.
  117. *
  118. * @param src
  119. * the string to escape; must not be null
  120. * @return the escaped string
  121. */
  122. public static String escape(String src) {
  123. // first pass to determine the length of the buffer so we only
  124. // allocate once
  125. int length = 0;
  126. for (int i = 0; i < src.length(); i++) {
  127. char c = src.charAt(i);
  128. String escape = getEscape(c);
  129. if (escape != null) {
  130. length += escape.length();
  131. } else {
  132. length += 1;
  133. }
  134. }
  135.  
  136. // skip copy if no escaping is needed
  137. if (length == src.length()) {
  138. return src;
  139. }
  140.  
  141. // second pass to build the escaped string
  142. StringBuilder buf = new StringBuilder(length);
  143. for (int i = 0; i < src.length(); i++) {
  144. char c = src.charAt(i);
  145. String escape = getEscape(c);
  146. if (escape != null) {
  147. buf.append(escape);
  148. } else {
  149. buf.append(c);
  150. }
  151. }
  152. return buf.toString();
  153. }
  154. }
  155. }
Add Comment
Please, Sign In to add comment