Search in sources :

Example 11 with WrappedFile

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);
    }
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) WrappedFile(org.apache.camel.WrappedFile) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator)

Example 12 with WrappedFile

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");
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) DirectXmlRequest(org.apache.solr.client.solrj.request.DirectXmlRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) WrappedFile(org.apache.camel.WrappedFile) List(java.util.List) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) Map(java.util.Map) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) SolrException(org.apache.solr.common.SolrException)

Aggregations

WrappedFile (org.apache.camel.WrappedFile)12 File (java.io.File)11 InputStream (java.io.InputStream)6 IOException (java.io.IOException)4 Message (org.apache.camel.Message)4 GenericFile (org.apache.camel.component.file.GenericFile)4 Reader (java.io.Reader)3 Exchange (org.apache.camel.Exchange)3 AccessControlList (com.amazonaws.services.s3.model.AccessControlList)2 CannedAccessControlList (com.amazonaws.services.s3.model.CannedAccessControlList)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)2 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 StringSource (org.apache.camel.StringSource)2 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)2 InvalidArgumentException (com.amazonaws.services.cloudfront.model.InvalidArgumentException)1 AbortMultipartUploadRequest (com.amazonaws.services.s3.model.AbortMultipartUploadRequest)1 CompleteMultipartUploadRequest (com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)1