orpiske commented on code in PR #14865:
URL: https://github.com/apache/camel/pull/14865#discussion_r1682650246


##########
components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrProducer.java:
##########
@@ -0,0 +1,302 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.solr;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import jakarta.activation.MimetypesFileTypeMap;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.WrappedFile;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION;
+import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
+import org.apache.solr.client.solrj.request.DirectXmlRequest;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.client.solrj.request.UpdateRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.params.ModifiableSolrParams;
+
+/**
+ * The Solr producer.
+ */
+public class SolrProducer extends DefaultProducer {
+
+    public SolrProducer(SolrEndpoint endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+
+        String operation = (String) 
exchange.getIn().getHeader(SolrConstants.OPERATION);
+        if (operation == null) {
+            throw new IllegalArgumentException(SolrConstants.OPERATION + " 
header is missing");
+        }
+
+        // solr configuration
+        SolrConfiguration solrConfiguration = 
getEndpoint().getSolrConfiguration(operation);
+
+        // solr client
+        SolrClient solrClient = 
exchange.getIn().getHeader(SolrConstants.CLIENT, SolrClient.class);
+        if (solrClient == null) {
+            solrClient = getEndpoint().getComponent().getSolrClient(this, 
solrConfiguration);
+        }
+
+        // solr parameters
+        ModifiableSolrParams solrParams = new ModifiableSolrParams();
+        for (Map.Entry<String, Object> entry : 
exchange.getIn().getHeaders().entrySet()) {
+            if (entry.getKey().startsWith(SolrConstants.PARAM)) {
+                String paramName = 
entry.getKey().substring(SolrConstants.PARAM.length());
+                solrParams.add(paramName, entry.getValue().toString());
+            }
+        }
+
+        // solr collection
+        String solrCollection = 
exchange.getIn().getHeader(SolrConstants.COLLECTION, String.class);
+
+        // solr operations
+        if (operation.equalsIgnoreCase(SolrConstants.OPERATION_INSERT)) {
+            insert(exchange, solrClient, solrConfiguration, solrParams);
+            if (solrConfiguration.isAutoCommit()) {
+                commit(exchange, solrClient, solrConfiguration, solrParams);
+            }
+        } else if 
(operation.equalsIgnoreCase(SolrConstants.OPERATION_INSERT_STREAMING)) {
+            insert(exchange, solrClient, solrConfiguration, solrParams);
+            if (solrConfiguration.isAutoCommit()) {
+                commit(exchange, solrClient, solrConfiguration, solrParams);
+            }
+        } else if 
(operation.equalsIgnoreCase(SolrConstants.OPERATION_DELETE_BY_ID)) {
+            UpdateRequest updateRequest = 
createUpdateRequest(solrConfiguration, solrParams);
+            updateRequest.deleteById(exchange.getIn().getBody(String.class));
+            updateRequest.process(solrClient, solrCollection);
+            if (solrConfiguration.isAutoCommit()) {
+                commit(exchange, solrClient, solrConfiguration, solrParams);
+            }
+        } else if 
(operation.equalsIgnoreCase(SolrConstants.OPERATION_DELETE_BY_QUERY)) {
+            UpdateRequest updateRequest = 
createUpdateRequest(solrConfiguration, solrParams);
+            
updateRequest.deleteByQuery(exchange.getIn().getBody(String.class));
+            updateRequest.process(solrClient, solrCollection);
+            if (solrConfiguration.isAutoCommit()) {
+                commit(exchange, solrClient, solrConfiguration, solrParams);
+            }
+        } else if 
(operation.equalsIgnoreCase(SolrConstants.OPERATION_ADD_BEAN)) {
+            UpdateRequest updateRequest = 
createUpdateRequest(solrConfiguration, solrParams);
+            
updateRequest.add(solrClient.getBinder().toSolrInputDocument(exchange.getIn().getBody()));
+            updateRequest.process(solrClient, solrCollection);
+            if (solrConfiguration.isAutoCommit()) {
+                commit(exchange, solrClient, solrConfiguration, solrParams);
+            }
+        } else if 
(operation.equalsIgnoreCase(SolrConstants.OPERATION_ADD_BEANS)) {
+            UpdateRequest updateRequest = 
createUpdateRequest(solrConfiguration, solrParams);
+            Collection<Object> body = 
exchange.getIn().getBody(Collection.class);
+            
updateRequest.add(body.stream().map(solrClient.getBinder()::toSolrInputDocument).collect(Collectors.toList()));
+            updateRequest.process(solrClient, solrCollection);
+            if (solrConfiguration.isAutoCommit()) {
+                commit(exchange, solrClient, solrConfiguration, solrParams);
+            }
+        } else if (operation.equalsIgnoreCase(SolrConstants.OPERATION_COMMIT)) 
{
+            commit(exchange, solrClient, solrConfiguration, solrParams);
+        } else if 
(operation.equalsIgnoreCase(SolrConstants.OPERATION_SOFT_COMMIT)) {
+            commit(exchange, solrClient, solrConfiguration, solrParams, true);
+        } else if 
(operation.equalsIgnoreCase(SolrConstants.OPERATION_ROLLBACK)) {
+            UpdateRequest updateRequest = 
createUpdateRequest(solrConfiguration, solrParams);
+            updateRequest.rollback();
+            updateRequest.process(solrClient, solrCollection);
+            if (solrConfiguration.isAutoCommit()) {
+                commit(exchange, solrClient, solrConfiguration, solrParams);
+            }
+        } else if 
(operation.equalsIgnoreCase(SolrConstants.OPERATION_OPTIMIZE)) {
+            UpdateRequest updateRequest = 
createUpdateRequest(solrConfiguration, solrParams);
+            updateRequest.setAction(ACTION.OPTIMIZE, true, true, 1);
+            updateRequest.process(solrClient, solrCollection);
+            if (solrConfiguration.isAutoCommit()) {
+                commit(exchange, solrClient, solrConfiguration, solrParams);
+            }
+        } else if (operation.equalsIgnoreCase(SolrConstants.OPERATION_QUERY)) {
+            query(exchange, solrClient, solrConfiguration, solrParams);
+        } else {
+            throw new IllegalArgumentException(
+                    SolrConstants.OPERATION + " header value '" + operation + 
"' is not supported");
+        }
+    }
+
+    private void commit(
+            Exchange exchange,
+            SolrClient solrClient,
+            SolrConfiguration solrConfiguration,
+            ModifiableSolrParams solrParams)
+            throws SolrServerException, IOException {
+        commit(exchange, solrClient, solrConfiguration, solrParams, false);
+    }
+
+    private void commit(
+            Exchange exchange,
+            SolrClient solrClient,
+            SolrConfiguration solrConfiguration,
+            ModifiableSolrParams solrParams,
+            boolean softCommit)
+            throws SolrServerException, IOException {
+        String solrCollection = 
exchange.getIn().getHeader(SolrConstants.COLLECTION, String.class);
+        UpdateRequest updateRequest = createUpdateRequest(solrConfiguration, 
solrParams);
+        updateRequest.setAction(
+                ACTION.COMMIT,
+                solrParams.getBool("waitFlush", true),
+                solrParams.getBool("waitSearcher", true),
+                softCommit);
+        updateRequest.process(solrClient, solrCollection);
+    }
+
+    private void query(
+            Exchange exchange, SolrClient solrClient, SolrConfiguration 
solrConfiguration, ModifiableSolrParams solrParams)
+            throws SolrServerException, IOException {
+        String solrCollection = 
exchange.getIn().getHeader(SolrConstants.COLLECTION, String.class);
+        SolrQuery solrQuery = new SolrQuery();
+        if 
(ObjectHelper.isNotEmpty(exchange.getMessage().getHeader(SolrConstants.QUERY_STRING)))
 {
+            
solrQuery.setQuery(exchange.getMessage().getHeader(SolrConstants.QUERY_STRING, 
String.class));
+        } else {
+            throw new IllegalArgumentException("Query String needs to be set 
as header while querying Solr");

Review Comment:
   `NoSuchHeaderException` here too (also in other places where we need to 
check for mandatory headers).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to