Search in sources :

Example 16 with DefaultExchange

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));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Test(org.junit.Test)

Example 17 with DefaultExchange

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;
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange)

Example 18 with DefaultExchange

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());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) Test(org.junit.Test)

Example 19 with DefaultExchange

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);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) Test(org.junit.Test)

Example 20 with DefaultExchange

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);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange)

Aggregations

DefaultExchange (org.apache.camel.impl.DefaultExchange)473 Exchange (org.apache.camel.Exchange)381 Test (org.junit.Test)254 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)127 CamelContext (org.apache.camel.CamelContext)54 RegisteredDelivery (org.jsmpp.bean.RegisteredDelivery)39 HashMap (java.util.HashMap)33 Message (org.apache.camel.Message)32 Before (org.junit.Before)32 Tx (org.nhindirect.common.tx.model.Tx)31 ESMClass (org.jsmpp.bean.ESMClass)30 Processor (org.apache.camel.Processor)22 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)22 Expression (org.apache.camel.Expression)21 File (java.io.File)20 DefaultMessage (org.apache.camel.impl.DefaultMessage)20 ArrayList (java.util.ArrayList)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 URL (java.net.URL)16 Date (java.util.Date)16