View Javadoc
1   /*
2    * SPDX-License-Identifier: MIT
3    * See LICENSE file for details.
4    *
5    * Copyright 2010-2026 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
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   * Simple HTTP Response.
21   */
22  public class SimpleHttpResponse extends Response {
23  
24      /**
25       * Instantiates a new simple http response.
26       *
27       * @param coyoteResponse
28       *            the coyote response
29       */
30      public SimpleHttpResponse(final org.apache.coyote.Response coyoteResponse) {
31          super(coyoteResponse);
32      }
33  
34      /** The Constant LOGGER. */
35      private static final Logger LOGGER = LoggerFactory.getLogger(SimpleHttpResponse.class);
36  
37      /** The status. */
38      private int status = 500;
39  
40      /** The headers. */
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       * Gets the header values.
76       *
77       * @param headerName
78       *            the header name
79       *
80       * @return the header values
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       * Gets the status string.
94       *
95       * @return the status string
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 }