Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

New SolrJ CollectionScopedSolrClient (#4418)

David Smiley0c8dcb806d6b
diff --git a/changelog/unreleased/CollectionScopedSolrClient.yml b/changelog/unreleased/CollectionScopedSolrClient.yml
new file mode 100644
index 00000000000..9bd6b677d82
--- /dev/null
+++ b/changelog/unreleased/CollectionScopedSolrClient.yml
@@ -0,0 +1,7 @@
+title: New SolrJ CollectionScopedSolrClient
+type: added
+authors:
+ - name: David Smiley
+links:
+ - name: PR#4418
+ url: https://github.com/apache/solr/pull/4418
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CollectionScopedSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CollectionScopedSolrClient.java
new file mode 100644
index 00000000000..5b0b4ee86ce
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CollectionScopedSolrClient.java
@@ -0,0 +1,61 @@
+/*
+ * 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.solr.client.solrj.impl;
+
+import java.io.IOException;
+import java.util.Objects;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.SolrRequest;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.common.util.NamedList;
+
+/**
+ * This SolrClient delegates to a backing client with a specified collection. Simple attempts to
+ * refer to another collection will fail. DISCLAIMER: this isn't a security mechanism.
+ */
+public class CollectionScopedSolrClient extends SolrClient {
+
+ private final SolrClient delegate;
+
+ public CollectionScopedSolrClient(SolrClient delegate, String defaultCollection) {
+ this.delegate = Objects.requireNonNull(delegate);
+ this.defaultCollection = Objects.requireNonNull(defaultCollection);
+ }
+
+ @Override
+ public NamedList<Object> request(SolrRequest<?> request, String collection)
+ throws SolrServerException, IOException {
+ if (collection == null) {
+ collection = defaultCollection;
+ } else if (!collection.equals(defaultCollection)) {
+ throw new IllegalArgumentException(
+ "Restricted to default collection " + defaultCollection + " but was given " + collection);
+ }
+ return delegate.request(request, collection);
+ }
+
+ @Override
+ public String toString() {
+ return delegate + "/" + defaultCollection;
+ }
+
+ @Override
+ public void close() throws IOException {
+ delegate.close();
+ }
+}