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
43
44
45
46
47 public SimpleHttpResponse(final org.apache.coyote.Response coyoteResponse) {
48 super(coyoteResponse);
49 }
50
51
52 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleHttpResponse.class);
53
54
55 private int status = 500;
56
57
58 private final Map<String, List<String>> headers = new HashMap<>();
59
60 @Override
61 public void addHeader(final String headerName, final String headerValue) {
62 List<String> current = this.headers.get(headerName);
63 if (current == null) {
64 current = new ArrayList<>();
65 }
66 current.add(headerValue);
67 this.headers.put(headerName, current);
68 }
69
70 @Override
71 public void flushBuffer() {
72 SimpleHttpResponse.LOGGER.info("{} {}", Integer.valueOf(this.status), this.getStatusString());
73 for (final String header : this.headers.keySet()) {
74 for (final String headerValue : this.headers.get(header)) {
75 SimpleHttpResponse.LOGGER.info("{}: {}", header, headerValue);
76 }
77 }
78 }
79
80 @Override
81 public String getHeader(final String headerName) {
82 final List<String> headerValues = this.headers.get(headerName);
83 return headerValues == null ? null : String.join(", ", headerValues);
84 }
85
86 @Override
87 public Collection<String> getHeaderNames() {
88 return this.headers.keySet();
89 }
90
91
92
93
94
95
96
97
98
99 public String[] getHeaderValues(final String headerName) {
100 final List<String> headerValues = this.headers.get(headerName);
101 return headerValues == null ? null : headerValues.toArray(new String[0]);
102 }
103
104 @Override
105 public int getStatus() {
106 return this.status;
107 }
108
109
110
111
112
113
114 public String getStatusString() {
115 return this.status == 401 ? "Unauthorized" : "Unknown";
116 }
117
118 @Override
119 public void sendError(final int rc) {
120 this.status = rc;
121 }
122
123 @Override
124 public void sendError(final int rc, final String message) {
125 this.status = rc;
126 }
127
128 @Override
129 public void setHeader(final String headerName, final String headerValue) {
130 List<String> current = this.headers.get(headerName);
131 if (current == null) {
132 current = new ArrayList<>();
133 } else {
134 current.clear();
135 }
136 current.add(headerValue);
137 this.headers.put(headerName, current);
138 }
139
140 @Override
141 public void setStatus(final int value) {
142 this.status = value;
143 }
144
145 }