1
1
package de .gwdg .metadataqa .api .uniqueness ;
2
2
3
3
import org .apache .solr .client .solrj .SolrServerException ;
4
- import org .apache .solr .client .solrj .impl .HttpSolrClient ;
4
+ import org .apache .solr .client .solrj .impl .BaseHttpSolrClient ;
5
+ import org .apache .solr .client .solrj .impl .Http2SolrClient ;
5
6
import org .apache .solr .common .SolrInputDocument ;
6
7
7
8
import java .io .BufferedInputStream ;
29
30
public class DefaultSolrClient implements SolrClient , Serializable {
30
31
31
32
private static final Logger LOGGER = Logger .getLogger (DefaultSolrClient .class .getCanonicalName ());
33
+ private static final String CONNECTION_ERROR_MESSAGE = "Error with connecting to %s: %s" ;
32
34
33
35
private static final String USER_AGENT = "Custom Java application" ;
34
36
private static final int VALUE_LIMIT = 50 ;
@@ -40,12 +42,12 @@ public class DefaultSolrClient implements SolrClient, Serializable {
40
42
private String solrSearchPattern ;
41
43
private String solrSearchAllPattern ;
42
44
private SolrConfiguration solrConfiguration ;
43
- private HttpSolrClient solr ;
45
+ private Http2SolrClient solr ;
44
46
boolean trimId = true ;
45
47
46
48
public DefaultSolrClient (SolrConfiguration solrConfiguration ) {
47
49
this .solrConfiguration = solrConfiguration ;
48
- solr = new HttpSolrClient .Builder (solrConfiguration .getUrl ()).build ();
50
+ solr = new Http2SolrClient .Builder (solrConfiguration .getUrl ()).build ();
49
51
}
50
52
51
53
public String getSolrSearchResponse (String solrField , String value ) {
@@ -77,7 +79,7 @@ public String buildUrl(String solrField, String value) {
77
79
78
80
private String connect (String url , String solrField , String value ) {
79
81
URL fragmentPostUrl = null ;
80
- String record = null ;
82
+ String rawSolrResponse = null ;
81
83
try {
82
84
fragmentPostUrl = new URL (url );
83
85
HttpURLConnection urlConnection = null ;
@@ -90,37 +92,36 @@ private String connect(String url, String solrField, String value) {
90
92
try {
91
93
if (urlConnection .getResponseCode () == HttpURLConnection .HTTP_OK ) {
92
94
InputStream in = new BufferedInputStream (urlConnection .getInputStream ());
93
- record = readStream (in );
95
+ rawSolrResponse = readStream (in );
94
96
} else {
95
97
int lenght = urlConnection .getContentLength ();
98
+ String shortenedValue = value .length () < VALUE_LIMIT ? value : value .substring (0 , VALUE_LIMIT ) + "..." ;
99
+ String solrResponse = lenght == 0 ? "" : readStream (new BufferedInputStream (urlConnection .getInputStream ()));
96
100
LOGGER .severe (String .format ("%s: %s returned code %d. Solr responde: %s" ,
97
- solrField ,
98
- (value .length () < VALUE_LIMIT ? value : value .substring (0 , VALUE_LIMIT ) + "..." ),
99
- urlConnection .getResponseCode (),
100
- (lenght == 0 ? "" : readStream (new BufferedInputStream (urlConnection .getInputStream ())))
101
+ solrField , shortenedValue , urlConnection .getResponseCode (), solrResponse
101
102
));
102
103
}
103
104
} catch (IOException e ) {
104
- LOGGER .severe ("Error with connecting to " + url + ": " + e .getMessage ());
105
+ LOGGER .severe (String . format ( CONNECTION_ERROR_MESSAGE , url , e .getMessage () ));
105
106
}
106
107
} catch (IOException e ) {
107
- LOGGER .severe ("Error with connecting to " + url + ": " + e .getMessage ());
108
+ LOGGER .severe (String . format ( CONNECTION_ERROR_MESSAGE , url , e .getMessage () ));
108
109
} finally {
109
110
if (urlConnection != null ) {
110
111
urlConnection .disconnect ();
111
112
}
112
113
}
113
114
} catch (MalformedURLException e ) {
114
- LOGGER .severe ("Error with connecting to " + url + ": " + e .getMessage ());
115
+ LOGGER .severe (String . format ( CONNECTION_ERROR_MESSAGE , url , e .getMessage () ));
115
116
}
116
117
117
118
// add request header
118
- return record ;
119
+ return rawSolrResponse ;
119
120
}
120
121
121
122
private String readStream (InputStream in ) throws IOException {
122
123
var rd = new BufferedReader (new InputStreamReader (in , StandardCharsets .UTF_8 ));
123
- var result = new StringBuffer ();
124
+ var result = new StringBuilder ();
124
125
var line = "" ;
125
126
while ((line = rd .readLine ()) != null ) {
126
127
result .append (line );
@@ -173,8 +174,8 @@ public void indexMap(String id, Map<String, List<String>> objectMap) throws IOEx
173
174
174
175
try {
175
176
solr .add (document );
176
- } catch (HttpSolrClient .RemoteSolrException ex ) {
177
- LOGGER .log (Level .WARNING , " document" , document );
177
+ } catch (BaseHttpSolrClient .RemoteSolrException ex ) {
178
+ LOGGER .log (Level .WARNING , String . format ( "Solr input document: %s " , document . toString ()) );
178
179
LOGGER .log (Level .WARNING , "Commit exception" , ex );
179
180
}
180
181
}
0 commit comments