use of com.github.tomakehurst.wiremock.common.TextFile in project xs2a-adapter by adorsys.
the class JarReadingClasspathFileSource method listFilesRecursively.
@Override
public List<TextFile> listFilesRecursively() {
if (isFileSystem()) {
assertExistsAndIsDirectory();
List<File> fileList = newArrayList();
recursivelyAddFilesToList(rootDirectory, fileList);
return toTextFileList(fileList);
}
List<TextFile> files = new ArrayList<>();
ZipEntry zipEntry = warFile.getEntry(jarFileName);
try (InputStream zipFileStream = warFile.getInputStream(zipEntry)) {
final ZipInputStream zipInputStream = new ZipInputStream(zipFileStream);
ZipEntry nestedZipEntry;
while ((nestedZipEntry = zipInputStream.getNextEntry()) != null) {
if (!nestedZipEntry.isDirectory() && nestedZipEntry.getName().startsWith(path)) {
files.add(new TextFile(new JarClassPathResource(nestedZipEntry.getName()).getURI()));
}
}
} catch (IOException e) {
// log.error("Unable to read war file", e);
}
return files;
}
use of com.github.tomakehurst.wiremock.common.TextFile in project wiremock by wiremock.
the class GetAllStubFilesTask method execute.
@Override
public ResponseDefinition execute(Admin admin, Request request, PathParams pathParams) {
FileSource fileSource = admin.getOptions().filesRoot().child(FILES_ROOT);
List<String> filePaths = new ArrayList<>();
for (TextFile textFile : fileSource.listFilesRecursively()) {
filePaths.add(textFile.getPath());
}
Collections.sort(filePaths);
return ResponseDefinition.okForJson(filePaths);
}
use of com.github.tomakehurst.wiremock.common.TextFile in project wiremock by wiremock.
the class RemoteMappingsLoader method load.
public void load() {
Iterable<TextFile> mappingFiles = filter(mappingsFileSource.listFilesRecursively(), byFileExtension("json"));
for (TextFile mappingFile : mappingFiles) {
StubMapping mapping = StubMapping.buildFrom(mappingFile.readContentsAsString());
convertBodyFromFileIfNecessary(mapping);
wireMock.register(mapping);
}
}
use of com.github.tomakehurst.wiremock.common.TextFile in project wiremock by wiremock.
the class RemoteMappingsLoader method convertBodyFromFileIfNecessary.
private void convertBodyFromFileIfNecessary(StubMapping mapping) {
String bodyFileName = mapping.getResponse().getBodyFileName();
if (bodyFileName != null) {
ResponseDefinitionBuilder responseDefinitionBuilder = ResponseDefinitionBuilder.like(mapping.getResponse()).withBodyFile(null);
String extension = substringAfterLast(bodyFileName, ".");
String mimeType = getMimeType(mapping);
if (ContentTypes.determineIsText(extension, mimeType)) {
TextFile bodyFile = filesFileSource.getTextFileNamed(bodyFileName);
responseDefinitionBuilder.withBody(bodyFile.readContentsAsString());
} else {
BinaryFile bodyFile = filesFileSource.getBinaryFileNamed(bodyFileName);
responseDefinitionBuilder.withBody(bodyFile.readContents());
}
mapping.setResponse(responseDefinitionBuilder.build());
}
}
use of com.github.tomakehurst.wiremock.common.TextFile in project wiremock by wiremock.
the class ResponseTemplateTransformer method transform.
@Override
public ResponseDefinition transform(Request request, final ResponseDefinition responseDefinition, FileSource files, Parameters parameters) {
ResponseDefinitionBuilder newResponseDefBuilder = ResponseDefinitionBuilder.like(responseDefinition);
final ImmutableMap<String, Object> model = ImmutableMap.<String, Object>builder().put("parameters", firstNonNull(parameters, Collections.<String, Object>emptyMap())).put("request", RequestTemplateModel.from(request)).putAll(addExtraModelElements(request, responseDefinition, files, parameters)).build();
if (responseDefinition.specifiesTextBodyContent()) {
boolean isJsonBody = responseDefinition.getReponseBody().isJson();
HandlebarsOptimizedTemplate bodyTemplate = templateEngine.getTemplate(HttpTemplateCacheKey.forInlineBody(responseDefinition), responseDefinition.getTextBody());
applyTemplatedResponseBody(newResponseDefBuilder, model, bodyTemplate, isJsonBody);
} else if (responseDefinition.specifiesBodyFile()) {
HandlebarsOptimizedTemplate filePathTemplate = templateEngine.getUncachedTemplate(responseDefinition.getBodyFileName());
String compiledFilePath = uncheckedApplyTemplate(filePathTemplate, model);
boolean disableBodyFileTemplating = parameters.getBoolean("disableBodyFileTemplating", false);
if (disableBodyFileTemplating) {
newResponseDefBuilder.withBodyFile(compiledFilePath);
} else {
TextFile file = files.getTextFileNamed(compiledFilePath);
HandlebarsOptimizedTemplate bodyTemplate = templateEngine.getTemplate(HttpTemplateCacheKey.forFileBody(responseDefinition, compiledFilePath), file.readContentsAsString());
applyTemplatedResponseBody(newResponseDefBuilder, model, bodyTemplate, false);
}
}
if (responseDefinition.getHeaders() != null) {
Iterable<HttpHeader> newResponseHeaders = Iterables.transform(responseDefinition.getHeaders().all(), header -> {
ImmutableList.Builder<String> valueListBuilder = ImmutableList.builder();
int index = 0;
for (String headerValue : header.values()) {
HandlebarsOptimizedTemplate template = templateEngine.getTemplate(HttpTemplateCacheKey.forHeader(responseDefinition, header.key(), index++), headerValue);
valueListBuilder.add(uncheckedApplyTemplate(template, model));
}
return new HttpHeader(header.key(), valueListBuilder.build());
});
newResponseDefBuilder.withHeaders(new HttpHeaders(newResponseHeaders));
}
if (responseDefinition.getProxyBaseUrl() != null) {
HandlebarsOptimizedTemplate proxyBaseUrlTemplate = templateEngine.getTemplate(HttpTemplateCacheKey.forProxyUrl(responseDefinition), responseDefinition.getProxyBaseUrl());
String newProxyBaseUrl = uncheckedApplyTemplate(proxyBaseUrlTemplate, model);
ResponseDefinitionBuilder.ProxyResponseDefinitionBuilder newProxyResponseDefBuilder = newResponseDefBuilder.proxiedFrom(newProxyBaseUrl);
if (responseDefinition.getAdditionalProxyRequestHeaders() != null) {
Iterable<HttpHeader> newResponseHeaders = Iterables.transform(responseDefinition.getAdditionalProxyRequestHeaders().all(), header -> {
ImmutableList.Builder<String> valueListBuilder = ImmutableList.builder();
int index = 0;
for (String headerValue : header.values()) {
HandlebarsOptimizedTemplate template = templateEngine.getTemplate(HttpTemplateCacheKey.forHeader(responseDefinition, header.key(), index++), headerValue);
valueListBuilder.add(uncheckedApplyTemplate(template, model));
}
return new HttpHeader(header.key(), valueListBuilder.build());
});
HttpHeaders proxyHttpHeaders = new HttpHeaders(newResponseHeaders);
for (String key : proxyHttpHeaders.keys()) {
newProxyResponseDefBuilder.withAdditionalRequestHeader(key, proxyHttpHeaders.getHeader(key).firstValue());
}
}
return newProxyResponseDefBuilder.build();
} else {
return newResponseDefBuilder.build();
}
}
Aggregations