From 947dfef8050e16e6316276587a3b5d2af26069ef Mon Sep 17 00:00:00 2001 From: David Smiley Date: Mon, 11 May 2026 17:05:33 -0400 Subject: [PATCH 1/3] CollectionScopedSolrClient 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. --- .../impl/CollectionScopedSolrClient.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/impl/CollectionScopedSolrClient.java 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 000000000000..417abb169fd8 --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CollectionScopedSolrClient.java @@ -0,0 +1,55 @@ +/* + * 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 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 = delegate; + this.defaultCollection = defaultCollection; + } + + @Override + public NamedList 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 void close() throws IOException { + delegate.close(); + } +} From 661ea463bc6c7e4850d9f67cadfac5c0b434cf99 Mon Sep 17 00:00:00 2001 From: David Smiley Date: Mon, 11 May 2026 17:38:41 -0400 Subject: [PATCH 2/3] toString --- .../solrj/impl/CollectionScopedSolrClient.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) 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 index 417abb169fd8..5b0b4ee86ce6 100644 --- 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 @@ -18,22 +18,23 @@ 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. + * 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 = delegate; - this.defaultCollection = defaultCollection; + this.delegate = Objects.requireNonNull(delegate); + this.defaultCollection = Objects.requireNonNull(defaultCollection); } @Override @@ -48,6 +49,11 @@ public NamedList request(SolrRequest request, String collection) return delegate.request(request, collection); } + @Override + public String toString() { + return delegate + "/" + defaultCollection; + } + @Override public void close() throws IOException { delegate.close(); From 516090512ea91d0c9c83c58b1a946eecd618e76e Mon Sep 17 00:00:00 2001 From: David Smiley Date: Sun, 17 May 2026 09:49:25 -0400 Subject: [PATCH 3/3] changelog --- changelog/unreleased/CollectionScopedSolrClient.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 changelog/unreleased/CollectionScopedSolrClient.yml diff --git a/changelog/unreleased/CollectionScopedSolrClient.yml b/changelog/unreleased/CollectionScopedSolrClient.yml new file mode 100644 index 000000000000..9bd6b677d827 --- /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