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      /**
42       * Instantiates a new simple http response.
43       *
44       * @param coyoteResponse
45       *            the coyote response
46       */
47      public SimpleHttpResponse(final org.apache.coyote.Response coyoteResponse) {
48          super(coyoteResponse);
49      }
50  
51      /** The Constant LOGGER. */
52      private static final Logger LOGGER = LoggerFactory.getLogger(SimpleHttpResponse.class);
53  
54      /** The status. */
55      private int status = 500;
56  
57      /** The headers. */
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       * Gets the header values.
93       *
94       * @param headerName
95       *            the header name
96       *
97       * @return the header values
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      * Gets the status string.
111      *
112      * @return the status string
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 }