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.mock.http;
15  
16  import java.io.ByteArrayOutputStream;
17  import java.io.IOException;
18  import java.io.PrintWriter;
19  import java.io.UnsupportedEncodingException;
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.servlet.ServletOutputStream;
26  import javax.servlet.http.HttpServletResponse;
27  import javax.servlet.http.HttpServletResponseWrapper;
28  
29  import org.mockito.Mockito;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import com.google.common.base.Joiner;
34  
35  /**
36   * The Class SimpleHttpResponse.
37   *
38   * @author dblock[at]dblock[dot]org
39   */
40  public class SimpleHttpResponse extends HttpServletResponseWrapper {
41  
42      /** The Constant LOGGER. */
43      private static final Logger       LOGGER  = LoggerFactory.getLogger(SimpleHttpResponse.class);
44  
45      /** The status. */
46      private int                       status  = 500;
47      
48      /** The headers. */
49      private final Map<String, List<String>> headers = new HashMap<String, List<String>>();
50  
51      /** The bytes. */
52      final ByteArrayOutputStream       bytes   = new ByteArrayOutputStream();
53  
54      /** The out. */
55      private final ServletOutputStream out     = new ServletOutputStream() {
56                                                    @Override
57                                                    public void write(final int b) throws IOException {
58                                                        SimpleHttpResponse.this.bytes.write(b);
59                                                    }
60                                                };
61  
62      /** The writer. */
63      private final PrintWriter         writer  = new PrintWriter(this.bytes);
64  
65      /**
66       * Instantiates a new simple http response.
67       */
68      public SimpleHttpResponse() {
69          super(Mockito.mock(HttpServletResponse.class));
70      }
71  
72      /**
73       * Gets the status.
74       *
75       * @return the status
76       */
77      public int getStatus() {
78          return this.status;
79      }
80  
81      /* (non-Javadoc)
82       * @see javax.servlet.http.HttpServletResponseWrapper#addHeader(java.lang.String, java.lang.String)
83       */
84      @Override
85      public void addHeader(final String headerName, final String headerValue) {
86          List<String> current = this.headers.get(headerName);
87          if (current == null) {
88              current = new ArrayList<String>();
89          }
90          current.add(headerValue);
91          this.headers.put(headerName, current);
92      }
93  
94      /* (non-Javadoc)
95       * @see javax.servlet.http.HttpServletResponseWrapper#setHeader(java.lang.String, java.lang.String)
96       */
97      @Override
98      public void setHeader(final String headerName, final String headerValue) {
99          List<String> current = this.headers.get(headerName);
100         if (current == null) {
101             current = new ArrayList<String>();
102         } else {
103             current.clear();
104         }
105         current.add(headerValue);
106         this.headers.put(headerName, current);
107     }
108 
109     /* (non-Javadoc)
110      * @see javax.servlet.http.HttpServletResponseWrapper#setStatus(int)
111      */
112     @Override
113     public void setStatus(final int value) {
114         this.status = value;
115     }
116 
117     /**
118      * Gets the status string.
119      *
120      * @return the status string
121      */
122     public String getStatusString() {
123         if (this.status == 401) {
124             return "Unauthorized";
125         }
126         return "Unknown";
127     }
128 
129     /* (non-Javadoc)
130      * @see javax.servlet.ServletResponseWrapper#flushBuffer()
131      */
132     @Override
133     public void flushBuffer() {
134         SimpleHttpResponse.LOGGER.info("{}: {}", Integer.valueOf(this.status), this.getStatusString());
135         for (final String header : this.headers.keySet()) {
136             for (final String headerValue : this.headers.get(header)) {
137                 SimpleHttpResponse.LOGGER.info("{}: {}", header, headerValue);
138             }
139         }
140     }
141 
142     /**
143      * Use this for testing the number of headers.
144      * 
145      * @return int header name size.
146      */
147     public int getHeaderNamesSize() {
148         return this.headers.size();
149     }
150 
151     /**
152      * Gets the header values.
153      *
154      * @param headerName
155      *            the header name
156      * @return the header values
157      */
158     public String[] getHeaderValues(final String headerName) {
159         final List<String> headerValues = this.headers.get(headerName);
160         return headerValues == null ? null : headerValues.toArray(new String[0]);
161     }
162 
163     /**
164      * Gets the header.
165      *
166      * @param headerName
167      *            the header name
168      * @return the header
169      */
170     public String getHeader(final String headerName) {
171         final List<String> headerValues = this.headers.get(headerName);
172         return headerValues == null ? null : Joiner.on(", ").join(headerValues);
173     }
174 
175     /* (non-Javadoc)
176      * @see javax.servlet.http.HttpServletResponseWrapper#sendError(int, java.lang.String)
177      */
178     @Override
179     public void sendError(final int rc, final String message) {
180         this.status = rc;
181     }
182 
183     /* (non-Javadoc)
184      * @see javax.servlet.http.HttpServletResponseWrapper#sendError(int)
185      */
186     @Override
187     public void sendError(final int rc) {
188         this.status = rc;
189     }
190 
191     /* (non-Javadoc)
192      * @see javax.servlet.ServletResponseWrapper#getWriter()
193      */
194     @Override
195     public PrintWriter getWriter() {
196         return this.writer;
197     }
198 
199     /* (non-Javadoc)
200      * @see javax.servlet.ServletResponseWrapper#getOutputStream()
201      */
202     @Override
203     public ServletOutputStream getOutputStream() throws IOException {
204         return this.out;
205     }
206 
207     /**
208      * Gets the output text.
209      *
210      * @return the output text
211      */
212     public String getOutputText() {
213         this.writer.flush();
214         try {
215             return this.bytes.toString("UTF-8");
216         } catch (final UnsupportedEncodingException e) {
217             SimpleHttpResponse.LOGGER.error("{}", e);
218         }
219         return null;
220     }
221 }