Search in sources :

Example 1 with TextFile

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;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) TextFile(com.github.tomakehurst.wiremock.common.TextFile) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) IOException(java.io.IOException) TextFile(com.github.tomakehurst.wiremock.common.TextFile) BinaryFile(com.github.tomakehurst.wiremock.common.BinaryFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 2 with TextFile

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);
}
Also used : FileSource(com.github.tomakehurst.wiremock.common.FileSource) ArrayList(java.util.ArrayList) TextFile(com.github.tomakehurst.wiremock.common.TextFile)

Example 3 with TextFile

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);
    }
}
Also used : TextFile(com.github.tomakehurst.wiremock.common.TextFile) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping)

Example 4 with TextFile

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());
    }
}
Also used : ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder) TextFile(com.github.tomakehurst.wiremock.common.TextFile) BinaryFile(com.github.tomakehurst.wiremock.common.BinaryFile)

Example 5 with TextFile

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();
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) TextFile(com.github.tomakehurst.wiremock.common.TextFile) ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)

Aggregations

TextFile (com.github.tomakehurst.wiremock.common.TextFile)5 ResponseDefinitionBuilder (com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)2 BinaryFile (com.github.tomakehurst.wiremock.common.BinaryFile)2 ArrayList (java.util.ArrayList)2 FileSource (com.github.tomakehurst.wiremock.common.FileSource)1 StubMapping (com.github.tomakehurst.wiremock.stubbing.StubMapping)1 ImmutableList (com.google.common.collect.ImmutableList)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ZipInputStream (java.util.zip.ZipInputStream)1