1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.voting.voters;
35
36 import info.magnolia.context.MgnlContext;
37
38 import java.util.ArrayList;
39 import java.util.List;
40
41 import javax.servlet.http.HttpServletResponse;
42
43 import org.apache.commons.lang3.StringUtils;
44
45
46
47
48
49
50
51 public class ResponseContentTypeVoter extends AbstractBoolVoter {
52 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ResponseContentTypeVoter.class);
53
54 private final List<String> allowed = new ArrayList<String>();
55 private final List<String> rejected = new ArrayList<String>();
56
57 public List<String> getAllowed() {
58 return allowed;
59 }
60
61 public void setAllowed(List<String> allowed) {
62 this.allowed.addAll(allowed);
63 }
64
65 public void addAllowed(String contentType) {
66 allowed.add(contentType);
67 }
68
69 public List<String> getRejected() {
70 return rejected;
71 }
72
73 public void setRejected(List<String> rejected) {
74 this.rejected.addAll(rejected);
75 }
76
77 public void addRejected(String contentType) {
78 rejected.add(contentType);
79 }
80
81 @Override
82 protected boolean boolVote(Object value) {
83 final HttpServletResponse response;
84 if (value instanceof HttpServletResponse) {
85 response = (HttpServletResponse) value;
86 } else {
87 if (MgnlContext.isWebContext()) {
88 response = MgnlContext.getWebContext().getResponse();
89 } else {
90 return false;
91 }
92 }
93
94
95 final String contentType = StringUtils.substringBefore(response.getContentType(), ";");
96 if (contentType == null) {
97 log.warn("No content type set on the response, can't vote.");
98 return false;
99 }
100 if (allowed.size() > 0 && !allowed.contains(contentType)) {
101 return false;
102 }
103 if (rejected.size() > 0 && rejected.contains(contentType)) {
104 return false;
105 }
106 return true;
107 }
108 }