View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2024 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
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   * Simple HTTP Response.
38   */
39  public class SimpleHttpResponse extends Response {
40  
41      /** The Constant LOGGER. */
42      private static final Logger LOGGER = LoggerFactory.getLogger(SimpleHttpResponse.class);
43  
44      /** The status. */
45      private int status = 500;
46  
47      /** The headers. */
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       * Gets the header values.
83       *
84       * @param headerName
85       *            the header name
86       *
87       * @return the header values
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      * Gets the status string.
101      *
102      * @return the status string
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 }