use of org.apache.camel.WrappedFile in project camel by apache.
the class TestEndpoint method doStart.
@Override
protected void doStart() throws Exception {
LOG.debug("Consuming expected messages from: {}", expectedMessageEndpoint);
final List<Object> expectedBodies = new ArrayList<Object>();
EndpointHelper.pollEndpoint(expectedMessageEndpoint, new Processor() {
public void process(Exchange exchange) throws Exception {
// if file based we need to load the file into memory as the file may be deleted/moved afterwards
Object body = getInBody(exchange);
if (body instanceof WrappedFile) {
body = exchange.getIn().getBody(String.class);
}
if (split) {
// use new lines in both styles
Iterator it = ObjectHelper.createIterator(body, delimiter, false, true);
while (it.hasNext()) {
Object line = it.next();
LOG.trace("Received message body {}", line);
expectedBodies.add(line);
}
} else {
expectedBodies.add(body);
}
}
}, timeout);
LOG.info("Received: {} expected message(s) from: {}", expectedBodies.size(), expectedMessageEndpoint);
if (anyOrder) {
expectedBodiesReceivedInAnyOrder(expectedBodies);
} else {
expectedBodiesReceived(expectedBodies);
}
}
use of org.apache.camel.WrappedFile in project camel by apache.
the class SolrProducer method insert.
private void insert(Exchange exchange, SolrClient solrServer) throws Exception {
Object body = exchange.getIn().getBody();
boolean invalid = false;
if (body instanceof WrappedFile) {
body = ((WrappedFile<?>) body).getFile();
}
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class))) {
String mimeType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
ContentStreamUpdateRequest updateRequest = new ContentStreamUpdateRequest(getRequestHandler());
updateRequest.addFile((File) body, mimeType);
for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
if (entry.getKey().startsWith(SolrConstants.PARAM)) {
String paramName = entry.getKey().substring(SolrConstants.PARAM.length());
updateRequest.setParam(paramName, entry.getValue().toString());
}
}
updateRequest.process(solrServer);
} else {
if (body instanceof File) {
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String mimeType = mimeTypesMap.getContentType((File) body);
ContentStreamUpdateRequest updateRequest = new ContentStreamUpdateRequest(getRequestHandler());
updateRequest.addFile((File) body, mimeType);
for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
if (entry.getKey().startsWith(SolrConstants.PARAM)) {
String paramName = entry.getKey().substring(SolrConstants.PARAM.length());
updateRequest.setParam(paramName, entry.getValue().toString());
}
}
updateRequest.process(solrServer);
} else if (body instanceof SolrInputDocument) {
UpdateRequest updateRequest = new UpdateRequest(getRequestHandler());
updateRequest.add((SolrInputDocument) body);
updateRequest.process(solrServer);
} else if (body instanceof List<?>) {
List<?> list = (List<?>) body;
if (list.size() > 0 && list.get(0) instanceof SolrInputDocument) {
UpdateRequest updateRequest = new UpdateRequest(getRequestHandler());
updateRequest.add((List<SolrInputDocument>) list);
updateRequest.process(solrServer);
} else {
invalid = true;
}
} else {
boolean hasSolrHeaders = false;
for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
if (entry.getKey().startsWith(SolrConstants.FIELD)) {
hasSolrHeaders = true;
break;
}
}
if (hasSolrHeaders) {
UpdateRequest updateRequest = new UpdateRequest(getRequestHandler());
SolrInputDocument doc = new SolrInputDocument();
for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
if (entry.getKey().startsWith(SolrConstants.FIELD)) {
String fieldName = entry.getKey().substring(SolrConstants.FIELD.length());
doc.setField(fieldName, entry.getValue());
}
}
updateRequest.add(doc);
updateRequest.process(solrServer);
} else if (body instanceof String) {
String bodyAsString = (String) body;
if (!bodyAsString.startsWith("<add")) {
bodyAsString = "<add>" + bodyAsString + "</add>";
}
DirectXmlRequest xmlRequest = new DirectXmlRequest(getRequestHandler(), bodyAsString);
solrServer.request(xmlRequest);
} else {
invalid = true;
}
}
}
if (invalid) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unable to find data in Exchange to update Solr");
}
}
Aggregations