From 20fe7a6d428b94ce24d83a1af827b3e51da383cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Kir=C3=A1ly?= Date: Fri, 8 Dec 2023 10:35:01 +0100 Subject: [PATCH] Implement SonarCloud quality suggestions #159 --- .../api/calculator/solr/QaSolrClient.java | 4 +++ .../metadataqa/api/cli/RecordFactory.java | 32 ++++++++---------- .../api/uniqueness/DefaultSolrClient.java | 33 ++++++++++--------- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/main/java/de/gwdg/metadataqa/api/calculator/solr/QaSolrClient.java b/src/main/java/de/gwdg/metadataqa/api/calculator/solr/QaSolrClient.java index 7d3e4fdd..5109e431 100644 --- a/src/main/java/de/gwdg/metadataqa/api/calculator/solr/QaSolrClient.java +++ b/src/main/java/de/gwdg/metadataqa/api/calculator/solr/QaSolrClient.java @@ -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; diff --git a/src/main/java/de/gwdg/metadataqa/api/cli/RecordFactory.java b/src/main/java/de/gwdg/metadataqa/api/cli/RecordFactory.java index 7bdbb247..392186f7 100644 --- a/src/main/java/de/gwdg/metadataqa/api/cli/RecordFactory.java +++ b/src/main/java/de/gwdg/metadataqa/api/cli/RecordFactory.java @@ -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, @@ -35,11 +33,11 @@ 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); @@ -47,15 +45,14 @@ public static RecordReader getRecordReader(String inputFile, } 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 { @@ -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(); } } diff --git a/src/main/java/de/gwdg/metadataqa/api/uniqueness/DefaultSolrClient.java b/src/main/java/de/gwdg/metadataqa/api/uniqueness/DefaultSolrClient.java index 29b2b08f..1fbef4e4 100644 --- a/src/main/java/de/gwdg/metadataqa/api/uniqueness/DefaultSolrClient.java +++ b/src/main/java/de/gwdg/metadataqa/api/uniqueness/DefaultSolrClient.java @@ -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; @@ -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; @@ -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) { @@ -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; @@ -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); @@ -173,8 +174,8 @@ public void indexMap(String id, Map> 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); } }