1 package de.juplo.facebook.aspects;
2
3
4 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.aspectj.lang.annotation.Around;
6 import org.aspectj.lang.annotation.Aspect;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10
11
12
13
14
15 @Aspect
16 public class SanitizeAspect
17 {
18 private static final Logger log =
19 LoggerFactory.getLogger(SanitizeAspect.class);
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 @Around("set(String *) && args(in) && @annotation(sanitize)")
44 public void sanitize(
45 ProceedingJoinPoint jp,
46 String in,
47 Sanitize sanitize
48 )
49 throws Throwable
50 {
51 if (in == null)
52 {
53 jp.proceed(new Object[] { null });
54 return;
55 }
56
57 in = in.trim();
58 if ("".equals(in))
59 {
60 jp.proceed(new Object[] { null });
61 return;
62 }
63
64 StringBuilder out = new StringBuilder();
65 char current;
66
67 for (int i = 0; i < in.length(); i++)
68 {
69 current = in.charAt(i);
70 if ((current == 0x9) ||
71 (current == 0xA) ||
72 (current == 0xD) ||
73 ((current >= 0x20) && (current <= 0xD7FF)) ||
74 ((current >= 0xE000) && (current <= 0xFFFD)) ||
75 ((current >= 0x10000) && (current <= 0x10FFFF)))
76 out.append(current);
77 }
78 if (out.length() > sanitize.length())
79 {
80 log.error(
81 "Maximum length for attribute {} exceeded: should={}, was={}",
82 jp.getSignature().getName(),
83 sanitize.length(),
84 out.length()
85 );
86 if (sanitize.fail())
87 throw new RuntimeException("String is longer than " + sanitize.length());
88 else
89 out.setLength(sanitize.length());
90 }
91 jp.proceed(new Object[] { out.toString() });
92 }
93 }