1818package org .apache .solr .packagemanager ;
1919
2020import com .fasterxml .jackson .annotation .JsonIgnore ;
21+ import com .fasterxml .jackson .core .type .TypeReference ;
2122import java .io .IOException ;
2223import java .lang .invoke .MethodHandles ;
2324import java .net .URI ;
2425import java .net .URL ;
2526import java .nio .file .Files ;
2627import java .nio .file .Path ;
27- import java .util .Collection ;
28+ import java .util .List ;
2829import java .util .Map ;
29- import java .util .Set ;
30+ import java .util .function .Function ;
31+ import java .util .stream .Collectors ;
3032import org .apache .commons .io .FileUtils ;
3133import 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 ;
3734import org .apache .solr .common .SolrException ;
3835import org .apache .solr .common .SolrException .ErrorCode ;
39- import org .apache .solr .common .util .CollectionUtil ;
40- import org .apache .solr .common .util .NamedList ;
4136import org .slf4j .Logger ;
4237import 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}
0 commit comments