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;
}
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;
}
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;
}
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;
}
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));
}
Aggregations