1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package waffle.apache.catalina;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.apache.catalina.connector.Response;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39 public class SimpleHttpResponse extends Response {
40
41
42 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleHttpResponse.class);
43
44
45 private int status = 500;
46
47
48 private final Map<String, List<String>> headers = new HashMap<>();
49
50 @Override
51 public void addHeader(final String headerName, final String headerValue) {
52 List<String> current = this.headers.get(headerName);
53 if (current == null) {
54 current = new ArrayList<>();
55 }
56 current.add(headerValue);
57 this.headers.put(headerName, current);
58 }
59
60 @Override
61 public void flushBuffer() {
62 SimpleHttpResponse.LOGGER.info("{} {}", Integer.valueOf(this.status), this.getStatusString());
63 for (final String header : this.headers.keySet()) {
64 for (final String headerValue : this.headers.get(header)) {
65 SimpleHttpResponse.LOGGER.info("{}: {}", header, headerValue);
66 }
67 }
68 }
69
70 @Override
71 public String getHeader(final String headerName) {
72 final List<String> headerValues = this.headers.get(headerName);
73 return headerValues == null ? null : String.join(", ", headerValues);
74 }
75
76 @Override
77 public Collection<String> getHeaderNames() {
78 return this.headers.keySet();
79 }
80
81
82
83
84
85
86
87
88
89 public String[] getHeaderValues(final String headerName) {
90 final List<String> headerValues = this.headers.get(headerName);
91 return headerValues == null ? null : headerValues.toArray(new String[0]);
92 }
93
94 @Override
95 public int getStatus() {
96 return this.status;
97 }
98
99
100
101
102
103
104 public String getStatusString() {
105 return this.status == 401 ? "Unauthorized" : "Unknown";
106 }
107
108 @Override
109 public void sendError(final int rc) {
110 this.status = rc;
111 }
112
113 @Override
114 public void sendError(final int rc, final String message) {
115 this.status = rc;
116 }
117
118 @Override
119 public void setHeader(final String headerName, final String headerValue) {
120 List<String> current = this.headers.get(headerName);
121 if (current == null) {
122 current = new ArrayList<>();
123 } else {
124 current.clear();
125 }
126 current.add(headerValue);
127 this.headers.put(headerName, current);
128 }
129
130 @Override
131 public void setStatus(final int value) {
132 this.status = value;
133 }
134
135 }