Skip to content

Commit

Permalink
Implement SonarCloud quality suggestions #159
Browse files Browse the repository at this point in the history
  • Loading branch information
pkiraly committed Dec 8, 2023
1 parent d5e1df7 commit 20fe7a6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class QaSolrClient {
protected UniquenessExtractor extractor = new UniquenessExtractor();
protected final Schema schema;

public QaSolrClient() {
this(null, null);
}

public QaSolrClient(SolrClient solrClient, Schema schema) {
this.solrClient = solrClient;
this.schema = schema;
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/de/gwdg/metadataqa/api/cli/RecordFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;

public class RecordFactory {

private static Logger logger = Logger.getLogger(RecordFactory.class.getCanonicalName());;

private RecordFactory() {}

public static RecordReader getRecordReader(String inputFile,
Expand All @@ -35,27 +33,26 @@ public static RecordReader getRecordReader(String inputFile,
throws CsvValidationException, IOException {
final Schema schema = calculator.getSchema();

BufferedReader inputReader;
BufferedReader inputReader = null;
if (gzip) {
FileInputStream fis = new FileInputStream(inputFile);
GZIPInputStream gis = new GZIPInputStream(fis);
InputStreamReader inputStreamReader = new InputStreamReader(gis, "UTF-8");
InputStreamReader inputStreamReader = new InputStreamReader(gis, StandardCharsets.UTF_8);
inputReader = new BufferedReader(inputStreamReader);
} else {
Path inputPath = Paths.get(inputFile);
inputReader = Files.newBufferedReader(inputPath);
}

switch (schema.getFormat()) {
case CSV:
return new CSVRecordReader(inputReader, calculator);
case JSON:
return new JSONRecordReader(inputReader, calculator);
case XML:
XMLRecordReader reader = new XMLRecordReader(inputReader, calculator);
return reader;
return new XMLRecordReader(inputReader, calculator);
case CSV:
default:
return new CSVRecordReader(inputReader, calculator);
}
return new CSVRecordReader(inputReader, calculator);
}

public static ResultWriter getResultWriter(String outputFormat, String outputFile) throws IOException {
Expand All @@ -65,27 +62,26 @@ public static ResultWriter getResultWriter(String outputFormat, String outputFil
}

switch (outputFormat) {
case App.CSV:
return new CSVResultWriter(outputFile);
case App.JSON:
case App.NDJSON:
return new JSONResultWriter(outputFile);
case App.CSVJSON:
return new CSVJSONResultWriter(outputFile);
case App.CSV:
default:
return new CSVResultWriter(outputFile);
}

return new CSVResultWriter(outputFile);
}

public static ResultWriter getResultWriter(String outputFormat) throws IOException {
public static ResultWriter getResultWriter(String outputFormat) {
switch (outputFormat) {
case App.CSV:
return new CSVResultWriter();
case App.NDJSON:
return new JSONResultWriter();
case App.CSVJSON:
return new CSVJSONResultWriter();
case App.CSV:
default:
return new CSVResultWriter();
}
return new CSVResultWriter();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package de.gwdg.metadataqa.api.uniqueness;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.BaseHttpSolrClient;
import org.apache.solr.client.solrj.impl.Http2SolrClient;
import org.apache.solr.common.SolrInputDocument;

import java.io.BufferedInputStream;
Expand Down Expand Up @@ -29,6 +30,7 @@
public class DefaultSolrClient implements SolrClient, Serializable {

private static final Logger LOGGER = Logger.getLogger(DefaultSolrClient.class.getCanonicalName());
private static final String CONNECTION_ERROR_MESSAGE = "Error with connecting to %s: %s";

private static final String USER_AGENT = "Custom Java application";
private static final int VALUE_LIMIT = 50;
Expand All @@ -40,12 +42,12 @@ public class DefaultSolrClient implements SolrClient, Serializable {
private String solrSearchPattern;
private String solrSearchAllPattern;
private SolrConfiguration solrConfiguration;
private HttpSolrClient solr;
private Http2SolrClient solr;
boolean trimId = true;

public DefaultSolrClient(SolrConfiguration solrConfiguration) {
this.solrConfiguration = solrConfiguration;
solr = new HttpSolrClient.Builder(solrConfiguration.getUrl()).build();
solr = new Http2SolrClient.Builder(solrConfiguration.getUrl()).build();
}

public String getSolrSearchResponse(String solrField, String value) {
Expand Down Expand Up @@ -77,7 +79,7 @@ public String buildUrl(String solrField, String value) {

private String connect(String url, String solrField, String value) {
URL fragmentPostUrl = null;
String record = null;
String rawSolrResponse = null;
try {
fragmentPostUrl = new URL(url);
HttpURLConnection urlConnection = null;
Expand All @@ -90,37 +92,36 @@ private String connect(String url, String solrField, String value) {
try {
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
record = readStream(in);
rawSolrResponse = readStream(in);
} else {
int lenght = urlConnection.getContentLength();
String shortenedValue = value.length() < VALUE_LIMIT ? value : value.substring(0, VALUE_LIMIT) + "...";
String solrResponse = lenght == 0 ? "" : readStream(new BufferedInputStream(urlConnection.getInputStream()));
LOGGER.severe(String.format("%s: %s returned code %d. Solr responde: %s",
solrField,
(value.length() < VALUE_LIMIT ? value : value.substring(0, VALUE_LIMIT) + "..."),
urlConnection.getResponseCode(),
(lenght == 0 ? "" : readStream(new BufferedInputStream(urlConnection.getInputStream())))
solrField, shortenedValue, urlConnection.getResponseCode(), solrResponse
));
}
} catch (IOException e) {
LOGGER.severe("Error with connecting to " + url + ": " + e.getMessage());
LOGGER.severe(String.format(CONNECTION_ERROR_MESSAGE, url, e.getMessage()));
}
} catch (IOException e) {
LOGGER.severe("Error with connecting to " + url + ": " + e.getMessage());
LOGGER.severe(String.format(CONNECTION_ERROR_MESSAGE, url, e.getMessage()));
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
} catch (MalformedURLException e) {
LOGGER.severe("Error with connecting to " + url + ": " + e.getMessage());
LOGGER.severe(String.format(CONNECTION_ERROR_MESSAGE, url, e.getMessage()));
}

// add request header
return record;
return rawSolrResponse;
}

private String readStream(InputStream in) throws IOException {
var rd = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
var result = new StringBuffer();
var result = new StringBuilder();
var line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
Expand Down Expand Up @@ -173,8 +174,8 @@ public void indexMap(String id, Map<String, List<String>> objectMap) throws IOEx

try {
solr.add(document);
} catch (HttpSolrClient.RemoteSolrException ex) {
LOGGER.log(Level.WARNING, "document", document);
} catch (BaseHttpSolrClient.RemoteSolrException ex) {
LOGGER.log(Level.WARNING, String.format("Solr input document: %s", document.toString()));
LOGGER.log(Level.WARNING, "Commit exception", ex);
}
}
Expand Down

0 comments on commit 20fe7a6

Please sign in to comment.