1
2
3
4
5
6
7
8
9
10
11
12
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
33
34
35
36
37
38
39
40
41
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
54
55
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
100
101
102
103
104
105 public HttpResponseBuilder status(int paramStatus) {
106 this.status = paramStatus;
107 return this;
108 }
109 }