Skip to content

Commit af97ef7

Browse files
authored
DefaultPackageRepository: simplify HTTP & JSON (#3253)
The use of a SolrClient didn't seem to make sense if the web server isn't (necessarily) Solr. Seemed to be getting more in the way. There was some double or even triple(?) JSON back & forth parsing / serializing that I couldn't let be on principle. * Don't comment out bats tests (annoying to uncomment); use the "skip" feature
1 parent 641f0f8 commit af97ef7

2 files changed

Lines changed: 34 additions & 58 deletions

File tree

solr/core/src/java/org/apache/solr/packagemanager/DefaultPackageRepository.java

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,21 @@
1818
package org.apache.solr.packagemanager;
1919

2020
import com.fasterxml.jackson.annotation.JsonIgnore;
21+
import com.fasterxml.jackson.core.type.TypeReference;
2122
import java.io.IOException;
2223
import java.lang.invoke.MethodHandles;
2324
import java.net.URI;
2425
import java.net.URL;
2526
import java.nio.file.Files;
2627
import java.nio.file.Path;
27-
import java.util.Collection;
28+
import java.util.List;
2829
import java.util.Map;
29-
import java.util.Set;
30+
import java.util.function.Function;
31+
import java.util.stream.Collectors;
3032
import org.apache.commons.io.FileUtils;
3133
import org.apache.commons.io.FilenameUtils;
32-
import org.apache.solr.client.solrj.SolrRequest;
33-
import org.apache.solr.client.solrj.SolrServerException;
34-
import org.apache.solr.client.solrj.impl.Http2SolrClient;
35-
import org.apache.solr.client.solrj.impl.JsonMapResponseParser;
36-
import org.apache.solr.client.solrj.request.GenericSolrRequest;
3734
import org.apache.solr.common.SolrException;
3835
import org.apache.solr.common.SolrException.ErrorCode;
39-
import org.apache.solr.common.util.CollectionUtil;
40-
import org.apache.solr.common.util.NamedList;
4136
import org.slf4j.Logger;
4237
import org.slf4j.LoggerFactory;
4338

@@ -86,10 +81,7 @@ public boolean hasPackage(String packageName) {
8681
public Path download(String artifactName) throws SolrException, IOException {
8782
Path tmpDirectory = Files.createTempDirectory("solr-packages");
8883
tmpDirectory.toFile().deleteOnExit();
89-
URL url =
90-
URI.create(repositoryURL.endsWith("/") ? repositoryURL : repositoryURL + "/")
91-
.resolve(artifactName)
92-
.toURL();
84+
URL url = getRepoUri().resolve(artifactName).toURL();
9385
String fileName = FilenameUtils.getName(url.getPath());
9486
Path destination = tmpDirectory.resolve(fileName);
9587

@@ -107,42 +99,24 @@ public Path download(String artifactName) throws SolrException, IOException {
10799
return destination;
108100
}
109101

102+
private URI getRepoUri() {
103+
return URI.create(repositoryURL.endsWith("/") ? repositoryURL : repositoryURL + "/");
104+
}
105+
110106
private void initPackages() {
111-
// We need http 1.1 protocol here because we are talking to the repository server and not to
112-
// an actual Solr server.
113-
// We use an Http2SolrClient so that we do not need a raw jetty http client for this GET.
114-
// We may get a text/plain mimetype for the repository.json (for instance when looking up a repo
115-
// on Github), so use custom ResponseParser.
116-
try (Http2SolrClient client =
117-
new Http2SolrClient.Builder(repositoryURL).useHttp1_1(true).build()) {
118-
GenericSolrRequest request =
119-
new GenericSolrRequest(SolrRequest.METHOD.GET, "/repository.json");
120-
request.setResponseParser(new TalkToRepoResponseParser());
121-
NamedList<Object> resp = client.request(request);
122-
SolrPackage[] items =
123-
PackageUtils.getMapper().readValue("[" + resp.jsonStr() + "]", SolrPackage[].class);
124-
packages = CollectionUtil.newHashMap(items.length);
125-
for (SolrPackage pkg : items) {
126-
pkg.setRepository(name);
127-
packages.put(pkg.name, pkg);
128-
}
129-
} catch (SolrServerException | IOException ex) {
107+
try {
108+
final var url = getRepoUri().resolve("repository.json").toURL();
109+
packages =
110+
PackageUtils.getMapper()
111+
.readValue(url, new TypeReference<List<SolrPackage>>() {})
112+
.stream()
113+
.peek(pkg -> pkg.setRepository(name))
114+
.collect(Collectors.toMap(pkg -> pkg.name, Function.identity()));
115+
} catch (IOException ex) {
130116
throw new SolrException(ErrorCode.INVALID_STATE, ex);
131117
}
132118
if (log.isDebugEnabled()) {
133-
log.debug("Found {} packages in repository '{}'", packages.size(), name);
134-
}
135-
}
136-
137-
/**
138-
* Github links for repository.json are returned in JSON format but with text/plain mimetype, so
139-
* this works around that issue.
140-
*/
141-
private static class TalkToRepoResponseParser extends JsonMapResponseParser {
142-
143-
@Override
144-
public Collection<String> getContentTypes() {
145-
return Set.of("application/json", "text/plain");
119+
log.debug("Found {} packages in repository '{}'", this.packages.size(), name);
146120
}
147121
}
148122
}

solr/packaging/test/test_packages.bats

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,24 @@ teardown() {
5858
}
5959

6060
# This test is useful if you are debugging/working with packages.
61-
# We have commented it out for now since it depends on a live internet
61+
# We have disabled it for now since it depends on a live internet
6262
# connection to run. This could be updated with a local Repo server if we had
6363
# a package that is part of the Solr project to use.
64-
# @test "deploying and undeploying a cluster level package" {
65-
# run solr start -Denable.packages=true
64+
@test "deploying and undeploying a cluster level package" {
65+
skip "For developing package infra; requires a connection to github"
66+
67+
run solr start -Denable.packages=true
6668

67-
# run solr package add-repo splainer "https://raw.githubusercontent.com/o19s/splainer/main/solr-splainer-package/repo"
68-
# assert_output --partial "Added repository: splainer"
69+
run solr package add-repo splainer "https://raw.githubusercontent.com/o19s/splainer/refs/heads/main/solr-splainer-package/repo/"
70+
assert_output --partial "Added repository: splainer"
6971

70-
# run solr package list-available
71-
# assert_output --partial "solr-splainer Splainer for Solr"
72-
# run solr package install solr-splainer
73-
# assert_output --partial "solr-splainer installed."
72+
run solr package list-available
73+
assert_output --partial "solr-splainer Splainer for Solr"
74+
run solr package install solr-splainer
75+
assert_output --partial "solr-splainer installed."
7476

75-
# run solr package deploy solr-splainer -y --cluster
76-
# assert_output --partial "Deployment successful"
77+
run solr package deploy solr-splainer -y --cluster
78+
assert_output --partial "Deployment successful"
7779

78-
# run -0 curl --fail http://localhost:${SOLR_PORT}/v2/splainer/index.html
79-
# }
80+
run -0 curl --fail http://localhost:${SOLR_PORT}/v2/splainer/index.html
81+
}

0 commit comments

Comments
 (0)