Skip to content

Commit

Permalink
added headers in http server
Browse files Browse the repository at this point in the history
  • Loading branch information
harishb2k committed Dec 30, 2023
1 parent 1327e8e commit 5866a6d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void processRequest(Server server, Api api, RequestObject requestObject,
webClient
.get()
.uri(api.getUrlForRequestObject(requestObject, stringHelper))
.headers(consumerHeaders(requestObject, api.getHeaders()))
.headers(consumerHeaders(requestObject, api.getHeaders(), server.getHeaders()))
.retrieve()
.bodyToMono(byte[].class)
.doOnError(onErrorConsumer(server, api, observableEmitter, startTime))
Expand All @@ -79,7 +79,7 @@ public void processRequest(Server server, Api api, RequestObject requestObject,
webClient
.delete()
.uri(api.getUrlForRequestObject(requestObject, stringHelper))
.headers(consumerHeaders(requestObject, api.getHeaders()))
.headers(consumerHeaders(requestObject, api.getHeaders(), server.getHeaders()))
.retrieve()
.bodyToMono(byte[].class)
.doOnError(onErrorConsumer(server, api, observableEmitter, startTime))
Expand All @@ -91,7 +91,7 @@ public void processRequest(Server server, Api api, RequestObject requestObject,
webClient
.post()
.uri(api.getUrlForRequestObject(requestObject, stringHelper))
.headers(consumerHeaders(requestObject, api.getHeaders()))
.headers(consumerHeaders(requestObject, api.getHeaders(), server.getHeaders()))
.bodyValue(requestObject.getBody())
.retrieve()
.bodyToMono(byte[].class)
Expand All @@ -104,7 +104,7 @@ public void processRequest(Server server, Api api, RequestObject requestObject,
webClient
.put()
.uri(api.getUrlForRequestObject(requestObject, stringHelper))
.headers(consumerHeaders(requestObject, api.getHeaders()))
.headers(consumerHeaders(requestObject, api.getHeaders(), server.getHeaders()))
.bodyValue(requestObject.getBody())
.retrieve()
.bodyToMono(byte[].class)
Expand Down Expand Up @@ -143,8 +143,13 @@ private Consumer<Throwable> onErrorConsumer(Server server, Api api, ObservableEm
};
}

private Consumer<HttpHeaders> consumerHeaders(RequestObject requestObject, StringObjectMap apiHeaders) {
private Consumer<HttpHeaders> consumerHeaders(RequestObject requestObject, StringObjectMap apiHeaders, StringObjectMap serverHeaders) {
return httpHeaders -> {
if (serverHeaders != null) {
serverHeaders.forEach((key, value) -> {
httpHeaders.add(key, stringHelper.stringify(value));
});
}
requestObject.preProcessHeaders(apiHeaders);
requestObject.getHeaders().forEach((key, value) -> {
httpHeaders.add(key, stringHelper.stringify(value));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.github.devlibx.easy.http.config;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.gitbub.devlibx.easy.helper.map.StringObjectMap;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Map;

@Data
@NoArgsConstructor
Expand Down Expand Up @@ -60,6 +62,8 @@ public class Server implements Serializable {
*/
private int connectTimeout = 100;

private StringObjectMap headers;

public String getUrl() {
StringBuilder sb = new StringBuilder();
if (isHttps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ private <REQ_TYPE extends HttpRequestBase> ResponseObject internalProcess(Server
HttpRequestBase requestBase = func.apply(uri);
requestBase.setConfig(buildRequestConfig(server, api, requestObject));

// Take headers from server
if (server.getHeaders() != null) {
server.getHeaders().forEach((key, value) -> {
requestBase.addHeader(key, stringHelper.stringify(value));
});
}

// Add headers to the request
requestObject.preProcessHeaders(api.getHeaders());
requestObject.getHeaders().forEach((key, value) -> {
Expand Down Expand Up @@ -210,29 +217,14 @@ private RequestConfig buildRequestConfig(Server server, Api api, RequestObject r
if (api.getTimeoutDeltaFactor() > 0) {
socketTimeoutToBeUsed = (int) (socketTimeoutToBeUsed + (api.getTimeoutDeltaFactor() * socketTimeoutToBeUsed));
}
return RequestConfig.custom()
.setConnectTimeout(server.getConnectTimeout())
.setConnectionRequestTimeout(server.getConnectionRequestTimeout())
.setSocketTimeout(socketTimeoutToBeUsed)
.build();
return RequestConfig.custom().setConnectTimeout(server.getConnectTimeout()).setConnectionRequestTimeout(server.getConnectionRequestTimeout()).setSocketTimeout(socketTimeoutToBeUsed).build();
}

private URI generateURI(Server server, Api api, RequestObject request) throws URISyntaxException {
if (server.getPort() == -1) {
return new URIBuilder()
.setScheme(server.isHttps() ? "https" : "http")
.setHost(server.getHost())
.setPath(resolvePath(server, api, request))
.setParameters(getQueryParams(server, api, request))
.build();
return new URIBuilder().setScheme(server.isHttps() ? "https" : "http").setHost(server.getHost()).setPath(resolvePath(server, api, request)).setParameters(getQueryParams(server, api, request)).build();
}
return new URIBuilder()
.setScheme(server.isHttps() ? "https" : "http")
.setHost(server.getHost())
.setPort(server.getPort())
.setPath(resolvePath(server, api, request))
.setParameters(getQueryParams(server, api, request))
.build();
return new URIBuilder().setScheme(server.isHttps() ? "https" : "http").setHost(server.getHost()).setPort(server.getPort()).setPath(resolvePath(server, api, request)).setParameters(getQueryParams(server, api, request)).build();
}

@SuppressWarnings("deprecation")
Expand All @@ -250,9 +242,7 @@ private String resolvePath(Server server, Api api, RequestObject request) {
private List<NameValuePair> getQueryParams(Server server, Api api, RequestObject request) {
final List<NameValuePair> queryParams = new ArrayList<>();
if (null != request.getQueryParam()) {
request.getQueryParam()
.forEach((key, values) -> values
.forEach(value -> queryParams.add(new BasicNameValuePair(key, stringHelper.stringify(value)))));
request.getQueryParam().forEach((key, values) -> values.forEach(value -> queryParams.add(new BasicNameValuePair(key, stringHelper.stringify(value)))));
}
return queryParams;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public void setup(Config config, IMetrics metricsObj) {
metrics = new IMetrics.NoOpMetrics();
}

if (metrics == null) {
metrics = new IMetrics.NoOpMetrics();
}

// Make server registry
ServerRegistry serverRegistry = ApplicationContext.getInstance(ServerRegistry.class);
serverRegistry.configure(config);
Expand Down

0 comments on commit 5866a6d

Please sign in to comment.