View Javadoc
1   /**
2    * Waffle (https://github.com/dblock/waffle)
3    *
4    * Copyright (c) 2010 - 2015 Application Security, Inc.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/epl-v10.html
10   *
11   * Contributors:
12   *     Application Security, Inc.
13   */
14  package waffle.apache.catalina;
15  
16  import java.util.ArrayList;
17  import java.util.Collection;
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.catalina.connector.Response;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import com.google.common.base.Joiner;
27  
28  /**
29   * Simple HTTP Response.
30   * 
31   * @author dblock[at]dblock[dot]org
32   */
33  public class SimpleHttpResponse extends Response {
34  
35      private static final Logger             LOGGER  = LoggerFactory.getLogger(SimpleHttpResponse.class);
36  
37      private int                             status  = 500;
38      private final Map<String, List<String>> headers = new HashMap<String, List<String>>();
39  
40      @Override
41      public void addHeader(final String headerName, final String headerValue) {
42          List<String> current = this.headers.get(headerName);
43          if (current == null) {
44              current = new ArrayList<String>();
45          }
46          current.add(headerValue);
47          this.headers.put(headerName, current);
48      }
49  
50      @Override
51      public void flushBuffer() {
52          SimpleHttpResponse.LOGGER.info("{} {}", Integer.valueOf(this.status), this.getStatusString());
53          for (final String header : this.headers.keySet()) {
54              for (final String headerValue : this.headers.get(header)) {
55                  SimpleHttpResponse.LOGGER.info("{}: {}", header, headerValue);
56              }
57          }
58      }
59  
60      @Override
61      public String getHeader(final String headerName) {
62          final List<String> headerValues = this.headers.get(headerName);
63          return headerValues == null ? null : Joiner.on(", ").join(headerValues);
64      }
65  
66      @Override
67      public Collection<String> getHeaderNames() {
68          return this.headers.keySet();
69      }
70  
71      public String[] getHeaderValues(final String headerName) {
72          final List<String> headerValues = this.headers.get(headerName);
73          return headerValues == null ? null : headerValues.toArray(new String[0]);
74      }
75  
76      @Override
77      public int getStatus() {
78          return this.status;
79      }
80  
81      public String getStatusString() {
82          return this.status == 401 ? "Unauthorized" : "Unknown";
83      }
84  
85      @Override
86      public void sendError(final int rc) {
87          this.status = rc;
88      }
89  
90      @Override
91      public void sendError(final int rc, final String message) {
92          this.status = rc;
93      }
94  
95      @Override
96      public void setHeader(final String headerName, final String headerValue) {
97          List<String> current = this.headers.get(headerName);
98          if (current == null) {
99              current = new ArrayList<String>();
100         } else {
101             current.clear();
102         }
103         current.add(headerValue);
104         this.headers.put(headerName, current);
105     }
106 
107     @Override
108     public void setStatus(final int value) {
109         this.status = value;
110     }
111 }