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