Search in sources :

Example 11 with DefaultMessage

use of org.apache.camel.impl.DefaultMessage in project camel by apache.

the class HdfsConsumer method doPoll.

protected int doPoll() throws Exception {
    class ExcludePathFilter implements PathFilter {

        public boolean accept(Path path) {
            return !(path.toString().endsWith(config.getOpenedSuffix()) || path.toString().endsWith(config.getReadSuffix()));
        }
    }
    int numMessages = 0;
    HdfsInfo info = setupHdfs(false);
    FileStatus[] fileStatuses;
    if (info.getFileSystem().isFile(info.getPath())) {
        fileStatuses = info.getFileSystem().globStatus(info.getPath());
    } else {
        Path pattern = info.getPath().suffix("/" + this.config.getPattern());
        fileStatuses = info.getFileSystem().globStatus(pattern, new ExcludePathFilter());
    }
    for (FileStatus status : fileStatuses) {
        if (normalFileIsDirectoryNoSuccessFile(status, info)) {
            continue;
        }
        if (config.getOwner() != null) {
            // must match owner
            if (!config.getOwner().equals(status.getOwner())) {
                if (log.isDebugEnabled()) {
                    log.debug("Skipping file: {} as not matching owner: {}", status.getPath().toString(), config.getOwner());
                }
                continue;
            }
        }
        try {
            this.rwlock.writeLock().lock();
            this.istream = HdfsInputStream.createInputStream(status.getPath().toString(), this.config);
            if (!this.istream.isOpened()) {
                if (log.isDebugEnabled()) {
                    log.debug("Skipping file: {} because it doesn't exist anymore", status.getPath().toString());
                }
                continue;
            }
        } finally {
            this.rwlock.writeLock().unlock();
        }
        try {
            Holder<Object> key = new Holder<Object>();
            Holder<Object> value = new Holder<Object>();
            while (this.istream.next(key, value) >= 0) {
                Exchange exchange = this.getEndpoint().createExchange();
                Message message = new DefaultMessage();
                String fileName = StringUtils.substringAfterLast(status.getPath().toString(), "/");
                message.setHeader(Exchange.FILE_NAME, fileName);
                message.setHeader(Exchange.FILE_PATH, status.getPath().toString());
                if (key.value != null) {
                    message.setHeader(HdfsHeader.KEY.name(), key.value);
                }
                message.setBody(value.value);
                exchange.setIn(message);
                log.debug("Processing file {}", fileName);
                try {
                    processor.process(exchange);
                } catch (Exception e) {
                    exchange.setException(e);
                }
                // in case of unhandled exceptions then let the exception handler handle them
                if (exchange.getException() != null) {
                    getExceptionHandler().handleException(exchange.getException());
                }
                numMessages++;
            }
        } finally {
            IOHelper.close(istream, "input stream", log);
        }
    }
    return numMessages;
}
Also used : Path(org.apache.hadoop.fs.Path) DefaultMessage(org.apache.camel.impl.DefaultMessage) PathFilter(org.apache.hadoop.fs.PathFilter) FileStatus(org.apache.hadoop.fs.FileStatus) Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage) IOException(java.io.IOException) Exchange(org.apache.camel.Exchange)

Example 12 with DefaultMessage

use of org.apache.camel.impl.DefaultMessage in project camel by apache.

the class HdfsConsumer method doPoll.

protected int doPoll() throws Exception {
    class ExcludePathFilter implements PathFilter {

        public boolean accept(Path path) {
            return !(path.toString().endsWith(config.getOpenedSuffix()) || path.toString().endsWith(config.getReadSuffix()));
        }
    }
    int numMessages = 0;
    HdfsInfo info = setupHdfs(false);
    FileStatus[] fileStatuses;
    if (info.getFileSystem().isFile(info.getPath())) {
        fileStatuses = info.getFileSystem().globStatus(info.getPath());
    } else {
        Path pattern = info.getPath().suffix("/" + this.config.getPattern());
        fileStatuses = info.getFileSystem().globStatus(pattern, new ExcludePathFilter());
    }
    for (FileStatus status : fileStatuses) {
        if (normalFileIsDirectoryNoSuccessFile(status, info)) {
            continue;
        }
        if (config.getOwner() != null) {
            // must match owner
            if (!config.getOwner().equals(status.getOwner())) {
                if (log.isDebugEnabled()) {
                    log.debug("Skipping file: {} as not matching owner: {}", status.getPath().toString(), config.getOwner());
                }
                continue;
            }
        }
        try {
            this.rwlock.writeLock().lock();
            this.istream = HdfsInputStream.createInputStream(status.getPath().toString(), this.config);
            if (!this.istream.isOpened()) {
                if (log.isDebugEnabled()) {
                    log.debug("Skipping file: {} because it doesn't exist anymore", status.getPath().toString());
                }
                continue;
            }
        } finally {
            this.rwlock.writeLock().unlock();
        }
        try {
            Holder<Object> key = new Holder<Object>();
            Holder<Object> value = new Holder<Object>();
            while (this.istream.next(key, value) >= 0) {
                Exchange exchange = this.getEndpoint().createExchange();
                Message message = new DefaultMessage();
                String fileName = StringUtils.substringAfterLast(status.getPath().toString(), "/");
                message.setHeader(Exchange.FILE_NAME, fileName);
                if (key.value != null) {
                    message.setHeader(HdfsHeader.KEY.name(), key.value);
                }
                message.setBody(value.value);
                exchange.setIn(message);
                log.debug("Processing file {}", fileName);
                try {
                    processor.process(exchange);
                } catch (Exception e) {
                    exchange.setException(e);
                }
                // in case of unhandled exceptions then let the exception handler handle them
                if (exchange.getException() != null) {
                    getExceptionHandler().handleException(exchange.getException());
                }
                numMessages++;
            }
        } finally {
            IOHelper.close(istream, "input stream", log);
        }
    }
    return numMessages;
}
Also used : Path(org.apache.hadoop.fs.Path) DefaultMessage(org.apache.camel.impl.DefaultMessage) PathFilter(org.apache.hadoop.fs.PathFilter) FileStatus(org.apache.hadoop.fs.FileStatus) Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage) IOException(java.io.IOException) Exchange(org.apache.camel.Exchange)

