Search in sources :

Example 31 with GenericFile

use of org.apache.camel.component.file.GenericFile in project camel by apache.

the class FileLanguageTest method testFileNameDoubleExtension.

public void testFileNameDoubleExtension() throws Exception {
    file = new File("target/filelanguage/test/bigfile.tar.gz");
    String uri = "file://target/filelanguage?fileExist=Override";
    GenericFile<File> gf = FileConsumer.asGenericFile("target/filelanguage", file, null, false);
    FileEndpoint endpoint = getMandatoryEndpoint(uri, FileEndpoint.class);
    Exchange answer = endpoint.createExchange(gf);
    endpoint.configureMessage(gf, answer.getIn());
    assertEquals("bigfile.tar.gz", file.getName());
    assertExpression(answer, "${file:onlyname}", "bigfile.tar.gz");
    assertExpression(answer, "${file:ext}", "tar.gz");
}
Also used : Exchange(org.apache.camel.Exchange) FileEndpoint(org.apache.camel.component.file.FileEndpoint) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 32 with GenericFile

use of org.apache.camel.component.file.GenericFile in project camel by apache.

the class DefaultAhcBinding method populateBody.

protected void populateBody(RequestBuilder builder, AhcEndpoint endpoint, Exchange exchange) throws CamelExchangeException {
    Message in = exchange.getIn();
    if (in.getBody() == null) {
        return;
    }
    String contentType = ExchangeHelper.getContentType(exchange);
    BodyGenerator body = in.getBody(BodyGenerator.class);
    String charset = IOHelper.getCharsetName(exchange, false);
    if (body == null) {
        try {
            Object data = in.getBody();
            if (data != null) {
                if (contentType != null && AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) {
                    if (!endpoint.getComponent().isAllowJavaSerializedObject()) {
                        throw new CamelExchangeException("Content-type " + AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed", exchange);
                    }
                    // serialized java object
                    Serializable obj = in.getMandatoryBody(Serializable.class);
                    // write object to output stream
                    ByteArrayOutputStream bos = new ByteArrayOutputStream(endpoint.getBufferSize());
                    AhcHelper.writeObjectToStream(bos, obj);
                    byte[] bytes = bos.toByteArray();
                    body = new ByteArrayBodyGenerator(bytes);
                    IOHelper.close(bos);
                } else if (data instanceof File || data instanceof GenericFile) {
                    // file based (could potentially also be a FTP file etc)
                    File file = in.getBody(File.class);
                    if (file != null) {
                        body = new FileBodyGenerator(file);
                    }
                } else if (data instanceof String) {
                    // (for example application/x-www-form-urlencoded forms being sent)
                    if (charset != null) {
                        body = new ByteArrayBodyGenerator(((String) data).getBytes(charset));
                    } else {
                        body = new ByteArrayBodyGenerator(((String) data).getBytes());
                    }
                }
                // fallback as input stream
                if (body == null) {
                    // force the body as an input stream since this is the fallback
                    InputStream is = in.getMandatoryBody(InputStream.class);
                    body = new InputStreamBodyGenerator(is);
                }
            }
        } catch (UnsupportedEncodingException e) {
            throw new CamelExchangeException("Error creating BodyGenerator from message body", exchange, e);
        } catch (IOException e) {
            throw new CamelExchangeException("Error serializing message body", exchange, e);
        }
    }
    if (body != null) {
        log.trace("Setting body {}", body);
        builder.setBody(body);
    }
    if (charset != null) {
        log.trace("Setting body charset {}", charset);
        builder.setCharset(Charset.forName(charset));
    }
    // must set content type, even if its null, otherwise it may default to
    // application/x-www-form-urlencoded which may not be your intention
    log.trace("Setting Content-Type {}", contentType);
    if (ObjectHelper.isNotEmpty(contentType)) {
        builder.setHeader(Exchange.CONTENT_TYPE, contentType);
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) Serializable(java.io.Serializable) Message(org.apache.camel.Message) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileBodyGenerator(org.asynchttpclient.request.body.generator.FileBodyGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BodyGenerator(org.asynchttpclient.request.body.generator.BodyGenerator) FileBodyGenerator(org.asynchttpclient.request.body.generator.FileBodyGenerator) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) ByteArrayBodyGenerator(org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator) ByteArrayBodyGenerator(org.asynchttpclient.request.body.generator.ByteArrayBodyGenerator) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) GenericFile(org.apache.camel.component.file.GenericFile)

Example 33 with GenericFile

use of org.apache.camel.component.file.GenericFile in project ddf by codice.

the class DurableFileConsumer method createExchangeHelper.

private void createExchangeHelper(File file, WatchEvent.Kind<Path> fileEvent) {
    GenericFile<EventfulFileWrapper> genericFile = new GenericFile<>();
    genericFile.setFile(new EventfulFileWrapper(fileEvent, 1, file.toPath()));
    genericFile.setEndpointPath(endpoint.getConfiguration().getDirectory());
    try {
        genericFile.setAbsoluteFilePath(file.getCanonicalPath());
    } catch (IOException e) {
        LOGGER.warn("Unable to canonicalize {}. Verify location is accessible.", file.toString());
    }
    Exchange exchange = endpoint.createExchange(genericFile);
    exchange.addOnCompletion(new ErrorLoggingSynchronization(file, fileEvent));
    processExchange(exchange);
}
Also used : Exchange(org.apache.camel.Exchange) IOException(java.io.IOException) GenericFile(org.apache.camel.component.file.GenericFile)

Example 34 with GenericFile

use of org.apache.camel.component.file.GenericFile in project ddf by codice.

the class ContentProducerDataAccessObject method getFileUsingRefKey.

public File getFileUsingRefKey(boolean storeRefKey, Message in) throws ContentComponentException {
    File ingestedFile;
    try {
        if (!storeRefKey) {
            ingestedFile = ((GenericFile<File>) in.getBody()).getFile();
        } else {
            WatchEvent<Path> pathWatchEvent = (WatchEvent<Path>) ((GenericFileMessage) in).getGenericFile().getFile();
            ingestedFile = pathWatchEvent.context().toFile();
        }
    } catch (ClassCastException e) {
        throw new ContentComponentException("Unable to cast message body to Camel GenericFile, so unable to process ingested file");
    }
    return ingestedFile;
}
Also used : Path(java.nio.file.Path) GenericFileMessage(org.apache.camel.component.file.GenericFileMessage) WatchEvent(java.nio.file.WatchEvent) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Aggregations

GenericFile (org.apache.camel.component.file.GenericFile)34 File (java.io.File)25 IOException (java.io.IOException)11 InputStream (java.io.InputStream)11 Exchange (org.apache.camel.Exchange)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 CamelExchangeException (org.apache.camel.CamelExchangeException)4 Message (org.apache.camel.Message)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 Serializable (java.io.Serializable)3 FallbackConverter (org.apache.camel.FallbackConverter)3 TypeConverter (org.apache.camel.TypeConverter)3 WrappedFile (org.apache.camel.WrappedFile)3 FileEndpoint (org.apache.camel.component.file.FileEndpoint)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 Test (org.junit.Test)3