Skip to content

Commit

Permalink
Added the option to add dynamic time values for http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Miggets7 committed Feb 26, 2020
1 parent 823ae67 commit 89a8572
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ public interface Protocol extends ContainerService {

String DYNAMIC_VALUE_PLACEHOLDER_REGEXP = "\"?\\{\\$value}\"?";

//Specify to use time. First # delimiter used to specify format. Second # delimiter used to add or subtract millis
String DYNAMIC_TIME_PLACEHOLDER_REGEXP = "\"?\\{\\$time#?([a-z,A-Z,\\-,\\s,:]*)#?(-?\\d*)?}\"?";

/**
* Get the name for this protocol
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ protected WebTarget createRequestTarget(String path) {
}

if (queryParameters != null) {
requestTarget.register(new QueryParameterInjectorFilter(queryParameters, dynamicQueryParameters ? DYNAMIC_VALUE_PLACEHOLDER_REGEXP : null));
requestTarget.register(new QueryParameterInjectorFilter(queryParameters, dynamicQueryParameters ? DYNAMIC_VALUE_PLACEHOLDER_REGEXP : null,
DYNAMIC_TIME_PLACEHOLDER_REGEXP));
}

return requestTarget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* A filter for injecting query parameters into the request URI. If {@link #dynamicPlaceholderRegex} is set any request
Expand All @@ -39,11 +44,13 @@ public class QueryParameterInjectorFilter implements ClientRequestFilter {
protected MultivaluedMap<String, String> queryParameters;
protected String dynamicPlaceholderRegex;
protected boolean dynamic;
protected String dynamicTimePlaceHolderRegex;

public QueryParameterInjectorFilter(MultivaluedMap<String, String> queryParameters,
String dynamicPlaceholderRegex) {
String dynamicPlaceholderRegex, String dynamicTimePlaceHolderRegex) {
this.queryParameters = queryParameters;
this.dynamicPlaceholderRegex = dynamicPlaceholderRegex;
this.dynamicTimePlaceHolderRegex = dynamicTimePlaceHolderRegex;

dynamic = queryParameters != null && dynamicPlaceholderRegex != null
&& queryParameters
Expand Down Expand Up @@ -78,6 +85,24 @@ public void filter(ClientRequestContext requestContext) throws IOException {
valueArr[i] = valueArr[i].replaceAll(dynamicPlaceholderRegex, finalDynamicValue);
}
}
if (dynamicTimePlaceHolderRegex != null) {
for (int i = 0; i < valueArr.length; i++) {
if (valueArr[i].matches(dynamicTimePlaceHolderRegex)) {
Matcher matcher = Pattern.compile(dynamicTimePlaceHolderRegex).matcher(valueArr[i]);
long millisToAdd = 0;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
if (matcher.find()) {
if (matcher.groupCount() > 0) {
dateTimeFormatter = DateTimeFormatter.ofPattern(matcher.group(1));
}
if (matcher.groupCount() == 2) {
millisToAdd = Long.parseLong(matcher.group(2));
}
}
valueArr[i] = dateTimeFormatter.format(Instant.now().plusMillis(millisToAdd).atZone(ZoneId.systemDefault()));
}
}
}
uriBuilder.queryParam(name, (Object[]) valueArr);
});
requestContext.setUri(uriBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public ResteasyWebTarget build() {
}

if (injectQueryParameters != null) {
target.register(new QueryParameterInjectorFilter(injectQueryParameters, null));
target.register(new QueryParameterInjectorFilter(injectQueryParameters, null, null));
}

if (followRedirects) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class WebTargetTest extends Specification {
params.add("param1", "param1Value2")
params.add("param3", "param3Value1")
response = target.path("get")
.register(new QueryParameterInjectorFilter(params, null))
.register(new QueryParameterInjectorFilter(params, null, null))
.request()
.get()

Expand All @@ -489,7 +489,7 @@ class WebTargetTest extends Specification {
and: "a web target for a server"
def target = new WebTargetBuilder(client, new URIBuilder("https://dynamicparamserver").build())
.build()
.register(new QueryParameterInjectorFilter(params, /"{0,1}\{\$(value)\}"{0,1}/))
.register(new QueryParameterInjectorFilter(params, /"{0,1}\{\$(value)\}"{0,1}/, null))

when: "a request is made to the server"
def response = target.request()
Expand Down Expand Up @@ -524,4 +524,4 @@ class WebTargetTest extends Specification {
// def str = response.readEntity(String.class);
// assert response.getStatus() == 200
// }
}
}

0 comments on commit 89a8572

Please sign in to comment.