use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class EhcacheAggregationRepositoryOperationTest method testRemoveExists.
@Test
public void testRemoveExists() {
// Given
String key = "Remove_Exists";
Exchange exchange = new DefaultExchange(context());
aggregationRepository.add(context(), key, exchange);
assertTrue(exists(key));
// When
aggregationRepository.remove(context(), key, exchange);
// Then
assertFalse(exists(key));
}
use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class EhcacheAggregationRepository method unmarshallExchange.
public static Exchange unmarshallExchange(CamelContext camelContext, DefaultExchangeHolder holder) {
Exchange exchange = null;
if (holder != null) {
exchange = new DefaultExchange(camelContext);
DefaultExchangeHolder.unmarshal(exchange, holder);
}
return exchange;
}
use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class FopEndpointTest method encryptPdfWithUserPassword.
@Test
public void encryptPdfWithUserPassword() throws Exception {
Endpoint endpoint = context().getEndpoint("fop:pdf");
Producer producer = endpoint.createProducer();
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setHeader("CamelFop.Encrypt.userPassword", "secret");
exchange.getIn().setBody(FopHelper.decorateTextWithXSLFO("Test Content"));
producer.process(exchange);
PDDocument document = getDocumentFrom(exchange);
assertTrue(document.isEncrypted());
}
use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class FopEndpointTest method overridePdfOutputFormatToPlainText.
@Test
public void overridePdfOutputFormatToPlainText() throws Exception {
String defaultOutputFormat = "pdf";
Endpoint endpoint = context().getEndpoint("fop:" + defaultOutputFormat);
Producer producer = endpoint.createProducer();
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setHeader(FopConstants.CAMEL_FOP_OUTPUT_FORMAT, "txt");
exchange.getIn().setBody(FopHelper.decorateTextWithXSLFO("Test Content"));
producer.process(exchange);
String plainText = exchange.getOut().getBody(String.class).trim();
assertEquals("Test Content", plainText);
}
use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class GenericFileProducer method processExchange.
/**
* Perform the work to process the fileExchange
*
* @param exchange fileExchange
* @param target the target filename
* @throws Exception is thrown if some error
*/
protected void processExchange(Exchange exchange, String target) throws Exception {
log.trace("Processing file: {} for exchange: {}", target, exchange);
try {
preWriteCheck();
// should we write to a temporary name and then afterwards rename to real target
boolean writeAsTempAndRename = ObjectHelper.isNotEmpty(endpoint.getTempFileName());
String tempTarget = null;
// remember if target exists to avoid checking twice
Boolean targetExists = null;
if (writeAsTempAndRename) {
// compute temporary name with the temp prefix
tempTarget = createTempFileName(exchange, target);
log.trace("Writing using tempNameFile: {}", tempTarget);
//if we should eager delete target file before deploying temporary file
if (endpoint.getFileExist() != GenericFileExist.TryRename && endpoint.isEagerDeleteTargetFile()) {
// cater for file exists option on the real target as
// the file operations code will work on the temp file
// if an existing file already exists what should we do?
targetExists = operations.existsFile(target);
if (targetExists) {
log.trace("EagerDeleteTargetFile, target exists");
if (endpoint.getFileExist() == GenericFileExist.Ignore) {
// ignore but indicate that the file was written
log.trace("An existing file already exists: {}. Ignore and do not override it.", target);
return;
} else if (endpoint.getFileExist() == GenericFileExist.Fail) {
throw new GenericFileOperationFailedException("File already exist: " + target + ". Cannot write new file.");
} else if (endpoint.isEagerDeleteTargetFile() && endpoint.getFileExist() == GenericFileExist.Override) {
// we override the target so we do this by deleting it so the temp file can be renamed later
// with success as the existing target file have been deleted
log.trace("Eagerly deleting existing file: {}", target);
if (!operations.deleteFile(target)) {
throw new GenericFileOperationFailedException("Cannot delete file: " + target);
}
}
}
}
// delete any pre existing temp file
if (operations.existsFile(tempTarget)) {
log.trace("Deleting existing temp file: {}", tempTarget);
if (!operations.deleteFile(tempTarget)) {
throw new GenericFileOperationFailedException("Cannot delete file: " + tempTarget);
}
}
}
// write/upload the file
writeFile(exchange, tempTarget != null ? tempTarget : target);
// name after we have written the file
if (tempTarget != null) {
// if we did not eager delete the target file
if (endpoint.getFileExist() != GenericFileExist.TryRename && !endpoint.isEagerDeleteTargetFile()) {
// if an existing file already exists what should we do?
targetExists = operations.existsFile(target);
if (targetExists) {
log.trace("Not using EagerDeleteTargetFile, target exists");
if (endpoint.getFileExist() == GenericFileExist.Ignore) {
// ignore but indicate that the file was written
log.trace("An existing file already exists: {}. Ignore and do not override it.", target);
return;
} else if (endpoint.getFileExist() == GenericFileExist.Fail) {
throw new GenericFileOperationFailedException("File already exist: " + target + ". Cannot write new file.");
} else if (endpoint.getFileExist() == GenericFileExist.Override) {
// we override the target so we do this by deleting it so the temp file can be renamed later
// with success as the existing target file have been deleted
log.trace("Deleting existing file: {}", target);
if (!operations.deleteFile(target)) {
throw new GenericFileOperationFailedException("Cannot delete file: " + target);
}
}
}
}
// now we are ready to rename the temp file to the target file
log.trace("Renaming file: [{}] to: [{}]", tempTarget, target);
boolean renamed = operations.renameFile(tempTarget, target);
if (!renamed) {
throw new GenericFileOperationFailedException("Cannot rename file from: " + tempTarget + " to: " + target);
}
}
// any done file to write?
if (endpoint.getDoneFileName() != null) {
String doneFileName = endpoint.createDoneFileName(target);
ObjectHelper.notEmpty(doneFileName, "doneFileName", endpoint);
// create empty exchange with empty body to write as the done file
Exchange empty = new DefaultExchange(exchange);
empty.getIn().setBody("");
log.trace("Writing done file: [{}]", doneFileName);
// delete any existing done file
if (operations.existsFile(doneFileName)) {
if (!operations.deleteFile(doneFileName)) {
throw new GenericFileOperationFailedException("Cannot delete existing done file: " + doneFileName);
}
}
writeFile(empty, doneFileName);
}
// let's store the name we really used in the header, so end-users
// can retrieve it
exchange.getIn().setHeader(Exchange.FILE_NAME_PRODUCED, target);
} catch (Exception e) {
handleFailedWrite(exchange, e);
}
postWriteCheck(exchange);
}
Aggregations