-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathHttpContext.java
More file actions
194 lines (158 loc) · 7.25 KB
/
HttpContext.java
File metadata and controls
194 lines (158 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package solutions.bellatrix.data.http.httpContext;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.filter.log.LogDetail;
import io.restassured.specification.RequestSpecification;
import lombok.Getter;
import lombok.Setter;
import solutions.bellatrix.data.http.authentication.AuthSchemaFactory;
import solutions.bellatrix.data.http.authentication.AuthenticationMethod;
import solutions.bellatrix.data.http.configuration.HttpSettings;
import solutions.bellatrix.data.http.infrastructure.HTTPMethod;
import solutions.bellatrix.data.http.infrastructure.HttpHeader;
import solutions.bellatrix.data.http.infrastructure.QueryParameter;
import solutions.bellatrix.core.utilities.SecretsResolver;
import java.util.*;
import java.util.function.Consumer;
public class HttpContext {
private final HttpSettings httpSettings;
private final LinkedList<Object> pathParameters;
private final LinkedHashSet<QueryParameter> queryParameters;
private final LinkedHashSet<HttpHeader> httpHeaders;
@Setter private RequestSpecBuilder specBuilder;
@Getter private String requestBody;
@Getter private HTTPMethod httpMethod;
public HttpContext(HttpSettings settings) {
this.httpSettings = settings;
this.pathParameters = new LinkedList<>();
this.queryParameters = new LinkedHashSet<>();
this.httpHeaders = httpSettings.getHeaders();
this.specBuilder = createInitialSpecBuilder(httpSettings);
}
public HttpContext(HttpContext httpContext) {
this.httpSettings = httpContext.getHttpSettings();
this.queryParameters = httpContext.getQueryParameters();
this.pathParameters = httpContext.getPathParameters();
this.specBuilder = httpContext.getRequestBuilder();
this.httpHeaders = httpContext.getHeaders();
}
public HttpSettings getHttpSettings() {
return new HttpSettings(httpSettings);
}
public void updateRequestSpecification(Consumer<RequestSpecBuilder> specBuilderConsumer) {
specBuilderConsumer.accept(specBuilder);
}
public void addRequestBody(String body) {
this.requestBody = body;
}
public void addRequestMethod(HTTPMethod httpMethod) {
this.httpMethod = httpMethod;
}
public LinkedList<Object> getPathParameters() {
return new LinkedList<>(pathParameters);
}
public LinkedHashSet<HttpHeader> getHeaders() {
return new LinkedHashSet<>(httpHeaders);
}
public LinkedHashSet<QueryParameter> getQueryParameters() {
return new LinkedHashSet<>(queryParameters);
}
private RequestSpecBuilder getRequestBuilder() {
return new RequestSpecBuilder().addRequestSpecification(specBuilder.build());
}
public RequestSpecification requestSpecification() {
if (Objects.nonNull(requestBody)) {
specBuilder.setBody(requestBody);
}
// Always add query parameters (including authentication parameters)
specBuilder.addQueryParams(getRequestQueryParameters());
specBuilder.addHeaders(getRequestHeaders());
specBuilder.setBasePath(buildRequestPath());
return specBuilder.build();
}
public void addPathParameter(Object param) {
if (Objects.isNull(param)) {
throw new IllegalArgumentException("Path parameter cannot be null.");
}
pathParameters.add(String.valueOf(param));
}
public void addPathParameters(Collection<Object> params) {
for (Object param : params) {
addPathParameter(param);
}
}
public void addQueryParameters(Collection<QueryParameter> parameters) {
parameters.forEach(this::addQueryParameter);
}
public void addHeader(HttpHeader httpHeader) {
httpHeaders.add(httpHeader);
}
public void addHeaders(Collection<HttpHeader> headers) {
headers.forEach(this::addHeader);
}
public void addQueryParameter(QueryParameter parameter) {
queryParameters.add(parameter);
}
private Map<String, String> getRequestHeaders() {
LinkedHashMap<String, String> requestHeaders = new LinkedHashMap<>();
httpHeaders.forEach(x -> {
requestHeaders.put(x.getName(), x.getValue());
});
return requestHeaders;
}
private Map<String, String> getRequestQueryParameters() {
//todo: this logic should be extracted because create tightly coupling with settings
LinkedHashMap<String, String> queryParams = new LinkedHashMap<>();
if (httpSettings.getAuthenticationMethod() == AuthenticationMethod.QUERY_PARAMETER) {
var option = httpSettings.getAuthentication().getAuthenticationOptions().stream().filter(x -> x.get("type").equals("QueryParameters")).findFirst().get();
String insertionOrder = (String)option.get("insertionOrder");
if (insertionOrder.equals("start")) {
for (var key : option.keySet()) {
if (!key.equals("type") && !key.equals("insertionOrder")) {
String value = SecretsResolver.getSecret(option.get(key).toString());
queryParams.put(key, value);
}
}
for (QueryParameter queryParameter : queryParameters) {
queryParams.put(queryParameter.getKey(), String.valueOf(queryParameter.getValue()));
}
} else {
for (QueryParameter queryParameter : queryParameters) {
queryParams.put(queryParameter.getKey(), queryParameter.getValue().toString());
}
for (var key : option.keySet()) {
if (!key.equals("type") && !key.equals("insertionOrder")) {
String value = SecretsResolver.getSecret(option.get(key).toString());
queryParams.put(key, value);
}
}
}
return queryParams;
}
for (QueryParameter queryParameter : queryParameters) {
queryParams.put(queryParameter.getKey(), queryParameter.getValue().toString());
}
return queryParams;
}
public String buildRequestPath() {
if (!pathParameters.isEmpty()) {
String[] pathList = pathParameters.stream().filter(path -> !String.valueOf(path).isEmpty()).map(String::valueOf).toArray(String[]::new);
String formattedPath = String.join("/", pathList);
return httpSettings.getBasePath() + "/%s".formatted(formattedPath);
}
return httpSettings.getBasePath();
}
private RequestSpecBuilder createInitialSpecBuilder(HttpSettings httpSettings) {
LinkedHashMap<String, String> initialHeaders = new LinkedHashMap<>();
httpHeaders.forEach(x -> {
initialHeaders.put(x.getName(), x.getValue());
});
return new RequestSpecBuilder()
.setBaseUri(httpSettings.getBaseUrl())
.addHeaders(initialHeaders)
.setBasePath(httpSettings.getBasePath())
.setContentType(httpSettings.getContentType())
.setAuth(AuthSchemaFactory.getAuthenticationScheme(httpSettings.getAuthentication()))
.setUrlEncodingEnabled(httpSettings.isUrlEncoderEnabled())
.log(LogDetail.ALL);
}
}