Example 13 with DefaultMessage

use of org.apache.camel.impl.DefaultMessage in project camel by apache.

the class DefaultUndertowHttpBinding method toCamelMessage.

@Override
public Message toCamelMessage(HttpServerExchange httpExchange, Exchange exchange) throws Exception {
    Message result = new DefaultMessage();
    populateCamelHeaders(httpExchange, result.getHeaders(), exchange);
    // Map form data which is parsed by undertow form parsers
    FormData formData = httpExchange.getAttachment(FormDataParser.FORM_DATA);
    if (formData != null) {
        Map<String, Object> body = new HashMap<>();
        formData.forEach(key -> {
            formData.get(key).forEach(value -> {
                if (value.isFile()) {
                    DefaultAttachment attachment = new DefaultAttachment(new FilePartDataSource(value));
                    result.addAttachmentObject(key, attachment);
                    body.put(key, attachment.getDataHandler());
                } else if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(key, value.getValue(), exchange)) {
                    UndertowHelper.appendHeader(result.getHeaders(), key, value.getValue());
                    UndertowHelper.appendHeader(body, key, value.getValue());
                }
            });
        });
        result.setBody(body);
    } else {
        //body is extracted as byte[] then auto TypeConverter kicks in
        if (Methods.POST.equals(httpExchange.getRequestMethod()) || Methods.PUT.equals(httpExchange.getRequestMethod())) {
            result.setBody(readFromChannel(httpExchange.getRequestChannel()));
        } else {
            result.setBody(null);
        }
    }
    return result;
}
Also used : DefaultMessage(org.apache.camel.impl.DefaultMessage) FormData(io.undertow.server.handlers.form.FormData) Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage) HashMap(java.util.HashMap) HttpString(io.undertow.util.HttpString) DefaultAttachment(org.apache.camel.impl.DefaultAttachment)

Example 14 with DefaultMessage

use of org.apache.camel.impl.DefaultMessage in project camel by apache.

the class DefaultUndertowHttpBinding method toCamelMessage.

@Override
public Message toCamelMessage(ClientExchange clientExchange, Exchange exchange) throws Exception {
    Message result = new DefaultMessage();
    //retrieve response headers
    populateCamelHeaders(clientExchange.getResponse(), result.getHeaders(), exchange);
    result.setBody(readFromChannel(clientExchange.getResponseChannel()));
    return result;
}
Also used : DefaultMessage(org.apache.camel.impl.DefaultMessage) Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage)

Example 15 with DefaultMessage

use of org.apache.camel.impl.DefaultMessage in project camel by apache.

the class SpringLdapProducerTest method testAuthenticate.

@Test
public void testAuthenticate() throws Exception {
    String dn = "cn=dn";
    String filter = "filter";
    String password = "password";
    Exchange exchange = new DefaultExchange(context);
    Message in = new DefaultMessage();
    Map<String, Object> body = new HashMap<String, Object>();
    body.put(SpringLdapProducer.DN, dn);
    body.put(SpringLdapProducer.FILTER, filter);
    body.put(SpringLdapProducer.PASSWORD, password);
    when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.AUTHENTICATE);
    processBody(exchange, in, body);
    verify(ldapTemplate).authenticate(Matchers.any(LdapQuery.class), eq(password));
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultMessage(org.apache.camel.impl.DefaultMessage) Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage) HashMap(java.util.HashMap) LdapQuery(org.springframework.ldap.query.LdapQuery) Test(org.junit.Test)

Aggregations

DefaultMessage (org.apache.camel.impl.DefaultMessage)45 Message (org.apache.camel.Message)44 Exchange (org.apache.camel.Exchange)22 DefaultExchange (org.apache.camel.impl.DefaultExchange)19 Test (org.junit.Test)16 HashMap (java.util.HashMap)15 IOException (java.io.IOException)4 Method (java.lang.reflect.Method)3 NoSuchElementException (java.util.NoSuchElementException)3 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)3 Before (org.junit.Before)3 Map (java.util.Map)2 BiFunction (java.util.function.BiFunction)2 BasicAttribute (javax.naming.directory.BasicAttribute)2 BasicAttributes (javax.naming.directory.BasicAttributes)2 ModificationItem (javax.naming.directory.ModificationItem)2 Processor (org.apache.camel.Processor)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 FileStatus (org.apache.hadoop.fs.FileStatus)2 Path (org.apache.hadoop.fs.Path)2