diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.java index f30a3787a92..1f42accbb73 100644 --- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java +++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java @@ -831,7 +831,7 @@ private void loadInternal() { solrClientProvider = new HttpSolrClientProvider(cfg.getUpdateShardHandlerConfig(), solrMetricsContext); updateShardHandler.initializeMetrics(solrMetricsContext, "updateShardHandler"); - solrClientCache = new SolrClientCache(updateShardHandler.getDefaultHttpClient()); + solrClientCache = new SolrClientCache(solrClientProvider.getSolrClient()); Map cachesConfig = cfg.getCachesConfig(); if (cachesConfig.isEmpty()) { diff --git a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerAPI.java b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerAPI.java index f34793aa744..03efab8f985 100644 --- a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerAPI.java +++ b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerAPI.java @@ -354,7 +354,7 @@ public void getSampleValue(SolrQueryRequest req, SolrQueryResponse rsp) throws I } if (textValue != null) { - Map analysis = configSetHelper.analyzeField(configSet, fieldName, textValue); + var analysis = configSetHelper.analyzeField(configSet, fieldName, textValue); rsp.getValues().addAll(Map.of(idField, docId, fieldName, textValue, "analysis", analysis)); } } diff --git a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java index 0b14b0d4cf4..2cccc0a44df 100644 --- a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java +++ b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java @@ -18,7 +18,6 @@ package org.apache.solr.handler.designer; import static org.apache.solr.common.params.CommonParams.VERSION_FIELD; -import static org.apache.solr.common.util.Utils.fromJSONString; import static org.apache.solr.common.util.Utils.toJavabin; import static org.apache.solr.handler.admin.ConfigSetsHandler.DEFAULT_CONFIGSET_NAME; import static org.apache.solr.handler.designer.SchemaDesignerAPI.getConfigSetZkPath; @@ -31,8 +30,6 @@ import java.io.IOException; import java.io.InputStream; import java.lang.invoke.MethodHandles; -import java.net.URI; -import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -57,33 +54,30 @@ import java.util.zip.ZipOutputStream; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.file.PathUtils; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.utils.URIBuilder; -import org.apache.http.client.utils.URLEncodedUtils; -import org.apache.http.entity.ByteArrayEntity; -import org.apache.http.util.EntityUtils; import org.apache.lucene.util.IOSupplier; +import org.apache.solr.client.solrj.SolrRequest; import org.apache.solr.client.solrj.SolrResponse; import org.apache.solr.client.solrj.SolrServerException; -import org.apache.solr.client.solrj.impl.CloudLegacySolrClient; +import org.apache.solr.client.solrj.impl.BinaryResponseParser; import org.apache.solr.client.solrj.impl.CloudSolrClient; +import org.apache.solr.client.solrj.impl.InputStreamResponseParser; import org.apache.solr.client.solrj.request.CollectionAdminRequest; +import org.apache.solr.client.solrj.request.GenericSolrRequest; import org.apache.solr.client.solrj.request.schema.FieldTypeDefinition; import org.apache.solr.client.solrj.request.schema.SchemaRequest; import org.apache.solr.client.solrj.response.schema.SchemaResponse; import org.apache.solr.cloud.ZkConfigSetService; import org.apache.solr.cloud.ZkSolrResourceLoader; import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrException.ErrorCode; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.cloud.DocCollection; import org.apache.solr.common.cloud.Replica; import org.apache.solr.common.cloud.SolrZkClient; import org.apache.solr.common.cloud.ZkMaintenanceUtils; import org.apache.solr.common.cloud.ZkStateReader; -import org.apache.solr.common.params.CommonParams; +import org.apache.solr.common.params.ModifiableSolrParams; +import org.apache.solr.common.util.IOUtils; import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.SimpleOrderedMap; import org.apache.solr.common.util.Utils; @@ -124,46 +118,22 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants { } @SuppressWarnings("unchecked") - Map analyzeField(String configSet, String fieldName, String fieldText) + NamedList analyzeField(String configSet, String fieldName, String fieldText) throws IOException { final String mutableId = getMutableId(configSet); - final URI uri; + var solrParams = new ModifiableSolrParams(); + solrParams.add("analysis.showmatch", "true"); + solrParams.add("analysis.fieldname", fieldName); + solrParams.add("analysis.fieldvalue", "POST"); + var request = new GenericSolrRequest(SolrRequest.METHOD.POST, "/analysis/field", solrParams); + request.withContent(fieldText.getBytes(StandardCharsets.UTF_8), "text/plain"); + request.setRequiresCollection(true); try { - uri = - collectionApiEndpoint(mutableId, "analysis", "field") - .setParameter(CommonParams.WT, CommonParams.JSON) - .setParameter("analysis.showmatch", "true") - .setParameter("analysis.fieldname", fieldName) - .setParameter("analysis.fieldvalue", "POST") - .build(); - } catch (URISyntaxException e) { - throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e); + var resp = request.process(cloudClient(), mutableId).getResponse(); + return (NamedList) resp.get("analysis"); + } catch (SolrServerException e) { + throw new SolrException(ErrorCode.SERVER_ERROR, e); } - - Map analysis = Collections.emptyMap(); - HttpPost httpPost = new HttpPost(uri); - httpPost.setHeader("Content-Type", "text/plain"); - httpPost.setEntity(new ByteArrayEntity(fieldText.getBytes(StandardCharsets.UTF_8))); - try { - HttpResponse resp = ((CloudLegacySolrClient) cloudClient()).getHttpClient().execute(httpPost); - int statusCode = resp.getStatusLine().getStatusCode(); - if (statusCode != HttpStatus.SC_OK) { - throw new SolrException( - SolrException.ErrorCode.getErrorCode(statusCode), - EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8)); - } - - Map response = - (Map) - fromJSONString(EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8)); - if (response != null) { - analysis = (Map) response.get("analysis"); - } - } finally { - httpPost.releaseConnection(); - } - - return analysis; } List listCollectionsForConfig(String configSet) { @@ -511,40 +481,22 @@ void deleteStoredSampleDocs(String configSet) { @SuppressWarnings("unchecked") List getStoredSampleDocs(final String configSet) throws IOException { - List docs = null; - - final URI uri; - try { - uri = - collectionApiEndpoint(BLOB_STORE_ID, "blob", configSet + "_sample") - .setParameter(CommonParams.WT, "filestream") - .build(); - } catch (URISyntaxException e) { - throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e); - } - - HttpGet httpGet = new HttpGet(uri); + var request = new GenericSolrRequest(SolrRequest.METHOD.GET, "/blob/" + configSet + "_sample"); + request.setRequiresCollection(true); + request.setResponseParser(new InputStreamResponseParser("filestream")); + InputStream inputStream = null; try { - HttpResponse entity = - ((CloudLegacySolrClient) cloudClient()).getHttpClient().execute(httpGet); - int statusCode = entity.getStatusLine().getStatusCode(); - if (statusCode == HttpStatus.SC_OK) { - byte[] bytes = readAllBytes(() -> entity.getEntity().getContent()); - if (bytes.length > 0) { - docs = (List) Utils.fromJavabin(bytes); - } - } else if (statusCode != HttpStatus.SC_NOT_FOUND) { - byte[] bytes = readAllBytes(() -> entity.getEntity().getContent()); - throw new IOException( - "Failed to lookup stored docs for " - + configSet - + " due to: " - + new String(bytes, StandardCharsets.UTF_8)); - } // else not found is ok + var resp = request.process(cloudClient(), BLOB_STORE_ID).getResponse(); + inputStream = (InputStream) resp.get("stream"); + var bytes = inputStream.readAllBytes(); + if (bytes.length > 0) { + return (List) Utils.fromJavabin(bytes); + } else return Collections.emptyList(); + } catch (SolrServerException e) { + throw new IOException("Failed to lookup stored docs for " + configSet + " due to: " + e); } finally { - httpGet.releaseConnection(); + IOUtils.closeQuietly(inputStream); } - return docs != null ? docs : Collections.emptyList(); } void storeSampleDocs(final String configSet, List docs) throws IOException { @@ -561,26 +513,13 @@ static byte[] readAllBytes(IOSupplier hasStream) throws IOException protected void postDataToBlobStore(CloudSolrClient cloudClient, String blobName, byte[] bytes) throws IOException { - final URI uri; - try { - uri = collectionApiEndpoint(BLOB_STORE_ID, "blob", blobName).build(); - } catch (URISyntaxException e) { - throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e); - } - - HttpPost httpPost = new HttpPost(uri); + var request = new GenericSolrRequest(SolrRequest.METHOD.POST, "/blob/" + blobName); + request.withContent(bytes, BinaryResponseParser.BINARY_CONTENT_TYPE); + request.setRequiresCollection(true); try { - httpPost.setHeader("Content-Type", "application/octet-stream"); - httpPost.setEntity(new ByteArrayEntity(bytes)); - HttpResponse resp = ((CloudLegacySolrClient) cloudClient).getHttpClient().execute(httpPost); - int statusCode = resp.getStatusLine().getStatusCode(); - if (statusCode != HttpStatus.SC_OK) { - throw new SolrException( - SolrException.ErrorCode.getErrorCode(statusCode), - EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8)); - } - } finally { - httpPost.releaseConnection(); + request.process(cloudClient, BLOB_STORE_ID); + } catch (SolrServerException e) { + throw new SolrException(ErrorCode.SERVER_ERROR, e); } } @@ -607,19 +546,6 @@ private String getBaseUrl(final String collection) { return baseUrl; } - private URIBuilder collectionApiEndpoint( - final String collection, final String... morePathSegments) throws URISyntaxException { - URI baseUrl = new URI(getBaseUrl(collection)); - // build up a list of path segments including any path in the base URL, collection, and - // additional segments provided by caller - List path = new ArrayList<>(URLEncodedUtils.parsePathSegments(baseUrl.getPath())); - path.add(collection); - if (morePathSegments != null && morePathSegments.length > 0) { - path.addAll(Arrays.asList(morePathSegments)); - } - return new URIBuilder(baseUrl).setPathSegments(path); - } - protected String getManagedSchemaZkPath(final String configSet) { return getConfigSetZkPath(configSet, DEFAULT_MANAGED_SCHEMA_RESOURCE_NAME); } diff --git a/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerConfigSetHelper.java b/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerConfigSetHelper.java index af6412da947..461bdb198b2 100644 --- a/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerConfigSetHelper.java +++ b/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerConfigSetHelper.java @@ -35,6 +35,7 @@ import org.apache.solr.client.solrj.request.CollectionAdminRequest; import org.apache.solr.cloud.SolrCloudTestCase; import org.apache.solr.common.SolrInputDocument; +import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.SimpleOrderedMap; import org.apache.solr.core.CoreContainer; import org.apache.solr.core.SolrConfig; @@ -280,15 +281,11 @@ public void testAnalyzeField() throws Exception { helper.addSchemaObject(configSet, Collections.singletonMap("add-field", addField)); assertEquals("title", addedFieldName); - Map analysis = + NamedList analysis = helper.analyzeField(configSet, "title", "The Pillars of the Earth"); - Map title = - (Map) ((Map) analysis.get("field_names")).get("title"); - assertNotNull(title); - List index = (List) title.get("index"); - assertNotNull(index); - assertFalse(index.isEmpty()); + var indexNl = (NamedList) analysis.findRecursive("field_names", "title", "index"); + assertTrue(indexNl.size() > 0); } @Test