Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2011 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.tomakehurst.wiremock.extension.responsetemplating;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.helper.AssignHelper;
import com.github.jknack.handlebars.helper.ConditionalHelpers;
import com.github.jknack.handlebars.helper.NumberHelper;
import com.github.jknack.handlebars.helper.StringHelpers;
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.common.Exceptions;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.common.Json;
import com.github.tomakehurst.wiremock.common.TextFile;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformer;
import com.github.tomakehurst.wiremock.extension.StubLifecycleListener;
import com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.HandlebarsHelper;
import com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.SystemValueHelper;
import com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.WireMockHelpers;
import com.github.tomakehurst.wiremock.http.HttpHeader;
import com.github.tomakehurst.wiremock.http.HttpHeaders;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import com.google.common.base.Function;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;
import static com.google.common.base.MoreObjects.firstNonNull;
public class ResponseTemplateTransformer extends ResponseDefinitionTransformer implements StubLifecycleListener {
public static final String NAME = "response-template";
private final boolean global;
private final Handlebars handlebars;
private final Cache cache;
private final Long maxCacheEntries;
public static Builder builder() {
return new Builder();
}
public ResponseTemplateTransformer(boolean global) {
this(global, Collections.emptyMap());
}
public ResponseTemplateTransformer(boolean global, String helperName, Helper helper) {
this(global, ImmutableMap.of(helperName, helper));
}
public ResponseTemplateTransformer(boolean global, Map helpers) {
this(global, new Handlebars(), helpers, null, null);
}
public ResponseTemplateTransformer(boolean global, Handlebars handlebars, Map helpers, Long maxCacheEntries, Set permittedSystemKeys) {
this.global = global;
this.handlebars = handlebars;
for (StringHelpers helper: StringHelpers.values()) {
if (!helper.name().equals("now")) {
this.handlebars.registerHelper(helper.name(), helper);
}
}
for (NumberHelper helper: NumberHelper.values()) {
this.handlebars.registerHelper(helper.name(), helper);
}
for (ConditionalHelpers helper: ConditionalHelpers.values()) {
this.handlebars.registerHelper(helper.name(), helper);
}
this.handlebars.registerHelper(AssignHelper.NAME, new AssignHelper());
//Add all available wiremock helpers
for (WireMockHelpers helper: WireMockHelpers.values()) {
this.handlebars.registerHelper(helper.name(), helper);
}
this.handlebars.registerHelper("systemValue", new SystemValueHelper(new SystemKeyAuthoriser(permittedSystemKeys)));
for (Map.Entry entry: helpers.entrySet()) {
this.handlebars.registerHelper(entry.getKey(), entry.getValue());
}
this.maxCacheEntries = maxCacheEntries;
CacheBuilder