org.citrusframework.message.builder.FileResourceHeaderDataBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of citrus-base Show documentation
Show all versions of citrus-base Show documentation
Citrus base and default implementation
The newest version!
/*
* Copyright the original author or authors.
*
* 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 org.citrusframework.message.builder;
import java.io.IOException;
import java.nio.charset.Charset;
import org.citrusframework.CitrusSettings;
import org.citrusframework.context.TestContext;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.message.MessageHeaderDataBuilder;
import org.citrusframework.util.FileUtils;
/**
* @author Christoph Deppisch
*/
public class FileResourceHeaderDataBuilder implements MessageHeaderDataBuilder {
private final String charsetName;
private final String resourcePath;
/**
* Constructor using file resource path and default charset.
* @param resourcePath
*/
public FileResourceHeaderDataBuilder(String resourcePath) {
this(resourcePath, CitrusSettings.CITRUS_FILE_ENCODING);
}
/**
* Constructor using file resource path and charset.
* @param resourcePath
* @param charsetName
*/
public FileResourceHeaderDataBuilder(String resourcePath, String charsetName) {
this.charsetName = charsetName;
this.resourcePath = resourcePath;
}
@Override
public String buildHeaderData(TestContext context) {
try {
return context.replaceDynamicContentInString(FileUtils.readToString(
FileUtils.getFileResource(resourcePath, context),
Charset.forName(context.resolveDynamicValue(charsetName))));
} catch (final IOException e) {
throw new CitrusRuntimeException("Failed to read message header data resource", e);
}
}
}