View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  package org.esigate.test.http;
16  
17  import java.io.UnsupportedEncodingException;
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.http.Header;
22  import org.apache.http.HttpEntity;
23  import org.apache.http.HttpStatus;
24  import org.apache.http.ProtocolVersion;
25  import org.apache.http.client.methods.CloseableHttpResponse;
26  import org.apache.http.entity.StringEntity;
27  import org.apache.http.message.BasicHeader;
28  import org.apache.http.message.BasicHttpResponse;
29  import org.esigate.http.BasicCloseableHttpResponse;
30  
31  /**
32   * Fluent-style builder for HttpResponse.
33   * 
34   * <p>
35   * Default response is
36   * 
37   * <pre>
38   * 200 OK  HTTP/1.1
39   * </pre>
40   * 
41   * @author Nicolas Richeton
42   * 
43   */
44  public class HttpResponseBuilder {
45  
46      private ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
47      private int status = HttpStatus.SC_OK;
48      private String reason = "Ok";
49      private List<Header> headers = new ArrayList<>();
50      private HttpEntity entity = null;
51  
52      /**
53       * Build the HTTP response using all data previously set on this builder and/or use defaults.
54       * 
55       * @return The HTTP response
56       */
57      public CloseableHttpResponse build() {
58          BasicHttpResponse response = new BasicHttpResponse(this.protocolVersion, this.status, this.reason);
59  
60          for (Header h : this.headers) {
61              response.addHeader(h.getName(), h.getValue());
62          }
63  
64          if (this.entity != null) {
65              response.setEntity(this.entity);
66          }
67          return BasicCloseableHttpResponse.adapt(response);
68      }
69  
70      public HttpResponseBuilder entity(HttpEntity paramEntity) {
71          this.entity = paramEntity;
72          if (this.entity.getContentType() != null) {
73              this.headers.add(this.entity.getContentType());
74          }
75          return this;
76      }
77  
78      public HttpResponseBuilder entity(String entityBody) throws UnsupportedEncodingException {
79          this.entity = new StringEntity(entityBody);
80          return this;
81      }
82  
83      public HttpResponseBuilder header(String name, String value) {
84          this.headers.add(new BasicHeader(name, value));
85          return this;
86      }
87  
88      public HttpResponseBuilder protocolVersion(ProtocolVersion paramProtocolVersion) {
89          this.protocolVersion = paramProtocolVersion;
90          return this;
91      }
92  
93      public HttpResponseBuilder reason(String paramReason) {
94          this.reason = paramReason;
95          return this;
96      }
97  
98      /**
99       * Set HTTP Status.
100      * 
101      * @param paramStatus
102      *            the response status.
103      * @return this builder
104      */
105     public HttpResponseBuilder status(int paramStatus) {
106         this.status = paramStatus;
107         return this;
108     }
109 }