Repository: camel Updated Branches: refs/heads/master 79a178d81 -> a351e3a4b
CAMEL-9736 SolrComponent gets the wrong Content Type Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a351e3a4 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a351e3a4 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a351e3a4 Branch: refs/heads/master Commit: a351e3a4b3aea6843916ab42381730f5a8a5797e Parents: 79a178d Author: Andrea Cosentino <anco...@gmail.com> Authored: Thu Mar 24 15:54:38 2016 +0100 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Thu Mar 24 15:55:10 2016 +0100 ---------------------------------------------------------------------- .../camel/component/solr/SolrProducer.java | 108 +++++++++++-------- 1 file changed, 64 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a351e3a4/components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrProducer.java b/components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrProducer.java index 7d0f32b..0052b7d 100644 --- a/components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrProducer.java +++ b/components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrProducer.java @@ -26,6 +26,7 @@ import javax.activation.MimetypesFileTypeMap; import org.apache.camel.Exchange; import org.apache.camel.WrappedFile; import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.ObjectHelper; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest; import org.apache.solr.client.solrj.request.DirectXmlRequest; @@ -41,13 +42,14 @@ public class SolrProducer extends DefaultProducer { private SolrClient concSolrServer; private SolrClient cloudSolrServer; - public SolrProducer(SolrEndpoint endpoint, SolrClient solrServer, SolrClient concSolrServer, SolrClient cloudSolrServer) { + public SolrProducer(SolrEndpoint endpoint, SolrClient solrServer, SolrClient concSolrServer, + SolrClient cloudSolrServer) { super(endpoint); this.httpServer = solrServer; this.concSolrServer = concSolrServer; this.cloudSolrServer = cloudSolrServer; } - + private SolrClient getBestSolrServer(String operation) { if (this.cloudSolrServer != null) { return this.cloudSolrServer; @@ -66,7 +68,7 @@ public class SolrProducer extends DefaultProducer { if (operation == null) { throw new IllegalArgumentException(SolrConstants.OPERATION + " header is missing"); } - + SolrClient serverToUse = getBestSolrServer(operation); if (operation.equalsIgnoreCase(SolrConstants.OPERATION_INSERT)) { @@ -88,7 +90,8 @@ public class SolrProducer extends DefaultProducer { } else if (operation.equalsIgnoreCase(SolrConstants.OPERATION_OPTIMIZE)) { serverToUse.optimize(); } else { - throw new IllegalArgumentException(SolrConstants.OPERATION + " header value '" + operation + "' is not supported"); + throw new IllegalArgumentException( + SolrConstants.OPERATION + " header value '" + operation + "' is not supported"); } } @@ -96,12 +99,11 @@ public class SolrProducer extends DefaultProducer { Object body = exchange.getIn().getBody(); boolean invalid = false; if (body instanceof WrappedFile) { - body = ((WrappedFile<?>)body).getFile(); + body = ((WrappedFile<?>) body).getFile(); } - if (body instanceof File) { - MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap(); - String mimeType = mimeTypesMap.getContentType((File)body); + 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); @@ -113,68 +115,86 @@ public class SolrProducer extends DefaultProducer { } updateRequest.process(solrServer); + } else { - } else if (body instanceof SolrInputDocument) { + if (body instanceof File) { + MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap(); + String mimeType = mimeTypesMap.getContentType((File) body); + ContentStreamUpdateRequest updateRequest = new ContentStreamUpdateRequest(getRequestHandler()); + updateRequest.addFile((File) body, mimeType); - UpdateRequest updateRequest = new UpdateRequest(getRequestHandler()); - updateRequest.add((SolrInputDocument) body); + 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); + updateRequest.process(solrServer); - } else if (body instanceof List<?>) { - List<?> list = (List<?>) body; + } else if (body instanceof SolrInputDocument) { - if (list.size() > 0 && list.get(0) instanceof SolrInputDocument) { UpdateRequest updateRequest = new UpdateRequest(getRequestHandler()); - updateRequest.add((List<SolrInputDocument>) list); + updateRequest.add((SolrInputDocument) body); updateRequest.process(solrServer); - } else { - invalid = true; - } - } else { + } else if (body instanceof List<?>) { + List<?> list = (List<?>) body; - boolean hasSolrHeaders = false; - for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) { - if (entry.getKey().startsWith(SolrConstants.FIELD)) { - hasSolrHeaders = true; - break; - } - } + if (list.size() > 0 && list.get(0) instanceof SolrInputDocument) { + UpdateRequest updateRequest = new UpdateRequest(getRequestHandler()); + updateRequest.add((List<SolrInputDocument>) list); - if (hasSolrHeaders) { + updateRequest.process(solrServer); + } else { + invalid = true; + } - UpdateRequest updateRequest = new UpdateRequest(getRequestHandler()); + } else { - SolrInputDocument doc = new SolrInputDocument(); + boolean hasSolrHeaders = false; 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()); + hasSolrHeaders = true; + break; } } - updateRequest.add(doc); - updateRequest.process(solrServer); - } else if (body instanceof String) { + if (hasSolrHeaders) { - String bodyAsString = (String) body; + UpdateRequest updateRequest = new UpdateRequest(getRequestHandler()); - if (!bodyAsString.startsWith("<add")) { - bodyAsString = "<add>" + bodyAsString + "</add>"; - } + 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); - DirectXmlRequest xmlRequest = new DirectXmlRequest(getRequestHandler(), bodyAsString); + } else if (body instanceof String) { - solrServer.request(xmlRequest); - } else { - invalid = true; + 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"); + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, + "unable to find data in Exchange to update Solr"); } }